@@ -185,11 +185,17 @@ STRINGLIB(rfind_char)(const STRINGLIB_CHAR* s, Py_ssize_t n, STRINGLIB_CHAR ch)
185
185
# define LOG_LEVEL 0
186
186
#if LOG_LEVEL == 1 && STRINGLIB_SIZEOF_CHAR == 1
187
187
# define LOG (...) printf(__VA_ARGS__)
188
- # define LOG_STRING (s , n )
188
+ # define LOG2 (...)
189
+ # define LOG_STRING (s , n ) if (n < 100) { \
190
+ printf("\"%.*s\"", (int)(n), s); \
191
+ }
189
192
# define LOG_LINEUP ()
190
193
#elif LOG_LEVEL == 2 && STRINGLIB_SIZEOF_CHAR == 1
191
194
# define LOG (...) printf(__VA_ARGS__)
192
- # define LOG_STRING (s , n ) printf("\"%.*s\"", (int)(n), s)
195
+ # define LOG2 (...) printf(__VA_ARGS__)
196
+ # define LOG_STRING (s , n ) if (n < 100) { \
197
+ printf("\"%.*s\"", (int)(n), s); \
198
+ }
193
199
# define LOG_LINEUP () do { \
194
200
if (n < 100) { \
195
201
LOG("> "); LOG_STRING(s, n); \
@@ -199,6 +205,7 @@ STRINGLIB(rfind_char)(const STRINGLIB_CHAR* s, Py_ssize_t n, STRINGLIB_CHAR ch)
199
205
} while(0)
200
206
#else
201
207
# define LOG (...)
208
+ # define LOG2 (...)
202
209
# define LOG_STRING (s , n )
203
210
# define LOG_LINEUP ()
204
211
#endif
@@ -434,12 +441,13 @@ STRINGLIB(_two_way)(const STRINGLIB_CHAR *s, Py_ssize_t n,
434
441
// Crochemore and Perrin's (1991) Two-Way algorithm.
435
442
// See http://www-igm.univ-mlv.fr/~lecroq/string/node26.html#SECTION00260
436
443
if (mode == FAST_COUNT ) {
437
- LOG ("Two-way Counting \"%s\" in \"%s\" .\n" , pw -> p , s );
444
+ LOG ("Two-way Count .\n" );
438
445
}
439
446
else {
440
- LOG ("Two-way Finding \"%s\" in \"%s\" .\n" , pw -> p , s );
447
+ LOG ("Two-way Find .\n" );
441
448
}
442
-
449
+ LOG ("haystack: " ); LOG_STRING (s , n ); LOG ("\n" );
450
+ LOG ("needle : " ); LOG_STRING (pw -> p , pw -> m ); LOG ("\n" );
443
451
int dir = direction < 0 ? -1 : 1 ;
444
452
int reversed = dir < 0 ;
445
453
@@ -480,10 +488,10 @@ STRINGLIB(_two_way)(const STRINGLIB_CHAR *s, Py_ssize_t n,
480
488
for (i = 0 ; i <= w ;) {
481
489
iloop ++ ;
482
490
ip = reversed ? - i : i ;
483
- LOG ("Last window ch: %c\n" , ss [ip ]);
491
+ LOG2 ("Last window ch: %c\n" , ss [ip ]);
484
492
LOG_LINEUP ();
485
493
shift = table [ss [ip ] & TABLE_MASK ];
486
- if (shift != 0 ){
494
+ if (shift ){
487
495
if (do_mem_jump ) {
488
496
// A mismatch has been identified to the right
489
497
// of where i will next start, so we can jump
@@ -505,15 +513,15 @@ STRINGLIB(_two_way)(const STRINGLIB_CHAR *s, Py_ssize_t n,
505
513
for (; j < m ; j ++ ) {
506
514
ihits ++ ;
507
515
jp = p_stt + (reversed ? - j : j );
508
- LOG ("Checking: %c vs %c\n" , ss [j_off + jp ], p [jp ]);
509
- if (ss [j_off + j ] != p [j ]) {
516
+ LOG2 ("Checking j=%ld : %c vs %c\n" , j , ss [j_off + jp ], p [jp ]);
517
+ if (ss [j_off + jp ] != p [jp ]) {
510
518
if (j < gap_jump_end ) {
511
- LOG ("Early right half mismatch: jump by gap.\n" );
519
+ LOG ("Early later half mismatch: jump by gap.\n" );
512
520
assert (gap >= j - cut + 1 );
513
521
i += gap ;
514
522
}
515
523
else {
516
- LOG ("Late right half mismatch: jump by n (>gap)\n" );
524
+ LOG ("Late later half mismatch: jump by n (>gap)\n" );
517
525
assert (j - cut + 1 > gap );
518
526
i += j - cut + 1 ;
519
527
}
@@ -524,12 +532,13 @@ STRINGLIB(_two_way)(const STRINGLIB_CHAR *s, Py_ssize_t n,
524
532
if (j != m ) {
525
533
continue ;
526
534
}
527
- for (j = memory ; j < cut ; j ++ ) {
535
+ j = Py_MIN (memory , cut );
536
+ for (; j < cut ; j ++ ) {
528
537
ihits ++ ;
529
538
jp = p_stt + (reversed ? - j : j );
530
- LOG ("Checking: %c vs %c\n" , ss [j_off + jp ], p [jp ]);
531
- if (ss [j_off + j ] != p [j ]) {
532
- LOG ("Left half does not match.\n" );
539
+ LOG2 ("Checking j=%ld : %c vs %c\n" , j , ss [j_off + jp ], p [jp ]);
540
+ if (ss [j_off + jp ] != p [jp ]) {
541
+ LOG ("First half does not match.\n" );
533
542
if (is_periodic ) {
534
543
memory = m - period ;
535
544
do_mem_jump = 1 ;
@@ -546,6 +555,7 @@ STRINGLIB(_two_way)(const STRINGLIB_CHAR *s, Py_ssize_t n,
546
555
if (++ count == maxcount ) {
547
556
return maxcount ;
548
557
}
558
+ memory = 0 ;
549
559
i += m ;
550
560
}
551
561
@@ -583,11 +593,13 @@ STRINGLIB(horspool_find)(const STRINGLIB_CHAR* s, Py_ssize_t n,
583
593
/* Boyer–Moore–Horspool algorithm
584
594
with optional dynamic fallback to Two-Way algorithm */
585
595
if (mode == FAST_COUNT ) {
586
- LOG ("Horspool Counting \"%s\" in \"%s\" .\n" , p , s );
596
+ LOG ("Horspool Count .\n" );
587
597
}
588
598
else {
589
- LOG ("Horspool Finding \"%s\" in \"%s\".\n" , p , s );
599
+ LOG ("Horspool Find\n" );
590
600
}
601
+ LOG ("haystack: " ); LOG_STRING (s , n ); LOG ("\n" );
602
+ LOG ("needle : " ); LOG_STRING (p , m ); LOG ("\n" );
591
603
int dir = direction < 0 ? -1 : 1 ;
592
604
int reversed = dir < 0 ;
593
605
@@ -657,7 +669,7 @@ STRINGLIB(horspool_find)(const STRINGLIB_CHAR* s, Py_ssize_t n,
657
669
iloop ++ ;
658
670
ip = reversed ? - i : i ;
659
671
s_last = ss [ip ];
660
- LOG ("Last window ch: %c\n" , s_last );
672
+ LOG2 ("Last window ch: %c\n" , s_last );
661
673
if (true_gap ) {
662
674
shift = 0 ;
663
675
if (s_last != p_last ) {
@@ -672,7 +684,7 @@ STRINGLIB(horspool_find)(const STRINGLIB_CHAR* s, Py_ssize_t n,
672
684
else {
673
685
shift = table [s_last & TABLE_MASK ];
674
686
}
675
- if (shift != 0 ) {
687
+ if (shift ) {
676
688
LOG ("Shift: %ld\n" , shift );
677
689
i += shift ;
678
690
continue ;
@@ -683,7 +695,7 @@ STRINGLIB(horspool_find)(const STRINGLIB_CHAR* s, Py_ssize_t n,
683
695
for (j = 0 ; j < j_stop ; j ++ ) {
684
696
ihits ++ ;
685
697
jp = p_stt + (reversed ? - j : j );
686
- LOG ("Checking: %c vs %c\n" , ss [j_off + jp ], p [jp ]);
698
+ LOG2 ("Checking j=%ld : %c vs %c\n" , j , ss [j_off + jp ], p [jp ]);
687
699
if (ss [j_off + jp ] != p [jp ]) {
688
700
break ;
689
701
}
@@ -990,14 +1002,7 @@ FASTSEARCH(const STRINGLIB_CHAR* s, Py_ssize_t n,
990
1002
return res == 0 ? 0 : -1 ;
991
1003
}
992
1004
}
993
- if (mode != FAST_RSEARCH ) {
994
- // return STRINGLIB(horspool_find_old)(s, n, p, m, maxcount, mode, 0);
995
- // return STRINGLIB(horspool_find)(s, n, p, m, maxcount, mode, 1, 1);
996
- return STRINGLIB (two_way_find )(s , n , p , m , maxcount , mode , 1 );
997
- }
998
- else {
999
- /* FAST_RSEARCH */
1000
- // return STRINGLIB(horspool_find)(s, n, p, m, maxcount, mode, -1, 1);
1001
- return STRINGLIB (two_way_find )(s , n , p , m , maxcount , mode , -1 );
1002
- }
1005
+ int dyn = 1 ;
1006
+ int dir = mode != FAST_RSEARCH ? 1 : -1 ;
1007
+ return STRINGLIB (horspool_find )(s , n , p , m , maxcount , mode , dir , dyn );
1003
1008
}
0 commit comments