BasicString - multiple declarations

Struct BasicString

The BasicString is the generalization of struct string for character of type char, wchar or dchar.

struct BasicString(_Char, ulong N = 1, _Allocator)
  
if (isSomeChar!_Char && is(Unqual!_Char == _Char));

BasicString use utf-8, utf-16 or utf-32 encoding.

BasicString use SSO (Small String Optimization).

Template parameters:

_Char Character type. (char, wchar or dchar).

_Allocator Type of the allocator object used to define the storage allocation model. By default DefaultAllocator is used.

N Minimal capacity of BasicString (increase max length of small string).

Constructors

NameDescription
this (nil, fw) Constructs a empty BasicString object.
this (allocator) Constructs a empty BasicString object with allocator.
this (character, fw) Constructs a BasicString object, initializing its value to char value character.
this (character, allocator) Constructs a BasicString object, initializing its value to char value character.
this (slice, fw) Constructs a BasicString object from char slice slice.
this (slice, allocator) Constructs a BasicString object from char slice slice.
this (integer, fw) Constructs a BasicString object, initializing its value from integer integer.
this (integer, allocator) Constructs a BasicString object, initializing its value from integer integer.
this (rhs) Constructs a BasicString object from other BasicString object.
this (rhs, ) Forward constructor.
this (rhs) Copy constructor if AllocatorType is statless.
this (rhs) Copy constructor if AllocatorType has state.

Properties

NameTypeDescription
backCodePoint[get, set] dcharReturns last utf code point(dchar) of the BasicString.
backCodeUnit[get, set] BasicString.CharTypeReturns the last character(utf8: char, utf16: wchar, utf32: dchar) of the BasicString.
capacity[get] size_tReturns the size of the storage space currently allocated for the BasicString, expressed in terms of characters (utf code units).
empty[get] boolReturns whether the string is empty (i.e. whether its length is 0).
frontCodePoint[get] dcharReturns first utf code point(dchar) of the BasicString.
frontCodeUnit[get, set] BasicString.CharTypeReturns the first character(utf8: char, utf16: wchar, utf32: dchar) of the BasicString.
full[get] boolReturns whether the string is full (i.e. whether its length is equal to maximalCapacity).
length[get] size_tReturns the length of the string, in terms of number of characters (utf code units).
ptr[get] inout(BasicString.CharType)*Return pointer to the first element.
small[get] boolReturn true if string is small (Small String Optimization)
valid[get] boolReturn true if string is valid utf string.

Methods

NameDescription
append (val, count) Extends the BasicString by appending additional characters at the end of string.
build (args) Static function which return BasicString construct from arguments args.
clear () Erases the contents of the BasicString, which becomes an empty string (with a length of 0 characters).
contains (elm) Same as operator in
downsize (n) Downsizes the string to a length of n characters (utf code units).
erase (pos) Removes specified characters from the string.
insert (pos, val, count) Inserts additional characters into the BasicString right before the character indicated by pos or ptr.
opAssign (nil) Assigns a new value rhs to the string, replacing its current contents.
opBinary (rhs) Returns a newly constructed BasicString object with its value being the concatenation of the characters in this followed by those of rhs.
opBinaryRight (lhs) Returns a newly constructed BasicString object with its value being the concatenation of the characters in lhs followed by those of this.
opBinaryRight (elm) Operator in
opCast () Support for quelifier cast.
opCmp (rhs) Compares the contents of a string with another string, range, char/wchar/dchar or integer.
opDollar () Returns the length of the string, in terms of number of characters.
opEquals (rhs) Compares the contents of a string with another string, range, char/wchar/dchar or integer.
opIndex () Return slice of all character.
opIndex (pos) Returns character at specified location pos.
opIndexAssign (val, pos) Assign character at specified location pos to value val.
opSlice (begin, end) Returns a slice [begin .. end]. If the requested substring extends past the end of the string, the returned slice is [begin .. length()].
popBackCodePoint () Erases the last utf code point of the BasicString, effectively reducing its length by code point length.
popBackCodeUnit () Erases the last code unit of the BasicString, effectively reducing its length by 1.
proxySwap (rhs) Swaps the contents of this and rhs.
release () Erases and deallocate the contents of the BasicString, which becomes an empty string (with a length of 0 characters).
replace (pos, len, val, count) Replaces the portion of the string that begins at character pos and spans len characters (or the part of the string in the slice slice) by new contents.
reserve (n) Requests that the string capacity be adapted to a planned change in size to a length of up to n characters (utf code units).
resize (n, ch) Resizes the string to a length of n characters (utf code units).
shrinkToFit () Requests the BasicString to reduce its capacity to fit its length.
toHash () Calculates the hash value of string.
upsize (n, ch) Upsizes the string to a length of n characters (utf code units).

Aliases

NameDescription
allocator Returns allocator.
AllocatorType Type of the allocator object used to define the storage allocation model. By default DefaultAllocator is used.
allowHeap Allow heap (false only if Allcoator is void)
back Alias to backCodeUnit.
CharType Character type. (char, wchar or dchar).
front Alias to frontCodeUnit.
maximalCapacity Maximal capacity of string, in terms of number of characters (utf code units).
minimalCapacity Minimal capacity of string (same as maximum capacity of small string), in terms of number of characters (utf code units).
opOpAssign Extends the BasicString by appending additional characters at the end of its current value.
popBack Alias to popBackCodeUnit.
put Alias to append.

Example

alias String = BasicString!(
	char,               //character type
	32,                 //additional padding to increas max size of small string (small string does not allocate memory).
	DefaultAllocator    //allocator type (can be stateless or with state)
);


//copy:
{
	String a = "123";
	String b = a;

	a = "456"d;

	assert(a == "456");
	assert(b == "123");
}


//append:
{
	String str = "12";

	str.append("34");   //same as str += "34"
	str.append("56"w);  //same as str += "56"w
	str.append(7);      //same as str += 7;
	str.append('8');

	assert(str == "12345678");

	str.clear();

	assert(str.empty);
}

//erase:
{
	String str = "123456789";

	str.erase(2, 2);

	assert(str == "1256789");
}

//insert:
{
	String str = "123456789";

	str.insert(1, "xyz");

	assert(str == "1xyz23456789");
}

//replace:
{
	String str = "123456789";

	str.replace(1, 2, "xyz");

	assert(str == "1xyz456789");
}

//slice to string:
()@trusted{
	String str = "123456789";

	const(char)[] dstr = str[];

	assert(str == dstr);
}();

Alias BasicString

Alias to BasicString with different order of template parameters

alias BasicString(_Char, _Allocator, ulong N = 1) = BasicString!(_Char,N,_Allocator);