digitalmars.D - std.string.strip(char[] str. char delimiters[]) - possible addition?
- Alan Knowles (51/51) Mar 23 2006 I needed to strip quote's from a string, and thought this may be a
- Derek Parnell (93/146) Mar 23 2006 Couldn't resist "D-ifying" these functions ... hope you don't mind.
I needed to strip quote's from a string, and thought this may be a useful addition to std.string (kind of follow's PHP trim() API http://www.php.net/trim) Regards Alan char[] stripl(char[] s, char[] c) { uint i; for (i = 0; i < s.length; i++) { bool found = false; for(uint ii =0;ii < c.length; ii++) { if (c[ii] == s[i]) { found = true; break; } } if (!found) break; } return s[i .. s.length]; } char[] stripr(char[] s, char[] c) /// ditto { uint i; for (i = s.length ; i > 0; i--) { bool found = false; for(uint ii =0;ii < c.length; ii++) { if (c[ii] == s[i-1]) { found = true; break; } } if (!found) break; } return s[0 .. i]; } char[] strip(char[] s, char[] c) /// ditto { return stripr(stripl(s,c),c); }
Mar 23 2006
On Fri, 24 Mar 2006 11:57:57 +0800, Alan Knowles wrote:I needed to strip quote's from a string, and thought this may be a useful addition to std.string (kind of follow's PHP trim() API http://www.php.net/trim) Regards Alan char[] stripl(char[] s, char[] c) { uint i; for (i = 0; i < s.length; i++) { bool found = false; for(uint ii =0;ii < c.length; ii++) { if (c[ii] == s[i]) { found = true; break; } } if (!found) break; } return s[i .. s.length]; } char[] stripr(char[] s, char[] c) /// ditto { uint i; for (i = s.length ; i > 0; i--) { bool found = false; for(uint ii =0;ii < c.length; ii++) { if (c[ii] == s[i-1]) { found = true; break; } } if (!found) break; } return s[0 .. i]; } char[] strip(char[] s, char[] c) /// ditto { return stripr(stripl(s,c),c); }Couldn't resist "D-ifying" these functions ... hope you don't mind. char[] stripl(char[] s, char[] c) out (result) { assert(result.length <= s.length); } body { if (s.length == 0 || c.length == 0) return s; foreach(int i, char target; s) { foreach(char delim; c) { if (delim == target) return s[i+1..$]; } } return s; } char[] stripr(char[] s, char[] c) out (result) { assert(result.length <= s.length); } body { if (s.length == 0 || c.length == 0) return s; for(int i = s.length-1; i >= 0; i--) { foreach(char delim; c) { if (delim == s[i]) return s[0..i]; } } return s; } char[] strip(char[] s, char[] c) out (result) { assert(result.length <= s.length); } body { if (s.length == 0 || c.length == 0) return s; foreach(int i, char target; s) { foreach(int j, char delim; c) { if (delim == target) // strip off matching delimiter from right. return stripr(s[i+1..$], c[j..j+1]); } } return s; } unittest { assert(stripl(" 'abc' ", "'") == "abc' "); assert(stripr(" 'abc' ", "'") == " 'abc"); assert(strip (" 'abc' ", "'") == "abc"); assert(stripl(" 'abc' ", "'`") == "abc' "); assert(stripr(" 'abc' ", "'`") == " 'abc"); assert(strip (" 'abc' ", "'`") == "abc"); assert(stripl(" `'abc'` ", "'`") == "'abc'` "); assert(stripr(" `'abc'` ", "'`") == " `'abc'"); assert(strip (" `'abc'` ", "'`") == "'abc'"); assert(stripl(" '`abc`' ", "'`") == "`abc`' "); assert(stripr(" '`abc`' ", "'`") == " '`abc`"); assert(strip (" '`abc`' ", "'`") == "`abc`"); assert(stripl(" '`abc`' ", ",") == " '`abc`' "); assert(stripr(" '`abc`' ", ",") == " '`abc`' "); assert(strip (" '`abc`' ", ",") == " '`abc`' "); assert(stripl(" '`abc`' ", "") == " '`abc`' "); assert(stripr(" '`abc`' ", "") == " '`abc`' "); assert(strip (" '`abc`' ", "") == " '`abc`' "); assert(stripl("", ",") == ""); assert(stripr("", ",") == ""); assert(strip ("", ",") == ""); assert(stripl(" '`abc'` ", "'`") == "`abc'` "); assert(stripr(" '`abc'` ", "'`") == " '`abc'"); assert(strip (" '`abc'` ", "'`") == "`abc"); } -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocracy!" 24/03/2006 3:56:41 PM
Mar 23 2006