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.
This commit is contained in:
parent
19f2cabed8
commit
31e5d138d7
Notes:
git
2020-06-29 11:06:47 +09:00
19
string.c
19
string.c
@ -4942,17 +4942,12 @@ rb_str_slice_bang(int argc, VALUE *argv, VALUE str)
|
|||||||
else if (nth >= regs->num_regs) return Qnil;
|
else if (nth >= regs->num_regs) return Qnil;
|
||||||
beg = BEG(nth);
|
beg = BEG(nth);
|
||||||
len = END(nth) - beg;
|
len = END(nth) - beg;
|
||||||
subseq:
|
goto subseq;
|
||||||
result = rb_str_new_with_class(str, RSTRING_PTR(str)+beg, len);
|
|
||||||
rb_enc_cr_str_copy_for_substr(result, str);
|
|
||||||
}
|
}
|
||||||
else if (argc == 2) {
|
else if (argc == 2) {
|
||||||
beg = NUM2LONG(indx);
|
beg = NUM2LONG(indx);
|
||||||
len = NUM2LONG(argv[1]);
|
len = NUM2LONG(argv[1]);
|
||||||
num_index:
|
goto num_index;
|
||||||
if (!(p = rb_str_subpos(str, beg, &len))) return Qnil;
|
|
||||||
beg = p - RSTRING_PTR(str);
|
|
||||||
goto subseq;
|
|
||||||
}
|
}
|
||||||
else if (FIXNUM_P(indx)) {
|
else if (FIXNUM_P(indx)) {
|
||||||
beg = FIX2LONG(indx);
|
beg = FIX2LONG(indx);
|
||||||
@ -4966,6 +4961,7 @@ rb_str_slice_bang(int argc, VALUE *argv, VALUE str)
|
|||||||
if (beg == -1) return Qnil;
|
if (beg == -1) return Qnil;
|
||||||
len = RSTRING_LEN(indx);
|
len = RSTRING_LEN(indx);
|
||||||
result = rb_str_dup(indx);
|
result = rb_str_dup(indx);
|
||||||
|
goto squash;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
switch (rb_range_beg_len(indx, &beg, &len, str_strlen(str, NULL), 0)) {
|
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 (len > 0) {
|
||||||
if (beg == 0) {
|
if (beg == 0) {
|
||||||
rb_str_drop_bytes(str, len);
|
rb_str_drop_bytes(str, len);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user