From df0a5e838d2ce451a671d86da8f17fd4797f8fd2 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Wed, 10 Jan 2024 12:41:28 +0900 Subject: [PATCH] string.c (chars2bytes): use early return --- src/string.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/string.c b/src/string.c index c19f6003e..934b37ff3 100644 --- a/src/string.c +++ b/src/string.c @@ -329,17 +329,19 @@ chars2bytes(mrb_value s, mrb_int off, mrb_int idx) if (RSTR_ASCII_P(mrb_str_ptr(s))) { return idx; } - else { - const char *p0 = RSTRING_PTR(s) + off; - const char *p = p0; - const char *e = RSTRING_END(s); - while (p < e && idx--) { - p += mrb_utf8len(p, e); - } - mrb_int len = (mrb_int)(p-p0); - if (idx > 0 || p > e) len++; - return len; + + const char *p0 = RSTRING_PTR(s) + off; + const char *p = p0; + const char *e = RSTRING_END(s); + + while (p < e && idx--) { + if ((*p & 0x80) == 0) p++; + else p += mrb_utf8len(p, e); } + + mrb_int len = (mrb_int)(p-p0); + if (idx > 0 || p > e) len++; + return len; } /* map byte offset to character index */