Search in sources :

Example 1 with SpanCondition

use of android.icu.text.UnicodeSet.SpanCondition in project j2objc by google.

the class UnicodeSetSpanner method replaceFrom.

/**
 * Replace all matching spans in sequence by replacement, according to the countMethod and spanCondition.
 * The code alternates spans; see the class doc for {@link UnicodeSetSpanner} for a note about boundary conditions.
 * @param sequence
 *            charsequence to replace matching spans in.
 * @param replacement
 *            replacement sequence. To delete, use ""
 * @param countMethod
 *            whether to treat an entire span as a match, or individual elements as matches
 * @param spanCondition
 *            specify whether to modify the matching spans (CONTAINED or SIMPLE) or the non-matching
 *            (NOT_CONTAINED)
 * @return modified string.
 */
public String replaceFrom(CharSequence sequence, CharSequence replacement, CountMethod countMethod, SpanCondition spanCondition) {
    SpanCondition copySpan = spanCondition == SpanCondition.NOT_CONTAINED ? SpanCondition.SIMPLE : SpanCondition.NOT_CONTAINED;
    final boolean remove = replacement.length() == 0;
    StringBuilder result = new StringBuilder();
    // TODO, we can optimize this to
    // avoid this allocation unless needed
    final int length = sequence.length();
    OutputInt spanCount = null;
    for (int endCopy = 0; endCopy != length; ) {
        int endModify;
        if (countMethod == CountMethod.WHOLE_SPAN) {
            endModify = unicodeSet.span(sequence, endCopy, spanCondition);
        } else {
            if (spanCount == null) {
                spanCount = new OutputInt();
            }
            endModify = unicodeSet.spanAndCount(sequence, endCopy, spanCondition, spanCount);
        }
        if (remove || endModify == 0) {
        // do nothing
        } else if (countMethod == CountMethod.WHOLE_SPAN) {
            result.append(replacement);
        } else {
            for (int i = spanCount.value; i > 0; --i) {
                result.append(replacement);
            }
        }
        if (endModify == length) {
            break;
        }
        endCopy = unicodeSet.span(sequence, endModify, copySpan);
        result.append(sequence.subSequence(endModify, endCopy));
    }
    return result.toString();
}
Also used : OutputInt(android.icu.util.OutputInt) SpanCondition(android.icu.text.UnicodeSet.SpanCondition)

Example 2 with SpanCondition

use of android.icu.text.UnicodeSet.SpanCondition in project j2objc by google.

the class UnicodeSetStringSpanTest method getSpans.

/*
     * Count spans on a string with the method according to type and set the span limits. The set may be the complement
     * of the original. When using spanBack() and comparing with span(), use a span condition for the first spanBack()
     * according to the expected number of spans. Sets typeName to an empty string if there is no such type. Returns -1
     * if the span option is filtered out.
     */
