std.gregorian contribution

Tomek Sowiński just at ask.me
Mon May 17 10:03:21 PDT 2010


negerns wrote:

> Also, I have introduced a unjoin() function as a helper function. It
> splits a string into an array of lines using the specified array of
> characters as delimiters. I am not sure if there is already an 
existing
> function that does the same but I could not find it. For lack of a
> better word I opted for the opposite of the join() function in 
std.string.
> 
> string[] unjoin(string s, char[] ch)
> {
>      uint start = 0;
>      uint i = 0;
>      string[] result;
> 
>      for (i = 0; i < s.length; i++) {
>          if (indexOf(ch, s[i]) != -1) {
>              result ~= s[start..i];
>              start = i + 1;
>          }
>      }
>      if (start < i) {
>          result ~= s[start..$];
>      }
>      return result;
> }
> 
> unittest {
>      string s = "2010-05-31";
>      string[] r = unjoin(s, ['/', '-', '.', ',', '\\']);
>      assert(r[0] == "2010");
>      assert(r[1] == "05");
>      assert(r[2] == "31");
> }

Thanks, it's useful. There's std.string.split but it takes only one 
delimiter. It'd be nice to have it as an overload that takes any range 
of delims. Yet, a delim can be a string (an array) and there would be 
problems how to understand split(..., "://"). So I suggest calling it 
splitBy to disambiguate. Like it?


Tomek


More information about the Digitalmars-d mailing list