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: no regular expression and no per-call allocation beyond the result.
Instances are immutable and thread-safe.
-
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.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.booleancontains(int codePoint) Tests membership.static CharClassdashes()Returns the dash preset: the UnicodeDashset excluding the mathematical minus signs, replacementU+002D.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.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.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)).
-
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) without copying.- 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) without copying.- 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. The whitespace "squish" that preserves a line break uses this with the line-break code points askeepand'\n'askeepReplacement.- 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) without copying.- 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.
-
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. This is the shared cursor pass behind the expanding folds, with no regular expression.When
substitutionreturnsnullfor every code point oftext, the text is returned unchanged (as itsstring form) without copying. 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.
-