lu.string

String manipulation functions complementing the standard library.

Members

Classes

AdvanceException
class AdvanceException

Exception, to be thrown when a call to advancePast went wrong.

AdvanceExceptionImpl
class AdvanceExceptionImpl(Haystack, Needle)

Exception, to be thrown when a call to advancePast went wrong.

Enums

SplitResults
enum SplitResults

The result of a call to splitInto.

Functions

advancePast
auto advancePast(Haystack haystack, Needle needle, bool inherit, string callingFile, size_t callingLine)

Given some string, finds the supplied needle token in it, returns the string up to that point, and advances the passed string by ref to after the token.

decode64
string decode64(string encoded)

Base64-decodes a string.

encode64
string encode64(string line)

Base64-encodes a string.

escapeControlCharacters
string escapeControlCharacters(string line)

Replaces the control characters '\n', '\t', '\r' and '\0' with the escaped "\\n", "\\t", "\\r" and "\\0". Does not allocate a new string if there was nothing to escape.

indent
string indent(string wallOfText, uint numTabs, uint skip)

Indents lines in a string with the supplied number of tabs. Returns a newly allocated string.

indentInto
void indentInto(string wallOfText, Sink sink, uint numTabs, uint skip)

Indents lines in a string into an output range sink with the supplied number of tabs.

plurality
T plurality(Num num, T singular, T plural)

Selects the correct singular or plural form of a word depending on the numerical count of it.

removeControlCharacters
string removeControlCharacters(string line)

Removes the control characters '\n', '\t', '\r' and '\0' from a string. Does not allocate a new string if there was nothing to remove.

replaceFromAA
auto replaceFromAA(string line, Fn[string] aa)

Replaces space-separated tokens (that begin with a token character) in a string with values from a supplied associative array.

splitInto
auto splitInto(string slice, Strings strings, string[] overflow)

Splits a string by a passed separator and assign the delimited words to the passed strings by ref. Overload that stores overflow strings into a passed array.

splitInto
auto splitInto(string slice, Strings strings)

Splits a string by a passed separator and assign the delimited words to the passed strings by ref.

splitLineAtPosition
auto splitLineAtPosition(Line line, Separator separator, size_t maxLength)

Splits a string with on boundary as delimited by a supplied separator, into one or more more lines not longer than the passed maximum length.

splitWithQuotes
auto splitWithQuotes(string line)

Splits a string into an array of strings by whitespace, but honours quotes.

stripSuffix
auto stripSuffix(string line, string suffix)

Strips the supplied string from the end of a string.

stripped
auto stripped(string line)

Returns a slice of the passed string with any preceding or trailing whitespace or linebreaks sliced off both ends. Overload that implicitly strips " \n\r\t".

stripped
auto stripped(Line line, Chaff chaff)

Returns a slice of the passed string with any preceding or trailing passed characters sliced off. Implementation template capable of handling both individual characters and strings of tokens to strip.

strippedLeft
auto strippedLeft(string line)

Returns a slice of the passed string with any preceding whitespace and/or linebreaks sliced off. Overload that implicitly strips " \n\r\t".

strippedLeft
auto strippedLeft(Line line, Chaff chaff)

Returns a slice of the passed string with any preceding passed characters sliced off. Implementation capable of handling both individual characters and strings of tokens to strip.

strippedRight
auto strippedRight(string line)

Returns a slice of the passed string with any trailing whitespace and/or linebreaks sliced off. Overload that implicitly strips " \n\r\t".

strippedRight
auto strippedRight(Line line, Chaff chaff)

Returns a slice of the passed string with any trailing passed characters. Implementation template capable of handling both individual characters and string of tokens to strip.

tabs
auto tabs(int num)

Returns a range of *spaces* equal to that of num tabs (\t).

unquoted
auto unquoted(string line)

Removes paired preceding and trailing double quotes, unquoting a word. Assumes ASCII.

unsinglequoted
auto unsinglequoted(string line)

Removes paired preceding and trailing single quotes around a line. Assumes ASCII.

Examples

{
    string line = "Lorem ipsum :sit amet";
    immutable lorem = line.advancePast(" :");
    assert(lorem == "Lorem ipsum", lorem);
    assert(line == "sit amet", line);
}
{
    string line = "Lorem ipsum :sit amet";
    immutable lorem = line.advancePast(':');
    assert(lorem == "Lorem ipsum ", lorem);
    assert(line == "sit amet", line);
}
{
    string line = "Lorem ipsum sit amet";  // mutable, will be modified by ref
    string[] words;

    while (line.length > 0)
    {
        immutable word = line.advancePast(" ", inherit: true);
        words ~= word;
    }

    assert(words == [ "Lorem", "ipsum", "sit", "amet" ]);
}

Meta