diff options
author | 卜部昌平 <shyouhei@ruby-lang.org> | 2020-06-18 15:21:18 +0900 |
---|---|---|
committer | 卜部昌平 <shyouhei@ruby-lang.org> | 2020-06-29 11:05:41 +0900 |
commit | 31e5d138d71f186c5ab86b6a13b3b7472a693f80 (patch) | |
tree | a6954284b15be226815c50d3ee3cf7f1fa4b67d6 | |
parent | 19f2cabed88943f8adae00c9588e47f7e9112a9e (diff) |
rb_str_slice_bang: do not goto into a branch
I'm not necessarily against every goto in general, but jumping into a
branch is definitely a bad idea. Better refactor.
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/3247
-rw-r--r-- | string.c | 19 |
1 files changed, 12 insertions, 7 deletions
@@ -4942,17 +4942,12 @@ rb_str_slice_bang(int argc, VALUE *argv, VALUE str) else if (nth >= regs->num_regs) return Qnil; beg = BEG(nth); len = END(nth) - beg; - subseq: - result = rb_str_new_with_class(str, RSTRING_PTR(str)+beg, len); - rb_enc_cr_str_copy_for_substr(result, str); + goto subseq; } else if (argc == 2) { beg = NUM2LONG(indx); len = NUM2LONG(argv[1]); - num_index: - if (!(p = rb_str_subpos(str, beg, &len))) return Qnil; - beg = p - RSTRING_PTR(str); - goto subseq; + goto num_index; } else if (FIXNUM_P(indx)) { beg = FIX2LONG(indx); @@ -4966,6 +4961,7 @@ rb_str_slice_bang(int argc, VALUE *argv, VALUE str) if (beg == -1) return Qnil; len = RSTRING_LEN(indx); result = rb_str_dup(indx); + goto squash; } else { switch (rb_range_beg_len(indx, &beg, &len, str_strlen(str, NULL), 0)) { @@ -4979,6 +4975,15 @@ rb_str_slice_bang(int argc, VALUE *argv, VALUE str) } } + num_index: + if (!(p = rb_str_subpos(str, beg, &len))) return Qnil; + beg = p - RSTRING_PTR(str); + + subseq: + result = rb_str_new_with_class(str, RSTRING_PTR(str)+beg, len); + rb_enc_cr_str_copy_for_substr(result, str); + + squash: if (len > 0) { if (beg == 0) { rb_str_drop_bytes(str, len); |