Does something like std.algorithm.iteration:splitter with multiple seperators exist?

ParticlePeter via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Mar 27 00:45:00 PDT 2016


On Wednesday, 23 March 2016 at 20:00:55 UTC, wobbles wrote:
> Again, totally untested, but I think logically it should work. 
> ( No D compiler on this machine so it mightn't even compile :] )

Thanks Wobbles, I took your approach. There were some minor 
issues, here is a working version:

auto multiSlice(string data, string[] delims)  {

    import std.algorithm : canFind;
    import std.string : indexOf;

    struct MultiSliceRange  {
       string m_str;
       string[] m_delims;
       bool empty(){
          return m_str.length == 0;
       }

       void popFront(){
          auto idx = findNextIndex;
          m_str = m_str[idx..$];
          return;
       }

       string front(){
          auto idx = findNextIndex;
          return m_str[0..idx];
       }

       private size_t findNextIndex()  {
          auto index = size_t.max;
          foreach(delim; m_delims)  {
             if(m_str.canFind(delim))  {
                auto foundIndex = m_str.indexOf(delim);
                if(index > foundIndex && foundIndex > 0)  {
                   index = foundIndex;
                }
             }
          }
          return index;
       }
    }

    return MultiSliceRange(data, delims);
}


More information about the Digitalmars-d-learn mailing list