Skip to content

Commit bf10f7d

Browse files
authored
We are not using Java 1.4 anymore. (#950)
1 parent 109fcb4 commit bf10f7d

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

unicodetools/src/main/java/org/unicode/tools/Segmenter.java

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,8 @@ public RegexRule(String before, Breaks result, String after, String line) {
393393
before = ".*(" + before + ")";
394394
String parsing = null;
395395
try {
396-
matchPrevious = Pattern.compile(parsing = before, REGEX_FLAGS).matcher("");
397-
matchSucceeding = Pattern.compile(parsing = after, REGEX_FLAGS).matcher("");
396+
this.before = Pattern.compile(parsing = before, REGEX_FLAGS);
397+
this.after = Pattern.compile(parsing = after, REGEX_FLAGS);
398398
} catch (PatternSyntaxException e) {
399399
// Format: Unclosed character class near index 927
400400
int index = e.getIndex();
@@ -440,8 +440,12 @@ public Breaks applyAt(
440440
CharSequence remappedString,
441441
Integer[] indexInRemapped,
442442
Consumer<CharSequence> remap) {
443-
if (matchAfter(matchSucceeding, remappedString, indexInRemapped[position])
444-
&& matchBefore(matchPrevious, remappedString, indexInRemapped[position])) {
443+
if (after.matcher(remappedString)
444+
.region(indexInRemapped[position], remappedString.length())
445+
.lookingAt()
446+
&& before.matcher(remappedString)
447+
.region(0, indexInRemapped[position])
448+
.matches()) {
445449
return breaks;
446450
}
447451
return Breaks.UNKNOWN_BREAK;
@@ -455,29 +459,16 @@ public String toString(boolean showResolved) {
455459
}
456460

457461
// ============== Internals ================
458-
// in Java 5, this can be more efficient, and use a single regex
459-
// of the form "(?<= before) after". MUST then have transparent bounds
460-
private Matcher matchPrevious;
461-
private Matcher matchSucceeding;
462+
// We cannot use a single regex of the form "(?<= before) after" because
463+
// (RI RI)* RI × RI would require unbounded lookbehind.
464+
private Pattern before;
465+
private Pattern after;
462466
private String name;
463467

464468
private String resolved;
465469
private Breaks breaks;
466470
}
467471

468-
/** utility, since we are using Java 1.4 */
469-
static boolean matchAfter(Matcher matcher, CharSequence text, int position) {
470-
return matcher.reset(text.subSequence(position, text.length())).lookingAt();
471-
}
472-
473-
/**
474-
* utility, since we are using Java 1.4 depends on the pattern having been built with .* not
475-
* very efficient, works for testing and the best we can do.
476-
*/
477-
static boolean matchBefore(Matcher matcher, CharSequence text, int position) {
478-
return matcher.reset(text.subSequence(0, position)).matches();
479-
}
480-
481472
/** Separate the builder for clarity */
482473

483474
/** Sort the longest strings first. Used for variable lists. */

0 commit comments

Comments
 (0)