From: Alex Vandiver Date: Sat, 30 May 2009 16:38:28 +0000 (-0400) Subject: Faster utf8_length method -- fixes [RT#50250] X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8e91ec7fd4ffa8cda89687c8214203a3b3e8a0c8;p=p5sagit%2Fp5-mst-13.2.git Faster utf8_length method -- fixes [RT#50250] UTF8SKIP appears to be a rather slow call; use UTF8_IS_INVARIANT to skip it whenever possible. We also move the malformed utf8 check until after the loop, since it can be checked after the termination condition, instead of at every pass through the loop. --- diff --git a/utf8.c b/utf8.c index 4f4c3ea..b5a3809 100644 --- a/utf8.c +++ b/utf8.c @@ -682,7 +682,6 @@ Perl_utf8_length(pTHX_ const U8 *s, const U8 *e) { dVAR; STRLEN len = 0; - U8 t = 0; PERL_ARGS_ASSERT_UTF8_LENGTH; @@ -693,20 +692,23 @@ Perl_utf8_length(pTHX_ const U8 *s, const U8 *e) if (e < s) goto warn_and_return; while (s < e) { - t = UTF8SKIP(s); - if (e - s < t) { - warn_and_return: - if (ckWARN_d(WARN_UTF8)) { - if (PL_op) - Perl_warner(aTHX_ packWARN(WARN_UTF8), + if (!UTF8_IS_INVARIANT(*s)) + s += UTF8SKIP(s); + else + s++; + len++; + } + + if (e != s) { + len--; + warn_and_return: + if (ckWARN_d(WARN_UTF8)) { + if (PL_op) + Perl_warner(aTHX_ packWARN(WARN_UTF8), "%s in %s", unees, OP_DESC(PL_op)); - else - Perl_warner(aTHX_ packWARN(WARN_UTF8), unees); - } - return len; + else + Perl_warner(aTHX_ packWARN(WARN_UTF8), unees); } - s += t; - len++; } return len;