Skip to content

Commit 4527a97

Browse files
authored
Merge pull request #2966 from idodeclare/feature/reuse_uri_code
Share implementation of trimUri()
2 parents a2acfbd + fb1fd46 commit 4527a97

File tree

3 files changed

+118
-56
lines changed

3 files changed

+118
-56
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/JFlexSymbolMatcher.java

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2017-2018, Chris Fraire <[email protected]>.
21+
* Copyright (c) 2017-2019, Chris Fraire <[email protected]>.
2222
*/
2323

2424
package org.opengrok.indexer.analysis;
@@ -27,6 +27,7 @@
2727
import java.util.Set;
2828
import java.util.regex.Pattern;
2929
import org.opengrok.indexer.util.StringUtils;
30+
import org.opengrok.indexer.util.UriUtils;
3031

3132
/**
3233
* Represents an abstract base class for subclasses of
@@ -245,36 +246,15 @@ protected void onUriMatched(String uri, int start) {
245246
* may have been captured as valid URI characters but in a particular
246247
* context should mark the start of a pushback
247248
*/
248-
protected void onUriMatched(String uri, int start,
249-
Pattern collateralCapture) {
250-
251-
int n = 0;
252-
int subn;
253-
do {
254-
// An ending-pushback could be present before a collateral capture,
255-
// so detect both in a loop (on a shrinking `url') until no more
256-
// shrinking should occur.
257-
258-
subn = StringUtils.countURIEndingPushback(uri);
259-
int ccn = StringUtils.countPushback(uri, collateralCapture);
260-
if (ccn > subn) {
261-
subn = ccn;
262-
}
263-
264-
// Push back if positive, but not if equal to the current length.
265-
if (subn > 0 && subn < uri.length()) {
266-
uri = uri.substring(0, uri.length() - subn);
267-
n += subn;
268-
} else {
269-
subn = 0;
270-
}
271-
} while (subn != 0);
272-
if (n > 0) {
273-
yypushback(n);
249+
protected void onUriMatched(String uri, int start, Pattern collateralCapture) {
250+
UriUtils.TrimUriResult result = UriUtils.trimUri(uri, true, collateralCapture);
251+
if (result.getPushBackCount() > 0) {
252+
yypushback(result.getPushBackCount());
274253
}
275254

276255
NonSymbolMatchedListener l = nonSymbolListener;
277256
if (l != null) {
257+
uri = result.getUri();
278258
LinkageMatchedEvent evt = new LinkageMatchedEvent(this, uri,
279259
LinkageType.URI, start, start + uri.length());
280260
l.linkageMatched(evt);

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/JFlexXrefUtils.java

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/*
2121
* Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright 2011 Jens Elkner.
23-
* Portions Copyright (c) 2017-2018, Chris Fraire <[email protected]>.
23+
* Portions Copyright (c) 2017-2019, Chris Fraire <[email protected]>.
2424
*/
2525

2626
package org.opengrok.indexer.analysis;
@@ -41,6 +41,7 @@
4141
import org.opengrok.indexer.configuration.Project;
4242
import org.opengrok.indexer.configuration.RuntimeEnvironment;
4343
import org.opengrok.indexer.util.StringUtils;
44+
import org.opengrok.indexer.util.UriUtils;
4445
import org.opengrok.indexer.web.HtmlConsts;
4546
import org.opengrok.indexer.web.Util;
4647

@@ -80,37 +81,14 @@ public class JFlexXrefUtils {
8081
* @throws IOException if an error occurs while appending
8182
*/
8283
public static void appendLink(Writer out, JFlexLexer lexer, String url,
83-
boolean doEndingPushback, Pattern collateralCapture)
84-
throws IOException {
85-
86-
int n = 0;
87-
int subn;
88-
do {
89-
// An ending-pushback could be present before a collateral capture,
90-
// so detect both in a loop (on a shrinking `url') until no more
91-
// shrinking should occur.
92-
93-
subn = 0;
94-
if (doEndingPushback) {
95-
subn = StringUtils.countURIEndingPushback(url);
96-
}
97-
int ccn = StringUtils.countPushback(url, collateralCapture);
98-
if (ccn > subn) {
99-
subn = ccn;
100-
}
84+
boolean doEndingPushback, Pattern collateralCapture) throws IOException {
10185

102-
// Push back if positive, but not if equal to the current length.
103-
if (subn > 0 && subn < url.length()) {
104-
url = url.substring(0, url.length() - subn);
105-
n += subn;
106-
} else {
107-
subn = 0;
108-
}
109-
} while (subn != 0);
110-
if (n > 0) {
111-
lexer.yypushback(n);
86+
UriUtils.TrimUriResult result = UriUtils.trimUri(url, doEndingPushback, collateralCapture);
87+
if (result.getPushBackCount() > 0) {
88+
lexer.yypushback(result.getPushBackCount());
11289
}
11390

91+
url = result.getUri();
11492
out.write("<a href=\"");
11593
Util.htmlize(url, out);
11694
out.write("\">");
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2017-2019, Chris Fraire <[email protected]>.
22+
*/
23+
24+
package org.opengrok.indexer.util;
25+
26+
import java.util.regex.Pattern;
27+
28+
/**
29+
* Represents a container for utility methods concerned with URIs.
30+
*/
31+
public class UriUtils {
32+
33+
/**
34+
* Represents the immutable return value of
35+
* {@link #trimUri(String, boolean, Pattern)}.
36+
*/
37+
public static final class TrimUriResult {
38+
private final String uri;
39+
private final int pushBackCount;
40+
41+
TrimUriResult(String uri, int pushBackCount) {
42+
this.uri = uri;
43+
this.pushBackCount = pushBackCount;
44+
}
45+
46+
public String getUri() {
47+
return uri;
48+
}
49+
50+
public int getPushBackCount() {
51+
return pushBackCount;
52+
}
53+
}
54+
55+
/**
56+
* Trims a URI, specifying whether to enlist the
57+
* {@link StringUtils#countURIEndingPushback(String)} algorithm or the
58+
* {@link StringUtils#countPushback(String, Pattern)} or both.
59+
* <p>
60+
* If the pushback count is equal to the length of {@code url}, then the
61+
* pushback is set to zero -- in order to avoid a never-ending lexical loop.
62+
*
63+
* @param uri the URI string
64+
* @param shouldCheckEnding a value indicating whether to call
65+
* {@link StringUtils#countURIEndingPushback(String)}
66+
* @param collateralCapture optional pattern to call with
67+
* {@link StringUtils#countPushback(String, Pattern)}
68+
* @return a defined instance
69+
*/
70+
public static TrimUriResult trimUri(String uri, boolean shouldCheckEnding,
71+
Pattern collateralCapture) {
72+
73+
int n = 0;
74+
while (true) {
75+
/*
76+
* An ending-pushback could be present before a collateral capture,
77+
* so detect both in a loop (on a shrinking `url') until no more
78+
* shrinking should occur.
79+
*/
80+
81+
int subN = 0;
82+
if (shouldCheckEnding) {
83+
subN = StringUtils.countURIEndingPushback(uri);
84+
}
85+
int ccn = StringUtils.countPushback(uri, collateralCapture);
86+
if (ccn > subN) {
87+
subN = ccn;
88+
}
89+
90+
// Increment if positive, but not if equal to the current length.
91+
if (subN > 0 && subN < uri.length()) {
92+
uri = uri.substring(0, uri.length() - subN);
93+
n += subN;
94+
} else {
95+
break;
96+
}
97+
}
98+
return new TrimUriResult(uri, n);
99+
}
100+
101+
/** Private to enforce static. */
102+
private UriUtils() {
103+
}
104+
}

0 commit comments

Comments
 (0)