Class CharClass
A CharClass pairs a CodePointSet of member code points with a single
canonical ASCII replacement code point. Whitespace and dashes are the two built-in
presets (whitespace(), dashes()); any other class is one more configured
instance with no new engine code.
Every operation is a single forward cursor pass over the text.
Instances are immutable and thread-safe.
- Since:
- 3.0.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static interfaceChooses the code point that replaces one maximal run of member code points. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final CharClassprivate final CodePointSetprivate final intprivate static final CharClass -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncollapse(CharSequence text) Collapses each maximal run of member code points to a single replacement.collapseAligned(CharSequence text) Likecollapse(CharSequence)but also produces theAlignmentback to the original text.collapseParagraphPreserving(CharSequence text, CodePointSet lineBreaks, int paragraphReplacement) Collapses runs of members likecollapse(CharSequence), but emitsparagraphReplacementwhen a run contains two or more logical line breaks, or the usualreplacementwhen it contains at most one.collapseParagraphPreservingAligned(CharSequence text, CodePointSet lineBreaks, int paragraphReplacement) LikecollapseParagraphPreserving(CharSequence, CodePointSet, int)but also produces theAlignmentback to the original text.collapsePreserving(CharSequence text, CodePointSet keep, int keepReplacement) Collapses runs of members likecollapse(CharSequence), but emitskeepReplacementinstead of the usual replacement for any run that contains a code point inkeep.collapsePreservingAligned(CharSequence text, CodePointSet keep, int keepReplacement) LikecollapsePreserving(CharSequence, CodePointSet, int)but also produces theAlignmentback to the original text.private StringcollapseRuns(CharSequence text, CharClass.RunReplacement runReplacement, Alignment.Builder alignment) Collapses each maximal run of member code points to the code point chosen byrunReplacementand copies every other code point.private AlignedTextcollapseRunsAligned(CharSequence text, CharClass.RunReplacement runReplacement) Collapses runs likecollapseRuns(CharSequence, RunReplacement, Alignment.Builder)and pairs the result with itsAlignmentback totext.booleancontains(int codePoint) Tests membership.private static booleancontainsAny(CharSequence text, int from, int to, CodePointSet set) Tests whether a code point ofsetoccurs in a range oftext.private intcountLogicalLineBreaks(CharSequence text, int runStart, int runEnd, CodePointSet lineBreaks) Counts logical line breaks in a whitespace run.static CharClassdashes()Returns the dash preset: the UnicodeDashset excluding the mathematical minus signs, replacementU+002D.private intfirstMember(CharSequence text) Finds the index of the first member code point, so callers can return member-free text uncopied.members()Returns the member code points of this class.normalize(CharSequence text) Replaces each member code point with the replacement, one for one.normalizeAligned(CharSequence text) Likenormalize(CharSequence)but also produces theAlignmentback to the original text.static CharClassof(CodePointSet members, int replacement) Creates a class from a member set and a replacement code point.private CharClass.RunReplacementparagraphReplacement(CharSequence text, CodePointSet lineBreaks, int paragraphReplacement) Returns theCharClass.RunReplacementof thecollapseParagraphPreservingoperations:paragraphReplacementfor a run with two or more logical line breaks, the usual replacement otherwise.private CharClass.RunReplacementpreservingReplacement(CharSequence text, CodePointSet keep, int keepReplacement) Returns theCharClass.RunReplacementof thecollapsePreservingoperations:keepReplacementfor a run that contains akeepcode point, the usual replacement otherwise.removeAll(CharSequence text) Removes every member code point.removeAllAligned(CharSequence text) LikeremoveAll(CharSequence)but also produces theAlignmentback to the original text.intReturns the canonical replacement code point.private intskipRun(CharSequence text, int runStart) Advances past a run of member code points.private intskipRunBackward(CharSequence text, int floor, int runEnd) Moves back past a run of member code points that ends atrunEnd.String[]split(CharSequence text) Splits text into the maximal runs of non-member code points.splitSpans(CharSequence text) Splits text into the maximal runs of non-member code points, as character spans into the original text.static Stringsubstitute(CharSequence text, IntFunction<String> substitution) Applies a per-code-point substitution: each code point for whichsubstitutionreturns a non-null string is replaced by that string, and the rest are copied through.static AlignedTextsubstituteAligned(CharSequence text, IntFunction<String> substitution) Likesubstitute(CharSequence, IntFunction)but also produces theAlignmentback to the original text.trim(CharSequence text) Removes leading and trailing member code points.trimAligned(CharSequence text) Liketrim(CharSequence)but also produces theAlignmentback to the original text.static CharClassReturns the whitespace preset: the UnicodeWhite_Spaceset, replacementU+0020.withAdditional(CodePointSet extra) Returns a copy of this class whose member set is extended withextra(for example, user-defined code points loaded fromCodePointSet.fromFile(java.nio.file.Path, java.lang.String)).
-
Field Details
-
WHITESPACE
-
DASHES
-
members
-
replacement
private final int replacement
-
-
Constructor Details
-
CharClass
-
-
Method Details
-
of
Creates a class from a member set and a replacement code point.- Parameters:
members- The member code points.replacement- The canonical code point used bynormalize(CharSequence)andcollapse(CharSequence).- Returns:
- The class.
- Throws:
IllegalArgumentException- Thrown ifmembersisnullorreplacementis not a valid code point.
-
whitespace
Returns the whitespace preset: the UnicodeWhite_Spaceset, replacementU+0020.- Returns:
- the whitespace preset: the Unicode
White_Spaceset, replacementU+0020
-
dashes
Returns the dash preset: the UnicodeDashset excluding the mathematical minus signs, replacementU+002D.- Returns:
- the dash preset: the Unicode
Dashset excluding the mathematical minus signs, replacementU+002D
-
withAdditional
Returns a copy of this class whose member set is extended withextra(for example, user-defined code points loaded fromCodePointSet.fromFile(java.nio.file.Path, java.lang.String)).- Parameters:
extra- The additional member code points.- Returns:
- A new
CharClass; this instance is unchanged. - Throws:
IllegalArgumentException- Thrown ifextraisnull.
-
members
Returns the member code points of this class.- Returns:
- the member code points of this class
-
replacement
public int replacement()Returns the canonical replacement code point.- Returns:
- the canonical replacement code point
-
contains
public boolean contains(int codePoint) Tests membership.- Parameters:
codePoint- The code point to test.- Returns:
trueif the code point is a member of this class.
-
splitSpans
Splits text into the maximal runs of non-member code points, as character spans into the original text. Runs of members are delimiters and produce no empty spans.- Parameters:
text- The text to split.- Returns:
- The token spans, in order.
- Throws:
IllegalArgumentException- Thrown iftextisnull.
-
split
Splits text into the maximal runs of non-member code points.- Parameters:
text- The text to split.- Returns:
- The tokens, in order, with no empty entries.
- Throws:
IllegalArgumentException- Thrown iftextisnull.
-
normalize
Replaces each member code point with the replacement, one for one.When no code point of
textis a member, the text is returned unchanged as itsstring form, which is not a copy forStringinput.- Parameters:
text- The text to normalize.- Returns:
- The normalized text.
- Throws:
IllegalArgumentException- Thrown iftextisnull.
-
collapse
Collapses each maximal run of member code points to a single replacement.Edges are not trimmed: a leading or trailing run becomes a single replacement, and a string made up entirely of members collapses to one replacement (for whitespace, a single space, not the empty string). Use
trim(CharSequence)to drop edge members, or collapse and then trim to do both.When no code point of
textis a member, the text is returned unchanged as itsstring form, which is not a copy forStringinput.- Parameters:
text- The text to collapse.- Returns:
- The collapsed text.
- Throws:
IllegalArgumentException- Thrown iftextisnull.
-
collapsePreserving
Collapses runs of members likecollapse(CharSequence), but emitskeepReplacementinstead of the usual replacement for any run that contains a code point inkeep.- Parameters:
text- The text to collapse.keep- The member code points whose presence in a run preserves structure.keepReplacement- The replacement emitted for a run that contains akeepmember.- Returns:
- The collapsed text.
- Throws:
IllegalArgumentException- Thrown iftextorkeepisnull, orkeepReplacementis not a valid code point.
-
trim
Removes leading and trailing member code points.- Parameters:
text- The text to trim.- Returns:
- The trimmed text.
- Throws:
IllegalArgumentException- Thrown iftextisnull.
-
removeAll
Removes every member code point.When no code point of
textis a member, the text is returned unchanged as itsstring form, which is not a copy forStringinput.- Parameters:
text- The text to filter.- Returns:
- The text with all members removed.
- Throws:
IllegalArgumentException- Thrown iftextisnull.
-
normalizeAligned
Likenormalize(CharSequence)but also produces theAlignmentback to the original text.- Parameters:
text- The text to normalize.- Returns:
- The normalized text and its alignment.
- Throws:
IllegalArgumentException- Thrown iftextisnull.
-
collapseAligned
Likecollapse(CharSequence)but also produces theAlignmentback to the original text. Each collapsed run maps to the run's whole original extent.- Parameters:
text- The text to collapse.- Returns:
- The collapsed text and its alignment.
- Throws:
IllegalArgumentException- Thrown iftextisnull.
-
collapsePreservingAligned
public AlignedText collapsePreservingAligned(CharSequence text, CodePointSet keep, int keepReplacement) LikecollapsePreserving(CharSequence, CodePointSet, int)but also produces theAlignmentback to the original text.- Parameters:
text- The text to collapse.keep- The member code points whose presence in a run preserves structure.keepReplacement- The replacement emitted for a run that contains akeepmember.- Returns:
- The collapsed text and its alignment.
- Throws:
IllegalArgumentException- Thrown iftextorkeepisnull, orkeepReplacementis not a valid code point.
-
collapseParagraphPreserving
public String collapseParagraphPreserving(CharSequence text, CodePointSet lineBreaks, int paragraphReplacement) Collapses runs of members likecollapse(CharSequence), but emitsparagraphReplacementwhen a run contains two or more logical line breaks, or the usualreplacementwhen it contains at most one. A carriage return immediately followed by a line feed counts as one break, not two.- Parameters:
text- The text to collapse.lineBreaks- The member code points that count as a line break when tallying a run.paragraphReplacement- The replacement emitted for a run with two or more logical breaks.- Returns:
- The collapsed text.
- Throws:
IllegalArgumentException- Thrown iftextorlineBreaksisnull, orparagraphReplacementis not a valid code point.
-
collapseParagraphPreservingAligned
public AlignedText collapseParagraphPreservingAligned(CharSequence text, CodePointSet lineBreaks, int paragraphReplacement) LikecollapseParagraphPreserving(CharSequence, CodePointSet, int)but also produces theAlignmentback to the original text.- Parameters:
text- The text to collapse.lineBreaks- The member code points that count as a line break when tallying a run.paragraphReplacement- The replacement emitted for a run with two or more logical breaks.- Returns:
- The collapsed text and its alignment.
- Throws:
IllegalArgumentException- Thrown iftextorlineBreaksisnull, orparagraphReplacementis not a valid code point.
-
trimAligned
Liketrim(CharSequence)but also produces theAlignmentback to the original text. The trimmed leading and trailing members appear as deletions, so a span never reports through them.- Parameters:
text- The text to trim.- Returns:
- The trimmed text and its alignment.
- Throws:
IllegalArgumentException- Thrown iftextisnull.
-
removeAllAligned
LikeremoveAll(CharSequence)but also produces theAlignmentback to the original text. Every removed member appears as a deletion, so a span never reports through one.- Parameters:
text- The text to filter.- Returns:
- The filtered text and its alignment.
- Throws:
IllegalArgumentException- Thrown iftextisnull.
-
substitute
Applies a per-code-point substitution: each code point for whichsubstitutionreturns a non-null string is replaced by that string, and the rest are copied through.When
substitutionreturnsnullfor every code point oftext, the text is returned unchanged as itsstring form, which is not a copy forStringinput. The mapper is still applied exactly once per code point.- Parameters:
text- The text to transform.substitution- The replacement for a code point, ornullto copy it through.- Returns:
- The transformed text.
- Throws:
IllegalArgumentException- Thrown iftextorsubstitutionisnull.
-
substituteAligned
Likesubstitute(CharSequence, IntFunction)but also produces theAlignmentback to the original text. Each replaced code point maps to its replacement string as one block.- Parameters:
text- The text to transform.substitution- The replacement for a code point, ornullto copy it through.- Returns:
- The transformed text and its alignment.
- Throws:
IllegalArgumentException- Thrown iftextorsubstitutionisnull.
-
firstMember
Finds the index of the first member code point, so callers can return member-free text uncopied.- Parameters:
text- The text to scan.- Returns:
- The index of the first member code point, or
text.length()when the text contains no member.
-
collapseRuns
private String collapseRuns(CharSequence text, CharClass.RunReplacement runReplacement, Alignment.Builder alignment) Collapses each maximal run of member code points to the code point chosen byrunReplacementand copies every other code point.- Parameters:
text- The text to collapse.runReplacement- Chooses the replacement for each run.alignment- Receives the edits of the pass, ornullif no alignment is needed.- Returns:
- The collapsed text.
-
collapseRunsAligned
Collapses runs likecollapseRuns(CharSequence, RunReplacement, Alignment.Builder)and pairs the result with itsAlignmentback totext.- Parameters:
text- The text to collapse.runReplacement- Chooses the replacement for each run.- Returns:
- The collapsed text and its alignment, as
AlignedText.
-
preservingReplacement
private CharClass.RunReplacement preservingReplacement(CharSequence text, CodePointSet keep, int keepReplacement) Returns theCharClass.RunReplacementof thecollapsePreservingoperations:keepReplacementfor a run that contains akeepcode point, the usual replacement otherwise. -
paragraphReplacement
private CharClass.RunReplacement paragraphReplacement(CharSequence text, CodePointSet lineBreaks, int paragraphReplacement) Returns theCharClass.RunReplacementof thecollapseParagraphPreservingoperations:paragraphReplacementfor a run with two or more logical line breaks, the usual replacement otherwise. -
containsAny
Tests whether a code point ofsetoccurs in a range oftext.- Parameters:
text- The text to scan.from- The index where the range starts.to- The index where the range ends, exclusive.set- TheCodePointSetof code points to look for.- Returns:
trueif any code point in the range is inset.
-
skipRun
Advances past a run of member code points.- Parameters:
text- The text to scan.runStart- The index where the member run starts.- Returns:
- The index of the first non-member code point at or after
runStart, ortext.length()when the run extends to the end of the text.
-
skipRunBackward
Moves back past a run of member code points that ends atrunEnd.- Parameters:
text- The text to scan.floor- The lowest index the scan may reach.runEnd- The index after the last code point of the run.- Returns:
- The index after the last non-member code point before
runEnd, orfloorwhen every code point in[floor, runEnd)is a member.
-
countLogicalLineBreaks
private int countLogicalLineBreaks(CharSequence text, int runStart, int runEnd, CodePointSet lineBreaks) Counts logical line breaks in a whitespace run. A carriage return immediately followed by a line feed counts as one break.- Parameters:
text- The text containing the run.runStart- The index where the whitespace run starts.runEnd- The index of the first code point after the run.lineBreaks- The code points that count as a line break when tallying the run.- Returns:
- The number of logical line breaks in
text[runStart..runEnd).
-