diff --git a/mrbgems/mruby-string-ext/src/string.c b/mrbgems/mruby-string-ext/src/string.c index 4e2be81ff..0e3b56c2a 100644 --- a/mrbgems/mruby-string-ext/src/string.c +++ b/mrbgems/mruby-string-ext/src/string.c @@ -1002,8 +1002,8 @@ str_succ(mrb_state *mrb, mrb_value self) #ifdef MRB_UTF8_STRING extern const char mrb_utf8len_table[]; -static mrb_int -utf8code(unsigned char* p, mrb_int limit) +MRB_INLINE mrb_int +utf8code(const unsigned char* p, const unsigned char *e) { mrb_int len; @@ -1011,7 +1011,7 @@ utf8code(unsigned char* p, mrb_int limit) return p[0]; len = mrb_utf8len_table[p[0]>>3]; - if (len <= limit && len > 1 && (p[1] & 0xc0) == 0x80) { + if (p+len <= e && len > 1 && (p[1] & 0xc0) == 0x80) { if (len == 2) return ((p[0] & 0x1f) << 6) + (p[1] & 0x3f); if ((p[2] & 0xc0) == 0x80) { @@ -1033,26 +1033,27 @@ str_ord(mrb_state* mrb, mrb_value str) { if (RSTRING_LEN(str) == 0) mrb_raise(mrb, E_ARGUMENT_ERROR, "empty string"); - mrb_int c = utf8code((unsigned char*)RSTRING_PTR(str), RSTRING_LEN(str)); + const unsigned char *p = (unsigned char*)RSTRING_PTR(str); + const unsigned char *e = p + RSTRING_LEN(str); + + mrb_int c = utf8code(p, e); if (c < 0) mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid UTF-8 byte sequence"); return mrb_fixnum_value(c); } static mrb_value -str_codepoints(mrb_state *mrb, mrb_value self) +str_codepoints(mrb_state *mrb, mrb_value str) { mrb_value result; - char *p = RSTRING_PTR(self); - mrb_int len = RSTRING_LEN(self); - char *e = p + len; + const unsigned char *p = (unsigned char*)RSTRING_PTR(str); + const unsigned char *e = p + RSTRING_LEN(str); mrb->c->ci->mid = 0; result = mrb_ary_new(mrb); while (p < e) { - mrb_int c = utf8code((unsigned char*)p, len); + mrb_int c = utf8code((unsigned char*)p, e); mrb_ary_push(mrb, result, mrb_int_value(mrb, c)); mrb_int ulen = mrb_utf8len(p, e); - len -= ulen; p += ulen; } return result;