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
}
}
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));
}
}
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());
}
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();
}
};
}
};
}
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;
}
Aggregations