mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
string.c (chars2bytes): small refactoring
This commit is contained in:
+7
-8
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user