[phobos] substring for std.metastrings
Igor Lesik
curoles at yahoo.com
Fri Feb 26 02:45:00 PST 2010
I believe, I have a function that is good candidate to be in std.metastrings.
Feedback is appreciated.
/**
* Returns:
* .found = true if string t found in s,
* .found = false if string t not found in s
*/
template substring(string s, string t)
{
static if (s == null || t == null || t.length == 0 || s.length == 0) {
const match = false;
const found = false;
}
else static if (s[0] == t[0])
{
const match = true;
static if (t.length == 1)
const found = true;
else static if (s.length == 1)
const found = false;
else static if (substring!(s[1..$],t[1..$]).match)
const found = substring!(s[1..$],t[1..$]).found;
else
const found = substring!(s[1..$],t[0..$]).found;
}
else
{
const match = false;
const found = substring!(s[1..$],t[0..$]).found;
}
}
unittest
{
assert(!substring!(null,"fun").found);
assert(!substring!("","fun").found);
assert(!substring!("fun",null).found);
assert(!substring!("fun","").found);
assert(!substring!("","").found);
assert(substring!("fun with gun","fun").found);
assert(substring!("fun with gun","gun").found);
assert(!substring!("fun with gun","peace").found);
}
More information about the phobos
mailing list