www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Feature !request into std.algorithm

into std.algorithm they are countUntil that is a useful function buit
when you want the first index of many token is same sequence you need to
all many times countUntil and then loop many time in the same sequence.
This is a inefficiency way. i have wrote a function where loop at max
one time over the sequence.=20
for go to algorithm a template replace char[] by Range i think

---------------------------
 safe nothrow pure
sizediff_t[char[]] searchIndex( in char[] sequence, in char[][]
token...)
in{
  assert(sequence !is null, "Error given sequence is null");
}
body{
  bool   isComputing =3D true;
  size_t index       =3D 0;
  size_t flag        =3D 0;
  sizediff_t[char[]] result;

  foreach( tok; token)
    result[tok] =3D -1;

  while(isComputing){
    if( index >=3D sequence.length )
      isComputing =3D false;
    else if( flag =3D=3D token.length )
      isComputing =3D false;
    else{
      foreach( tok; token){
        if( sequence.length - index >=3D tok.length ){
          const(char)[] currentToken =3D (tok.length > 1) ?
sequence[index .. index + tok.length] : [sequence[index]];
          if( currentToken in result && result[currentToken] =3D=3D -1 ){
            result[currentToken] =3D index;
            flag++;
          }
        }
      }
      index++;
    }
  }
  return result;
}
May 03 2012