ruby-changes:62074
From: =E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3 <ko1@a...>
Date: Mon, 29 Jun 2020 11:08:48 +0900 (JST)
Subject: [ruby-changes:62074] 31e5d138d7 (master): rb_str_slice_bang: do not goto into a branch
https://git.ruby-lang.org/ruby.git/commit/?id=31e5d138d7 From 31e5d138d71f186c5ab86b6a13b3b7472a693f80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?= <shyouhei@r...> Date: Thu, 18 Jun 2020 15:21:18 +0900 Subject: 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. diff --git a/string.c b/string.c index 4bfd9f5..f7a26f5 100644 --- a/string.c +++ b/string.c @@ -4942,17 +4942,12 @@ rb_str_slice_bang(int argc, VALUE *argv, VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L4942 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) https://github.com/ruby/ruby/blob/trunk/string.c#L4961 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) https://github.com/ruby/ruby/blob/trunk/string.c#L4975 } } + 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); -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/