static int getSpans(final UnicodeSetWithStrings set, boolean isComplement, final String s, int whichSpans, int type, String[] typeName, int[] limits, int limitsCapacity, int expectCount) {
    final UnicodeSet realSet = set.getSet();
    int start, count, i;
    SpanCondition spanCondition, firstSpanCondition, contained;
    boolean isForward;
    int length = s.length();
    if (type < 0 || 7 < type) {
        typeName[0] = null;
        return 0;
    }
    final String[] typeNames16 = { "contains", "contains(LM)", "span", "span(LM)", "containsBack", "containsBack(LM)", "spanBack", "spanBack(LM)" };
    typeName[0] = typeNames16[type];
    // filter span options
    if (type <= 3) {
        // span forward
        if ((whichSpans & SPAN_FWD) == 0) {
            return -1;
        }
        isForward = true;
    } else {
        // span backward
        if ((whichSpans & SPAN_BACK) == 0) {
            return -1;
        }
        isForward = false;
    }
    if ((type & 1) == 0) {
        // use SpanCondition.CONTAINED
        if ((whichSpans & SPAN_CONTAINED) == 0) {
            return -1;
        }
        contained = SpanCondition.CONTAINED;
    } else {
        // use SIMPLE
        if ((whichSpans & SPAN_SIMPLE) == 0) {
            return -1;
        }
        contained = SpanCondition.SIMPLE;
    }
    // Default first span condition for going forward with an uncomplemented set.
    spanCondition = SpanCondition.NOT_CONTAINED;
    if (isComplement) {
        spanCondition = invertSpanCondition(spanCondition, contained);
    }
    // First span condition for span(), used to terminate the spanBack() iteration.
    firstSpanCondition = spanCondition;
    // expectCount and just start with firstSpanCondition.
    if (!isForward && (whichSpans & SPAN_FWD) != 0 && (expectCount & 1) == 0) {
        spanCondition = invertSpanCondition(spanCondition, contained);
    }
    count = 0;
    switch(type) {
        case 0:
        case 1:
            start = 0;
            for (; ; ) {
                start += containsSpanUTF16(set, s.substring(start), spanCondition);
                if (count < limitsCapacity) {
                    limits[count] = start;
                }
                ++count;
                if (start >= length) {
                    break;
                }
                spanCondition = invertSpanCondition(spanCondition, contained);
            }
            break;
        case 2:
        case 3:
            start = 0;
            for (; ; ) {
                start = realSet.span(s, start, spanCondition);
                if (count < limitsCapacity) {
                    limits[count] = start;
                }
                ++count;
                if (start >= length) {
                    break;
                }
                spanCondition = invertSpanCondition(spanCondition, contained);
            }
            break;
        case 4:
        case 5:
            for (; ; ) {
                ++count;
                if (count <= limitsCapacity) {
                    limits[limitsCapacity - count] = length;
                }
                length = containsSpanBackUTF16(set, s, length, spanCondition);
                if (length == 0 && spanCondition == firstSpanCondition) {
                    break;
                }
                spanCondition = invertSpanCondition(spanCondition, contained);
            }
            if (count < limitsCapacity) {
                for (i = count; i-- > 0; ) {
                    limits[i] = limits[limitsCapacity - count + i];
                }
            }
            break;
        case 6:
        case 7:
            for (; ; ) {
                ++count;
                if (count <= limitsCapacity) {
                    limits[limitsCapacity - count] = length >= 0 ? length : s.length();
                }
                length = realSet.spanBack(s, length, spanCondition);
                if (length == 0 && spanCondition == firstSpanCondition) {
                    break;
                }
                spanCondition = invertSpanCondition(spanCondition, contained);
            }
            if (count < limitsCapacity) {
                for (i = count; i-- > 0; ) {
                    limits[i] = limits[limitsCapacity - count + i];
                }
            }
            break;
        default:
            typeName = null;
            return -1;
    }
    return count;
}
Also used : SpanCondition(android.icu.text.UnicodeSet.SpanCondition) UnicodeSet(android.icu.text.UnicodeSet)

Example 3 with SpanCondition

use of android.icu.text.UnicodeSet.SpanCondition in project j2objc by google.

the class UnicodeSetSpanner method countIn.

/**
 * Returns the number of matching characters found in a character sequence.
 * The code alternates spans; see the class doc for {@link UnicodeSetSpanner} for a note about boundary conditions.
 * @param sequence
 *            the sequence to count characters in
 * @param countMethod
 *            whether to treat an entire span as a match, or individual elements as matches
 * @param spanCondition
 *            the spanCondition to use. SIMPLE or CONTAINED means only count the elements in the span;
 *            NOT_CONTAINED is the reverse.
 *            <br><b>WARNING: </b> when a UnicodeSet contains strings, there may be unexpected behavior in edge cases.
 * @return the count. Zero if there are none.
 */
