public class MutableString extends Object implements Serializable, CharSequence, Appendable, Comparable<MutableString>, Cloneable
The classical Java string classes, String
and StringBuffer
, lie at the extreme of a spectrum (immutable and
mutable).
However, large-scale text indexing requires some features that are not
provided by these classes: in particular, the possibility of using a mutable
string, once frozen, in the same optimised way of an immutable
string. Moreover, usually we do not need synchronisation (which makes
StringBuffer
slow).
In a typical scenario you are dividing text into words (so you use a
mutable string to accumulate characters). Once you've got your
word, you would like to check whether this word is in a dictionary
without creating a new object. However, equality of
StringBuffer
s is not defined on their content, and storing
words after a conversion to String
will not help either, as
then you would need to convert the current mutable string into an immutable
one (thus creating a new object) before deciding whether you need to
store it.
This class tries to make the best of both worlds, and thus aims at being a Better Mousetrap™.
You can read more details about the design of MutableString
in Paolo Boldi and Sebastiano Vigna,
“Mutable strings
in Java: Design, implementation and lightweight text-search
algorithms”, Sci. Comput. Programming, 54(1):3-23, 2005.
String
s and StringBuffer
s);
multi-character substitutions
;
CharSequence
, so, for instance, you can match or split a
mutable string against a regular expression using the standard Java API;
Appendable
, so they can be used with Formatter
and similar classes;
null
is not accepted as a string argument;
this
, so
you can chain methods as in s.length(0).append("foo").append("bar")
;
String
by using write(Writer)
, print(PrintWriter)
and println(PrintWriter)
; you can read it back
using read(Reader,int)
.
writeSelfDelimUTF8(DataOutput)
—you are not
limited to strings whose UTF-8 encoded length fits 16 bits;
wrap
any character array into a mutable string;
Committing to use this class for such an ubiquitous data structure as strings may seem dangerous, as standard string classes are by now tested and stable. However, this class has been heavily regression (and torture) tested on all methods, and we believe it is very reliable.
To simplify the transition to mutable strings, we have tried to make
mixing string classes simpler by providing polymorphic versions of all
methods accepting one or more strings—whenever you must specify a
string you can usually provide a MutableString
, a
String
, or a generic CharSequence
.
Note that usually we provide a specific method for
String
. This duplication may seem useless, as
String
implements CharSequence
. However, invoking
methods on an interface is slower than invoking methods on a class, and we
expect constant strings to appear often in such methods.
Backing array reallocations use a heuristic based on looseness. Whenever an operation changes the length, compact strings are resized to fit exactly the new content, whereas the capacity of a loose string is never shortened, and enlargements maximise the new length required with the double of the current capacity.
The effect of this policy is that loose strings will get large buffers quickly, but compact strings will occupy little space and perform very well in data structures using hash codes.
For instance, you can easily reuse a loose mutable string calling length(0)
(which does not reallocate the backing
array).
In any case, you can call compact()
and loose()
to force
the respective condition.
The main disadvantage of mutable strings is that their substrings
cannot share their backing arrays, so if you need to generate many
substrings you may want to use String
. However, subSequence()
returns a CharSequence
that
shares the backing array.
equals
method that compare the content of String
s and
CharSequence
s, so that you can easily do checks like
mutableString.equals( "Hello" )Thus, you must not mix mutable strings with
CharSequence
s in collections as equality between objects of
those types is not symmetric.
writeSelfDelimUTF8()
is
not compatible with the usual Java writeUTF()
.
array()
?).
Modifier and Type | Field and Description |
---|---|
protected char[] |
array
The backing array.
|
protected int |
hashLength
This mutable string is compact iff this attribute is negative.
|
static long |
serialVersionUID |
Constructor and Description |
---|
MutableString()
Creates a new loose empty mutable string with capacity 2.
|
MutableString(char[] a)
Creates a new compact mutable string copying a given character array.
|
MutableString(char[] a,
int offset,
int len)
Creates a new compact mutable string copying a part of a given character array.
|
MutableString(CharSequence s)
Creates a new compact mutable string copying a given
CharSequence . |
MutableString(int capacity)
Creates a new loose empty mutable string with given capacity.
|
MutableString(MutableString s)
Creates a new compact mutable string copying a given mutable string.
|
MutableString(String s)
Creates a new compact mutable string copying a given
String . |
Modifier and Type | Method and Description |
---|---|
MutableString |
append(boolean b)
Appends a boolean to this mutable string.
|
MutableString |
append(char c)
Appends a character to this mutable string.
|
MutableString |
append(char[] a)
Appends the given character array to this mutable string.
|
MutableString |
append(char[] a,
int offset,
int len)
Appends a part of the given character array to this mutable string.
|
MutableString |
append(it.unimi.dsi.fastutil.chars.CharList list)
Appends the given character list to this mutable string.
|
MutableString |
append(it.unimi.dsi.fastutil.chars.CharList list,
int offset,
int len)
Appends a part of the given character list to this mutable string.
|
MutableString |
append(CharSequence s)
Appends the given
CharSequence to this mutable string. |
MutableString |
append(CharSequence[] a,
CharSequence separator)
Appends the given character sequences to this mutable string using the given separator.
|
MutableString |
append(CharSequence[] a,
int offset,
int length,
CharSequence separator)
Appends the given character sequences to this mutable string using the given separator.
|
MutableString |
append(CharSequence s,
int start,
int end)
Appends a subsequence of the given
CharSequence to this mutable string. |
MutableString |
append(double d)
Appends a double to this mutable string.
|
MutableString |
append(float f)
Appends a float to this mutable string.
|
MutableString |
append(int i)
Appends an integer to this mutable string.
|
MutableString |
append(long l)
Appends a long to this mutable string.
|
MutableString |
append(MutableString s)
Appends the given mutable string to this mutable string.
|
MutableString |
append(Object o)
Appends the string representation of an object to this mutable string.
|
MutableString |
append(Object[] a,
CharSequence separator)
Appends the string representations of the given objects to this mutable string using the given separator.
|
MutableString |
append(Object[] a,
int offset,
int length,
CharSequence separator)
Appends the string representations of the given objects to this mutable string using the given separator.
|
MutableString |
append(String s)
Appends the given
String to this mutable string. |
char[] |
array()
Gets the backing array.
|
int |
capacity()
Returns the current length of the backing array.
|
MutableString |
changed()
Invalidates the current cached hash code if this mutable string is compact.
|
char |
charAt(int index)
Gets a character.
|
MutableString |
charAt(int index,
char c)
Sets the character at the given index.
|
Object |
clone()
Creates a new compact mutable string by copying this one.
|
MutableString |
compact()
Makes this mutable string compact (see the class description).
|
int |
compareTo(CharSequence s)
Compares this mutable string to a character sequence performing a lexicographical comparison.
|
int |
compareTo(MutableString s)
Compares this mutable string to another mutable string performing a lexicographical comparison.
|
int |
compareTo(String s)
Compares this mutable string to a string performing a lexicographical comparison.
|
int |
compareToIgnoreCase(CharSequence s)
Type-specific version of
compareToIgnoreCase() . |
int |
compareToIgnoreCase(MutableString s)
Compares this mutable string to another object disregarding case.
|
int |
compareToIgnoreCase(String s)
Type-specific version of
compareToIgnoreCase() . |
MutableString |
copy()
Creates a new compact mutable string by copying this one.
|
int |
cospan(char[] c)
Spans the initial segment of this mutable string made of the complement of the specified characters.
|
int |
cospan(char[] c,
int from)
Spans a segment of this mutable string made of the complement of the specified characters.
|
int |
cospan(it.unimi.dsi.fastutil.chars.CharSet s)
Spans the initial segment of this mutable string made of the complement of the specified characters.
|
int |
cospan(it.unimi.dsi.fastutil.chars.CharSet s,
int from)
Spans a segment of this mutable string made of the complement of the specified characters.
|
MutableString |
delete(char c)
Removes all occurrences of the given character.
|
MutableString |
delete(char[] c)
Removes all occurrences of the given characters.
|
MutableString |
delete(it.unimi.dsi.fastutil.chars.CharSet s)
Removes all occurrences of the given characters.
|
MutableString |
delete(int start,
int end)
Removes the characters of this mutable string with
indices in the range from
start (inclusive) to end
(exclusive). |
MutableString |
deleteCharAt(int index)
Removes the character at the given index.
|
boolean |
endsWith(CharSequence suffix)
Returns whether this mutable string ends with the given character sequence.
|
boolean |
endsWith(MutableString suffix)
Returns whether this mutable string ends with the given mutable string.
|
boolean |
endsWith(String suffix)
Returns whether this mutable string ends with the given string.
|
boolean |
endsWithIgnoreCase(CharSequence suffix)
Returns whether this mutable string ends with the given character sequence disregarding case.
|
boolean |
endsWithIgnoreCase(MutableString suffix)
Returns whether this mutable string ends with the given mutable string disregarding case.
|
boolean |
endsWithIgnoreCase(String suffix)
Returns whether this mutable string ends with the given string disregarding case.
|
MutableString |
ensureCapacity(int minimumCapacity)
Ensures that at least the given number of characters can be stored in this mutable string.
|
boolean |
equals(CharSequence s)
Type-specific version of
equals() . |
boolean |
equals(MutableString s)
Type-specific version of
equals() . |
boolean |
equals(Object o)
Compares this mutable string to another object.
|
boolean |
equals(String s)
Type-specific version of
equals() . |
boolean |
equalsIgnoreCase(CharSequence s)
Type-specific version of
equalsIgnoreCase() . |
boolean |
equalsIgnoreCase(MutableString s)
Checks two mutable strings for equality ignoring case.
|
boolean |
equalsIgnoreCase(String s)
Type-specific version of
equalsIgnoreCase() . |
char |
firstChar()
Returns the first character of this mutable string.
|
static void |
getChars(CharSequence s,
int start,
int end,
char[] dest,
int destStart)
Commodity static method implementing
String.getChars(int,int,char[],int) for a CharSequence s. |
void |
getChars(int start,
int end,
char[] dest,
int destStart)
Characters with indices from
start (inclusive) to
index end (exclusive) are copied from this
mutable string into the array dest , starting
from index destStart . |
int |
hashCode()
Returns a hash code for this mutable string.
|
int |
indexOf(char c)
Returns the index of the first occurrence of the
specified character.
|
int |
indexOf(char c,
int from)
Returns the index of the first occurrence of the
specified character, starting at the specified index.
|
int |
indexOf(CharSequence pattern)
Returns the index of the first occurrence of the specified character sequence.
|
int |
indexOf(CharSequence pattern,
int from)
Returns the index of the first occurrence of the specified character sequence, starting at the specified index.
|
int |
indexOf(MutableString pattern)
Returns the index of the first occurrence of the specified mutable string.
|
int |
indexOf(MutableString pattern,
int from)
Returns the index of the first occurrence of the specified mutable string, starting at the specified index.
|
int |
indexOf(TextPattern pattern)
Returns the index of the first occurrence of the specified text pattern, starting at the specified index.
|
int |
indexOf(TextPattern pattern,
int from)
Returns the index of the first occurrence of the
specified text pattern, starting at the specified index.
|
int |
indexOfAnyBut(char[] c)
Returns the index of the first occurrence of any character, except those specified.
|
int |
indexOfAnyBut(char[] c,
int from)
Returns the index of the first occurrence of any character, except those specified, starting at the specified index.
|
int |
indexOfAnyBut(it.unimi.dsi.fastutil.chars.CharSet s)
Returns the index of the first occurrence of any character, except those specified.
|
int |
indexOfAnyBut(it.unimi.dsi.fastutil.chars.CharSet s,
int from)
Returns the index of the first occurrence of any character, except those specified, starting at the specified index.
|
int |
indexOfAnyOf(char[] c)
Returns the index of the first occurrence of any of the specified characters.
|
int |
indexOfAnyOf(char[] c,
int from)
Returns the index of the first occurrence of any of the specified characters, starting at the specified index.
|
int |
indexOfAnyOf(it.unimi.dsi.fastutil.chars.CharSet s)
Returns the index of the first occurrence of any of the specified characters.
|
int |
indexOfAnyOf(it.unimi.dsi.fastutil.chars.CharSet s,
int from)
Returns the index of the first occurrence of any of the specified characters, starting at the specified index.
|
MutableString |
insert(int index,
boolean b)
Inserts a boolean in this mutable string, starting from index
index . |
MutableString |
insert(int index,
char c)
Inserts a char in this mutable string, starting from index
index . |
MutableString |
insert(int index,
char[] c)
Inserts characters in this mutable string.
|
MutableString |
insert(int index,
char[] c,
int offset,
int len)
Inserts characters in this mutable string.
|
MutableString |
insert(int index,
CharSequence s)
Inserts a
CharSequence in this mutable string, starting from index index . |
MutableString |
insert(int index,
double d)
Inserts a double in this mutable string, starting from index
index . |
MutableString |
insert(int index,
float f)
Inserts a float in this mutable string, starting from index
index . |
MutableString |
insert(int index,
int x)
Inserts an int in this mutable string, starting from index
index . |
MutableString |
insert(int index,
long l)
Inserts a long in this mutable string, starting from index
index . |
MutableString |
insert(int index,
MutableString s)
Inserts a mutable string in this mutable string, starting from index
index . |
MutableString |
insert(int index,
Object o)
Inserts the string representation of an object in this mutable string, starting from index
index . |
MutableString |
insert(int index,
String s)
Inserts a
String in this mutable string, starting from index index . |
boolean |
isCompact()
Returns whether this mutable string is compact (see the class description).
|
boolean |
isLoose()
Returns whether this mutable string is loose (see the class description).
|
char |
lastChar()
Returns the last character of this mutable string.
|
int |
lastIndexOf(char c)
Returns the index of the last occurrence of the
specified character.
|
int |
lastIndexOf(char c,
int from)
Returns the index of the last occurrence of the specified character, searching backward starting at the specified index.
|
int |
lastIndexOf(CharSequence pattern)
Returns the index of the last occurrence of the specified character sequence.
|
int |
lastIndexOf(CharSequence pattern,
int from)
Returns the index of the last occurrence of the specified character sequence, searching backward starting at the specified index.
|
int |
lastIndexOf(MutableString pattern)
Returns the index of the last occurrence of the specified mutable string.
|
int |
lastIndexOf(MutableString pattern,
int from)
Returns the index of the last occurrence of the specified mutable string, searching backward starting at the specified index.
|
int |
lastIndexOfAnyBut(char[] c)
Returns the index of the last occurrence of any character, except those specified.
|
int |
lastIndexOfAnyBut(char[] c,
int from)
Returns the index of the last occurrence of any character, except those specified, starting at the specified index.
|
int |
lastIndexOfAnyBut(it.unimi.dsi.fastutil.chars.CharSet s)
Returns the index of the last occurrence of any character, except those specified.
|
int |
lastIndexOfAnyBut(it.unimi.dsi.fastutil.chars.CharSet s,
int from)
Returns the index of the last occurrence of any character, except those specified, starting at the specified index.
|
int |
lastIndexOfAnyOf(char[] c)
Returns the index of the last occurrence of any of the specified characters.
|
int |
lastIndexOfAnyOf(char[] c,
int from)
Returns the index of the last occurrence of any of the specified characters, searching backwards starting at the specified index.
|
int |
lastIndexOfAnyOf(it.unimi.dsi.fastutil.chars.CharSet s)
Returns the index of the last occurrence of any of the specified characters.
|
int |
lastIndexOfAnyOf(it.unimi.dsi.fastutil.chars.CharSet s,
int from)
Returns the index of the last occurrence of any of the specified characters, searching backwards starting at the specified index.
|
int |
length()
Returns the number of characters in this mutable string.
|
MutableString |
length(int newLength)
Sets the length.
|
MutableString |
loose()
Makes this mutable string loose.
|
void |
print(PrintStream s)
Prints this mutable string to a
PrintStream . |
void |
print(PrintWriter w)
Prints this mutable string to a
PrintWriter . |
void |
println(PrintStream s)
Prints this mutable string to a
PrintStream and then terminates the line. |
void |
println(PrintWriter w)
Prints this mutable string to a
PrintWriter and then terminates the line. |
int |
read(Reader r,
int length)
Reads a mutable string that has been written by
write(Writer) from a Reader . |
MutableString |
readSelfDelimUTF8(DataInput s)
Reads a mutable string that has been written by
writeSelfDelimUTF8()
from a DataInput . |
MutableString |
readSelfDelimUTF8(InputStream s)
Reads a mutable string that has been written by
writeSelfDelimUTF8()
from an InputStream . |
MutableString |
readUTF8(DataInput s,
int length)
Reads a mutable string in UTF-8 encoding.
|
MutableString |
readUTF8(InputStream s,
int length)
Reads a mutable string in UTF-8 encoding.
|
MutableString |
replace(char c)
Replaces the content of this mutable string with the given character.
|
MutableString |
replace(char[] c,
char[] r)
Replaces each occurrence of a set characters with a corresponding character.
|
MutableString |
replace(char[] c,
CharSequence[] s)
Replaces each occurrence of a set of characters with a corresponding character sequence.
|
MutableString |
replace(char[] c,
MutableString[] s)
Replaces each occurrence of a set of characters with a corresponding mutable string.
|
MutableString |
replace(char[] c,
String[] s)
Replaces each occurrence of a set of characters with a corresponding string.
|
MutableString |
replace(it.unimi.dsi.fastutil.chars.Char2CharMap m)
Replaces characters following a replacement map.
|
MutableString |
replace(char c,
char r)
Replaces each occurrence of a character with a corresponding character.
|
MutableString |
replace(char c,
CharSequence s)
Replaces each occurrence of a character with a corresponding character sequence.
|
MutableString |
replace(char c,
MutableString s)
Replaces each occurrence of a character with a corresponding mutable string.
|
MutableString |
replace(CharSequence s)
Replaces the content of this mutable string with the given character sequence.
|
MutableString |
replace(CharSequence s,
CharSequence r)
Replaces each occurrence of a character sequence with a corresponding character sequence.
|
MutableString |
replace(char c,
String s)
Replaces each occurrence of a character with a corresponding string.
|
MutableString |
replace(int start,
int end,
char c)
Replaces the characters with indices ranging from
start (inclusive)
to end (exclusive) with the given character. |
MutableString |
replace(int start,
int end,
CharSequence s)
Replaces the characters with indices ranging from
start (inclusive)
to end (exclusive) with the given CharSequence . |
MutableString |
replace(int start,
int end,
MutableString s)
Replaces the characters with indices ranging from
start (inclusive)
to end (exclusive) with the given mutable string. |
MutableString |
replace(int start,
int end,
String s)
Replaces the characters with indices ranging from
start (inclusive)
to end (exclusive) with the given String . |
MutableString |
replace(MutableString s)
Replaces the content of this mutable string with the given mutable string.
|
MutableString |
replace(MutableString s,
MutableString r)
Replaces each occurrence of a mutable string with a corresponding mutable string.
|
MutableString |
replace(String s)
Replaces the content of this mutable string with the given string.
|
MutableString |
replace(String s,
String r)
Replaces each occurrence of a string with a corresponding string.
|
MutableString |
reverse()
The characters in this mutable string get reversed.
|
MutableString |
setCharAt(int index,
char c)
A nickname for
charAt(int,char) . |
MutableString |
setLength(int newLength)
A nickname for
length(int) . |
static int |
skipSelfDelimUTF8(InputStream s)
Skips a string encoded by
writeSelfDelimUTF8(OutputStream) . |
int |
span(char[] c)
Spans the initial segment of this mutable string made of the specified characters.
|
int |
span(char[] c,
int from)
Spans a segment of this mutable string made of the specified characters.
|
int |
span(it.unimi.dsi.fastutil.chars.CharSet s)
Spans the initial segment of this mutable string made of the specified characters.
|
int |
span(it.unimi.dsi.fastutil.chars.CharSet s,
int from)
Spans a segment of this mutable string made of the specified characters.
|
MutableString |
squeezeSpace()
Squeezes and normalises spaces in this mutable string.
|
MutableString |
squeezeSpaces(boolean squeezeOnlyWhitespace)
Squeezes and normalises spaces in this mutable string.
|
MutableString |
squeezeWhitespace()
Squeezes and normalises whitespace in this mutable string.
|
boolean |
startsWith(CharSequence prefix)
Returns whether this mutable string starts with the given character sequence.
|
boolean |
startsWith(MutableString prefix)
Returns whether this mutable string starts with the given mutable string.
|
boolean |
startsWith(String prefix)
Returns whether this mutable string starts with the given string.
|
boolean |
startsWithIgnoreCase(CharSequence prefix)
Returns whether this mutable string starts with the given character sequence disregarding case.
|
boolean |
startsWithIgnoreCase(MutableString prefix)
Returns whether this mutable string starts with the given mutable string disregarding case.
|
boolean |
startsWithIgnoreCase(String prefix)
Returns whether this mutable string starts with the given string disregarding case.
|
CharSequence |
subSequence(int start,
int end)
Returns a subsequence of this mutable string.
|
MutableString |
substring(int start)
Returns a substring of this mutable string.
|
MutableString |
substring(int start,
int end)
Returns a substring of this mutable string.
|
char[] |
toCharArray()
Converts this string to a new character array.
|
MutableString |
toLowerCase()
Converts all of the characters in this mutable string to lower
case using the rules of the default locale.
|
String |
toString() |
MutableString |
toUpperCase()
Converts all of the characters in this mutable string to upper
case using the rules of the default locale.
|
MutableString |
trim()
Trims all leading and trailing whitespace from this string.
|
MutableString |
trimLeft()
Trims all leading whitespace from this string.
|
MutableString |
trimRight()
Trims all trailing whitespace from this string.
|
static MutableString |
wrap(char[] a)
Wraps a given character array in a compact mutable string.
|
static MutableString |
wrap(char[] a,
int length)
Wraps a given character array for a given length in a loose mutable string.
|
void |
write(Writer w)
Writes this mutable string to a
Writer . |
void |
writeSelfDelimUTF8(DataOutput s)
Writes this mutable string to a
DataOutput as a
length followed by a UTF-8 encoding. |
void |
writeSelfDelimUTF8(OutputStream s)
Writes this mutable string to an
OutputStream as a
length followed by a UTF-8 encoding. |
void |
writeUTF8(DataOutput s)
Writes this mutable string in UTF-8 encoding.
|
void |
writeUTF8(OutputStream s)
Writes this mutable string in UTF-8 encoding.
|
protected transient char[] array
protected transient int hashLength
public static final long serialVersionUID
public MutableString()
public MutableString(int capacity)
capacity
- the required capacity.public MutableString(MutableString s)
s
- the initial contents of the string.public MutableString(String s)
String
.s
- the initial contents of the string.public MutableString(CharSequence s)
CharSequence
.s
- the initial contents of the string.public MutableString(char[] a)
a
- the initial contents of the string.public MutableString(char[] a, int offset, int len)
a
- a character array.offset
- an offset into the array.len
- how many characters to copy.public MutableString copy()
public Object clone()
This method is identical to copy()
, but the latter returns
a more specific type.
public static void getChars(CharSequence s, int start, int end, char[] dest, int destStart)
String.getChars(int,int,char[],int)
for a CharSequence
s.s
- a CharSequence
.start
- copy start from this index (inclusive).end
- copy ends at this index (exclusive).dest
- destination array.destStart
- the first character will be copied in dest[destStart]
.String.getChars(int,int,char[],int)
public final int length()
length
in interface CharSequence
public final int capacity()
public final char[] array()
For fast, repeated access to the characters of this mutable string,
you can obtain the actual backing array. Be careful, and if this mutable
string is compact do not modify its backing array without
calling changed()
immediately afterwards (or the cached hash
code will get out of sync, with unforeseeable consequences).
changed()
public final void getChars(int start, int end, char[] dest, int destStart)
start
(inclusive) to
index end
(exclusive) are copied from this
mutable string into the array dest
, starting
from index destStart
.start
- copy start from this index (inclusive).end
- copy ends at this index (exclusive).dest
- destination array.destStart
- the first character will be copied in dest[destStart]
.NullPointerException
- if dest
is null
.IndexOutOfBoundsException
- if any of the following is true:
start
or end
is negative
start
is greater than
end
end
is greater than
length()
end-start+destStart
is greater than
dest.length
String.getChars(int,int,char[],int)
public final MutableString ensureCapacity(int minimumCapacity)
The new capacity of this string will be exactly equal to the
provided argument if this mutable string is compact (this differs
markedly from StringBuffer
). If this mutable string is loose, the provided argument
is maximised with the current capacity doubled.
Note that if the given argument is greater than the current length, you will make this string loose (see the class description).
minimumCapacity
- we want at least this number of characters, but no more.public final MutableString length(int newLength)
If the provided length is greater than that of the current string,
the string is padded with zeros. If it is shorter, the string is
truncated to the given length. We do not reallocate the backing
array, to increase object reuse. Use rather compact()
for that
purpose.
Note that shortening a string will make it loose (see the class description).
newLength
- the new length for this mutable string.compact()
public final MutableString setLength(int newLength)
length(int)
.newLength
- the new length for this mutable string.length(int)
public final MutableString compact()
Note that this operation may require reallocating the backing array (of course, with a shorter length).
isCompact()
public final MutableString loose()
isLoose()
public final boolean isCompact()
compact()
public final boolean isLoose()
loose()
public final MutableString changed()
You will need to call this method only if you change the backing array of a compact mutable string directly.
public static MutableString wrap(char[] a)
The returned mutable string will be compact and backed by the given character array.
a
- a character array.public static MutableString wrap(char[] a, int length)
The returned mutable string will be loose and backed by the given character array.
a
- a character array.length
- a length.public final char charAt(int index)
If you end up calling repeatedly this method, you
should consider using array()
instead.
charAt
in interface CharSequence
index
- the index of a character.public final MutableString setCharAt(int index, char c)
charAt(int,char)
.index
- the index of a character.c
- the new character.charAt(int,char)
public final MutableString charAt(int index, char c)
If you end up calling repeatedly this method, you should consider
using array()
instead.
index
- the index of a character.c
- the new character.public final char firstChar()
StringIndexOutOfBoundsException
- when called on the empty string.public final char lastChar()
ArrayIndexOutOfBoundsException
- when called on the empty string.public final char[] toCharArray()
public final MutableString substring(int start, int end)
The creation of a substring implies the creation of a new backing array. The returned mutable string will be compact.
start
- first character of the substring (inclusive).end
- last character of the substring (exclusive).public final MutableString substring(int start)
start
- first character of the substring (inclusive).substring(int,int)
public final CharSequence subSequence(int start, int end)
Subsequences share the backing array. Thus, you should
not use a subsequence after changing the characters of this
mutable string between start
and end
.
Equality of CharSequence
s returned by this method is defined by
content equality, and hash codes are identical to mutable strings with the same
content. Thus, you can mix mutable strings and CharSequence
s
returned by this method in data structures, as the contracts of equals()
and
hashCode()
are honoured.
subSequence
in interface CharSequence
start
- first character of the subsequence (inclusive).end
- last character of the subsequence (exclusive).substring(int,int)
public final MutableString append(MutableString s)
s
- the mutable string to append.public final MutableString append(String s)
String
to this mutable string.s
- a String
(null
is not allowed).NullPointerException
- if the argument is null
public final MutableString append(CharSequence s)
CharSequence
to this mutable string.append
in interface Appendable
s
- a CharSequence
or null
.Appendable.append(java.lang.CharSequence)
public final MutableString append(CharSequence s, int start, int end)
CharSequence
to this mutable string.
Warning: the semantics of this method of that of
append(char[], int, int)
are different.append
in interface Appendable
s
- a CharSequence
or null
.start
- the index of the first character of the subsequence to append.end
- the index of the character after the last character in the subsequence.Appendable.append(java.lang.CharSequence, int, int)
public final MutableString append(CharSequence[] a, int offset, int length, CharSequence separator)
a
- an array.offset
- the index of the first character sequence to append.length
- the number of character sequences to append.separator
- a separator that will be appended inbetween the character sequences.public final MutableString append(CharSequence[] a, CharSequence separator)
a
- an array.separator
- a separator that will be appended inbetween the character sequences.public final MutableString append(Object[] a, int offset, int length, CharSequence separator)
a
- an array of objects.offset
- the index of the first object to append.length
- the number of objects to append.separator
- a separator that will be appended inbetween the string representations of the given objects.public final MutableString append(Object[] a, CharSequence separator)
a
- an array of objects.separator
- a separator that will be appended inbetween the string representations of the given objects.public final MutableString append(char[] a)
a
- an array to append.public final MutableString append(char[] a, int offset, int len)
append(CharSequence, int, int)
are different.a
- an array.offset
- the index of the first character to append.len
- the number of characters to append.public final MutableString append(it.unimi.dsi.fastutil.chars.CharList list)
list
- the list to append.public final MutableString append(it.unimi.dsi.fastutil.chars.CharList list, int offset, int len)
append(CharSequence, int, int)
are different.list
- a character list.offset
- the index of the first character to append.len
- the number of characters to append.public final MutableString append(boolean b)
b
- the boolean to be appended.public final MutableString append(char c)
Note that this method will reallocate the backing array of a compact mutable string for each character appended. Do not call it lightly.
append
in interface Appendable
c
- a character.public final MutableString append(int i)
i
- the integer to be appended.public final MutableString append(long l)
l
- the long to be appended.public final MutableString append(float f)
f
- the float to be appended.public final MutableString append(double d)
d
- the double to be appended.public final MutableString append(Object o)
o
- the object to append.public final MutableString insert(int index, MutableString s)
index
.index
- position at which to insert the String
.s
- the mutable string to be inserted.NullPointerException
- if s
is null
IndexOutOfBoundsException
- if index
is negative or greater than length()
.public final MutableString insert(int index, String s)
String
in this mutable string, starting from index index
.index
- position at which to insert the String
.s
- the String
to be inserted.NullPointerException
- if s
is null
IndexOutOfBoundsException
- if index
is negative or greater than length()
.public final MutableString insert(int index, CharSequence s)
CharSequence
in this mutable string, starting from index index
.index
- position at which to insert the CharSequence
.s
- the CharSequence
to be inserted.NullPointerException
- if s
is null
IndexOutOfBoundsException
- if index
is negative or greater than length()
.public final MutableString insert(int index, char[] c)
c
are inserted in this mutable string,
and the first inserted character is going to have index index
.index
- position at which to insert subarray.c
- the character array.NullPointerException
- if c
is null
IndexOutOfBoundsException
- if index
is negative or greater than length()
.public final MutableString insert(int index, char[] c, int offset, int len)
len
characters
of the array c
, with indices starting from
offset
, are inserted in this mutable string,
and the first inserted character is going to have index index
.index
- position at which to insert subarray.c
- the character array.offset
- the index of the first character of c
to
to be inserted.len
- the number of characters of c
to
to be inserted.NullPointerException
- if c
is null
IndexOutOfBoundsException
- if index
is negative or greater than length()
, or
offset
or len
are negative, or
offset+len
is greater than
c.length
.public final MutableString insert(int index, boolean b)
index
.index
- position at which to insert the String
.b
- the boolean to be inserted.IndexOutOfBoundsException
- if index
is negative or greater than length()
.public final MutableString insert(int index, char c)
index
.
Note that this method will not expand the capacity of a compact mutable string by more than one character. Do not call it lightly.
index
- position at which to insert the String
.c
- the char to be inserted.IndexOutOfBoundsException
- if index
is negative or greater than length()
.public final MutableString insert(int index, double d)
index
.index
- position at which to insert the String
.d
- the double to be inserted.IndexOutOfBoundsException
- if index
is negative or greater than length()
.public final MutableString insert(int index, float f)
index
.index
- position at which to insert the String
.f
- the float to be inserted.IndexOutOfBoundsException
- if index
is negative or greater than length()
.public final MutableString insert(int index, int x)
index
.index
- position at which to insert the String
.x
- the int to be inserted.IndexOutOfBoundsException
- if index
is negative or greater than length()
.public final MutableString insert(int index, long l)
index
.index
- position at which to insert the String
.l
- the long to be inserted.IndexOutOfBoundsException
- if index
is negative or greater than length()
.public final MutableString insert(int index, Object o)
index
.index
- position at which to insert the String
.o
- the object to be inserted.IndexOutOfBoundsException
- if index
is negative or greater than length()
.public final MutableString delete(int start, int end)
start
(inclusive) to end
(exclusive). If end
is greater than or equal to the length
of this mutable string, all characters with indices greater
than or equal to start
are deleted.start
- The beginning index (inclusive).end
- The ending index (exclusive).IndexOutOfBoundsException
- if start
is greater than length()
, or greater than end
.public final MutableString deleteCharAt(int index)
Note that this method will reallocate the backing array of a compact mutable string for each character deleted. Do not call it lightly.
index
- Index of character to removeIndexOutOfBoundsException
- if index
is negative, or greater than or equal to length()
.public final MutableString delete(char c)
c
- the character to remove.public final MutableString delete(it.unimi.dsi.fastutil.chars.CharSet s)
s
- the set of characters to remove.public final MutableString delete(char[] c)
c
- an array containing the characters to remove.public final MutableString replace(int start, int end, MutableString s)
start
(inclusive)
to end
(exclusive) with the given mutable string.start
- The starting index (inclusive).end
- The ending index (exclusive).s
- The mutable string to be copied.IndexOutOfBoundsException
- if start
is negative,
greater than length()
, or greater than end
.public final MutableString replace(int start, int end, String s)
start
(inclusive)
to end
(exclusive) with the given String
.start
- The starting index (inclusive).end
- The ending index (exclusive).s
- The String
to be copied.IndexOutOfBoundsException
- if start
is negative,
greater than length()
, or greater than end
.public final MutableString replace(int start, int end, CharSequence s)
start
(inclusive)
to end
(exclusive) with the given CharSequence
.start
- The starting index (inclusive).end
- The ending index (exclusive).s
- The CharSequence
to be copied.IndexOutOfBoundsException
- if start
is negative,
greater than length()
, or greater than end
.public final MutableString replace(int start, int end, char c)
start
(inclusive)
to end
(exclusive) with the given character.start
- The starting index (inclusive).end
- The ending index (exclusive).c
- The character to be copied.IndexOutOfBoundsException
- if start
is negative,
greater than length()
, or greater than end
.public final MutableString replace(MutableString s)
s
- the mutable string whose content will replace the present one.public final MutableString replace(String s)
s
- the string whose content will replace the present one.public final MutableString replace(CharSequence s)
s
- the character sequence whose content will replace the present one.public final MutableString replace(char c)
c
- the character whose content will replace the present content.public final MutableString replace(char[] c, MutableString[] s)
Each occurrences of the character c[i]
in this mutable
string will be replaced by s[i]
. Note that
c
and s
must have the same length, and that
c
must not contain duplicates. Moreover, each replacement
string must be nonempty.
This method uses a Bloom filter to avoid repeated linear scans of the
character array for length()
times; however, this optimisation
will be most effective with arrays of less than twenty characters,
and, in fact, will very slightly slow down replacements with more than one hundred characters.
This method will try at most one reallocation.
c
- an array of characters to be replaced.s
- an array of replacement mutable strings.IllegalArgumentException
- if one of the replacement strings is empty.public final MutableString replace(char[] c, String[] s)
Each occurrences of the character c[i]
in this mutable
string will be replaced by s[i]
. Note that
c
and s
must have the same length, and that
c
must not contain duplicates. Moreover, each replacement
string must be nonempty.
This method uses a Bloom filter to avoid repeated linear scans of the
character array for length()
times; however, this optimisation
will be most effective with arrays of less than twenty characters,
and, in fact, will very slightly slow down replacements with more than one hundred characters.
This method will try at most one reallocation.
c
- an array of characters to be replaced.s
- an array of replacement strings.IllegalArgumentException
- if one of the replacement strings is empty.public final MutableString replace(char[] c, CharSequence[] s)
Each occurrences of the character c[i]
in this mutable
string will be replaced by s[i]
. Note that
c
and s
must have the same length, and that
c
must not contain duplicates. Moreover, each replacement
sequence must be nonempty.
This method uses a Bloom filter to avoid repeated linear scans of the
character array for length()
times; however, this optimisation
will be most effective with arrays of less than twenty characters,
and, in fact, will very slightly slow down replacements with more than one hundred characters.
This method will try at most one reallocation.
c
- an array of characters to be replaced.s
- an array of replacement character sequences.IllegalArgumentException
- if one of the replacement sequences is empty.public final MutableString replace(char[] c, char[] r)
Each occurrences of the character c[i]
in this mutable
string will be replaced by r[i]
. Note that
c
and s
must have the same length, and that
c
must not contain duplicates.
This method uses a Bloom filter to avoid repeated linear scans of the
character array for length()
times; however, this optimisation
will be most effective with arrays of less than twenty characters,
and, in fact, will very slightly slow down replacements with more than one hundred characters.
c
- an array of characters to be replaced.r
- an array of replacement characters.public final MutableString replace(it.unimi.dsi.fastutil.chars.Char2CharMap m)
Each occurrence of a key of m
will be substituted with the corresponding value.
m
- a map specifiying the character replacements.public final MutableString replace(char c, MutableString s)
Each occurrences of the character c
in this mutable
string will be replaced by s
. Note that s
must be nonempty.
This method will try at most one reallocation.
c
- a character to be replaced.s
- a replacement mutable string.IllegalArgumentException
- if the replacement string is empty.public final MutableString replace(char c, String s)
Each occurrences of the character c
in this mutable
string will be replaced by s
. Note that s
must be nonempty.
This method will try at most one reallocation.
c
- a character to be replaced.s
- a replacement string.IllegalArgumentException
- if the replacement sequence is empty.public final MutableString replace(char c, CharSequence s)
Each occurrences of the character c
in this mutable
string will be replaced by s
. Note that s
must be nonempty.
This method will try at most one reallocation.
c
- a character to be replaced.s
- a replacement character sequence.IllegalArgumentException
- if the replacement sequence is empty.public final MutableString replace(char c, char r)
c
- a character to be replaced.r
- a replacement character.public final MutableString replace(MutableString s, MutableString r)
Each occurrences of the mutable string s
in this mutable
string will be replaced by r
. Note that s
must be nonempty, unless r
is empty, too.
If the replacement string is longer than the search string,
occurrences of the search string are matched from the end of
this mutable string (i.e., using lastIndexOf()
). Otherwise, occurrences of the search string are matched
from the start (i.e., using indexOf()
). This has no effect on the semantics, unless
there are overlapping occurrences.
This method will try at most one reallocation.
s
- a mutable string to be replaced.r
- a replacement mutable string.IllegalArgumentException
- if you try to replace the empty string with a nonempty string.public final MutableString replace(String s, String r)
Each occurrences of the string s
in this mutable
string will be replaced by r
. Note that s
must be nonempty, unless r
is empty, too.
This method will try at most one reallocation.
s
- a string to be replaced.r
- a replacement string.IllegalArgumentException
- if you try to replace the empty string with a nonempty string.replace(MutableString,MutableString)
public final MutableString replace(CharSequence s, CharSequence r)
Each occurrences of the string s
in this mutable
string will be replaced by r
. Note that s
must be nonempty, unless r
is empty, too.
This method will try at most one reallocation.
s
- a character sequence to be replaced.r
- a replacement character sequence.IllegalArgumentException
- if you try to replace the empty sequence with a nonempty sequence.replace(MutableString,MutableString)
public final int indexOf(char c)
c
- the character to look for.c
, or
-1
, if the character never appears.public final int indexOf(char c, int from)
c
- the character to look for.from
- the index from which the search must start.c
, or
-1
, if the character never appears with index greater than
or equal to from
.public final int indexOf(MutableString pattern, int from)
This method uses a lightweight combination of Sunday's QuickSearch (a simplified but very effective variant of the Boyer—Moore search algorithm) and Bloom filters. More precisely, instead of recording the last occurrence of all characters in the pattern, we simply record in a small Bloom filter which characters belong to the pattern. Every time there is a mismatch, we look at the character immediately following the pattern: if it is not in the Bloom filter, besides moving to the next position we can additionally skip a number of characters equal to the length of the pattern.
Unless called with a pattern that saturates the filter, this method
will usually outperform that of String
.
pattern
- the mutable string to look for.from
- the index from which the search must start.pattern
after
the first from
characters, or -1
, if the string never
appears.public final int indexOf(MutableString pattern)
pattern
- the mutable string to look for.pattern
, or
-1
, if the string never appears.indexOf(MutableString,int)
public final int indexOf(CharSequence pattern, int from)
Searching for a character sequence is slightly slower than searching for a mutable string
, as every
character of the pattern must be accessed through a method
call. Please consider wrapping pattern
in a mutable string, or use
TextPattern
for repeated searches.
pattern
- the character sequence to look for.from
- the index from which the search must start.pattern
after
the first from
characters, or -1
, if the string never
appears.indexOf(MutableString,int)
public final int indexOf(CharSequence pattern)
pattern
- the character sequence to look for.pattern
, or
-1
, if the string never appears.indexOf(CharSequence,int)
public int indexOf(TextPattern pattern, int from)
To use this method, you have to create a text pattern first.
pattern
- a compiled text pattern to be searched for.from
- the index from which the search must start.pattern
, or
-1
, if no such character ever appears.public int indexOf(TextPattern pattern)
pattern
- a compiled text pattern to be searched for.pattern
, or
-1
, if no such character ever appears.indexOf(TextPattern, int)
public int indexOfAnyOf(it.unimi.dsi.fastutil.chars.CharSet s, int from)
s
- a set of characters to be searched for.from
- the index from which the search must start.s
, or
-1
, if no such character ever appears.public int indexOfAnyOf(it.unimi.dsi.fastutil.chars.CharSet s)
s
- a set of characters to be searched for.s
, or
-1
, if no such character ever appears.indexOfAnyOf(CharSet,int)
public int indexOfAnyOf(char[] c, int from)
This method uses a Bloom filter to avoid repeated linear scans of the
array c
; however, this optimisation
will be most effective with arrays of less than twenty characters,
and, in fact, will very slightly slow down searches for more than one hundred characters.
c
- an array of characters to be searched for.from
- the index from which the search must start.c
, or
-1
, if no such character ever appears.public int indexOfAnyOf(char[] c)
c
- an array of characters to be searched for.c
, or
-1
, if no such character ever appears.indexOfAnyOf(char[],int)
public int indexOfAnyBut(it.unimi.dsi.fastutil.chars.CharSet s, int from)
s
- a set of characters to be searched for.from
- the index from which the search must start.s
, or
-1
, if no such character ever appears.public int indexOfAnyBut(it.unimi.dsi.fastutil.chars.CharSet s)
s
- a set of characters to be searched for.s
, or
-1
, if no such character ever appears.indexOfAnyBut(CharSet,int)
public int indexOfAnyBut(char[] c, int from)
This method uses a Bloom filter to avoid repeated linear scans of the
array c
; however, this optimisation
will be most effective with arrays of less than twenty characters,
and, in fact, will very slightly slow down searches for more than one hundred characters.
c
- an array of characters to be searched for.from
- the index from which the search must start.c
, or
-1
, if no such character ever appears.public int indexOfAnyBut(char[] c)
c
- an array of characters to be searched for.c
, or
-1
, if no such character ever appears.indexOfAnyBut(char[],int)
public final int lastIndexOf(char c)
c
- the character to look for.c
, or
-1
, if the character never appears.public final int lastIndexOf(char c, int from)
c
- the character to look for.from
- the index from which the search must start.c
, or
-1
, if the character never appears with index smaller than
or equal to from
.public final int lastIndexOf(MutableString pattern, int from)
pattern
- the mutable string to look for.from
- the index from which the search must start.pattern
, or
-1
, if the pattern
never appears with index
smaller than or equal to from
.public final int lastIndexOf(MutableString pattern)
pattern
- the mutable string to look for.pattern
, or
-1
, if the pattern
never appears.lastIndexOf(MutableString,int)
public final int lastIndexOf(CharSequence pattern, int from)
pattern
- the character sequence to look for.from
- the index from which the search must start.pattern
, or
-1
, if the pattern
never appears with index
smaller than or equal to from
.lastIndexOf(MutableString,int)
public final int lastIndexOf(CharSequence pattern)
pattern
- the character sequence to look for.pattern
, or
-1
, if the pattern
never appears.lastIndexOf(CharSequence,int)
public int lastIndexOfAnyOf(it.unimi.dsi.fastutil.chars.CharSet s, int from)
This method uses a Bloom filter to avoid repeated linear scans of the
array c
; however, this optimisation
will be most effective with arrays of less than twenty characters,
and, in fact, will very slightly slow down searches for more than one hundred characters.
s
- a set of characters to be searched for.from
- the index from which the search must start.s
, or
-1
, if no character in s
ever appears with index
smaller than or equal to from
.public int lastIndexOfAnyOf(it.unimi.dsi.fastutil.chars.CharSet s)
s
- a set of characters to be searched for.s
, or
-1
, if no such character ever appears.lastIndexOfAnyOf(CharSet, int)
public int lastIndexOfAnyOf(char[] c, int from)
This method uses a Bloom filter to avoid repeated linear scans of the
array c
; however, this optimisation
will be most effective with arrays of less than twenty characters,
and, in fact, will very slightly slow down searches for more than one hundred characters.
c
- an array of characters to be searched for.from
- the index from which the search must start.c
, or
-1
, if no character in c
ever appears with index
smaller than or equal to from
.public int lastIndexOfAnyOf(char[] c)
c
- an array of characters to be searched for.c
, or
-1
, if no such character ever appears.lastIndexOfAnyOf(char[], int)
public int lastIndexOfAnyBut(it.unimi.dsi.fastutil.chars.CharSet s, int from)
This method uses a Bloom filter to avoid repeated linear scans of the
array c
; however, this optimisation
will be most effective with arrays of less than twenty characters,
and, in fact, will very slightly slow down searches for more than one hundred characters.
s
- a set of characters to be searched for.from
- the index from which the search must start.c
, or
-1
, if no such character ever appears.public int lastIndexOfAnyBut(it.unimi.dsi.fastutil.chars.CharSet s)
s
- a set of characters to be searched for.s
, or
-1
, if no such character ever appears.lastIndexOfAnyBut(CharSet,int)
public int lastIndexOfAnyBut(char[] c, int from)
This method uses a Bloom filter to avoid repeated linear scans of the
array c
; however, this optimisation
will be most effective with arrays of less than twenty characters,
and, in fact, will very slightly slow down searches for more than one hundred characters.
c
- an array of characters to be searched for.from
- the index from which the search must start.c
, or
-1
, if no such character ever appears.public int lastIndexOfAnyBut(char[] c)
c
- an array of characters to be searched for.c
, or
-1
, if no such character ever appears.lastIndexOfAnyBut(char[],int)
public int span(it.unimi.dsi.fastutil.chars.CharSet s, int from)
s
- a set of characters.from
- the index from which to span.s
starting at from
.cospan(CharSet, int)
public int span(it.unimi.dsi.fastutil.chars.CharSet s)
s
- a set of characters.s
.span(CharSet, int)
public int span(char[] c, int from)
This method uses a Bloom filter to avoid repeated linear scans of the
array c
; however, this optimisation
will be most effective with arrays of less than twenty characters,
and, in fact, will very slightly slow down searches for more than one hundred characters.
c
- an array of characters.from
- the index from which to span.c
starting at from
.cospan(char[], int)
public int span(char[] c)
c
- an array of characters.c
.span(char[], int)
public int cospan(it.unimi.dsi.fastutil.chars.CharSet s, int from)
s
- a set of characters.from
- the index from which to span.s
starting at from
.span(CharSet, int)
public int cospan(it.unimi.dsi.fastutil.chars.CharSet s)
s
- a set of characters.s
.cospan(CharSet, int)
public int cospan(char[] c, int from)
This method uses a Bloom filter to avoid repeated linear scans of the
array c
; however, this optimisation
will be most effective with arrays of less than twenty characters,
and, in fact, will very slightly slow down searches for more than one hundred characters.
c
- an array of characters.from
- the index from which to span.c
starting at from
.span(char[], int)
public int cospan(char[] c)
c
- an array of characters.c
.cospan(char[], int)
public final boolean startsWith(MutableString prefix)
prefix
- a mutable string.true
if this mutable string starts with prefix
.public final boolean startsWith(String prefix)
prefix
- a string.true
if this mutable string starts with prefix
.public final boolean startsWith(CharSequence prefix)
prefix
- a character sequence.true
if this mutable string starts with prefix
.public final boolean startsWithIgnoreCase(MutableString prefix)
prefix
- a mutable string.true
if this mutable string starts with prefix
up to case.public final boolean startsWithIgnoreCase(String prefix)
prefix
- a string.true
if this mutable string starts with prefix
up to case.public final boolean startsWithIgnoreCase(CharSequence prefix)
prefix
- a character sequence.true
if this mutable string starts with prefix
up to case.public final boolean endsWith(MutableString suffix)
suffix
- a mutable string.true
if this mutable string ends with suffix
.public final boolean endsWith(String suffix)
suffix
- a string.true
if this mutable string ends with suffix
.public final boolean endsWith(CharSequence suffix)
suffix
- a character sequence.true
if this mutable string ends with prefix
.public final boolean endsWithIgnoreCase(MutableString suffix)
suffix
- a mutable string.true
if this mutable string ends with suffix
up to case.public final boolean endsWithIgnoreCase(String suffix)
suffix
- a string.true
if this mutable string ends with suffix
up to case.public final boolean endsWithIgnoreCase(CharSequence suffix)
suffix
- a character sequence.true
if this mutable string ends with prefix
up to case.public final MutableString toLowerCase()
public final MutableString toUpperCase()
public final MutableString trim()
public final MutableString trimLeft()
public final MutableString trimRight()
public final MutableString squeezeSpaces(boolean squeezeOnlyWhitespace)
Character.isSpaceChar(char)
(or
Character.isWhitespace(char)
if squeezeOnlyWhitespace
is true)
will be transformed into a single space.squeezeOnlyWhitespace
- if true, a space is defined by Character.isWhitespace(char)
;
otherwise, a space is defined by Character.isSpaceChar(char)
.public final MutableString squeezeWhitespace()
Character.isWhitespace(char)
will be transformed
into a single space.squeezeSpace()
public final MutableString squeezeSpace()
Character.isSpaceChar(char)
will be transformed
into a single space.squeezeWhitespace()
public final MutableString reverse()
public final void write(Writer w) throws IOException
Writer
.w
- a Writer
.IOException
- if thrown by the provided Writer
.public final int read(Reader r, int length) throws IOException
write(Writer)
from a Reader
.
If length
is smaller than the current capacity or the
number of characters actually read is smaller than length
the string will become loose.
r
- a Reader
.length
- the number of characters to read.IOException
- if thrown by the provided Reader
.public final void print(PrintWriter w)
PrintWriter
.w
- a PrintWriter
.public final void println(PrintWriter w)
PrintWriter
and then terminates the line.w
- a PrintWriter
.public final void print(PrintStream s)
PrintStream
.s
- a PrintStream
.public final void println(PrintStream s)
PrintStream
and then terminates the line.s
- a PrintStream
.public final void writeUTF8(DataOutput s) throws IOException
The string is coded in UTF-8, not in the Java modified UTF representation. Thus, an ASCII NUL is represented by a single zero. Watch out!
This method does not try to do any caching (in particular, it does not create any object). On non-buffered data outputs it might be very slow.
s
- a data output.IOException
- if s
does.public final MutableString readUTF8(DataInput s, int length) throws IOException
This method does not try to do any read-ahead (in particular, it does not create any object). On non-buffered data inputs it might be very slow.
This method is able to read only strings containing UTF-8 sequences
of at most 3 bytes. Longer sequences will cause a UTFDataFormatException
.
If length
is smaller than the current capacity,
the string will become loose.
s
- a data input.length
- the number of characters to read.UTFDataFormatException
- on UTF-8 sequences longer than three octects.IOException
- if s
does.public final void writeSelfDelimUTF8(DataOutput s) throws IOException
DataOutput
as a
length followed by a UTF-8 encoding.
The purpose of this method and of readSelfDelimUTF8(DataInput)
is to provide a simple, ready-to-use
(even if not particularly efficient) method for storing arbitrary
mutable strings in a self-delimiting way.
You can save any mutable string using this method. The length will be written in packed 7-bit format, that is, as a list of blocks of 7 bits (from higher to lower) in which the seventh bit determines whether there is another block in the list. For strings shorter than 27 characters, the length will be packed in one byte, for strings shorter than 214 in 2 bytes and so on.
The string following the length is coded in UTF-8, not in the Java modified UTF representation. Thus, an ASCII NUL is represented by a single zero. Watch out!
s
- a data output.IOException
- if s
does.writeUTF8(DataOutput)
public final MutableString readSelfDelimUTF8(DataInput s) throws IOException
writeSelfDelimUTF8()
from a DataInput
.s
- a data input.UTFDataFormatException
- on UTF-8 sequences longer than three octects.IOException
- if s
does.readUTF8(DataInput,int)
public final void writeUTF8(OutputStream s) throws IOException
s
- an output stream.IOException
- if s
does.writeUTF8(DataOutput)
public static int skipSelfDelimUTF8(InputStream s) throws IOException
writeSelfDelimUTF8(OutputStream)
.s
- an input stream.UTFDataFormatException
- on UTF-8 sequences longer than three octects.IOException
- if s
does, or we try to read beyond end-of-file.writeSelfDelimUTF8(OutputStream)
public final MutableString readUTF8(InputStream s, int length) throws IOException
s
- an input stream.length
- the number of characters to read.UTFDataFormatException
- on UTF-8 sequences longer than three octects.IOException
- if s
does, or we try to read beyond end-of-file.readUTF8(DataInput, int)
public final void writeSelfDelimUTF8(OutputStream s) throws IOException
OutputStream
as a
length followed by a UTF-8 encoding.s
- an output stream.IOException
- if s
does.writeUTF8(DataOutput)
,
writeSelfDelimUTF8(DataOutput)
public final MutableString readSelfDelimUTF8(InputStream s) throws IOException
writeSelfDelimUTF8()
from an InputStream
.s
- an input stream.UTFDataFormatException
- on UTF-8 sequences longer than three octects.IOException
- if s
does.IOException
- if s
does, or we try to read beyond end-of-file.readUTF8(DataInput,int)
,
readSelfDelimUTF8(DataInput)
public final boolean equals(Object o)
This method will return true
iff its argument
is a CharSequence
containing the same characters of this
mutable string.
A potentially nasty consequence is that equality is not symmetric. See the discussion in the class description.
public final boolean equals(MutableString s)
equals()
.
This version of the equals(Object)
method will be called
on mutable strings.s
- a mutable string.equals(Object)
public final boolean equals(String s)
equals()
.
This version of the equals(Object)
method will be called
on String
s. It is guaranteed that it will return true
iff the mutable string and the String
contain the same characters.
Thus, you can use expressions like
mutableString.equals( "Hello" )to check against string contants.
s
- a String
.String
contain the same characters of this mutable string.equals(Object)
public final boolean equals(CharSequence s)
equals()
.
This version of the equals(Object)
method will be called
on character sequences. It is guaranteed that it will return true
iff this mutable string and the character sequence contain the same characters.s
- a character sequence.equals(Object)
public final boolean equalsIgnoreCase(MutableString s)
s
- a mutable string.String.equalsIgnoreCase(String)
public final boolean equalsIgnoreCase(String s)
equalsIgnoreCase()
.s
- a string.equalsIgnoreCase(MutableString)
public final boolean equalsIgnoreCase(CharSequence s)
equalsIgnoreCase()
.s
- a character sequence.equalsIgnoreCase(MutableString)
public final int compareTo(MutableString s)
compareTo
in interface Comparable<MutableString>
s
- a mutable string.public final int compareTo(String s)
s
- a String
.String
.public final int compareTo(CharSequence s)
s
- a character sequence.public final int compareToIgnoreCase(MutableString s)
ClassCastException
.
A potentially nasty consequence is that comparisons are not symmetric. See the discussion in the class description.
s
- a mutable string.String.compareToIgnoreCase(String)
public final int compareToIgnoreCase(String s)
compareToIgnoreCase()
.s
- a mutable string.compareToIgnoreCase(MutableString)
public final int compareToIgnoreCase(CharSequence s)
compareToIgnoreCase()
.s
- a mutable string.compareToIgnoreCase(MutableString)
public final int hashCode()
The hash code of a mutable string is the same as that of a
String
with the same content, but with the leftmost bit
set.
A compact mutable string caches its hash code, so it is
very efficient on data structures that check hash codes before invoking
equals()
.
hashCode
in class Object
String.hashCode()
public final String toString()
toString
in interface CharSequence
toString
in class Object
Copyright © 2006–2019 SYSTAP, LLC DBA Blazegraph. All rights reserved.