New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add String#byteindex, String#byterindex, and MatchData#byteoffset #5518
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nobu
reviewed
Feb 2, 2022
nurse
reviewed
Feb 18, 2022
shugo
added a commit
to shugo/ruby
that referenced
this issue
Feb 18, 2022
Suggested by Naruse-san in ruby#5518 (comment)
shugo
added a commit
to shugo/ruby
that referenced
this issue
Feb 18, 2022
shugo
added a commit
to shugo/ruby
that referenced
this issue
Feb 18, 2022
The example implementation for checking byte position is as follows: diff --git a/string.c b/string.c
index 31c3f11045..4554699861 100644
--- a/string.c
+++ b/string.c
@@ -3979,6 +3979,20 @@ rb_str_index_m(int argc, VALUE *argv, VALUE str)
return LONG2NUM(pos);
}
+/* whether given pos is valid character boundary or not
+ * Note that in this function, "character" means a code point
+ * (Unicode scalar value), not a grapheme cluster.
+ */
+bool
+str_check_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;
+}
+
/*
* call-seq:
* byteindex(substring, offset = 0) -> integer or nil
@@ -4040,6 +4054,10 @@ rb_str_byteindex_m(int argc, VALUE *argv, VALUE str)
}
}
+ if (!str_check_byte_pos(str, pos)) {
+ rb_raise(rb_eArgError, "invalid pos");
+ }
+
if (RB_TYPE_P(sub, T_REGEXP)) {
if (pos > RSTRING_LEN(str))
return Qnil; ./ruby -e'p "あいう".byteindex("う", 4)'
-e:1:in `byteindex': invalid pos (ArgumentError)
from -e:1:in `<main>' |
Suggested by Naruse-san in ruby#5518 (comment)
Co-authored-by: NARUSE, Yui <naruse@airemix.jp>
Thank you . I've added the boundary check. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
See https://bugs.ruby-lang.org/issues/13110 for details.
The text was updated successfully, but these errors were encountered: