Search in sources :

Example 11 with StringSearch

use of android.icu.text.StringSearch in project j2objc by google.

the class SearchTest method TestReset.

@Test
public void TestReset() {
    StringCharacterIterator text = new StringCharacterIterator("fish fish");
    String pattern = "s";
    StringSearch strsrch = new StringSearch(pattern, text, m_en_us_, null);
    strsrch.setOverlapping(true);
    strsrch.setCanonical(true);
    strsrch.setIndex(9);
    strsrch.reset();
    if (strsrch.isCanonical() || strsrch.isOverlapping() || strsrch.getIndex() != 0 || strsrch.getMatchLength() != 0 || strsrch.getMatchStart() != SearchIterator.DONE) {
        errln("Error resetting string search");
    }
    strsrch.previous();
    if (strsrch.getMatchStart() != 7 || strsrch.getMatchLength() != 1) {
        errln("Error resetting string search\n");
    }
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) StringSearch(android.icu.text.StringSearch) Test(org.junit.Test)

Example 12 with StringSearch

use of android.icu.text.StringSearch in project j2objc by google.

the class SearchTest method TestGetSetOffsetCanonical.

@Test
public void TestGetSetOffsetCanonical() {
    String text = "text";
    String pattern = "pattern";
    StringSearch strsrch = null;
    try {
        strsrch = new StringSearch(pattern, new StringCharacterIterator(text), m_en_us_, null);
    } catch (Exception e) {
        errln("Fail to open StringSearch!");
        return;
    }
    strsrch.setCanonical(true);
    // TODO: setCanonical is not sufficient for canonical match. See #10725
    strsrch.getCollator().setDecomposition(Collator.CANONICAL_DECOMPOSITION);
    /* testing out of bounds error */
    try {
        strsrch.setIndex(-1);
        errln("Error expecting set offset error");
    } catch (IndexOutOfBoundsException e) {
        logln("PASS: strsrch.setIndex(-1) failed as expected");
    }
    try {
        strsrch.setIndex(128);
        errln("Error expecting set offset error");
    } catch (IndexOutOfBoundsException e) {
        logln("PASS: strsrch.setIndex(128) failed as expected");
    }
    for (int index = 0; index < BASICCANONICAL.length; index++) {
        SearchData search = BASICCANONICAL[index];
        text = search.text;
        pattern = search.pattern;
        strsrch.setTarget(new StringCharacterIterator(text));
        strsrch.setPattern(pattern);
        int count = 0;
        int matchindex = search.offset[count];
        while (matchindex >= 0) {
            int matchlength = search.size[count];
            strsrch.next();
            if (matchindex != strsrch.getMatchStart() || matchlength != strsrch.getMatchLength()) {
                errln("Text: " + text);
                errln("Pattern: " + strsrch.getPattern());
                errln("Error match found at " + strsrch.getMatchStart() + ", " + strsrch.getMatchLength());
                return;
            }
            matchindex = search.offset[count + 1] == -1 ? -1 : search.offset[count + 2];
            if (search.offset[count + 1] != -1) {
                strsrch.setIndex(search.offset[count + 1] + 1);
                if (strsrch.getIndex() != search.offset[count + 1] + 1) {
                    errln("Error setting offset");
                    return;
                }
            }
            count += 2;
        }
        strsrch.next();
        if (strsrch.getMatchStart() != StringSearch.DONE) {
            errln("Text: " + text);
            errln("Pattern: %s" + strsrch.getPattern());
            errln("Error match found at " + strsrch.getMatchStart() + ", " + strsrch.getMatchLength());
            return;
        }
    }
    strsrch.getCollator().setStrength(TERTIARY);
    strsrch.getCollator().setDecomposition(Collator.NO_DECOMPOSITION);
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) StringSearch(android.icu.text.StringSearch) Test(org.junit.Test)

Example 13 with StringSearch

use of android.icu.text.StringSearch in project j2objc by google.

the class SearchTest method assertCanonicalEqual.

