summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-02-16 09:30:03 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-02-16 09:30:03 +0000
commit0ad3d7ce2d679a92d2904430919c835639993a32 (patch)
tree37a9440b3252ba633f9bd8582bc0c40cf48a3b4d /string.c
parent7eeba5f440975912a63ee6e9da014c4a727cb565 (diff)
* string.c (str_strlen): little more optimize.
(rb_enc_nth): remove needless variable 'c'. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15507 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r--string.c20
1 files changed, 11 insertions, 9 deletions
diff --git a/string.c b/string.c
index 23f1d0addc..e6670d31ba 100644
--- a/string.c
+++ b/string.c
@@ -643,9 +643,11 @@ str_strlen(VALUE str, rb_encoding *enc)
}
while (s < t) {
unsigned long d = *s;
- d = (~d ^ (d&(d<<1)))&NONASCII_MASK;
- d = (d>>7) + (d>>15);
- d = d + (d>>16);
+ d = ~d | (d<<1);
+ d &= NONASCII_MASK;
+ d >>= 7;
+ d += (d>>8);
+ d += (d>>16);
#if NONASCII_MASK == 0x8080808080808080UL
d = d + (d>>32);
#endif
@@ -657,6 +659,7 @@ str_strlen(VALUE str, rb_encoding *enc)
for (; p<e; p++) {
if (((*p)&0xC0) != 0x80) len++;
}
+ return len;
}
else
#endif
@@ -664,6 +667,9 @@ str_strlen(VALUE str, rb_encoding *enc)
if (len < 0) {
rb_raise(rb_eArgError, "invalid mbstring sequence");
}
+ if (ENC_CODERANGE(str) != ENC_CODERANGE_VALID && enc == STR_ENC_GET(str)) {
+ ENC_CODERANGE_SET(str, ENC_CODERANGE_VALID);
+ }
return len;
}
@@ -949,8 +955,6 @@ rb_str_s_try_convert(VALUE dummy, VALUE str)
char*
rb_enc_nth(const char *p, const char *e, int nth, rb_encoding *enc)
{
- int c;
-
if (rb_enc_mbmaxlen(enc) == 1) {
p += nth;
}
@@ -981,10 +985,8 @@ rb_enc_nth(const char *p, const char *e, int nth, rb_encoding *enc)
return (char *)p;
}
else {
- for (c=0; p<e && nth--; c++) {
- int n = rb_enc_mbclen(p, e, enc);
-
- p += n;
+ while (p<e && nth--) {
+ p += rb_enc_mbclen(p, e, enc);
}
}
if (p > e) p = e;