int indexNth( SV *haystack, SV*needle, int nth ) {
STRLEN lh, ln, i, j;
char *h = SvPV( haystack, lh );
char *n = SvPV( needle, ln );
for( i = 0; i < lh; ++i ) {
for( j = 0; j < ln; ++j ) {
if( h[ i + j ] != n[ j ] ) goto nomatch;
}
if( --nth == 0 ) return i;
nomatch:;
}
return -1;
}
####
C:\test>1039228.pl
Rate regex buk cpp c
regex 4.06/s -- -97% -99% -100%
buk 137/s 3277% -- -75% -85%
cpp 546/s 13345% 298% -- -40%
c 915/s 22446% 568% 68% --
####
sub c {
my $copy = $main::string;
my $ix = indexNth( $copy, '!', 50000 );
substr( $copy, 0, $ix ) =~ tr/!/\n/;
return \$copy;
}