boolean assertCanonicalEqual(SearchData search) {
    Collator collator = getCollator(search.collator);
    BreakIterator breaker = getBreakIterator(search.breaker);
    StringSearch strsrch;
    String text = search.text;
    String pattern = search.pattern;
    if (breaker != null) {
        breaker.setText(text);
    }
    collator.setStrength(search.strength);
    collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
    try {
        strsrch = new StringSearch(pattern, new StringCharacterIterator(text), (RuleBasedCollator) collator, breaker);
        strsrch.setElementComparisonType(search.cmpType);
        strsrch.setCanonical(true);
    } catch (Exception e) {
        errln("Error opening string search" + e.getMessage());
        return false;
    }
    if (!assertEqualWithStringSearch(strsrch, search)) {
        collator.setStrength(TERTIARY);
        collator.setDecomposition(Collator.NO_DECOMPOSITION);
        return false;
    }
    collator.setStrength(TERTIARY);
    collator.setDecomposition(Collator.NO_DECOMPOSITION);
    return true;
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) RuleBasedCollator(android.icu.text.RuleBasedCollator) StringSearch(android.icu.text.StringSearch) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) BreakIterator(android.icu.text.BreakIterator)

Example 14 with StringSearch

use of android.icu.text.StringSearch in project j2objc by google.

the class SearchTest method TestInitialization.

@Test
public void TestInitialization() {
    String pattern;
    String text;
    String temp = "a";
    StringSearch result;
    /* simple test on the pattern ce construction */
    pattern = temp + temp;
    text = temp + temp + temp;
    try {
        result = new StringSearch(pattern, new StringCharacterIterator(text), m_en_us_, null);
    } catch (Exception e) {
        errln("Error opening search ");
        return;
    }
    /* testing if an extremely large pattern will fail the initialization */
    pattern = "";
    for (int count = 0; count < 512; count++) {
        pattern += temp;
    }
    try {
        result = new StringSearch(pattern, new StringCharacterIterator(text), m_en_us_, null);
        logln("pattern:" + result.getPattern());
    } catch (Exception e) {
        errln("Fail: an extremely large pattern will fail the initialization");
        return;
    }
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) StringSearch(android.icu.text.StringSearch) Test(org.junit.Test)

Example 15 with StringSearch

use of android.icu.text.StringSearch in project j2objc by google.

the class SearchTest method TestGetMatch.

@Test
public void TestGetMatch() {
    SearchData search = MATCH[0];
    String text = search.text;
    String pattern = search.pattern;
    StringSearch strsrch = null;
    try {
        strsrch = new StringSearch(pattern, new StringCharacterIterator(text), m_en_us_, null);
    } catch (Exception e) {
        errln("Error opening string search ");
        return;
    }
    int count = 0;
    int matchindex = search.offset[count];
    String matchtext;
    while (matchindex >= 0) {
        int matchlength = search.size[count];
        strsrch.next();
        if (matchindex != strsrch.getMatchStart() || matchlength != strsrch.getMatchLength()) {
            errln("Text: " + search.text);
            errln("Pattern: " + strsrch.getPattern());
            errln("Error match found at " + strsrch.getMatchStart() + ", " + strsrch.getMatchLength());
            return;
        }
        count++;
        matchtext = strsrch.getMatchedText();
        if (matchtext.length() != matchlength) {
            errln("Error getting match text");
        }
        matchindex = search.offset[count];
    }
    strsrch.next();
    if (strsrch.getMatchStart() != StringSearch.DONE || strsrch.getMatchLength() != 0) {
        errln("Error end of match not found");
    }
    matchtext = strsrch.getMatchedText();
    if (matchtext != null) {
        errln("Error getting null matches");
    }
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) StringSearch(android.icu.text.StringSearch) Test(org.junit.Test)

Aggregations

StringSearch (android.icu.text.StringSearch)27 StringCharacterIterator (java.text.StringCharacterIterator)27 Test (org.junit.Test)24 RuleBasedCollator (android.icu.text.RuleBasedCollator)14 BreakIterator (android.icu.text.BreakIterator)7 Collator (android.icu.text.Collator)4 ULocale (android.icu.util.ULocale)2 Locale (java.util.Locale)1