public class XMLString extends Object implements CharSequence
This class is meant to replace the old XMLString in all areas
where performance and memory-efficiency is key. XMLString compatibility
remains in place in case one has used that in their own code.
This buffer is mutable and when you use it, make sure you work with it responsibly. In many cases, we will reuse the buffer to avoid fresh memory allocations, hence you have to pay attention to its usage pattern. It is not meant to be a general String replacement.
This class avoids many of the standard runtime checks that will result in a runtime or array exception anyway. Why check twice and raise the same exception?
| Modifier and Type | Field and Description |
|---|---|
static int |
CAPACITY_GROWTH |
static XMLString |
EMPTY |
static int |
INITIAL_CAPACITY |
| Constructor and Description |
|---|
XMLString()
Constructs an XMLCharBuffer with a default size.
|
XMLString(char[] ch,
int offset,
int length)
Constructs an XMLString structure preset with the specified values.
|
XMLString(int startSize)
Constructs an XMLCharBuffer with a desired size.
|
XMLString(int startSize,
int growBy)
Constructs an XMLCharBuffer with a desired size.
|
XMLString(String src)
Constructs an XMLCharBuffer from a string.
|
XMLString(XMLString src)
Constructs an XMLCharBuffer from another buffer.
|
XMLString(XMLString src,
int addCapacity)
Constructs an XMLCharBuffer from another buffer.
|
| Modifier and Type | Method and Description |
|---|---|
XMLString |
append(char c)
Appends a single character to the buffer.
|
XMLString |
append(char[] src,
int offset,
int length)
Add data from a char array to this buffer with the ability to specify
a range to copy from
|
XMLString |
append(char c1,
char c2)
Append two characters at once, mainly to make
a codePoint add more efficient
|
XMLString |
append(String src)
Append a string to this buffer without copying the string first.
|
XMLString |
append(XMLString src)
Add another buffer to this one.
|
boolean |
appendCodePoint(int codePoint)
Append a character to an XMLCharBuffer.
|
int |
capacity()
Returns the current max capacity without growth.
|
void |
characters(ContentHandler contentHandler) |
char |
charAt(int index)
Returns the char a the given position.
|
XMLString |
clear()
Resets the buffer to 0 length.
|
XMLString |
clearAndAppend(char c)
Resets the buffer to 0 length and sets the new data.
|
XMLString |
clone()
Returns a content copy of this buffer
|
void |
comment(LexicalHandler lexicalHandler) |
boolean |
contains(XMLString s)
See if this string contains the other
|
boolean |
endsWith(String s)
Does this buffer end with this string?
|
static boolean |
equals(CharSequence sequence,
XMLString s)
Compares a CharSequence with an XMLString in a null-safe manner.
|
boolean |
equals(Object o)
Two buffers are identical when the length and
the content of the backing array (only for the
data in view) are identical.
|
boolean |
equalsIgnoreCase(CharSequence s)
Compares this with a CharSequence in a case-insensitive manner.
|
static boolean |
equalsIgnoreCase(CharSequence sequence,
XMLString s)
Compares a CharSequence with an XMLString in a null-safe manner.
|
char[] |
getChars()
Get the characters as char array, this will be a copy!
|
int |
getGrowBy()
Tell us how much the capacity grows if needed
|
int |
hashCode()
We don't cache the hashcode because we mutate often.
|
void |
ignorableWhitespace(ContentHandler contentHandler) |
int |
indexOf(char c)
Find the first occurrence of a char
|
int |
indexOf(XMLString s)
Search for the first occurrence of another buffer in this buffer
|
boolean |
isWhitespace()
Check if we have only whitespaces
|
int |
length()
Returns the current length
|
XMLString |
prepend(char c)
Inserts a character at the beginning
|
XMLString |
reduceToContent(String startMarker,
String endMarker)
Deprecated.
Use the new method
trimToContent(String, String) instead. |
XMLString |
shortenBy(int count)
Shortens the buffer by that many positions.
|
CharSequence |
subSequence(int start,
int end)
Returns a
CharSequence that is a subsequence of this sequence. |
XMLString |
toLowerCase(Locale locale)
This lowercases an XMLString in place and will likely not
consume extra memory unless the character might grow.
|
String |
toString()
Returns a string representation of this buffer.
|
String |
toString(FastHashMap<XMLString,String> cache)
Returns a string representation of this buffer using a cache as
source to avoid duplicates.
|
static String |
toString(XMLString seq)
Returns a string representation of a buffer.
|
static String |
toString(XMLString seq,
FastHashMap<XMLString,String> cache)
Returns a string representation of the buffer using a cache as
source to avoid duplicates.
|
XMLString |
toUpperCase(Locale locale)
This uppercases an XMLString in place and will likely not
consume extra memory unless the character might grow.
|
XMLString |
trim()
Trims the string similar to
String.trim() |
XMLString |
trimLeading()
Removes all whitespace before the first non-whitespace char.
|
XMLString |
trimToContent(String startMarker,
String endMarker)
Reduces the buffer to the content between start and end marker when
only whitespaces are found before the startMarker as well as after the end marker.
|
XMLString |
trimTrailing()
Removes all whitespace at the end.
|
XMLString |
trimWhitespaceAtEnd()
Deprecated.
Use
trimTrailing() instead. |
char |
unsafeCharAt(int index)
Returns the char at the given position.
|
finalize, getClass, notify, notifyAll, wait, wait, waitchars, codePointspublic static final int CAPACITY_GROWTH
public static final int INITIAL_CAPACITY
public static final XMLString EMPTY
public XMLString()
public XMLString(int startSize)
startSize - the size of the buffer to start withpublic XMLString(int startSize,
int growBy)
startSize - the size of the buffer to start withgrowBy - by how much do we want to grow when neededpublic XMLString(XMLString src)
src - the source buffer to copy frompublic XMLString(XMLString src, int addCapacity)
src - the source buffer to copy fromaddCapacity - how much capacity to add to origin lengthpublic XMLString(String src)
src - the string to copy frompublic XMLString(char[] ch,
int offset,
int length)
There are not range checks performed. Make sure your data is correct.
ch - The character array, must not be nulloffset - The offset into the character array.length - The length of characters from the offset.public int capacity()
length()
for that.public XMLString append(char c)
c - the character to appendpublic XMLString append(char c1, char c2)
c1 - the first character to appendc2 - the second character to appendpublic XMLString append(String src)
src - the string to appendpublic XMLString append(XMLString src)
src - the buffer to appendpublic XMLString append(char[] src, int offset, int length)
src - the source char arrayoffset - the pos to start to copy fromlength - the length of the data to copypublic XMLString prepend(char c)
c - the char to insert at the beginningpublic int length()
length in interface CharSequencepublic int getGrowBy()
public XMLString clear()
public XMLString clearAndAppend(char c)
c - the char to setpublic boolean endsWith(String s)
s - the string to check the end againstpublic XMLString reduceToContent(String startMarker, String endMarker)
trimToContent(String, String) instead.If a marker is empty, it behaves like String.trim() on that side.
startMarker - the start string to find, must not be nullendMarker - the end string to find, must not be nullpublic XMLString trimToContent(String startMarker, String endMarker)
If a marker is empty, it behaves like String.trim() on that side.
startMarker - the start string to find, must not be nullendMarker - the end string to find, must not be nullpublic boolean isWhitespace()
public XMLString trim()
String.trim()public XMLString trimLeading()
public XMLString trimWhitespaceAtEnd()
trimTrailing() instead.public XMLString trimTrailing()
public XMLString shortenBy(int count)
count - a positive number, no runtime checks, if count is larger than
length, we get length = 0public char[] getChars()
public String toString()
toString in interface CharSequencetoString in class Objectpublic static String toString(XMLString seq)
public String toString(FastHashMap<XMLString,String> cache)
The cache will be filled with a copy of the XMLString to ensure immutability. This copy is minimally sized.
cache - the cache to be usedpublic static String toString(XMLString seq, FastHashMap<XMLString,String> cache)
The cache will be filled with a copy of the XMLString to ensure immutability. This copy is minimally sized.
seq - the XMLString to convertcache - the cache to be usedpublic char charAt(int index)
charAt in interface CharSequenceindex - the position to read fromIndexOutOfBoundsException - in case one tries to read outside of valid buffer rangepublic char unsafeCharAt(int index)
IndexOutOfBoundsException but using an
incorrect position in the array (such as beyond length)
might stay unnoticed! This is a performance method,
use at your own risk.index - the position to read frompublic XMLString clone()
public CharSequence subSequence(int start, int end)
CharSequence that is a subsequence of this sequence.
The subsequence starts with the char value at the specified index and
ends with the char value at index end - 1. The length
(in chars) of the
returned sequence is end - start, so if start == end
then an empty sequence is returned.subSequence in interface CharSequencestart - the start index, inclusiveend - the end index, exclusiveIndexOutOfBoundsException - if start or end are negative,
if end is greater than length(),
or if start is greater than endpublic boolean equals(Object o)
public static boolean equals(CharSequence sequence, XMLString s)
equals(Object). The XMLString
can be null, but the CharSequence must not be null. This mimics the
typical use case "string".equalsIgnoreCase(null) which returns false
without raising an exception.sequence - the sequence to compare to, null is permitteds - the XMLString to use for comparisonpublic int hashCode()
public boolean appendCodePoint(int codePoint)
codePoint - The character value.IllegalArgumentException - if the specified
codePoint is not a valid Unicode code point.public XMLString toUpperCase(Locale locale)
String.toUpperCase().
We cannot correctly deal with ß for instance.
Note: We change the current XMLString and don't get a copy back but this instance.
locale - the locale to use in case we have to bail out and convert
using String, this also means, that the result is not perfect
when comparing to String.toLowerCase(Locale)public XMLString toLowerCase(Locale locale)
String.toUpperCase().
Note: We change the current XMLString and don't get a copy back but this instance.
locale - the locale to use in case we have to bail out and convert
using String, this also means, that the result is not perfect
when comparing to String.toLowerCase(Locale)public static boolean equalsIgnoreCase(CharSequence sequence, XMLString s)
equalsIgnoreCase(CharSequence). The XMLString
can be null, but the CharSequence must not be null. This mimic the
typical use case "string".equalsIgnoreCase(null) which returns false
without raising an exception.sequence - the sequence to compare to, null is permitteds - the XMLString to use for comparisonpublic boolean equalsIgnoreCase(CharSequence s)
This code might have subtle edge-case defects for some rare locales
and related characters. See String.toLowerCase(Locale).
The locales tr, at, lt and the extra letters GREEK CAPITAL LETTER SIGMA
and LATIN CAPITAL LETTER I WITH DOT ABOVE are our challengers. If the
input would match with equals(Object), everything is fine, just
in case we have to check for a casing difference, we might see a problem.
But this is for XML/HTML characters and we know what we compare, hence this should not be any issue for us.
s - the sequence to compare to, null is permittedpublic int indexOf(char c)
c - the char to searchpublic int indexOf(XMLString s)
s - the buffer to be search forpublic boolean contains(XMLString s)
s - the XMLString to search and matchpublic void characters(ContentHandler contentHandler) throws SAXException
SAXExceptionpublic void ignorableWhitespace(ContentHandler contentHandler) throws SAXException
SAXExceptionpublic void comment(LexicalHandler lexicalHandler) throws SAXException
SAXExceptionCopyright © 2024 HtmlUnit. All rights reserved.