public int countIn(CharSequence sequence, CountMethod countMethod, SpanCondition spanCondition) {
    int count = 0;
    int start = 0;
    SpanCondition skipSpan = spanCondition == SpanCondition.NOT_CONTAINED ? SpanCondition.SIMPLE : SpanCondition.NOT_CONTAINED;
    final int length = sequence.length();
    OutputInt spanCount = null;
    while (start != length) {
        int endOfSpan = unicodeSet.span(sequence, start, skipSpan);
        if (endOfSpan == length) {
            break;
        }
        if (countMethod == CountMethod.WHOLE_SPAN) {
            start = unicodeSet.span(sequence, endOfSpan, spanCondition);
            count += 1;
        } else {
            if (spanCount == null) {
                spanCount = new OutputInt();
            }
            start = unicodeSet.spanAndCount(sequence, endOfSpan, spanCondition, spanCount);
            count += spanCount.value;
        }
    }
    return count;
}
Also used : OutputInt(android.icu.util.OutputInt) SpanCondition(android.icu.text.UnicodeSet.SpanCondition)

Example 4 with SpanCondition

use of android.icu.text.UnicodeSet.SpanCondition in project j2objc by google.

the class UnicodeSetStringSpanTest method TestStringWithUnpairedSurrogateSpan.

@Test
public void TestStringWithUnpairedSurrogateSpan() {
    String string = Utility.unescape(stringWithUnpairedSurrogate);
    UnicodeSet uset = new UnicodeSet(Utility.unescape(patternWithUnpairedSurrogate));
    SpanCondition spanCondition = SpanCondition.NOT_CONTAINED;
    int start = 17;
    int expect = 5;
    UnicodeSetWithStrings set = new UnicodeSetWithStrings(uset);
    int len = containsSpanUTF16(set, string.substring(start), spanCondition);
    if (expect != len) {
        errln(String.format("FAIL: containsSpanUTF16(patternWithUnpairedSurrogate, \"%s(%d)\") = %d (expect %d)", string, start, len, expect));
    }
    len = uset.span(string, start, spanCondition) - start;
    if (expect != len) {
        errln(String.format("FAIL: UnicodeSet(patternWithUnpairedSurrogate).span(\"%s\", %d) = %d (expect %d)", string, start, len, expect));
    }
}
Also used : SpanCondition(android.icu.text.UnicodeSet.SpanCondition) UnicodeSet(android.icu.text.UnicodeSet) Test(org.junit.Test)

Example 5 with SpanCondition

use of android.icu.text.UnicodeSet.SpanCondition in project j2objc by google.

the class UnicodeSetStringSpanTest method TestInterestingStringSpan.

@Test
public void TestInterestingStringSpan() {
    UnicodeSet uset = new UnicodeSet(Utility.unescape(unicodeSet1));
    SpanCondition spanCondition = SpanCondition.NOT_CONTAINED;
    int expect = 2;
    int start = 14;
    int c = 0xd840;
    boolean contains = uset.contains(c);
    if (false != contains) {
        errln(String.format("FAIL: UnicodeSet(unicodeSet1).contains(%d) = true (expect false)", c));
    }
    UnicodeSetWithStrings set = new UnicodeSetWithStrings(uset);
    int len = containsSpanUTF16(set, interestingString.substring(start), spanCondition);
    if (expect != len) {
        errln(String.format("FAIL: containsSpanUTF16(unicodeSet1, \"%s(%d)\") = %d (expect %d)", interestingString, start, len, expect));
    }
    len = uset.span(interestingString, start, spanCondition) - start;
    if (expect != len) {
        errln(String.format("FAIL: UnicodeSet(unicodeSet1).span(\"%s\", %d) = %d (expect %d)", interestingString, start, len, expect));
    }
}
Also used : SpanCondition(android.icu.text.UnicodeSet.SpanCondition) UnicodeSet(android.icu.text.UnicodeSet) Test(org.junit.Test)

Aggregations

SpanCondition (android.icu.text.UnicodeSet.SpanCondition)5 UnicodeSet (android.icu.text.UnicodeSet)3 OutputInt (android.icu.util.OutputInt)2 Test (org.junit.Test)2