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