tango.text.Util

Members

Aliases

splitLines
alias splitLines = toLines

Convert text into a set of lines, where each line is identified by a \n or \r\n combination. The line terminator is stripped from each resultant array

Functions

chopl
T[] chopl(T[] source, S match)

Chop the given source by stripping the provided match from the left hand side. Returns a slice of the original content

chopr
T[] chopr(T[] source, S match)

Chop the given source by stripping the provided match from the right hand side. Returns a slice of the original content

combine
T[] combine(T[] dst, const(T)[] prefix, const(T)[] postfix, const(T[])[] src)

Combine a series of text segments together, each prefixed and/or postfixed with optional strings. An optional output buffer can be provided to avoid heap activity - which should be large enough to contain the entire output, otherwise the heap will be used instead.

contains
bool contains(const(T)[] source, const(T) match)

Returns whether or not the provided array contains an instance of the given match

containsPattern
bool containsPattern(const(T)[] source, const(T)[] match)

Returns whether or not the provided array contains an instance of the given match

count
size_t count(const(T)[] source, const(T)[] match)

Count all instances of match within source

delimit
T[][] delimit(T[] src, const(M)[] set)

Split the provided array wherever a delimiter-set instance is found, and return the resultant segments. The delimiters are excluded from each of the segments. Note that delimiters are matched as a set of alternates rather than as a pattern.

delimiters
DelimFruct!(T, M) delimiters(T[] src, const(M)[] set)

Iterator to isolate text elements.

head
T[] head(T[] src, S[] pattern, T[] tail)

Split the provided array on the first pattern instance, and return the resultant head and tail. The pattern is excluded from the two segments.

index
size_t index(const(T)[] source, const(T)[] match, size_t start)

Return the index of the next instance of 'match' starting at position 'start', or source.length where there is no match.

indexOf
size_t indexOf(const(T)* str, const(T) match, size_t length)

Returns the index of the first match in str, failing once length is reached. Note that we return 'length' for failure and a 0-based index on success

isSpace
bool isSpace(T c)

Is the argument a whitespace character?

jhash
size_t jhash(const(ubyte)* k, size_t len, size_t c)
size_t jhash(const(void)[] x, size_t c)

jhash() -- hash a variable-length key into a 32-bit value

join
T[] join(const(T[])[] src, const(T)[] postfix, T[] dst)

Combine a series of text segments together, each appended with a postfix pattern. An optional output buffer can be provided to avoid heap activity - it should be large enough to contain the entire output, otherwise the heap will be used instead.

layout
T[] layout(T[] output, const(T[])[] layout)

Arranges text strings in order, using indices to specify where each particular argument should be positioned within the text. This is handy for collating I18N components, or as a simplistic and lightweight formatter. Indices range from zero through nine.

lineOf
T[] lineOf(T[] src, size_t index)

Return the indexed line, where each line is identified by a \n or \r\n combination. The line terminator is stripped from the resultant line

lines
LineFruct!(T) lines(T[] src)

Iterator to isolate lines.

locate
size_t locate(const(T)[] source, const(T) match, size_t start)

Return the index of the next instance of 'match' starting at position 'start', or source.length where there is no match.

locatePattern
size_t locatePattern(const(T)[] source, const(T)[] match, size_t start)

Return the index of the next instance of 'match' starting at position 'start', or source.length where there is no match.

locatePatternPrior
size_t locatePatternPrior(const(T)[] source, const(T)[] match, size_t start)

Return the index of the prior instance of 'match' starting just before 'start', or source.length where there is no match.

locatePrior
size_t locatePrior(const(T)[] source, const(T) match, size_t start)

Return the index of the prior instance of 'match' starting just before 'start', or source.length where there is no match.

main
void main()
Undocumented in source. Be warned that the author may not have intended to support it.
matching
bool matching(const(T)* s1, const(T)* s2, size_t length)

Return whether or not the two arrays have matching content

mismatch
size_t mismatch(const(T)* s1, const(T)* s2, size_t length)

Returns the index of a mismatch between s1 & s2, failing when length is reached. Note that we return 'length' upon failure (array content matches) and a 0-based index upon success.

patterns
PatternFruct!(T) patterns(const(T)[] src, const(T)[] pattern, const(T)[] sub)

Iterator to isolate text elements.

postfix
T[] postfix(T[] dst, const(T)[] postfix, const(T[])[] src)

Combine a series of text segments together, each appended with an optional postfix pattern. An optional output buffer can be provided to avoid heap activity - it should be large enough to contain the entire output, otherwise the heap will be used instead.

prefix
T[] prefix(T[] dst, const(T)[] prefix, const(T[])[] src)

Combine a series of text segments together, each prepended with a prefix pattern. An optional output buffer can be provided to avoid heap activity - it should be large enough to contain the entire output, otherwise the heap will be used instead.

quotes
QuoteFruct!(T, M) quotes(T[] src, const(M)[] set)

Iterator to isolate optionally quoted text elements.

repeat
T[] repeat(const(T)[] src, size_t count, T[] dst)

