Skip to content

Commit cc73c4e

Browse files
authored
One symbol table to rule them all (#1080)
1 parent eec5584 commit cc73c4e

File tree

3 files changed

+820
-11
lines changed

3 files changed

+820
-11
lines changed

unicodetools/src/main/java/org/unicode/props/UnicodeProperty.java

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.TreeMap;
3333
import java.util.function.Predicate;
3434
import java.util.regex.Pattern;
35+
import org.unicode.cldr.util.Rational.RationalParser;
3536
import org.unicode.cldr.util.props.UnicodeLabel;
3637

3738
public abstract class UnicodeProperty extends UnicodeLabel {
@@ -198,6 +199,7 @@ public UnicodeProperty setDelimiter(String value) {
198199
EXTENDED_MASK = 1,
199200
CORE_MASK = ~EXTENDED_MASK,
200201
BINARY_MASK = (1 << BINARY) | (1 << EXTENDED_BINARY),
202+
NUMERIC_MASK = (1 << NUMERIC) | (1 << EXTENDED_NUMERIC),
201203
STRING_MASK = (1 << STRING) | (1 << EXTENDED_STRING),
202204
STRING_OR_MISC_MASK =
203205
(1 << STRING) | (1 << EXTENDED_STRING) | (1 << MISC) | (1 << EXTENDED_MISC),
@@ -443,19 +445,25 @@ public final UnicodeSet getSet(String propertyValue, UnicodeSet result) {
443445
if (isMultivalued && propertyValue != null && propertyValue.contains(delimiter)) {
444446
throw new IllegalArgumentException(
445447
"Multivalued property values can't contain the delimiter.");
448+
}
449+
if (propertyValue == null) {
450+
return getSet(NULL_MATCHER, result);
451+
}
452+
Comparator<String> comparator;
453+
if (isType(NUMERIC_MASK)) {
454+
// UAX44-LM1.
455+
comparator = RATIONAL_COMPARATOR;
456+
} else if (getName().equals("Name") || getName().equals("Name_Alias")) {
457+
// UAX44-LM2.
458+
comparator = CHARACTER_NAME_COMPARATOR;
459+
} else if (isType(BINARY_OR_ENUMERATED_OR_CATALOG_MASK)) {
460+
// UAX44-LM3
461+
comparator = PROPERTY_COMPARATOR;
446462
} else {
447-
return getSet(
448-
propertyValue == null
449-
? NULL_MATCHER
450-
: new SimpleMatcher(
451-
propertyValue,
452-
getName().equals("Name") || getName().equals("Name_Alias")
453-
? CHARACTER_NAME_COMPARATOR
454-
: isType(STRING_OR_MISC_MASK)
455-
? null
456-
: PROPERTY_COMPARATOR),
457-
result);
463+
// String-valued or Miscellaneous property.
464+
comparator = null;
458465
}
466+
return getSet(new SimpleMatcher(propertyValue, comparator), result);
459467
}
460468

461469
private UnicodeMap<String> unicodeMap = null;
@@ -725,6 +733,26 @@ public static String toSkeleton(String source) {
725733
return skeletonBuffer.toString();
726734
}
727735

736+
public static final Comparator<String> RATIONAL_COMPARATOR =
737+
new Comparator<String>() {
738+
@Override
739+
public int compare(String x, String y) {
740+
return compareRationals(x, y);
741+
}
742+
};
743+
744+
public static int compareRationals(String a, String b) {
745+
if (a == b) return 0;
746+
if (a == null) return -1;
747+
if (b == null) return 1;
748+
final boolean aIsNaN = equalNames(a, "NaN");
749+
final boolean bIsNaN = equalNames(b, "NaN");
750+
if (aIsNaN && bIsNaN) return 0;
751+
if (aIsNaN) return -1;
752+
if (bIsNaN) return 1;
753+
return RationalParser.BASIC.parse(a).compareTo(RationalParser.BASIC.parse(b));
754+
}
755+
728756
public static final Comparator<String> CHARACTER_NAME_COMPARATOR =
729757
new Comparator<String>() {
730758
@Override

0 commit comments

Comments
 (0)