FindFruct

Convenient bundle of lightweight find utilities, without the hassle of IFTI problems. Create one of these using the find() function:

auto match = find ("foo");
auto content = "wumpus foo bar"

// search in the forward direction
auto index = match.forward (content);
assert (index is 7);

// search again - returns length when no match found
assert (match.forward(content, index+1) is content.length);

Searching operates both forward and backward, with an optional start offset (can be more convenient than slicing the content). There are methods to replace matches within given content, and others which return foreach() iterators for traversing content.

SearchFruct is a more sophisticated variant, which operates more efficiently on longer matches and/or more extensive content.

Members

Functions

count
size_t count(cstring content)

Returns number of matches within the given content

forward
size_t forward(cstring content, size_t ofs)

Search forward in the given content, starting at the optional index.

indices
Indices indices(cstring content)

Returns a foreach() iterator which exposes the indices of all matches within the given content:

match
cstring match()

Return the match text

match
void match(cstring what)

Reset the text to match

replace
mstring replace(cstring content, cstring sub)

Replace all matches with the given substitution. Use method tokens() instead to avoid heap activity.

replace
mstring replace(cstring content, char chr)

Replace all matches with the given character. Use method tokens() instead to avoid heap activity.

reverse
size_t reverse(cstring content, size_t ofs)

Search backward in the given content, starting at the optional index.

tokens
Util.PatternFruct!(const(char)) tokens(cstring content, cstring sub)

Returns a foreach() iterator which exposes text segments between all matches within the given content. Substitution text is also injected in place of each match, and null can be used to indicate removal instead:

within
bool within(cstring content)

Returns true if there is a match within the given content

Meta