diff options
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | encoding.c | 17 | ||||
-rw-r--r-- | string.c | 42 |
3 files changed, 40 insertions, 26 deletions
@@ -1,3 +1,10 @@ +Sat Feb 16 11:53:35 2008 Tanaka Akira <akr@fsij.org> + + * encoding.c (rb_enc_strlen): moved to string.c. + + * string.c (rb_enc_strlen): use search_nonascii. + (str_strlen): don't use search_nonascii. + Sat Feb 16 11:45:31 2008 Tanaka Akira <akr@fsij.org> * lib/require_relative.rb: check require_relative call in eval. diff --git a/encoding.c b/encoding.c index 5a8369b6f3..da89d8ac70 100644 --- a/encoding.c +++ b/encoding.c @@ -720,23 +720,6 @@ rb_obj_encoding(VALUE obj) return rb_enc_from_encoding(enc); } -long -rb_enc_strlen(const char *p, const char *e, rb_encoding *enc) -{ - long c; - - if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) { - return (e - p) / rb_enc_mbminlen(enc); - } - - for (c=0; p<e; c++) { - int n = rb_enc_mbclen(p, e, enc); - - p += n; - } - return c; -} - int rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc) { @@ -588,24 +588,48 @@ rb_str_init(int argc, VALUE *argv, VALUE str) return str; } +long +rb_enc_strlen(const char *p, const char *e, rb_encoding *enc) +{ + long c; + const char *q; + + if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) { + return (e - p) / rb_enc_mbminlen(enc); + } + else if (rb_enc_asciicompat(enc)) { + c = 0; + while (p < e) { + if (ISASCII(*p)) { + q = search_nonascii(p, e); + if (!q) + return c + (e - p); + c += q - p; + p = q; + } + p += rb_enc_mbclen(p, e, enc); + c++; + } + return c; + } + + for (c=0; p<e; c++) { + p += rb_enc_mbclen(p, e, enc); + } + return c; +} + static long str_strlen(VALUE str, rb_encoding *enc) { - long len = 0; + long len; const char *p, *e; if (single_byte_optimizable(str)) return RSTRING_LEN(str); if (!enc) enc = STR_ENC_GET(str); p = RSTRING_PTR(str); e = RSTRING_END(str); - if (rb_enc_asciicompat(enc)) { - const char *p2 = search_nonascii(p, e); - - if (!p2) return RSTRING_LEN(str); - len = p2 - p; - p = p2; - } - len += rb_enc_strlen(p, e, enc); + len = rb_enc_strlen(p, e, enc); if (len < 0) { rb_raise(rb_eArgError, "invalid mbstring sequence"); } |