Search in sources :

Example 1 with UPropertyAliases

use of android.icu.impl.UPropertyAliases in project j2objc by google.

the class UnicodeSet method applyPropertyAlias.

/**
 * Modifies this set to contain those code points which have the
 * given value for the given property.  Prior contents of this
 * set are lost.
 * @param propertyAlias A string of the property alias.
 * @param valueAlias A string of the value alias.
 * @param symbols if not null, then symbols are first called to see if a property
 * is available. If true, then everything else is skipped.
 * @return this set
 */
public UnicodeSet applyPropertyAlias(String propertyAlias, String valueAlias, SymbolTable symbols) {
    checkFrozen();
    int p;
    int v;
    boolean mustNotBeEmpty = false, invert = false;
    if (symbols != null && (symbols instanceof XSymbolTable) && ((XSymbolTable) symbols).applyPropertyAlias(propertyAlias, valueAlias, this)) {
        return this;
    }
    if (XSYMBOL_TABLE != null) {
        if (XSYMBOL_TABLE.applyPropertyAlias(propertyAlias, valueAlias, this)) {
            return this;
        }
    }
    if (valueAlias.length() > 0) {
        p = UCharacter.getPropertyEnum(propertyAlias);
        // Treat gc as gcm
        if (p == UProperty.GENERAL_CATEGORY) {
            p = UProperty.GENERAL_CATEGORY_MASK;
        }
        if ((p >= UProperty.BINARY_START && p < UProperty.BINARY_LIMIT) || (p >= UProperty.INT_START && p < UProperty.INT_LIMIT) || (p >= UProperty.MASK_START && p < UProperty.MASK_LIMIT)) {
            try {
                v = UCharacter.getPropertyValueEnum(p, valueAlias);
            } catch (IllegalArgumentException e) {
                // Handle numeric CCC
                if (p == UProperty.CANONICAL_COMBINING_CLASS || p == UProperty.LEAD_CANONICAL_COMBINING_CLASS || p == UProperty.TRAIL_CANONICAL_COMBINING_CLASS) {
                    v = Integer.parseInt(PatternProps.trimWhiteSpace(valueAlias));
                    // old code was wrong; anything between 0 and 255 is valid even if unused.
                    if (v < 0 || v > 255)
                        throw e;
                } else {
                    throw e;
                }
            }
        } else {
            switch(p) {
                case UProperty.NUMERIC_VALUE:
                    {
                        double value = Double.parseDouble(PatternProps.trimWhiteSpace(valueAlias));
                        applyFilter(new NumericValueFilter(value), UCharacterProperty.SRC_CHAR);
                        return this;
                    }
                case UProperty.NAME:
                    {
                        // Must munge name, since
                        // UCharacter.charFromName() does not do
                        // 'loose' matching.
                        String buf = mungeCharName(valueAlias);
                        int ch = UCharacter.getCharFromExtendedName(buf);
                        if (ch == -1) {
                            throw new IllegalArgumentException("Invalid character name");
                        }
                        clear();
                        add_unchecked(ch);
                        return this;
                    }
                case UProperty.UNICODE_1_NAME:
                    // ICU 49 deprecates the Unicode_1_Name property APIs.
                    throw new IllegalArgumentException("Unicode_1_Name (na1) not supported");
                case UProperty.AGE:
                    {
                        // Must munge name, since
                        // VersionInfo.getInstance() does not do
                        // 'loose' matching.
                        VersionInfo version = VersionInfo.getInstance(mungeCharName(valueAlias));
                        applyFilter(new VersionFilter(version), UCharacterProperty.SRC_PROPSVEC);
                        return this;
                    }
                case UProperty.SCRIPT_EXTENSIONS:
                    v = UCharacter.getPropertyValueEnum(UProperty.SCRIPT, valueAlias);
                    // fall through to calling applyIntPropertyValue()
                    break;
                default:
                    // don't support (yet).
                    throw new IllegalArgumentException("Unsupported property");
            }
        }
    } else {
        // valueAlias is empty.  Interpret as General Category, Script,
        // Binary property, or ANY or ASCII.  Upon success, p and v will
        // be set.
        UPropertyAliases pnames = UPropertyAliases.INSTANCE;
        p = UProperty.GENERAL_CATEGORY_MASK;
        v = pnames.getPropertyValueEnum(p, propertyAlias);
        if (v == UProperty.UNDEFINED) {
            p = UProperty.SCRIPT;
            v = pnames.getPropertyValueEnum(p, propertyAlias);
            if (v == UProperty.UNDEFINED) {
                p = pnames.getPropertyEnum(propertyAlias);
                if (p == UProperty.UNDEFINED) {
                    p = -1;
                }
                if (p >= UProperty.BINARY_START && p < UProperty.BINARY_LIMIT) {
                    v = 1;
                } else if (p == -1) {
                    if (0 == UPropertyAliases.compare(ANY_ID, propertyAlias)) {
                        set(MIN_VALUE, MAX_VALUE);
                        return this;
                    } else if (0 == UPropertyAliases.compare(ASCII_ID, propertyAlias)) {
                        set(0, 0x7F);
                        return this;
                    } else if (0 == UPropertyAliases.compare(ASSIGNED, propertyAlias)) {
                        // [:Assigned:]=[:^Cn:]
                        p = UProperty.GENERAL_CATEGORY_MASK;
                        v = (1 << UCharacter.UNASSIGNED);
                        invert = true;
                    } else {
                        // Property name was never matched.
                        throw new IllegalArgumentException("Invalid property alias: " + propertyAlias + "=" + valueAlias);
                    }
                } else {
                    // must be supplied.
                    throw new IllegalArgumentException("Missing property value");
                }
            }
        }
    }
    applyIntPropertyValue(p, v);
    if (invert) {
        complement();
    }
    if (mustNotBeEmpty && isEmpty()) {
        // invalid input.
        throw new IllegalArgumentException("Invalid property value");
    }
    return this;
}
Also used : VersionInfo(android.icu.util.VersionInfo) UPropertyAliases(android.icu.impl.UPropertyAliases)

Aggregations

UPropertyAliases (android.icu.impl.UPropertyAliases)1 VersionInfo (android.icu.util.VersionInfo)1