Bug 9699 – strip functions should have stripLeft/stripRight counterparts and be generic

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-03-12T05:40:00Z
Last change time
2013-09-27T07:40:24Z
Keywords
pull
Assigned to
andrej.mitrovich
Creator
andrej.mitrovich

Comments

Comment #0 by andrej.mitrovich — 2013-03-12T05:40:16Z
This applies to strip, stripLeft, and stripRight.
Comment #1 by andrej.mitrovich — 2013-03-12T05:47:31Z
Comment #2 by bearophile_hugs — 2013-03-12T06:16:47Z
Regarding this code I have two comments: C[] stripRight(C)(C[] str) if(isSomeChar!C) { foreach_reverse(i, dchar c; str) { if(!std.uni.isWhite(c)) return str[0 .. i + codeLength!C(c)]; } return str[0 .. 0]; } 1) Isn't it enough a "return null;" at the end? 2) When I see such string functions I often think about stripping leading values from a generic 1D array: [0,0,0,0,10,7,1234,0]
Comment #3 by andrej.mitrovich — 2013-03-12T06:28:25Z
(In reply to comment #2) > Regarding this code I have two comments: > > C[] stripRight(C)(C[] str) > if(isSomeChar!C) > { > foreach_reverse(i, dchar c; str) > { > if(!std.uni.isWhite(c)) > return str[0 .. i + codeLength!C(c)]; > } > > return str[0 .. 0]; > } > > > 1) Isn't it enough a "return null;" at the end? It's enough but it's different. At the call site !is null checks will depend on what is returned here. > 2) When I see such string functions I often think about stripping leading > values from a generic 1D array: > [0,0,0,0,10,7,1234,0] I don't know what is being asked here.
Comment #4 by monarchdodra — 2013-03-12T07:04:38Z
(In reply to comment #3) > (In reply to comment #2) > > Regarding this code I have two comments: > > > > C[] stripRight(C)(C[] str) > > if(isSomeChar!C) > > { > > foreach_reverse(i, dchar c; str) > > { > > if(!std.uni.isWhite(c)) > > return str[0 .. i + codeLength!C(c)]; > > } > > > > return str[0 .. 0]; > > } > > > > > > 1) Isn't it enough a "return null;" at the end? > > It's enough but it's different. At the call site !is null checks will depend on > what is returned here. Further more, "strip" returns a "sub slice" of the original slice. If you return null, then "sameHead(str, stripRight(str))" will not be guaranteed. Very very minor points, but I see no reason to do things wrong when you can do them right. besides, str[0 .. 0] doesn't trigger bounds checking, so there is no overhead compared to null anyways. > > 2) When I see such string functions I often think about stripping leading > > values from a generic 1D array: > > [0,0,0,0,10,7,1234,0] > > I don't know what is being asked here. He's basically saying there's no real reason for strip to be limited to just strings, and could operate on arrays or ranges. I'd kind of agree, but given our current clusterfuck with splitter, I think we need to think long and hard before adding anything.
Comment #5 by andrej.mitrovich — 2013-03-12T14:04:55Z
I'll work on this some other time unless someone else implements it before me. Andrei now wants this to be completely generic and put into std.algorithm.
Comment #6 by bearophile_hugs — 2013-03-12T14:12:52Z
(In reply to comment #5) > I'll work on this some other time unless someone else implements it before me. > > Andrei now wants this to be completely generic and put into std.algorithm. In my code 99% of times I want to strip strings. Only once in a lot of time I'd like to strip a generic 1D array.
Comment #7 by andrej.mitrovich — 2013-05-27T04:33:56Z
Comment #8 by github-bugzilla — 2013-09-27T06:48:45Z
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/904682ac78e503fef8c57475634a8026bb6d2f20 Fixes Issue 9699 - Implement generic strip/stripLeft/stripRight functions which accept an element or a predicate. https://github.com/D-Programming-Language/phobos/commit/8503ed1e2329cd1b14a0e470dbdb12206a0e49a8 Merge pull request #1311 from AndrejMitrovic/Fix9699_2 Issue 9699 - Implement generic strip/stripLeft/stripRight functions