summaryrefslogtreecommitdiff
path: root/string.c
diff options
authorJean Boussier <jean.boussier@gmail.com>2024-08-09 15:27:42 +0200
committerJean Boussier <jean.boussier@gmail.com>2024-08-09 22:06:44 +0200
commit3bac5f6af577cc66d29658ca55b69f7496f8112c (patch)
tree2242a80460c9e6871cd4d3d68bf930c93dcd0beb /string.c
parenta332367dad3172e8a8c12efed3913c7fde684b06 (diff)
string.c: add fastpath in str_ensure_byte_pos
If the string only contain single byte characters we can skips all the costly checks.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/11353
Diffstat (limited to 'string.c')
-rw-r--r--string.c14
1 files changed, 8 insertions, 6 deletions
diff --git a/string.c b/string.c
index 5db35724e1..17a552e0de 100644
--- a/string.c
+++ b/string.c
@@ -4254,12 +4254,14 @@ rb_str_index_m(int argc, VALUE *argv, VALUE str)
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;
- if (!at_char_boundary(s, p, e, rb_enc_get(str))) {
- rb_raise(rb_eIndexError,
- "offset %ld does not land on character boundary", pos);
+ if (!single_byte_optimizable(str)) {
+ const char *s = RSTRING_PTR(str);
+ const char *e = RSTRING_END(str);
+ const char *p = s + pos;
+ if (!at_char_boundary(s, p, e, rb_enc_get(str))) {
+ rb_raise(rb_eIndexError,
+ "offset %ld does not land on character boundary", pos);
+ }
}
}
close