Skip to content

Commit

Permalink
We are not using Java 1.4 anymore. (#950)
Browse files Browse the repository at this point in the history
  • Loading branch information
eggrobin authored Oct 17, 2024
1 parent 109fcb4 commit bf10f7d
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions unicodetools/src/main/java/org/unicode/tools/Segmenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,8 @@ public RegexRule(String before, Breaks result, String after, String line) {
before = ".*(" + before + ")";
String parsing = null;
try {
matchPrevious = Pattern.compile(parsing = before, REGEX_FLAGS).matcher("");
matchSucceeding = Pattern.compile(parsing = after, REGEX_FLAGS).matcher("");
this.before = Pattern.compile(parsing = before, REGEX_FLAGS);
this.after = Pattern.compile(parsing = after, REGEX_FLAGS);
} catch (PatternSyntaxException e) {
// Format: Unclosed character class near index 927
int index = e.getIndex();
Expand Down Expand Up @@ -440,8 +440,12 @@ public Breaks applyAt(
CharSequence remappedString,
Integer[] indexInRemapped,
Consumer<CharSequence> remap) {
if (matchAfter(matchSucceeding, remappedString, indexInRemapped[position])
&& matchBefore(matchPrevious, remappedString, indexInRemapped[position])) {
if (after.matcher(remappedString)
.region(indexInRemapped[position], remappedString.length())
.lookingAt()
&& before.matcher(remappedString)
.region(0, indexInRemapped[position])
.matches()) {
return breaks;
}
return Breaks.UNKNOWN_BREAK;
Expand All @@ -455,29 +459,16 @@ public String toString(boolean showResolved) {
}

// ============== Internals ================
// in Java 5, this can be more efficient, and use a single regex
// of the form "(?<= before) after". MUST then have transparent bounds
private Matcher matchPrevious;
private Matcher matchSucceeding;
// We cannot use a single regex of the form "(?<= before) after" because
// (RI RI)* RI × RI would require unbounded lookbehind.
private Pattern before;
private Pattern after;
private String name;

private String resolved;
private Breaks breaks;
}

/** utility, since we are using Java 1.4 */
static boolean matchAfter(Matcher matcher, CharSequence text, int position) {
return matcher.reset(text.subSequence(position, text.length())).lookingAt();
}

/**
* utility, since we are using Java 1.4 depends on the pattern having been built with .* not
* very efficient, works for testing and the best we can do.
*/
static boolean matchBefore(Matcher matcher, CharSequence text, int position) {
return matcher.reset(text.subSequence(0, position)).matches();
}

/** Separate the builder for clarity */

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

0 comments on commit bf10f7d

Please sign in to comment.