Search in sources :

Example 1 with OutputInt

use of android.icu.util.OutputInt 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 OutputInt

use of android.icu.util.OutputInt in project j2objc by google.

the class UnicodeSetTest method TestCharSequenceArgs.

@Test
public void TestCharSequenceArgs() {
    // statics
    assertEquals("CharSequence from", new UnicodeSet("[{abc}]"), UnicodeSet.from(new StringBuilder("abc")));
    assertEquals("CharSequence fromAll", new UnicodeSet("[a-c]"), UnicodeSet.fromAll(new StringBuilder("abc")));
    assertEquals("CharSequence compare", 1.0f, Math.signum(UnicodeSet.compare(new StringBuilder("abc"), 0x61)));
    assertEquals("CharSequence compare", -1.0f, Math.signum(UnicodeSet.compare(0x61, new StringBuilder("abc"))));
    assertEquals("CharSequence compare", 0.0f, Math.signum(UnicodeSet.compare(new StringBuilder("a"), 0x61)));
    assertEquals("CharSequence compare", 0.0f, Math.signum(UnicodeSet.compare(0x61, new StringBuilder("a"))));
    assertEquals("CharSequence getSingleCodePoint", 0x1F466, UnicodeSet.getSingleCodePoint(new StringBuilder("👦")));
    // iterables/arrays
    Iterable<StringBuilder> iterable = Arrays.asList(new StringBuilder("A"), new StringBuilder("B"));
    assertEquals("CharSequence containsAll", true, new UnicodeSet("[AB]").containsAll(iterable));
    assertEquals("CharSequence containsAll", false, new UnicodeSet("[a-cA]").containsAll(iterable));
    assertEquals("CharSequence containsNone", true, new UnicodeSet("[a-c]").containsNone(iterable));
    assertEquals("CharSequence containsNone", false, new UnicodeSet("[a-cA]").containsNone(iterable));
    assertEquals("CharSequence containsSome", true, new UnicodeSet("[a-cA]").containsSome(iterable));
    assertEquals("CharSequence containsSome", false, new UnicodeSet("[a-c]").containsSome(iterable));
    assertEquals("CharSequence addAll", new UnicodeSet("[a-cAB]"), new UnicodeSet("[a-cA]").addAll(new StringBuilder("A"), new StringBuilder("B")));
    assertEquals("CharSequence removeAll", new UnicodeSet("[a-c]"), new UnicodeSet("[a-cA]").removeAll(iterable));
    assertEquals("CharSequence retainAll", new UnicodeSet("[A]"), new UnicodeSet("[a-cA]").retainAll(iterable));
    // UnicodeSet results
    assertEquals("CharSequence add", new UnicodeSet("[Aa-c{abc}{qr}]"), new UnicodeSet("[a-cA{qr}]").add(new StringBuilder("abc")));
    assertEquals("CharSequence retain", new UnicodeSet("[{abc}]"), new UnicodeSet("[a-cA{abc}{qr}]").retain(new StringBuilder("abc")));
    assertEquals("CharSequence remove", new UnicodeSet("[Aa-c{qr}]"), new UnicodeSet("[a-cA{abc}{qr}]").remove(new StringBuilder("abc")));
    assertEquals("CharSequence complement", new UnicodeSet("[Aa-c{qr}]"), new UnicodeSet("[a-cA{abc}{qr}]").complement(new StringBuilder("abc")));
    assertEquals("CharSequence complement", new UnicodeSet("[Aa-c{abc}{qr}]"), new UnicodeSet("[a-cA{qr}]").complement(new StringBuilder("abc")));
    assertEquals("CharSequence addAll", new UnicodeSet("[a-cABC]"), new UnicodeSet("[a-cA]").addAll(new StringBuilder("ABC")));
    assertEquals("CharSequence retainAll", new UnicodeSet("[a-c]"), new UnicodeSet("[a-cA]").retainAll(new StringBuilder("abcB")));
    assertEquals("CharSequence removeAll", new UnicodeSet("[Aab]"), new UnicodeSet("[a-cA]").removeAll(new StringBuilder("cC")));
    assertEquals("CharSequence complementAll", new UnicodeSet("[ABbc]"), new UnicodeSet("[a-cA]").complementAll(new StringBuilder("aB")));
    // containment
    assertEquals("CharSequence contains", true, new UnicodeSet("[a-cA{ab}]").contains(new StringBuilder("ab")));
    assertEquals("CharSequence containsNone", false, new UnicodeSet("[a-cA]").containsNone(new StringBuilder("ab")));
    assertEquals("CharSequence containsSome", true, new UnicodeSet("[a-cA{ab}]").containsSome(new StringBuilder("ab")));
    // spanning
    assertEquals("CharSequence span", 3, new UnicodeSet("[a-cA]").span(new StringBuilder("abc"), SpanCondition.SIMPLE));
    assertEquals("CharSequence span", 3, new UnicodeSet("[a-cA]").span(new StringBuilder("abc"), 1, SpanCondition.SIMPLE));
    assertEquals("CharSequence spanBack", 0, new UnicodeSet("[a-cA]").spanBack(new StringBuilder("abc"), SpanCondition.SIMPLE));
    assertEquals("CharSequence spanBack", 0, new UnicodeSet("[a-cA]").spanBack(new StringBuilder("abc"), 1, SpanCondition.SIMPLE));
    // internal
    OutputInt outCount = new OutputInt();
    assertEquals("CharSequence matchesAt", 2, new UnicodeSet("[a-cA]").matchesAt(new StringBuilder("abc"), 1));
    assertEquals("CharSequence spanAndCount", 3, new UnicodeSet("[a-cA]").spanAndCount(new StringBuilder("abc"), 1, SpanCondition.SIMPLE, outCount));
    assertEquals("CharSequence findIn", 3, new UnicodeSet("[a-cA]").findIn(new StringBuilder("abc"), 1, true));
    assertEquals("CharSequence findLastIn", -1, new UnicodeSet("[a-cA]").findLastIn(new StringBuilder("abc"), 1, true));
    assertEquals("CharSequence add", "c", new UnicodeSet("[abA]").stripFrom(new StringBuilder("abc"), true));
}
Also used : OutputInt(android.icu.util.OutputInt) UnicodeSet(android.icu.text.UnicodeSet) Test(org.junit.Test)

