mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
mruby-string-ext (mrb_valid_enc_p): simplify the code
By using `mrb_utf8len` directly. If `len == 1` and `ch >= 0x80`, the character should be an invalid character.
This commit is contained in:
@@ -1339,19 +1339,9 @@ str_valid_enc_p(mrb_state *mrb, mrb_value str)
|
||||
const char *p = RSTR_PTR(s);
|
||||
const char *e = p + byte_len;
|
||||
while (p < e) {
|
||||
mrb_int len = mrb_utf8len_table[(unsigned char)p[0] >> 3];
|
||||
if (len == 0 || len > e - p)
|
||||
return mrb_false_value();
|
||||
switch (len) {
|
||||
case 4:
|
||||
if (utf8_islead(p[3])) return mrb_false_value();
|
||||
case 3:
|
||||
if (utf8_islead(p[2])) return mrb_false_value();
|
||||
case 2:
|
||||
if (utf8_islead(p[1])) return mrb_false_value();
|
||||
default:
|
||||
break;
|
||||
}
|
||||
mrb_int len = mrb_utf8len(p, e);
|
||||
|
||||
if (len == 1 && (*p & 0x80)) return mrb_false_value();
|
||||
p += len;
|
||||
utf8_len++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user