ruby-changes:62062
From: =E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3 <ko1@a...>
Date: Mon, 29 Jun 2020 11:08:36 +0900 (JST)
Subject: [ruby-changes:62062] c7a4073154 (master): chompped_length: do not goto into a branch
https://git.ruby-lang.org/ruby.git/commit/?id=c7a4073154 From c7a40731549ce0dd6b52af512ebc0c588bce05f4 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 16:36:35 +0900 Subject: 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. diff --git a/string.c b/string.c index 1a9fd3b..c7be097 100644 --- a/string.c +++ b/string.c @@ -8960,6 +8960,37 @@ rb_str_chop(VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L8960 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) https://github.com/ruby/ruby/blob/trunk/string.c#L9005 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) https://github.com/ruby/ruby/blob/trunk/string.c#L9041 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); } } -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/