WojciechMula/sse4-strstr

Consider adding the following naive algorithm

lemire opened this issue · 1 comments

The problem with comparing with, say, the standard library, is that it might use non-trivial implementations. It would be useful to have a really basic reference like this:

int naive(char * hay, int size, char *needle, int needlesize) {
  const char first = needle[0];
  const int maxpos = size - needlesize;
  for(int i = 0; i < maxpos; i++) {
    if(hay[i] != first) {
       i++;
       while( i < maxpos && hay[i] != first ) i++;
       if ( i == maxpos ) break;
    }
    int j = 1;
    for( ; j < needlesize; ++j)
      if(hay[ i + j ] != needle[ j ] ) break;
    if( j == needlesize) return i;
  }
  return size;
}

Done, thanks!