From a67c8f9335323c8430afc635c4e80193e8c0b9fd Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 10 Jan 2025 14:54:02 +0900 Subject: [PATCH] string.c: use byte index for a string with binary encoding --- src/string.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/string.c b/src/string.c index 3c474d599..feb68510d 100644 --- a/src/string.c +++ b/src/string.c @@ -458,15 +458,16 @@ utf8_strlen(mrb_value str) /* map character index to byte offset index */ static mrb_int -chars2bytes(mrb_value s, mrb_int off, mrb_int idx) +chars2bytes(mrb_value str, mrb_int off, mrb_int idx) { - if (RSTR_SINGLE_BYTE_P(mrb_str_ptr(s))) { + struct RString *s = mrb_str_ptr(str); + if (RSTR_SINGLE_BYTE_P(s) || RSTR_BINARY_P(s)) { return idx; } - const char *p0 = RSTRING_PTR(s) + off; + const char *p0 = RSTR_PTR(s) + off; const char *p = p0; - const char *e = RSTRING_END(s); + const char *e = p0 + RSTR_LEN(s); mrb_int i = 0; while (p