Skip to content

Commit 5e34552

Browse files
committed
Extract StringUtils.countPushback(String, Pattern)
1 parent 75c33dc commit 5e34552

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/org/opensolaris/opengrok/analysis/JFlexXref.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,8 @@ protected void appendLink(String url, boolean doEndingPushback,
273273
if (doEndingPushback) {
274274
n = StringUtils.countURIEndingPushback(url);
275275
}
276-
if (collateralCapture != null) {
277-
int o = StringUtils.patindexOf(url, collateralCapture);
278-
if (o > 0) {
279-
int ccn = url.length() - o;
280-
if (ccn > n) n = ccn;
281-
}
282-
}
276+
int ccn = StringUtils.countPushback(url, collateralCapture);
277+
if (ccn > n) n = ccn;
283278
// Push back if positive, but not if equal to the current length.
284279
if (n > 0 && n < url.length()) {
285280
yypushback(n);

src/org/opensolaris/opengrok/util/StringUtils.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,28 @@ public static int patindexOf(String value, Pattern pattern) {
287287
public static boolean startsWithFpathChar(String value) {
288288
return FPATH_CHAR_STARTSMATCH.matcher(value).matches();
289289
}
290+
291+
/**
292+
* Determines if the specified pattern, {@code pat}, matches the specified
293+
* {@code capture}, and computes an eligible pushback.
294+
* @param capture a defined input
295+
* @param pat a pattern, or null to skip computation
296+
* @return a positive value if {@code pat} matches in {@code capture} at or
297+
* after the second character to indicate the number of characters to
298+
* pushback including the first-matched character; otherwise 0 to indicate
299+
* no match or a match at the 0-th character. (The 0-th chracter is
300+
* ineligible for fear of looping non-stop upon pushing back the entire
301+
* {@code yytext()}.)
302+
*/
303+
public static int countPushback(String capture, Pattern pat) {
304+
if (pat != null) {
305+
int o = StringUtils.patindexOf(capture, pat);
306+
if (o >= 0) {
307+
int n = capture.length() - o;
308+
// Push back if positive, but not if equal to the full length.
309+
if (n > 0 && n < capture.length()) return n;
310+
}
311+
}
312+
return 0;
313+
}
290314
}

0 commit comments

Comments
 (0)