Class WordSegmenter
The implementation is a single forward cursor pass with O(1) WordBreakProperty
lookups. It decodes each code point once, keeps only a constant amount of state, and allocates
nothing per character. It implements the "ignore" semantics of WB4 (a base character absorbs
following Extend, Format, and ZWJ), the look-ahead rules
WB6/WB7/WB7b/WB12, the Hebrew quote rules WB7a-WB7c, the emoji zero-width-joiner rule WB3c, and
regional-indicator pairing WB15/WB16. The look-ahead for the WB6/WB7b/WB12 rules is resolved
lazily and only at mid-word punctuation, so the common case never scans ahead.
forEachSegment(CharSequence, SegmentConsumer) streams the segments with no
allocation; boundaries(CharSequence) returns every boundary offset (always including
0 and the text length); segments(CharSequence) returns the spans between
them.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classstatic interfaceReceives each word segment as the half-open character range[start, end). -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final byteprivate static final intprivate static final WordBreak[]private static final byteprivate static final intprivate static final intprivate static final byte[]private static final intprivate static final byteprivate static final intprivate static final intprivate static final byteprivate static final intprivate static final intprivate static final boolean[]private static final byte[]private static final intprivate static final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static booleanafterPrefix(WordBreak current, WordBreak last, WordBreak secondLast, WordBreak next, int regionalIndicatorRun) static int[]boundaries(CharSequence text) Returns the word boundary character offsets intext, in ascending order, including the boundaries at0andtext.length().private static booleanbreakAtSpecial(int prev, int current, int codePoint, int last, int secondLast, int regionalIndicatorRun, CharSequence text, int nextFrom, int length, WordBreakProperty.Data wbData, BitSet pictographs) private static byte[]private static boolean[]private static byte[]private static booleanconsult(CharSequence text, int nextFrom, int length, int current, int last, int secondLast, int regionalIndicatorRun, WordBreakProperty.Data wbData) private static bytederiveDecision(WordBreak last, WordBreak current) static voidforEachSegment(CharSequence text, WordSegmenter.SegmentConsumer consumer) Streams the word segments oftexttoconsumerin order, allocating nothing.private static booleanisAhLetter(WordBreak value) private static booleanisIgnorable(int ordinal) private static booleanisIgnorable(WordBreak value) private static booleanisMidLetter(WordBreak value) private static booleanisMidNumber(WordBreak value) private static WordBreaknextSignificant(CharSequence text, int from, int length, WordBreakProperty.Data wbData) segments(CharSequence text) Returns the word segments oftextas spans between consecutive boundaries.
-
Field Details
-
NO_BREAK
private static final byte NO_BREAK- See Also:
-
BREAK
private static final byte BREAK- See Also:
-
CONSULT
private static final byte CONSULT- See Also:
-
GO_SLOW
private static final byte GO_SLOW- See Also:
-
CLASSES
-
CLASS_COUNT
private static final int CLASS_COUNT -
TRANSITION
private static final byte[] TRANSITION -
OTHER_ORDINAL
private static final int OTHER_ORDINAL -
CR_ORDINAL
private static final int CR_ORDINAL -
LF_ORDINAL
private static final int LF_ORDINAL -
NEWLINE_ORDINAL
private static final int NEWLINE_ORDINAL -
ZWJ_ORDINAL
private static final int ZWJ_ORDINAL -
WSEG_SPACE_ORDINAL
private static final int WSEG_SPACE_ORDINAL -
EXTEND_ORDINAL
private static final int EXTEND_ORDINAL -
FORMAT_ORDINAL
private static final int FORMAT_ORDINAL -
REGIONAL_INDICATOR_ORDINAL
private static final int REGIONAL_INDICATOR_ORDINAL -
SPECIAL
private static final boolean[] SPECIAL -
FAST
private static final byte[] FAST
-
-
Constructor Details
-
WordSegmenter
private WordSegmenter()
-
-
Method Details
-
buildSpecialTable
private static boolean[] buildSpecialTable() -
buildFastTable
private static byte[] buildFastTable() -
buildTransitionTable
private static byte[] buildTransitionTable() -
deriveDecision
-
forEachSegment
Streams the word segments oftexttoconsumerin order, allocating nothing. Each segment is delivered as the half-open character range[start, end); the segments are contiguous and together cover the whole text.- Parameters:
text- The text to segment; may be empty, in which case no segment is delivered.consumer- The receiver of the segment ranges.- Throws:
IllegalArgumentException- Thrown iftextorconsumerisnull.
-
breakAtSpecial
private static boolean breakAtSpecial(int prev, int current, int codePoint, int last, int secondLast, int regionalIndicatorRun, CharSequence text, int nextFrom, int length, WordBreakProperty.Data wbData, BitSet pictographs) -
consult
private static boolean consult(CharSequence text, int nextFrom, int length, int current, int last, int secondLast, int regionalIndicatorRun, WordBreakProperty.Data wbData) -
boundaries
Returns the word boundary character offsets intext, in ascending order, including the boundaries at0andtext.length().- Parameters:
text- The text to segment.- Returns:
- The boundary offsets; for empty text,
[0]. - Throws:
IllegalArgumentException- Thrown iftextisnull.
-
segments
Returns the word segments oftextas spans between consecutive boundaries.- Parameters:
text- The text to segment.- Returns:
- The segment spans, in order.
- Throws:
IllegalArgumentException- Thrown iftextisnull.
-
nextSignificant
private static WordBreak nextSignificant(CharSequence text, int from, int length, WordBreakProperty.Data wbData) -
afterPrefix
-
isIgnorable
-
isIgnorable
private static boolean isIgnorable(int ordinal) -
isAhLetter
-
isMidLetter
-
isMidNumber
-