string.c: avoid RSTRING_CHAR_LEN() if possible

Current code scan the string twice (once from RSTRING_CHAR_LEN, and once
from chars2bytes), but those scans are not necessary. Just point the end
of the string.
This commit is contained in:
Yukihiro "Matz" Matsumoto
2024-01-09 11:44:12 +09:00
parent f3ebef1172
commit 9135f0991b
+1 -2
View File
@@ -2070,8 +2070,7 @@ mrb_str_rindex_m(mrb_state *mrb, mrb_value str)
mrb_int pos;
if (mrb_get_args(mrb, "S|i", &sub, &pos) == 1) {
pos = RSTRING_CHAR_LEN(str);
pos = chars2bytes(str, 0, pos);
pos = RSTRING_LEN(str);
}
else if (pos >= 0) {
pos = chars2bytes(str, 0, pos);