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:
Yukihiro "Matz" Matsumoto
2024-01-12 09:48:09 +09:00
parent fb8bc1954b
commit c53d9a33fc
+3 -13
View File
@@ -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++;
}