lu.string

String manipulation functions complementing the standard library, as well as providing dumbed-down and optimised versions of existing functions therein.

Members

Classes

NomException
class NomException

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

NomExceptionImpl
class NomExceptionImpl(T, C)

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

Enums

SplitResults
enum SplitResults

The result of a call to splitInto.

Functions

beginsWith
bool beginsWith(T haystack, C needle)

A cheaper variant of std.algorithm.searching.startsWith, since this is such a hotspot.

beginsWithOneOf
bool beginsWithOneOf(T haystack, C needles)

Checks whether or not the first letter of a string begins with any of the passed string of characters, or single character.

contains
bool contains(T haystack, C needle)

Checks a string to see if it contains a given substring or character. Leverages std.string.indexOf and std.algorithm.searching.countUntil.

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.

nom
T nom(T haystack, C needle, 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.

nom
T nom(T haystack, C needle, 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.

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.

splitInto
SplitResults splitInto(string slice, Strings strings)

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

splitLineAtPosition
T[] splitLineAtPosition(T line, C separator, size_t maxLength)

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

stripSuffix
string stripSuffix(string line, string suffix)

Strips the supplied string from the end of a string.

stripped
string 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
T stripped(T line, C 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
string 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
T strippedLeft(T line, C 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
string 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
T strippedRight(T line, C 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
T unquoted(T line)

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

unsinglequoted
T unsinglequoted(T line)

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

Examples

{
    string line = "Lorem ipsum :sit amet";
    immutable lorem = line.nom(" :");
    assert(lorem == "Lorem ipsum", lorem);
    assert(line == "sit amet", line);
}
{
    string line = "Lorem ipsum :sit amet";
    immutable lorem = line.nom(':');
    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.nom!(Yes.inherit)(" ");
        words ~= word;
    }

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

Meta