Understanding Stream::findMulti()

in the findMulti method at the bottom exist the following code

        // otherwise we need to check the rest of the found string
        int diff = origIndex - t->index;
        size_t i;
        for (i = 0; i < t->index; ++i) {
          if (t->str[i] != t->str[i + diff])
            break;
        }

        // if we successfully got through the previous loop then our current
        // index is good.
        if (i == t->index) {
          t->index++;
          break;
        }

Im trying understand what the pattern should be and input string so that this is code “triggering”

with all my pattern and input string , code do not go to this point
so I can delete it and findMulti will work correct

and second question: is there some case when findMulti will return value > 0 ?
in all my cases or 0 or -1;

with all my pattern and input string , code do not go to this point

Have you tried with the example in the code comment?

so I can delete it and findMulti will work correct

No, it will work correctly for many patterns but will fail if you have parts of the pattern repeated.

and second question: is there some case when findMulti will return value > 0 ?
in all my cases or 0 or -1;

In that case you either didn't provide multiple patterns or only the first pattern ever matched. The function returns the index of the pattern that matched.

" is there some case when findMulti will return value > 0 ?"
only, as I understand, if we make findUntil with terminator, so findMulti will return the difference between SRAM pointer address

The function returns the index of the pattern that matched.

the function return only -> return t - targets;
what the index are you talking about ?

Have you tried with the example in the code comment?

yes. In all my cases (tests) I have no this part of code( delete it) and rest code work correctly

can you provide some input string and pattern for test ? which not will work w/o code, mentioned above

I have deleted the mentioned above code and
I have tried different pattern and input string, with or without terminator, with or without pattern.
in all cases, I get correct result .

Does anyone know why need this code

alexblade:
Does anyone know why need this code

The comment in the code says:

     // if not we need to walk back and see if we could have matched further
      // down the stream (ie '1112' doesn't match the first position in '11112'
      // but it will match the second position so we can't just reset the current
      // index to 0 when we find a mismatch.
      if (t->index == 0)
        continue;

Did you use a target of "1112" and enter an input of "11112"? Similarly, a target of "aaaabbbb" should match an inout of "aaaaaaaaaaaaaabbbbbbbbbbb". Do both of those match correctly when you delete the code?

johnwasser, thank you! you gave me a clue

I tried “1111112” and “112” and was ok BUT! I dont know right now why [I will figure out it shortly] BUT there are some formula, like:

if we find (3)O+K -OOOK
then w/o deleted code mentioned above , pattern will be found if “O” will be >= x2 in the input string -OOOOOOK

BUT if repeated ‘O’ < then O*2 in the input string then need mentioned above code :slight_smile:

the function return only -> return t - targets;
what the index are you talking about ?

That's called pointer arithmetic, you can calculate on pointers too. t holds the current string to look for and targets is the base pointer of the array.

Have you tried to supply multiple strings to look for and let not the first match? If you don't do what you're told to do why do you ask for explanations here?