From: Nicholas Clark Date: Mon, 20 Mar 2006 18:54:32 +0000 (+0000) Subject: In Perl_sv_pos_b2u, if we know the character offset of the end, we X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b9f984a55a3d61f57de87f7bbf6faaafd7674b67;p=p5sagit%2Fp5-mst-13.2.git In Perl_sv_pos_b2u, if we know the character offset of the end, we can take advantage of this to count backwards if close. Remove some now unnecessary casts. p4raw-id: //depot/perl@27558 --- diff --git a/sv.c b/sv.c index 992439c..2641da0 100644 --- a/sv.c +++ b/sv.c @@ -5681,7 +5681,7 @@ Perl_sv_pos_b2u(pTHX_ register SV* sv, I32* offsetp) s = (const U8*)SvPV_const(sv, len); - if ((I32)len < byte) + if (len < byte) Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset"); send = s + byte; @@ -5690,16 +5690,23 @@ Perl_sv_pos_b2u(pTHX_ register SV* sv, I32* offsetp) mg = mg_find(sv, PERL_MAGIC_utf8); if (mg && mg->mg_ptr) { STRLEN *cache = (STRLEN *) mg->mg_ptr; - if (cache[1] == (STRLEN)byte) { + if (cache[1] == byte) { /* An exact match. */ *offsetp = cache[0]; return; } - else if (cache[1] < (STRLEN)byte) { + else if (cache[1] < byte) { /* We already know part of the way. */ - len = cache[0] - + S_sv_pos_b2u_forwards(aTHX_ s + cache[1] , send); + if (mg->mg_len != -1) { + /* Actually, we know the end too. */ + len = cache[0] + + S_sv_pos_b2u_midway(aTHX_ s + cache[1], send, + s + len, mg->mg_len - cache[0]); + } else { + len = cache[0] + + S_sv_pos_b2u_forwards(aTHX_ s + cache[1], send); + } } else { /* cache[1] > byte */ len = S_sv_pos_b2u_midway(aTHX_ s, send, s + cache[1],