diff options
author | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2023-06-28 09:28:15 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2023-06-28 22:42:04 +0900 |
commit | 3d7a6bbc12d9eed9f1e0135c44fdeab08434e4b5 (patch) | |
tree | f3d4fc5caed646c9d31fc7f209a3c7eeab795caa /string.c | |
parent | 6528cf9fcf503706ec16d378b27469b0be51cbff (diff) |
Ensure the byte position is a valid boundary
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/7991
Diffstat (limited to 'string.c')
-rw-r--r-- | string.c | 31 |
1 files changed, 11 insertions, 20 deletions
@@ -3907,18 +3907,21 @@ rb_str_index_m(int argc, VALUE *argv, VALUE str) return LONG2NUM(pos); } -/* whether given pos is valid character boundary or not +/* Ensure that the given pos is a valid character boundary. * Note that in this function, "character" means a code point * (Unicode scalar value), not a grapheme cluster. */ -static bool -str_check_byte_pos(VALUE str, long pos) +static void +str_ensure_byte_pos(VALUE str, long pos) { const char *s = RSTRING_PTR(str); const char *e = RSTRING_END(str); const char *p = s + pos; const char *pp = rb_enc_left_char_head(s, p, e, rb_enc_get(str)); - return p == pp; + if (p != pp) { + rb_raise(rb_eIndexError, + "offset %ld does not land on character boundary", pos); + } } /* @@ -3986,10 +3989,7 @@ rb_str_byteindex_m(int argc, VALUE *argv, VALUE str) pos = 0; } - if (!str_check_byte_pos(str, pos)) { - rb_raise(rb_eIndexError, - "offset %ld does not land on character boundary", pos); - } + str_ensure_byte_pos(str, pos); if (RB_TYPE_P(sub, T_REGEXP)) { if (rb_reg_search(sub, str, pos, 0) < 0) { @@ -4316,10 +4316,7 @@ rb_str_byterindex_m(int argc, VALUE *argv, VALUE str) pos = len; } - if (!str_check_byte_pos(str, pos)) { - rb_raise(rb_eIndexError, - "offset %ld does not land on character boundary", pos); - } + str_ensure_byte_pos(str, pos); if (RB_TYPE_P(sub, T_REGEXP)) { if (rb_reg_search(sub, str, pos, 1) >= 0) { @@ -6197,14 +6194,8 @@ str_check_beg_len(VALUE str, long *beg, long *len) *len = slen - *beg; } end = *beg + *len; - if (!str_check_byte_pos(str, *beg)) { - rb_raise(rb_eIndexError, - "offset %ld does not land on character boundary", *beg); - } - if (!str_check_byte_pos(str, end)) { - rb_raise(rb_eIndexError, - "offset %ld does not land on character boundary", end); - } + str_ensure_byte_pos(str, *beg); + str_ensure_byte_pos(str, end); } /* |