@@ -254,59 +254,64 @@ String Stream::readStringUntil(char terminator)
254
254
int Stream::findMulti ( struct Stream ::MultiTarget *targets, int tCount) {
255
255
// any zero length target string automatically matches and would make
256
256
// 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) {
258
258
if (t->len <= 0 )
259
259
return t - targets;
260
+ }
260
261
261
- while (1 ) {
262
+ while (1 ) {
262
263
int c = timedRead ();
263
264
if (c < 0 )
264
265
return -1 ;
265
266
266
267
for (struct MultiTarget *t = targets; t < targets+tCount; ++t) {
267
268
// 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
+ }
273
275
274
276
// if not we need to walk back and see if we could have matched further
275
277
// down the stream (ie '1112' doesn't match the first position in '11112'
276
278
// but it will match the second position so we can't just reset the current
277
279
// index to 0 when we find a mismatch.
278
280
if (t->index == 0 )
279
- continue ;
281
+ continue ;
280
282
281
283
int origIndex = t->index ;
282
284
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
307
312
} while (t->index );
308
313
}
309
314
}
310
315
// unreachable
311
316
return -1 ;
312
- }
317
+ }
0 commit comments