Class BreakIterator
- java.lang.Object
-
- io.github.humbleui.skija.impl.Native
-
- io.github.humbleui.skija.impl.Managed
-
- io.github.humbleui.skija.BreakIterator
-
- All Implemented Interfaces:
java.lang.AutoCloseable,java.lang.Cloneable
public class BreakIterator extends Managed implements java.lang.Cloneable
A class that locates boundaries in text. This class defines a protocol for objects that break up a piece of natural-language text according to a set of criteria. Instances or subclasses of BreakIterator can be provided, for example, to break a piece of text into words, sentences, or logical characters according to the conventions of some language or group of languages. We provide four built-in types of BreakIterator:
- makeSentenceInstance() returns a BreakIterator that locates boundaries between sentences. This is useful for triple-click selection, for example.
- makeWordInstance() returns a BreakIterator that locates boundaries between words. This is useful for double-click selection or "find whole words" searches. This type of BreakIterator makes sure there is a boundary position at the beginning and end of each legal word. (Numbers count as words, too.) Whitespace and punctuation are kept separate from real words.
- makeLineInstance() returns a BreakIterator that locates positions where it is legal for a text editor to wrap lines. This is similar to word breaking, but not the same: punctuation and whitespace are generally kept with words (you don't want a line to start with whitespace, for example), and some special characters can force a position to be considered a line-break position or prevent a position from being a line-break position.
- makeCharacterInstance() returns a BreakIterator that locates boundaries between logical characters. Because of the structure of the Unicode encoding, a logical character may be stored internally as more than one Unicode code point. (A with an umlaut may be stored as an a followed by a separate combining umlaut character, for example, but the user still thinks of it as one character.) This iterator allows various processes (especially text editors) to treat as characters the units of text that a user would think of as characters, rather than the units of text that the computer sees as "characters".
BreakIterator's interface follows an "iterator" model (hence the name), meaning it has a concept of a "current position" and methods like first(), last(), next(), and previous() that update the current position. All BreakIterators uphold the following invariants:
- The beginning and end of the text are always treated as boundary positions.
- The current position of the iterator is always a boundary position (random- access methods move the iterator to the nearest boundary position before or after the specified position, not _to_ the specified position).
- DONE is used as a flag to indicate when iteration has stopped. DONE is only returned when the current position is the end of the text and the user calls next(), or when the current position is the beginning of the text and the user calls previous().
- Break positions are numbered by the positions of the characters that follow them. Thus, under normal circumstances, the position before the first character is 0, the position after the first character is 1, and the position after the last character is 1 plus the length of the string.
- The client can change the position of an iterator, or the text it analyzes, at will, but cannot change the behavior. If the user wants different behavior, he must instantiate a new iterator.
Examples:
Creating and using text boundaries
Print each element in orderpublic static void main(String args[]) { if (args.length == 1) { String stringToExamine = args[0]; //print each word in order BreakIterator boundary = BreakIterator.makeWordInstance(); boundary.setText(stringToExamine); printEachForward(boundary, stringToExamine); //print each sentence in reverse order boundary = BreakIterator.makeSentenceInstance(Locale.US); boundary.setText(stringToExamine); printEachBackward(boundary, stringToExamine); printFirst(boundary, stringToExamine); printLast(boundary, stringToExamine); } }
Print each element in reverse orderpublic static void printEachForward(BreakIterator boundary, String source) { int start = boundary.first(); for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) { System.out.println(source.substring(start,end)); } }
Print first elementpublic static void printEachBackward(BreakIterator boundary, String source) { int end = boundary.last(); for (int start = boundary.previous(); start != BreakIterator.DONE; end = start, start = boundary.previous()) { System.out.println(source.substring(start,end)); } }
Print last elementpublic static void printFirst(BreakIterator boundary, String source) { int start = boundary.first(); int end = boundary.next(); System.out.println(source.substring(start,end)); }
Print the element at a specified positionpublic static void printLast(BreakIterator boundary, String source) { int end = boundary.last(); int start = boundary.previous(); System.out.println(source.substring(start,end)); }
Find the next wordpublic static void printAt(BreakIterator boundary, int pos, String source) { int end = boundary.following(pos); int start = boundary.previous(); System.out.println(source.substring(start,end)); }public static int nextWordStartAfter(int pos, String text) { BreakIterator wb = BreakIterator.makeWordInstance(); wb.setText(text); int wordStart = wb.following(pos); for (;;) { int wordLimit = wb.next(); if (wordLimit == BreakIterator.DONE) { return BreakIterator.DONE; } int wordStatus = wb.getRuleStatus(); if (wordStatus != BreakIterator.WORD_NONE) { return wordStart; } wordStart = wordLimit; } }The iterator returned bymakeWordInstance()is unique in that the break positions it returns don't represent both the start and end of the thing being iterated over. That is, a sentence-break iterator returns breaks that each represent the end of one sentence and the beginning of the next. With the word-break iterator, the characters between two boundaries might be a word, or they might be the punctuation or whitespace between two words. The above code usesgetRuleStatus()to identify and ignore boundaries associated with punctuation or other non-word characters.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classBreakIterator._FinalizerHolder-
Nested classes/interfaces inherited from class io.github.humbleui.skija.impl.Managed
Managed.CleanerThunk
-
-
Field Summary
Fields Modifier and Type Field Description U16String_textstatic intDONEDONE is returned by previous() and next() after all valid boundaries have been returned.static intWORD_IDEOTag value for words containing ideographic characters, lower limitstatic intWORD_IDEO_LIMITTag value for words containing ideographic characters, upper limitstatic intWORD_KANATag value for words containing kana characters, lower limitstatic intWORD_KANA_LIMITTag value for words containing kana characters, upper limitstatic intWORD_LETTERTag value for words that contain letters, excluding hiragana, katakana or ideographic characters, lower limit.static intWORD_LETTER_LIMITTag value for words containing letters, upper limitstatic intWORD_NONETag value for "words" that do not fit into any of other categories.static intWORD_NONE_LIMITUpper bound for tags for uncategorized words.static intWORD_NUMBERTag value for words that appear to be numbers, lower limit.static intWORD_NUMBER_LIMITTag value for words that appear to be numbers, upper limit.-
Fields inherited from class io.github.humbleui.skija.impl.Managed
_cleanable
-
-
Constructor Summary
Constructors Constructor Description BreakIterator(long ptr)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static long_nClone(long ptr)static int_nCurrent(long ptr)static int_nFirst(long ptr)static int_nFollowing(long ptr, int offset)static long_nGetFinalizer()static int_nGetRuleStatus(long ptr)static int[]_nGetRuleStatuses(long ptr)static boolean_nIsBoundary(long ptr, int offset)static int_nLast(long ptr)static long_nMake(int type, java.lang.String locale)static int_nNext(long ptr)static int_nPreceding(long ptr, int offset)static int_nPrevious(long ptr)static void_nSetText(long ptr, long textPtr)BreakIteratorclone()Create a copy of this iteratorvoidclose()intcurrent()Returns character index of the text boundary that was most recently returned bynext(),next(int),previous(),first(),last(),following(int)orpreceding(int).intfirst()Returns the first boundary.intfollowing(int offset)Returns the first boundary following the specified character offset.intgetRuleStatus()For rule-based BreakIterators, return the status tag from the break rule that determined the boundary at the current iteration position.int[]getRuleStatuses()For RuleBasedBreakIterators, get the status (tag) values from the break rule(s) that determined the the boundary at the current iteration position.booleanisBoundary(int offset)Returns true if the specified character offset is a text boundary.intlast()Returns the last boundary.static BreakIteratormakeCharacterInstance()Returns a new BreakIterator instance for character breaks for the default locale.static BreakIteratormakeCharacterInstance(java.lang.String locale)Returns a new BreakIterator instance for character breaks for the given locale.static BreakIteratormakeLineInstance()Returns a new BreakIterator instance for line breaks for the default locale.static BreakIteratormakeLineInstance(java.lang.String locale)Returns a new BreakIterator instance for line breaks for the given locale.static BreakIteratormakeSentenceInstance()Returns a new BreakIterator instance for sentence breaks for the default locale.static BreakIteratormakeSentenceInstance(java.lang.String locale)Returns a new BreakIterator instance for sentence breaks for the given locale.static BreakIteratormakeWordInstance()Returns a new BreakIterator instance for word breaks for the default locale.static BreakIteratormakeWordInstance(java.lang.String locale)Returns a new BreakIterator instance for word breaks for the given locale.intnext()Returns the boundary following the current boundary.intnext(int n)Advances the iterator either forward or backward the specified number of steps.intpreceding(int offset)Returns the last boundary preceding the specified character offset.intprevious()Returns the boundary following the current boundary.voidsetText(java.lang.String text)Set a new text string to be scanned.-
Methods inherited from class io.github.humbleui.skija.impl.Managed
_nInvokeFinalizer, isClosed
-
-
-
-
Field Detail
-
DONE
public static final int DONE
DONE is returned by previous() and next() after all valid boundaries have been returned.- See Also:
- Constant Field Values
-
WORD_NONE
public static final int WORD_NONE
Tag value for "words" that do not fit into any of other categories. Includes spaces and most punctuation.- See Also:
- Constant Field Values
-
WORD_NONE_LIMIT
public static final int WORD_NONE_LIMIT
Upper bound for tags for uncategorized words.- See Also:
- Constant Field Values
-
WORD_NUMBER
public static final int WORD_NUMBER
Tag value for words that appear to be numbers, lower limit.- See Also:
- Constant Field Values
-
WORD_NUMBER_LIMIT
public static final int WORD_NUMBER_LIMIT
Tag value for words that appear to be numbers, upper limit.- See Also:
- Constant Field Values
-
WORD_LETTER
public static final int WORD_LETTER
Tag value for words that contain letters, excluding hiragana, katakana or ideographic characters, lower limit.- See Also:
- Constant Field Values
-
WORD_LETTER_LIMIT
public static final int WORD_LETTER_LIMIT
Tag value for words containing letters, upper limit- See Also:
- Constant Field Values
-
WORD_KANA
public static final int WORD_KANA
Tag value for words containing kana characters, lower limit- See Also:
- Constant Field Values
-
WORD_KANA_LIMIT
public static final int WORD_KANA_LIMIT
Tag value for words containing kana characters, upper limit- See Also:
- Constant Field Values
-
WORD_IDEO
public static final int WORD_IDEO
Tag value for words containing ideographic characters, lower limit- See Also:
- Constant Field Values
-
WORD_IDEO_LIMIT
public static final int WORD_IDEO_LIMIT
Tag value for words containing ideographic characters, upper limit- See Also:
- Constant Field Values
-
_text
@Internal public U16String _text
-
-
Method Detail
-
close
public void close()
-
clone
public BreakIterator clone()
Create a copy of this iterator- Overrides:
clonein classjava.lang.Object
-
makeCharacterInstance
public static BreakIterator makeCharacterInstance()
Returns a new BreakIterator instance for character breaks for the default locale.
-
makeCharacterInstance
public static BreakIterator makeCharacterInstance(java.lang.String locale)
Returns a new BreakIterator instance for character breaks for the given locale.
-
makeWordInstance
public static BreakIterator makeWordInstance()
Returns a new BreakIterator instance for word breaks for the default locale.
-
makeWordInstance
public static BreakIterator makeWordInstance(java.lang.String locale)
Returns a new BreakIterator instance for word breaks for the given locale.
-
makeLineInstance
public static BreakIterator makeLineInstance()
Returns a new BreakIterator instance for line breaks for the default locale.
-
makeLineInstance
public static BreakIterator makeLineInstance(java.lang.String locale)
Returns a new BreakIterator instance for line breaks for the given locale.
-
makeSentenceInstance
public static BreakIterator makeSentenceInstance()
Returns a new BreakIterator instance for sentence breaks for the default locale.
-
makeSentenceInstance
public static BreakIterator makeSentenceInstance(java.lang.String locale)
Returns a new BreakIterator instance for sentence breaks for the given locale.
-
current
public int current()
Returns character index of the text boundary that was most recently returned bynext(),next(int),previous(),first(),last(),following(int)orpreceding(int). If any of these methods returnsDONEbecause either first or last text boundary has been reached, it returns the first or last text boundary depending on which one is reached.
-
next
public int next()
Returns the boundary following the current boundary. If the current boundary is the last text boundary, it returnsDONEand the iterator's current position is unchanged. Otherwise, the iterator's current position is set to the boundary following the current boundary.
-
next
public int next(int n)
Advances the iterator either forward or backward the specified number of steps. Negative values move backward, and positive values move forward. This is equivalent to repeatedly calling next() or previous().- Parameters:
n- The number of steps to move. The sign indicates the direction (negative is backwards, and positive is forwards).- Returns:
- The character offset of the boundary position n boundaries away from the current one.
-
previous
public int previous()
Returns the boundary following the current boundary. If the current boundary is the last text boundary, it returnsDONEand the iterator's current position is unchanged. Otherwise, the iterator's current position is set to the boundary following the current boundary.
-
first
public int first()
Returns the first boundary. The iterator's current position is set to the first text boundary.
-
last
public int last()
Returns the last boundary. The iterator's current position is set to the last text boundary.
-
preceding
public int preceding(int offset)
Returns the last boundary preceding the specified character offset. If the specified offset is equal to the first text boundary, it returnsDONEand the iterator's current position is unchanged. Otherwise, the iterator's current position is set to the returned boundary. The value returned is always less than the offset or the valueDONE.
-
following
public int following(int offset)
Returns the first boundary following the specified character offset. If the specified offset is equal to the last text boundary, it returnsDONEand the iterator's current position is unchanged. Otherwise, the iterator's current position is set to the returned boundary. The value returned is always greater than the offset or the valueDONE.
-
isBoundary
public boolean isBoundary(int offset)
Returns true if the specified character offset is a text boundary.
-
getRuleStatus
public int getRuleStatus()
For rule-based BreakIterators, return the status tag from the break rule that determined the boundary at the current iteration position.
For break iterator types that do not support a rule status, a default value of 0 is returned.
- Returns:
- The status from the break rule that determined the boundary at the current iteration position.
-
getRuleStatuses
public int[] getRuleStatuses()
For RuleBasedBreakIterators, get the status (tag) values from the break rule(s) that determined the the boundary at the current iteration position.For break iterator types that do not support rule status, no values are returned.
- Returns:
- an array with the status values.
-
setText
public void setText(java.lang.String text)
Set a new text string to be scanned. The current scan position is reset tofirst().
-
_nGetFinalizer
@Internal public static long _nGetFinalizer()
-
_nMake
@Internal public static long _nMake(int type, java.lang.String locale)
-
_nClone
@Internal public static long _nClone(long ptr)
-
_nCurrent
@Internal public static int _nCurrent(long ptr)
-
_nNext
@Internal public static int _nNext(long ptr)
-
_nPrevious
@Internal public static int _nPrevious(long ptr)
-
_nFirst
@Internal public static int _nFirst(long ptr)
-
_nLast
@Internal public static int _nLast(long ptr)
-
_nPreceding
@Internal public static int _nPreceding(long ptr, int offset)
-
_nFollowing
@Internal public static int _nFollowing(long ptr, int offset)
-
_nIsBoundary
@Internal public static boolean _nIsBoundary(long ptr, int offset)
-
_nGetRuleStatus
@Internal public static int _nGetRuleStatus(long ptr)
-
_nGetRuleStatuses
@Internal public static int[] _nGetRuleStatuses(long ptr)
-
_nSetText
@Internal public static void _nSetText(long ptr, long textPtr)
-
-