diff options
author | 卜部昌平 <[email protected]> | 2020-06-18 16:36:35 +0900 |
---|---|---|
committer | 卜部昌平 <[email protected]> | 2020-06-29 11:05:41 +0900 |
commit | c7a40731549ce0dd6b52af512ebc0c588bce05f4 (patch) | |
tree | 474539faf8dfbde0edb56876b5db604ad1aea407 /string.c | |
parent | fdae2063fb4730c0194e2a312ff250902a23df08 (diff) |
chompped_length: 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
Diffstat (limited to 'string.c')
-rw-r--r-- | string.c | 64 |
1 files changed, 34 insertions, 30 deletions
@@ -8960,6 +8960,37 @@ rb_str_chop(VALUE str) return rb_str_subseq(str, 0, chopped_length(str)); } +static long +smart_chomp(VALUE str, const char *e, const char *p) +{ + rb_encoding *enc = rb_enc_get(str); + if (rb_enc_mbminlen(enc) > 1) { + const char *pp = rb_enc_left_char_head(p, e-rb_enc_mbminlen(enc), e, enc); + if (rb_enc_is_newline(pp, e, enc)) { + e = pp; + } + pp = e - rb_enc_mbminlen(enc); + if (pp >= p) { + pp = rb_enc_left_char_head(p, pp, e, enc); + if (rb_enc_ascget(pp, e, 0, enc) == '\r') { + e = pp; + } + } + } + else { + switch (*(e-1)) { /* not e[-1] to get rid of VC bug */ + case '\n': + if (--e > p && *(e-1) == '\r') { + --e; + } + break; + case '\r': + --e; + break; + } + } + return e - p; +} static long chompped_length(VALUE str, VALUE rs) @@ -8974,34 +9005,7 @@ chompped_length(VALUE str, VALUE rs) if (len == 0) return 0; e = p + len; if (rs == rb_default_rs) { - smart_chomp: - enc = rb_enc_get(str); - if (rb_enc_mbminlen(enc) > 1) { - pp = rb_enc_left_char_head(p, e-rb_enc_mbminlen(enc), e, enc); - if (rb_enc_is_newline(pp, e, enc)) { - e = pp; - } - pp = e - rb_enc_mbminlen(enc); - if (pp >= p) { - pp = rb_enc_left_char_head(p, pp, e, enc); - if (rb_enc_ascget(pp, e, 0, enc) == '\r') { - e = pp; - } - } - } - else { - switch (*(e-1)) { /* not e[-1] to get rid of VC bug */ - case '\n': - if (--e > p && *(e-1) == '\r') { - --e; - } - break; - case '\r': - --e; - break; - } - } - return e - p; + return smart_chomp(str, e, p); } enc = rb_enc_get(str); @@ -9037,11 +9041,11 @@ chompped_length(VALUE str, VALUE rs) if (rslen == rb_enc_mbminlen(enc)) { if (rslen == 1) { if (newline == '\n') - goto smart_chomp; + return smart_chomp(str, e, p); } else { if (rb_enc_is_newline(rsptr, rsptr+rslen, enc)) - goto smart_chomp; + return smart_chomp(str, e, p); } } |