Repeat an array for a specific number of times. An optional output buffer can be provided to avoid heap activity - it should be large enough to contain the entire output, otherwise the heap will be used instead.

replace
T[] replace(T[] source, S match, S replacement)

Replace all instances of one element with another (in place)

rindex
size_t rindex(const(T)[] source, const(T)[] match, size_t start)

Return the index of the prior instance of 'match' starting just before 'start', or source.length where there is no match.

split
inout(T)[][] split(inout(T)[] src, const(T)[] pattern)

Split the provided array wherever a pattern instance is found, and return the resultant segments. The pattern is excluded from each of the segments.

strip
T[] strip(T[] source, S match)

Trim the given array by stripping the provided match from both ends. Returns a slice of the original content

stripl
T[] stripl(T[] source, S match)

Trim the given array by stripping the provided match from the left hand side. Returns a slice of the original content

stripr
T[] stripr(T[] source, S match)

Trim the given array by stripping the provided match from the right hand side. Returns a slice of the original content

substitute
T[] substitute(const(T)[] source, const(T)[] match, const(T)[] replacement)

Substitute all instances of match from source. Set replacement to null in order to remove instead of replace

tail
T[] tail(T[] src, S[] pattern, T[] head)

Split the provided array on the last pattern instance, and return the resultant head and tail. The pattern is excluded from the two segments.

toLines
T[][] toLines(T[] src)
Undocumented in source. Be warned that the author may not have intended to support it.
trim
T[] trim(T[] source)

Trim the provided array by stripping whitespace from both ends. Returns a slice of the original content

triml
T[] triml(T[] source)

Trim the provided array by stripping whitespace from the left. Returns a slice of the original content

trimr
T[] trimr(T[] source)

Trim the provided array by stripping whitespace from the right. Returns a slice of the original content

unescape
inout(T)[] unescape(inout(T)[] src, T[] dst)

Convert 'escaped' chars to normal ones: \t => ^t for example. Supports \" \' \\ \a \b \f \n \r \t \v

Variables

x
auto x;
Undocumented in source.

Meta

License

BSD style: $(LICENSE)

Version

Apr 2004: Initial release Dec 2006: South Seas version

Authors

Kris

Placeholder for a variety of wee functions. These functions are all templated with the intent of being used for arrays of char, wchar, and dchar. However, they operate correctly with other array types also.

Several of these functions return an index value, representing where some criteria was identified. When said criteria is not matched, the functions return a value representing the array length provided to them. That is, for those scenarios where C functions might typically return -1 these functions return length instead. This operate nicely with D slices:

auto text = "happy:faces";

assert (text[0 .. locate (text, ':')] == "happy");

assert (text[0 .. locate (text, '!')] == "happy:faces");

The contains() function is more convenient for trivial lookup cases:

if (contains ("fubar", '!'))
    ...

Note that where some functions expect a size_t as an argument, the D template-matching algorithm will fail where an int is provided instead. This is the typically the cause of "template not found" errors. Also note that name overloading is not supported cleanly by IFTI at this time, so is not applied here.

Applying the D "import alias" mechanism to this module is highly recommended, in order to limit namespace pollution:

import Util = tango.text.Util;

auto s = Util.trim ("  foo ");

Function templates:

trim (source)                               // trim whitespace
triml (source)                              // trim whitespace
trimr (source)                              // trim whitespace
strip (source, match)                       // trim elements
stripl (source, match)                      // trim elements
stripr (source, match)                      // trim elements
chopl (source, match)                       // trim pattern match
chopr (source, match)                       // trim pattern match
delimit (src, set)                          // split on delims
split (source, pattern)                     // split on pattern
splitLines (source);                        // split on lines
head (source, pattern, tail)                // split to head & tail
join (source, postfix, output)              // join text segments
prefix (dst, prefix, content...)            // prefix text segments
postfix (dst, postfix, content...)          // postfix text segments
combine (dst, prefix, postfix, content...)  // combine lotsa stuff
repeat (source, count, output)              // repeat source 
replace (source, match, replacement)        // replace chars
substitute (source, match, replacement)     // replace/remove matches
count (source, match)                       // count instances
contains (source, match)                    // has char?
containsPattern (source, match)             // has pattern?
index (source, match, start)                // find match index
locate (source, match, start)               // find char
locatePrior (source, match, start)          // find prior char
locatePattern (source, match, start);       // find pattern
locatePatternPrior (source, match, start);  // find prior pattern
indexOf (s*, match, length)                 // low-level lookup
mismatch (s1*, s2*, length)                 // low-level compare
matching (s1*, s2*, length)                 // low-level compare
isSpace (match)                             // is whitespace?
unescape(source, output)                    // convert '\' prefixes
layout (destination, format ...)            // featherweight printf
lines (str)                                 // foreach lines
quotes (str, set)                           // foreach quotes
delimiters (str, set)                       // foreach delimiters
patterns (str, pattern)                     // foreach patterns

Please note that any 'pattern' referred to within this module refers to a pattern of characters, and not some kind of regex descriptor. Use the Regex module for regex operation.