From b4bae46211e47f445a930f529c36ad7563cbd7a3 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Sat, 20 Jan 2024 15:13:39 +0100 Subject: [PATCH] There can only be one MySymbolTable (#667) Whose symbol table? --- .../java/org/unicode/jsp/MySymbolTable.java | 225 ------------------ .../unicode/propstest/TestXUnicodeSet.java | 49 ---- 2 files changed, 274 deletions(-) delete mode 100644 unicodetools/src/main/java/org/unicode/jsp/MySymbolTable.java delete mode 100644 unicodetools/src/test/java/org/unicode/propstest/TestXUnicodeSet.java diff --git a/unicodetools/src/main/java/org/unicode/jsp/MySymbolTable.java b/unicodetools/src/main/java/org/unicode/jsp/MySymbolTable.java deleted file mode 100644 index 5ba954e1d..000000000 --- a/unicodetools/src/main/java/org/unicode/jsp/MySymbolTable.java +++ /dev/null @@ -1,225 +0,0 @@ -package org.unicode.jsp; - -import com.ibm.icu.text.UTF16; -import com.ibm.icu.text.UnicodeSet; -import java.util.Comparator; -import java.util.List; -import org.unicode.props.UnicodeProperty; - -public class MySymbolTable extends UnicodeSet.XSymbolTable { - private UnicodeRegex unicodeRegex; - private UnicodeProperty.Factory factory; - - public MySymbolTable(UnicodeProperty.Factory propertyFactory) { - factory = propertyFactory; - unicodeRegex = new UnicodeRegex().setSymbolTable(this); - } - - // public boolean applyPropertyAlias0(String propertyName, - // String propertyValue, UnicodeSet result) { - // if (!propertyName.contains("*")) { - // return applyPropertyAlias(propertyName, propertyValue, result); - // } - // String[] propertyNames = propertyName.split("[*]"); - // for (int i = propertyNames.length - 1; i >= 0; ++i) { - // String pname = propertyNames[i]; - // - // } - // return null; - // } - - public boolean applyPropertyAlias( - String propertyName, String propertyValue, UnicodeSet result) { - boolean status = false; - boolean invert = false; - int posNotEqual = propertyName.indexOf('\u2260'); - int posColon = propertyName.indexOf(':'); - if (posNotEqual >= 0 || posColon >= 0) { - if (posNotEqual < 0) posNotEqual = propertyName.length(); - if (posColon < 0) posColon = propertyName.length(); - int opPos = posNotEqual < posColon ? posNotEqual : posColon; - propertyValue = - propertyValue.length() == 0 - ? propertyName.substring(opPos + 1) - : propertyName.substring(opPos + 1) + "=" + propertyValue; - propertyName = propertyName.substring(0, opPos); - if (posNotEqual < posColon) { - invert = true; - } - } - if (propertyName.endsWith("!")) { - propertyName = propertyName.substring(0, propertyName.length() - 1); - invert = !invert; - } - propertyValue = propertyValue.trim(); - if (propertyValue.length() != 0) { - status = applyPropertyAlias0(propertyName, propertyValue, result); - } else { - try { - status = applyPropertyAlias0("gc", propertyName, result); - } catch (Exception e) { - } - ; - if (!status) { - try { - status = applyPropertyAlias0("sc", propertyName, result); - } catch (Exception e) { - } - ; - if (!status) { - try { - status = applyPropertyAlias0(propertyName, "Yes", result); - } catch (Exception e) { - } - ; - if (!status) { - status = applyPropertyAlias0(propertyName, "", result); - } - } - } - } - if (status && invert) { - result.complement(); - } - return status; - } - - public boolean applyPropertyAlias0( - String propertyName, String propertyValue, UnicodeSet result) { - result.clear(); - UnicodeProperty.PatternMatcher patternMatcher = null; - if (propertyValue.length() > 1 - && propertyValue.startsWith("/") - && propertyValue.endsWith("/")) { - String fixedRegex = - unicodeRegex.transform(propertyValue.substring(1, propertyValue.length() - 1)); - patternMatcher = new UnicodeProperty.RegexMatcher().set(fixedRegex); - } - UnicodeProperty otherProperty = null; - boolean testCp = false; - if (propertyValue.length() > 1 - && propertyValue.startsWith("@") - && propertyValue.endsWith("@")) { - String otherPropName = propertyValue.substring(1, propertyValue.length() - 1).trim(); - if ("cp".equalsIgnoreCase(otherPropName)) { - testCp = true; - } else { - otherProperty = factory.getProperty(otherPropName); - } - } - boolean isAge = UnicodeProperty.equalNames("age", propertyName); - UnicodeProperty prop = factory.getProperty(propertyName); - if (prop != null) { - UnicodeSet set; - if (testCp) { - set = new UnicodeSet(); - for (int i = 0; i <= 0x10FFFF; ++i) { - if (UnicodeProperty.equals(i, prop.getValue(i))) { - set.add(i); - } - } - } else if (otherProperty != null) { - set = new UnicodeSet(); - for (int i = 0; i <= 0x10FFFF; ++i) { - String v1 = prop.getValue(i); - String v2 = otherProperty.getValue(i); - if (UnicodeProperty.equals(v1, v2)) { - set.add(i); - } - } - } else if (patternMatcher == null) { - if (!isValid(prop, propertyValue)) { - throw new IllegalArgumentException( - "The value '" - + propertyValue - + "' is illegal. Values for " - + propertyName - + " must be in " - + prop.getAvailableValues() - + " or in " - + prop.getValueAliases()); - } - if (isAge) { - set = - prop.getSet( - new ComparisonMatcher( - propertyValue, ComparisonMatcher.Relation.geq)); - } else { - set = prop.getSet(propertyValue); - } - } else if (isAge) { - set = new UnicodeSet(); - List values = prop.getAvailableValues(); - for (String value : values) { - if (patternMatcher.test(value)) { - for (String other : values) { - if (other.compareTo(value) <= 0) { - set.addAll(prop.getSet(other)); - } - } - } - } - } else { - set = prop.getSet(patternMatcher); - } - result.addAll(set); - return true; - } - throw new IllegalArgumentException("Illegal property: " + propertyName); - } - - private boolean isValid(UnicodeProperty prop, String propertyValue) { - // if (prop.getName().equals("General_Category")) { - // if (propertyValue) - // } - return prop.isValidValue(propertyValue); - } - - public static class ComparisonMatcher implements UnicodeProperty.PatternMatcher { - Relation relation; - - enum Relation { - less, - leq, - equal, - geq, - greater - } - - static Comparator comparator = new UTF16.StringComparator(true, false, 0); - - String pattern; - - public ComparisonMatcher(String pattern, Relation comparator) { - this.relation = comparator; - this.pattern = pattern; - } - - @Override - public boolean test(String value) { - int comp = comparator.compare(pattern, value.toString()); - switch (relation) { - case less: - return comp < 0; - case leq: - return comp <= 0; - default: - return comp == 0; - case geq: - return comp >= 0; - case greater: - return comp > 0; - } - } - - public UnicodeProperty.PatternMatcher set(String pattern) { - this.pattern = pattern; - return this; - } - } - - public static void setDefaultXSymbolTable(UnicodeProperty.Factory factory) { - UnicodeSet.setDefaultXSymbolTable(new MySymbolTable(factory)); - UnicodeProperty.ResetCacheProperties(); - } -} diff --git a/unicodetools/src/test/java/org/unicode/propstest/TestXUnicodeSet.java b/unicodetools/src/test/java/org/unicode/propstest/TestXUnicodeSet.java deleted file mode 100644 index a75e6e17f..000000000 --- a/unicodetools/src/test/java/org/unicode/propstest/TestXUnicodeSet.java +++ /dev/null @@ -1,49 +0,0 @@ -package org.unicode.propstest; - -import com.ibm.icu.text.UnicodeSet; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; -import org.unicode.jsp.MySymbolTable; -import org.unicode.props.IndexUnicodeProperties; -import org.unicode.props.UnicodeProperty; -import org.unicode.text.UCD.Default; -import org.unicode.unittest.TestFmwkMinusMinus; - -public class TestXUnicodeSet extends TestFmwkMinusMinus { - private static final UnicodeProperty name; - private static final UnicodeProperty age; - private static IndexUnicodeProperties IUP = IndexUnicodeProperties.make(Default.ucdVersion()); - - static { - name = IUP.getProperty("name"); - age = IUP.getProperty("age"); - for (String prop : IUP.getAvailableNames()) { - // System.out.println(prop); - UnicodeProperty property = IUP.getProperty(prop); - } - } - - @Disabled("Broken") - @Test - public void TestAge() { - try { - MySymbolTable.setDefaultXSymbolTable(IUP); - - UnicodeSet v70 = new UnicodeSet("[:age=7.0:]").complement().complement(); - UnicodeSet v63 = new UnicodeSet("[:age=6.3:]").complement().complement(); - assertNotEquals("", UnicodeSet.EMPTY, v70); - assertNotEquals("", 0, v70.size()); - assertNotEquals("", 0, v63.size()); - // for (String s : new UnicodeSet("[[:age=7.0:]-[:age=6.3:]]")) { - // System.out.println(Utility.hex(s) + "\t" + - // name.getValue(s.codePointAt(0))); - // } - // for (String s : new UnicodeSet("[:name=/MARK/:]")) { - // System.out.println(Utility.hex(s) + "\t" + age.getValue(s.codePointAt(0)) - // + "\t" + name.getValue(s.codePointAt(0))); - // } - } finally { - MySymbolTable.setDefaultXSymbolTable(null); - } - } -}