public class TextStore
extends java.lang.Object
implements java.io.Serializable
stores a text and parts of it in a (hopefully) efficient
manner. A TextStore
maintains a string as well as
possibly overlapping substrings thereof which are called
parts. The string can be enlarged with append()
and shortened by calling setLength()
. The parts, defined by start and end index, are
created with addPart()
and setPart()
.
Furthermore, a small nonnegative integer may be associated with
each part. This is a hack which was
introduced so that a TextStore
is exactly what is
needed to represent a match, its submatches as well as the small
identifying integers which can be associated with them.
The parts can be accessed with two getPart
methods
(getPart()
, getPart()
), which append the
requested part, or a piece thereof to a given
StringBuilder
.
Note: Many applications define part 0 to refer to the whole stored string, but this is not mandatory.
Part IndexingAll methods which have a part index as parameter accept negative values to address parts relative to the end of the part list. In particular -1 refers to the last part.
TextSplitter
,
Formatter
,
Serialized FormConstructor and Description |
---|
TextStore() |
Modifier and Type | Method and Description |
---|---|
void |
addPart(int start,
int end) |
void |
addPart(int start,
int end,
byte id)
like
setPart() except that the given part is
appended to the list of parts. |
void |
append(char ch) |
void |
append(java.lang.String s)
appends the given text to the
this . |
void |
append(java.lang.StringBuilder s,
int start,
int end)
appends the given text to the
this . |
void |
appendPart(java.lang.StringBuilder s,
int start,
int end) |
void |
appendPart(java.lang.StringBuilder s,
int start,
int end,
byte id)
combines operations
append(java.lang.String) and addPart() such that the whole appended text will also define a
new part. |
void |
clear()
resets
this to contain neither text nor any
parts. |
void |
deleteParts(int first,
int last)
deletes all parts in the inclusive range referenced by
first and last . |
void |
drain(CharSource dst,
int start)
calls
CharSource.pushBack() to get
rid of the substring of the text beginning at start. |
int |
getEnd(int part) |
byte |
getId(int part)
retrieves the small integer value which can be associated with
a part.
|
int |
getNumParts()
returns the number of currently stored parts.
|
java.lang.String |
getPart(int part)
returns the requested part.
|
java.lang.String |
getPart(int part,
int from,
int to)
returns a substring of the requested part.
|
void |
getPart(java.lang.StringBuilder sb,
int part)
appends the requested part to the given
StringBuilder . |
void |
getPart(java.lang.StringBuilder sb,
int part,
int from,
int to)
appends a substring of the requested part to the given
StringBuilder . |
int |
getPartLen(int part)
returns the length of the requested part.
|
int |
getStart(int part) |
int |
length()
returns the size of the currently stored text.
|
void |
setLength(int l) |
void |
setPart(int part,
int start,
int end)
calls the 4 parameter
setPart() with id==-1 . |
void |
setPart(int part,
int start,
int end,
byte id)
defines the given part as the stretch of characters in the stored
text starting at position
start and reaching to the
character just before end . |
java.lang.String |
toString() |
public int getNumParts()
public int length()
public void clear()
resets this
to contain neither text nor any
parts.
public void setLength(int l)
public void append(java.lang.String s)
public void append(char ch)
public void append(java.lang.StringBuilder s, int start, int end)
public void setPart(int part, int start, int end, byte id)
defines the given part as the stretch of characters in the stored
text starting at position start
and reaching to the
character just before end
.
part
- see part indexingid
- is a small id to be assigned to new partjava.lang.ArrayIndexOutOfBoundsException
- if part
is
out of rangejava.lang.IllegalArgumentException
- if start
and
end
do not satisfy the
relation 0<=start<=end<=textSize()
.public void setPart(int part, int start, int end)
calls the 4 parameter setPart()
with id==-1
.
public void deleteParts(int first, int last)
deletes all parts in the inclusive range referenced by
first
and last
. This does
not change the underlying string. Negative part indexes are
allowed, but the parts referenced by first
and
last
must be in order.
public void addPart(int start, int end, byte id)
like setPart()
except that the given part is
appended to the list of parts.
public void addPart(int start, int end)
public void appendPart(java.lang.StringBuilder s, int start, int end, byte id)
combines operations append(java.lang.String)
and addPart()
such that the whole appended text will also define a
new part.
public void appendPart(java.lang.StringBuilder s, int start, int end)
public void getPart(java.lang.StringBuilder sb, int part)
appends the requested part to the given
StringBuilder
. If part
is out of range,
this is silently ignored and sb
is not changed.
part
- see part indexing.public java.lang.String getPart(int part)
returns the requested part.
getPart(StringBuilder,int)
public byte getId(int part)
retrieves the small integer value which can be associated with a part.
java.lang.ArrayIndexOutOfBoundsException
- if part
is
out of rangepublic void getPart(java.lang.StringBuilder sb, int part, int from, int to)
appends a substring of the requested part to the given
StringBuilder
. The substring is indicated by
from
and to
, where to
refers to the character just after the last one requested. Both
indices are relative to the part, not the whole internally stored
text. Both indices can be negative in order to be taken relative
to getPartLen(part)
. In addition,
to==0
is also taken relative to the end of the
part.
If part
is out of range,
this is silently ignored and sb
is not changed.
If start
or end
are out of range,
they are truncated to the nearest fitting value.
Examples
from=0, to=0
refers to the whole partfrom=-2, to=-1
returns the 2nd to last
characterpart
- see part indexingpublic java.lang.String getPart(int part, int from, int to)
getPart(StringBuilder,int,int,int)
public int getPartLen(int part)
returns the length of the requested part. If part
is
out of range, 0 is returned.
part
- see part indexingpublic int getStart(int part)
public int getEnd(int part)
public java.lang.String toString()
toString
in class java.lang.Object
public void drain(CharSource dst, int start)
calls CharSource.pushBack()
to get
rid of the substring of the text beginning at start. Any parts
pointing into the drained substring will be shrunk, possible to
zero length. As with other TextStore
indices,
start
may be negative to denote a start relative to
the length of the text stored in this
.