[Issue 7146] New: enhance strip* (implementation provided)
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Wed Dec 21 00:50:20 PST 2011
http://d.puremagic.com/issues/show_bug.cgi?id=7146
Summary: enhance strip* (implementation provided)
Product: D
Version: D2
Platform: Other
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P2
Component: Phobos
AssignedTo: nobody at puremagic.com
ReportedBy: pompei2 at gmail.com
--- Comment #0 from pompei2 at gmail.com 2011-12-21 00:50:17 PST ---
In some languages (e.g. python), the string "strip" function family accepts an
optional parameter which specifies the set of characters one wants to strip. If
not given, this is "whitespace". I think this is very useful and the D strip
should support that too.
For example, I needed it when getting a char buffer filled by a C function and
converting it to a string. Here is the implementation of these including
unittests that you can just drop into the string.d file if you remove the
import line. I would very much like to see this included in phobos - no
copyright/whatever needed.
// Remove when integrating into std.string.
import std.range, std.string, std.traits, std.utf;
/++
Strips leading characters.
+/
S stripLeft(S)(S s, S charsToStrip) pure
if(isSomeString!S)
{
bool foundIt;
size_t notInSet;
foreach(i, dchar c; s)
{
// Because of the call to indexOf, this function can't be @safe
anymore.
if(indexOf(charsToStrip, c) == -1)
{
foundIt = true;
notInSet = i;
break;
}
}
if(foundIt)
return s[notInSet .. $];
return s[0 .. 0]; //Empty string with correct type.
}
unittest {
debug(string) printf("string.stripLeft2.unittest\n");
string hi = "Hello, world!";
assert(hi.stripLeft("") == hi);
assert(hi.stripLeft("H") == "ello, world!");
assert(hi.stripLeft("Hel") == "o, world!");
assert(hi.stripLeft("def") == hi);
assert(hi.stripLeft(hi) == "");
}
/++
Strips trailing characters.
+/
S stripRight(S)(S s, S charsToStrip)
if(isSomeString!S)
{
alias typeof(s[0]) C;
size_t codeLen;
foreach(dchar c; retro(s))
{
// Because of the call to indexOf, this function can't be @safe
anymore.
if(indexOf(charsToStrip, c) >= 0)
codeLen += codeLength!C(c);
else
break;
}
return s[0 .. $ - codeLen];
}
unittest {
debug(string) printf("string.stripRight2.unittest\n");
string hi = "Hello, worldd!";
assert(hi.stripRight("") == hi);
assert(hi.stripRight("!") == "Hello, worldd");
assert(hi.stripRight("!.d") == "Hello, worl");
assert(hi.stripRight("hex") == hi);
assert(hi.stripRight(hi) == "");
}
/++
Strips both leading and trailing characters.
+/
S strip(S)(S s, S charsToStrip)
if(isSomeString!S)
{
return stripRight(stripLeft(s, charsToStrip), charsToStrip);
}
unittest {
debug(string) printf("string.strip2.unittest\n");
string hi = " ohfoo";
assert(hi.strip("") == hi);
assert(hi.strip(" ") == "ohfoo");
assert(hi.strip(" o") == "hf");
assert(hi.strip("ext") == hi);
assert(hi.strip(" ohf") == "");
}
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list