removeControlCharacters

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

@safe pure nothrow
string
removeControlCharacters
(
return scope string line
)

Parameters

line string

String line to "remove" characters from.

Return Value

Type: string

A new string with control characters removed, or the original one unchanged.

Examples

{
    immutable line = "abc\ndef";
    immutable expected = "abcdef";
    immutable actual = removeControlCharacters(line);
    assert((actual == expected), actual);
}
{
    immutable line = "\n\t\r\0";
    immutable expected = "";
    immutable actual = removeControlCharacters(line);
    assert((actual == expected), actual);
}
{
    immutable line = "";
    immutable expected = "";
    immutable actual = removeControlCharacters(line);
    assert((actual == expected), actual);
}
{
    immutable line = "nothing to escape";
    immutable expected = "nothing to escape";
    immutable actual = removeControlCharacters(line);
    assert((actual == expected), actual);
    assert(line is actual);  // No new string was allocated
}