@@ -254,59 +254,64 @@ String Stream::readStringUntil(char terminator)
254254int Stream::findMulti ( struct Stream ::MultiTarget *targets, int tCount) {
255255 // any zero length target string automatically matches and would make
256256 // a mess of the rest of the algorithm.
257- for (struct MultiTarget *t = targets; t < targets+tCount; ++t)
257+ for (struct MultiTarget *t = targets; t < targets+tCount; ++t) {
258258 if (t->len <= 0 )
259259 return t - targets;
260+ }
260261
261- while (1 ) {
262+ while (1 ) {
262263 int c = timedRead ();
263264 if (c < 0 )
264265 return -1 ;
265266
266267 for (struct MultiTarget *t = targets; t < targets+tCount; ++t) {
267268 // the simple case is if we match, deal with that first.
268- if (c == t->str [t->index ])
269- if (++t->index == t->len )
270- return t - targets;
271- else
272- continue ;
269+ if (c == t->str [t->index ]) {
270+ if (++t->index == t->len )
271+ return t - targets;
272+ else
273+ continue ;
274+ }
273275
274276 // if not we need to walk back and see if we could have matched further
275277 // down the stream (ie '1112' doesn't match the first position in '11112'
276278 // but it will match the second position so we can't just reset the current
277279 // index to 0 when we find a mismatch.
278280 if (t->index == 0 )
279- continue ;
281+ continue ;
280282
281283 int origIndex = t->index ;
282284 do {
283- --t->index ;
284- // first check if current char works against the new current index
285- if (c != t->str [t->index ])
286- continue ;
287-
288- // if it's the only char then we're good, nothing more to check
289- if (t->index == 0 ) {
290- t->index ++;
291- break ;
292- }
293-
294- // otherwise we need to check the rest of the found string
295- int diff = origIndex - t->index ;
296- int i;
297- for (i = 0 ; i < t->index ; ++i)
298- if (t->str [i] != t->str [i + diff])
299- break ;
300- // if we successfully got through the previous loop then our current
301- // index is good.
302- if (i == t->index ) {
303- t->index ++;
304- break ;
305- }
306- // otherwise we just try the next index
285+ --t->index ;
286+ // first check if current char works against the new current index
287+ if (c != t->str [t->index ])
288+ continue ;
289+
290+ // if it's the only char then we're good, nothing more to check
291+ if (t->index == 0 ) {
292+ t->index ++;
293+ break ;
294+ }
295+
296+ // otherwise we need to check the rest of the found string
297+ int diff = origIndex - t->index ;
298+ size_t i;
299+ for (i = 0 ; i < t->index ; ++i) {
300+ if (t->str [i] != t->str [i + diff])
301+ break ;
302+ }
303+
304+ // if we successfully got through the previous loop then our current
305+ // index is good.
306+ if (i == t->index ) {
307+ t->index ++;
308+ break ;
309+ }
310+
311+ // otherwise we just try the next index
307312 } while (t->index );
308313 }
309314 }
310315 // unreachable
311316 return -1 ;
312- }
317+ }
0 commit comments