Search in sources :

Example 41 with BreakIterator

use of java.text.BreakIterator in project robovm by robovm.

the class BreakIteratorTest method testFollowing.

public void testFollowing() {
    BreakIterator it = BreakIterator.getCharacterInstance(Locale.US);
    it.setText("hello");
    try {
        it.following(-1);
        fail();
    } catch (IllegalArgumentException expected) {
    // Expected exception
    }
    assertEquals(1, it.following(0));
    assertEquals(2, it.following(1));
    assertEquals(5, it.following(4));
    assertEquals(BreakIterator.DONE, it.following(5));
    try {
        it.following(6);
        fail();
    } catch (IllegalArgumentException expected) {
    // Expected exception
    }
}
Also used : BreakIterator(java.text.BreakIterator)

Example 42 with BreakIterator

use of java.text.BreakIterator in project robovm by robovm.

the class BreakIteratorTest method testStress.

// http://b/7307154 - we used to pin an unbounded number of char[]s, relying on finalization.
public void testStress() throws Exception {
    char[] cs = { 'a' };
    for (int i = 0; i < 4096; ++i) {
        BreakIterator it = BreakIterator.getWordInstance(Locale.US);
        it.setText(new String(cs));
    }
}
Also used : BreakIterator(java.text.BreakIterator)

Example 43 with BreakIterator

use of java.text.BreakIterator in project robovm by robovm.

the class BreakIteratorTest method testGetWordInstanceLocale.

public void testGetWordInstanceLocale() {
    BreakIterator it1 = BreakIterator.getWordInstance(Locale.CANADA_FRENCH);
    assertTrue("Incorrect BreakIterator", it1 != BreakIterator.getWordInstance());
    BreakIterator it2 = BreakIterator.getWordInstance(new Locale("bad locale"));
    assertTrue("Incorrect BreakIterator", it2 != BreakIterator.getWordInstance());
}
Also used : Locale(java.util.Locale) BreakIterator(java.text.BreakIterator)

Example 44 with BreakIterator

use of java.text.BreakIterator in project smali by JesusFreke.

the class StringWrapper method wrapStringOnBreaks.

/**
     * Splits the given string into lines of maximum width maxWidth. The splitting is done using the current locale's
     * rules for splitting lines.
     *
     * @param string The string to split
     * @param maxWidth The maximum length of any line
     * @return An iterable of Strings containing the wrapped lines
     */
public static Iterable<String> wrapStringOnBreaks(@Nonnull final String string, final int maxWidth) {
    // TODO: should we strip any trailing newlines?
    final BreakIterator breakIterator = BreakIterator.getLineInstance();
    breakIterator.setText(string);
    return new Iterable<String>() {

        @Override
        public Iterator<String> iterator() {
            return new Iterator<String>() {

                private int currentLineStart = 0;

                private boolean nextLineSet = false;

                private String nextLine;

                @Override
                public boolean hasNext() {
                    if (!nextLineSet) {
                        calculateNext();
                    }
                    return nextLine != null;
                }

                private void calculateNext() {
                    int lineEnd = currentLineStart;
                    while (true) {
                        lineEnd = breakIterator.following(lineEnd);
                        if (lineEnd == BreakIterator.DONE) {
                            lineEnd = breakIterator.last();
                            if (lineEnd <= currentLineStart) {
                                nextLine = null;
                                nextLineSet = true;
                                return;
                            }
                            break;
                        }
                        if (lineEnd - currentLineStart > maxWidth) {
                            lineEnd = breakIterator.preceding(lineEnd);
                            if (lineEnd <= currentLineStart) {
                                lineEnd = currentLineStart + maxWidth;
                            }
                            break;
                        }
                        if (string.charAt(lineEnd - 1) == '\n') {
                            nextLine = string.substring(currentLineStart, lineEnd - 1);
                            nextLineSet = true;
                            currentLineStart = lineEnd;
                            return;
                        }
                    }
                    nextLine = string.substring(currentLineStart, lineEnd);
                    nextLineSet = true;
                    currentLineStart = lineEnd;
                }

                @Override
                public String next() {
                    String ret = nextLine;
                    nextLine = null;
                    nextLineSet = false;
                    return ret;
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    };
}
Also used : Iterator(java.util.Iterator) BreakIterator(java.text.BreakIterator) BreakIterator(java.text.BreakIterator)

Example 45 with BreakIterator

use of java.text.BreakIterator in project jdk8u_jdk by JetBrains.

the class ConditionalSpecialCasing method isFinalCased.

/**
     * Implements the "Final_Cased" condition
     *
     * Specification: Within the closest word boundaries containing C, there is a cased
     * letter before C, and there is no cased letter after C.
     *
     * Regular Expression:
     *   Before C: [{cased==true}][{wordBoundary!=true}]*
     *   After C: !([{wordBoundary!=true}]*[{cased}])
     */
private static boolean isFinalCased(String src, int index, Locale locale) {
    BreakIterator wordBoundary = BreakIterator.getWordInstance(locale);
    wordBoundary.setText(src);
    int ch;
    // Look for a preceding 'cased' letter
    for (int i = index; (i >= 0) && !wordBoundary.isBoundary(i); i -= Character.charCount(ch)) {
        ch = src.codePointBefore(i);
        if (isCased(ch)) {
            int len = src.length();
            // Check that there is no 'cased' letter after the index
            for (i = index + Character.charCount(src.codePointAt(index)); (i < len) && !wordBoundary.isBoundary(i); i += Character.charCount(ch)) {
                ch = src.codePointAt(i);
                if (isCased(ch)) {
                    return false;
                }
            }
            return true;
        }
    }
    return false;
}
Also used : BreakIterator(java.text.BreakIterator)

Aggregations

BreakIterator (java.text.BreakIterator)59 ArrayList (java.util.ArrayList)10 Locale (java.util.Locale)6 IntPair (edu.illinois.cs.cogcomp.core.datastructures.IntPair)3 BytesRef (org.apache.lucene.util.BytesRef)3 Snippet (org.apache.lucene.search.highlight.Snippet)2 Intent (android.content.Intent)1 TagElement (com.google.devtools.j2objc.ast.TagElement)1 Pair (edu.illinois.cs.cogcomp.core.datastructures.Pair)1 TextAnnotation (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation)1 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 PriorityQueue (java.util.PriorityQueue)1 JComponent (javax.swing.JComponent)1 Text (org.apache.hadoop.io.Text)1 Analyzer (org.apache.lucene.analysis.Analyzer)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 Encoder (org.apache.lucene.search.highlight.Encoder)1 CustomSeparatorBreakIterator (org.apache.lucene.search.postingshighlight.CustomSeparatorBreakIterator)1 CustomPassageFormatter (org.apache.lucene.search.uhighlight.CustomPassageFormatter)1