splitter for strings

Chris via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 9 12:47:28 PDT 2014


On Monday, 9 June 2014 at 15:52:24 UTC, monarch_dodra wrote:
> On Monday, 9 June 2014 at 15:19:05 UTC, Chris wrote:
>> On Monday, 9 June 2014 at 14:47:45 UTC, Steven Schveighoffer 
>> wrote:
>>> On Mon, 09 Jun 2014 10:39:39 -0400, Chris <wendlec at tcd.ie> 
>>> wrote:
>>>
>>>> Atm, I have
>>>>
>>>> auto parts = appender!(string[]);
>>>> w.splitter('-').filter!(a => !a.empty).copy(parts);
>>>>
>>>> Which looks more elegant and gives me what I want. IMO, the 
>>>> module that handles the splitting of hyphenated words should 
>>>> be able to deal with cases like "blah-" without the input 
>>>> being prepared in a certain way.
>>>
>>> It's not mishandled. It's handled exactly as I would have 
>>> expected. If "blah-" and "blah" result in the same thing, 
>>> then how do you know the difference?
>>>
>>> Stripping any possible leading or trailing hyphens is much 
>>> more efficient than checking every single word to see if it's 
>>> empty.
>>>
>>> However, if you have an instance of "--", your solution will 
>>> remove the extra empty string, whereas mine does not. Not 
>>> sure if that's important.
>>>
>>> -Steve
>>
>> It is important. "blah--" should come out as "blah". The logic 
>> is along the following lines:
>>
>> if (canFind(w, "-")) {
>>  auto parts = appender!(string[]);
>>  w.splitter('-').filter!(a => !a.empty).copy(parts);
>>  if (parts.data.length == 1) {
>>    // false alarm. Trailing hyphen
>>  }
>> }
>>
>> The more common case is that it's not a trailing hyphen. 
>> std.string.strip() only works for whitespaces. Would be nice 
>> to have something like that for random characters. strip(s, 
>> '-') or strip(s, ['-', '+', '@'])
>
> http://dlang.org/phobos/std_algorithm.html#strip
>
> w = w.strip('-');
> if (canFind(w, "-")) {
>    ...

Uh, I see, I misread the signature of std.string.strip(). So 
that's one option now, to strip all trailing hyphens with 
std.string.strip(). Well, I'll give it a shot tomorrow.


More information about the Digitalmars-d-learn mailing list