- if (matcher.find(end)) {
- final String matchText = matcher.group();
- final int matchStart = matcher.start();
-
- assert matcher.end() > end || matchText.length() == 0: "Group=" + matchText;
+ // Manual replacement for matcher.find(end),
+ // because Java regexp is a ridiculously slow implementation.
+ // Initialize to always match the end.
+ int matchIdx = 0;
+ for (int i = 1; i < 8; ++i) {
+ if (nextMatch[i] < nextMatch[matchIdx]) {
+ matchIdx = i;
+ }
+ }
+
+ int matchStart = nextMatch[matchIdx];
+ String matchText = patterns[matchIdx];
+ int matchEnd = matchStart + matchText.length();
+ nextMatch[matchIdx] = wikiText.indexOf(patterns[matchIdx], matchEnd);
+ if (nextMatch[matchIdx] == -1) nextMatch[matchIdx] = matchIdx > 0 ? 0x7fffffff : wikiText.length();
+ if (matchIdx == 0) {
+ matchText = "";
+ matchEnd = matchStart;
+ }
+
+ assert matchEnd > end || matchText.length() == 0: "Group=" + matchText;