string.c (chars2bytes): small refactoring

This commit is contained in:
Yukihiro "Matz" Matsumoto
2024-01-10 11:09:47 +09:00
parent 1d243bc58b
commit e39e4ea26d
+7 -8
View File
@@ -330,16 +330,15 @@ chars2bytes(mrb_value s, mrb_int off, mrb_int idx)
return idx;
}
else {
mrb_int b = 0;
const char *p = RSTRING_PTR(s) + off;
const char *p0 = RSTRING_PTR(s) + off;
const char *p = p0;
const char *e = RSTRING_END(s);
for (mrb_int i=0; p<e && i<idx; i++) {
mrb_int n = mrb_utf8len(p, e);
b += n;
p += n;
while (p < e && idx--) {
p += mrb_utf8len(p, e);
}
return b;
mrb_int len = (mrb_int)(p-p0);
if (idx > 0 || p > e) len++;
return len;
}
}