GString

A GString, or grapheme-string, is a unicode-aware string supporting simple array-index operations that operate on graphemes (displayable characters).

For example, consider the string "R̆um". At what array index is the character 'u'? Because this is a UTF-8 encoded string, it is actually at index 3, not 1. "R̆" is composed of 2 unicode code-points, "R" and " ̆ " (combining breve). The "R" code-point is encoded in a single byte, but the combining breve requires two bytes. These two code-points are combined into a single visible character (grapheme), "R̆".

This type permits simple array-index operations based on graphemes, that is, the visible position of characters. It uses a string as its undrelying storage without an additional memory footprint. However, array-like operations tend to have O(n) complexity, because the type must compute the offset of each grapheme. Thus, GString is best used with smaller strings, especially those that need to perform operations by character position, such as in user interfaces.

If memory is not an issue, and array-like operations by Grapheme are needed for very large strings, it is better to use dchar[] or Grapheme[].

Constructors

this
this(StringT str)
Undocumented in source.
this
this(R str)
Undocumented in source.

Alias This

rawString

A GString can be used with libraries that operate on normal strings, however, they are treated like normal strings, and these libraries should be used with caution.

Members

Aliases

StringT
alias StringT = immutable(CharT)[]
Undocumented in source.

Functions

indexOf
ptrdiff_t indexOf(R str)

Finds the grapheme-index of the given search string.

opApply
int opApply(int delegate(Grapheme) @(safe) dg)

Enables the use of foreach (Grapheme; ...) statements.

opApply
int opApply(int delegate(size_t index, Grapheme) @(safe) dg)

Enables the use of foreach (size_t, Grapheme; ...) statements.

opBinary
typeof(this) opBinary(Grapheme g)

A binary operator to allow appending with a Grapheme.

opBinary
typeof(this) opBinary(R str)

A binary operator to allow appending with arbitrary strings.

opBinary
typeof(this) opBinary(C ch)

A binary operator supporting appending a single character.

opBinaryRight
typeof(this) opBinaryRight(Grapheme g)

A binary operator to allow appending with a Grapheme.

opBinaryRight
typeof(this) opBinaryRight(R str)

A binary operator to allow appending with arbitrary strings.

opBinaryRight
typeof(this) opBinaryRight(C ch)

A binary operator supporting appending a single character.

opIndex
Grapheme opIndex(size_t i)

Returns the i'th displayable character (Grapheme) within the GString.

opIndex
Grapheme[] opIndex()

Returns a range of Graphemes representing the original string.

opIndex
Grapheme[] opIndex(size_t[2] slice)
Undocumented in source. Be warned that the author may not have intended to support it.
opIndexAssign
void opIndexAssign(R str, size_t i)
void opIndexAssign(C ch, size_t i)
void opIndexAssign(Grapheme g, size_t i)

Replaces a single grapheme in the GString.

opOpAssign
void opOpAssign(C ch)

Allows appending to an existing GString using the ~= operator.

opOpAssign
void opOpAssign(R str)
void opOpAssign(Grapheme g)

Allows appending to an existing GString using the ~= operator.

opSlice
size_t[2] opSlice(size_t i, size_t j)

Creates a data structure to represent the 0'th index during array slicing, e.g. gstring[i..j].

toDString
dstring toDString()
Undocumented in source. Be warned that the author may not have intended to support it.
toString
string toString()
Undocumented in source. Be warned that the author may not have intended to support it.
toWString
wstring toWString()
Undocumented in source. Be warned that the author may not have intended to support it.

Variables

rawString
StringT rawString;

The underlying stored string using UTF encoding according to CharT.

Parameters

CharT

Controls the type of encoding to use. char => UTF-8, wchar => UTF-16, and dchar => UTF-32.

Examples

auto t1 = CGString("Test R̆ȧm͆b̪õ");
assert(t1.indexOf("ȧ") == 6);
assert(t1.indexOf("m̪") == -1);
t1[9] = "í";
assert(t1.toString() == "Test R̆ȧm͆b̪í");

Meta