Skip to content

Commit b02cc1a

Browse files
committed
Unicode support is conditional on Java version.
1 parent 6ede3fd commit b02cc1a

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

core/src/main/java/com/github/simy4/coregex/core/CoregexParser.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package com.github.simy4.coregex.core;
1818

19+
import java.lang.invoke.MethodHandle;
20+
import java.lang.invoke.MethodHandles;
21+
import java.lang.invoke.MethodType;
1922
import java.util.ArrayList;
2023
import java.util.Arrays;
2124
import java.util.List;
@@ -600,6 +603,17 @@ private static final class Context {
600603
private static final char SKIP = '\u0000';
601604
private static final char EOF = '\uFFFF';
602605

606+
private static final MethodHandle CHARACTER_CODE_POINT_OF;
607+
static {
608+
MethodHandle codePointOf = null;
609+
try {
610+
codePointOf = MethodHandles.lookup().findStatic(Character.class, "codePointOf", MethodType.methodType(int.class, String.class));
611+
} catch (Exception ignored) {
612+
} finally {
613+
CHARACTER_CODE_POINT_OF = codePointOf;
614+
}
615+
}
616+
603617
private final String regex;
604618
private final char[] tokens = {SKIP, SKIP, SKIP, SKIP};
605619
private int flags, groupIndex, cursor, tokensCursor;
@@ -698,13 +712,21 @@ private char token() {
698712
ch = chars[0];
699713
break loop;
700714
case 'N':
715+
if (null == CHARACTER_CODE_POINT_OF) {
716+
break;
717+
}
701718
start = cursor += 3;
702719
while ('}' != regex.charAt(cursor)) {
703720
cursor++;
704721
}
705-
chars = Character.toChars(Character.codePointOf(regex.substring(start, cursor)));
706-
System.arraycopy(chars, 0, tokens, tokensCursor, chars.length);
707-
ch = chars[0];
722+
try {
723+
chars = Character.toChars((int) CHARACTER_CODE_POINT_OF.invoke(regex.substring(start, cursor)));
724+
System.arraycopy(chars, 0, tokens, tokensCursor, chars.length);
725+
ch = chars[0];
726+
} catch (Throwable t) {
727+
//noinspection DataFlowIssue
728+
throw (RuntimeException) t;
729+
}
708730
break;
709731
case 'u':
710732
String u = regex.substring(cursor + 2, cursor + 6);

0 commit comments

Comments
 (0)