On Wednesday, 23 March 2016 at 11:57:49 UTC, ParticlePeter wrote:
I need to parse an ascii with multiple tokens. The tokens can be seen as keys. After every token there is a bunch of lines belonging to that token, the values.
The order of tokens is unknown.

I would like to read the file in as a whole string, and split the string with:
splitter(fileString, [token1, token2, ... tokenN]);

And would like to get a range of strings each starting with tokenX and ending before the next token.

Does something like this exist?

I know how to parse the string line by line and create new strings and append the appropriate lines, but I don't know how to do this with a lazy result range and new allocations.

This isn't tested, but this is my first thought:

void main(){
    string testString = "this:is:a-test;"
    foreach(str; testString.multiSlice([":","-",";"]))
       writefln("Got: %s", str);
}

auto multiSlice(string string, string[] delims){
   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 long findNextIndex(){
            long foundIndex=-1;
            foreach(delim; m_delims){
                if(m_str.canFind(delim)){
if(foundIndex == -1 || m_str.indexOf(delim) >= 0)){
                         foundIndex = m_str.indexOf(delim);
                    }
                }
            }
            return foundIndex;
        }
   }

   return MultiSliceRange(string, delims);
}


Again, totally untested, but I think logically it should work. ( No D compiler on this machine so it mightn't even compile :] )

Reply via email to