зеркало из https://github.com/github/ruby.git
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:
Родитель
19f2cabed8
Коммит
31e5d138d7
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;
|
||||
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);
|
||||
|
|
Загрузка…
Ссылка в новой задаче