BasicString.opIndex - multiple declarations

Function BasicString.opIndex

Return slice of all character.

inout inout(BasicString.CharType)[] opIndex() pure nothrow @nogc @system;

The slice returned may be invalidated by further calls to other member functions that modify the object.

Examples

()@system{
    BasicString!char str = "123";

    char[] slice = str[];
    assert(slice.length == str.length);
    assert(slice.ptr is str.ptr);

    str.reserve(str.capacity * 2);
    assert(slice.length == str.length);
    assert(slice.ptr !is str.ptr);  // slice contains dangling pointer.
}();
()@safe{
    FixedString!(char, 20) str = "123";

    char[] slice = str[];
    assert(slice.length == str.length);
    assert(slice.ptr is str.ptr);

    assert(slice == "123");
}();

Function BasicString.opIndex

Returns character at specified location pos.

BasicString.CharType opIndex (
  const size_t pos
) const pure nothrow @nogc scope @trusted;

Examples

BasicString!char str = "abcd";

assert(str[1] == 'b');