Example 3 with OutputInt

use of android.icu.util.OutputInt 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 OutputInt

use of android.icu.util.OutputInt in project j2objc by google.

the class UnicodeSetStringSpanTest method TestSpanAndCount.

@Test
public void TestSpanAndCount() {
    // a set with no strings
    UnicodeSet abc = new UnicodeSet('a', 'c');
    // a set with an "irrelevant" string (fully contained in the code point set)
    UnicodeSet crlf = new UnicodeSet().add('\n').add('\r').add("\r\n");
    // a set with no "irrelevant" string but some interesting overlaps
    UnicodeSet ab_cd = new UnicodeSet().add('a').add("ab").add("abc").add("cd");
    String s = "ab\n\r\r\n" + UTF16.valueOf(0x50000) + "abcde";
    OutputInt count = new OutputInt();
    assertEquals("abc span[8, 11[", 11, abc.spanAndCount(s, 8, SpanCondition.SIMPLE, count));
    assertEquals("abc count=3", 3, count.value);
    assertEquals("no abc span[2, 8[", 8, abc.spanAndCount(s, 2, SpanCondition.NOT_CONTAINED, count));
    assertEquals("no abc count=5", 5, count.value);
    assertEquals("line endings span[2, 6[", 6, crlf.spanAndCount(s, 2, SpanCondition.CONTAINED, count));
    assertEquals("line endings count=3", 3, count.value);
    assertEquals("no ab+cd span[2, 8[", 8, ab_cd.spanAndCount(s, 2, SpanCondition.NOT_CONTAINED, count));
    assertEquals("no ab+cd count=5", 5, count.value);
    assertEquals("ab+cd span[8, 12[", 12, ab_cd.spanAndCount(s, 8, SpanCondition.CONTAINED, count));
    assertEquals("ab+cd count=2", 2, count.value);
    assertEquals("1x abc span[8, 11[", 11, ab_cd.spanAndCount(s, 8, SpanCondition.SIMPLE, count));
    assertEquals("1x abc count=1", 1, count.value);
    abc.freeze();
    crlf.freeze();
    ab_cd.freeze();
    assertEquals("abc span[8, 11[ (frozen)", 11, abc.spanAndCount(s, 8, SpanCondition.SIMPLE, count));
    assertEquals("abc count=3 (frozen)", 3, count.value);
    assertEquals("no abc span[2, 8[ (frozen)", 8, abc.spanAndCount(s, 2, SpanCondition.NOT_CONTAINED, count));
    assertEquals("no abc count=5 (frozen)", 5, count.value);
    assertEquals("line endings span[2, 6[ (frozen)", 6, crlf.spanAndCount(s, 2, SpanCondition.CONTAINED, count));
    assertEquals("line endings count=3 (frozen)", 3, count.value);
    assertEquals("no ab+cd span[2, 8[ (frozen)", 8, ab_cd.spanAndCount(s, 2, SpanCondition.NOT_CONTAINED, count));
    assertEquals("no ab+cd count=5 (frozen)", 5, count.value);
    assertEquals("ab+cd span[8, 12[ (frozen)", 12, ab_cd.spanAndCount(s, 8, SpanCondition.CONTAINED, count));
    assertEquals("ab+cd count=2 (frozen)", 2, count.value);
    assertEquals("1x abc span[8, 11[ (frozen)", 11, ab_cd.spanAndCount(s, 8, SpanCondition.SIMPLE, count));
    assertEquals("1x abc count=1 (frozen)", 1, count.value);
}
Also used : OutputInt(android.icu.util.OutputInt) UnicodeSet(android.icu.text.UnicodeSet) Test(org.junit.Test)

Aggregations

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