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.
This commit is contained in:
卜部昌平 2020-06-18 16:36:35 +09:00
parent fdae2063fb
commit c7a4073154
Notes: git 2020-06-29 11:06:45 +09:00

View File

@ -8960,24 +8960,12 @@ rb_str_chop(VALUE str)
return rb_str_subseq(str, 0, chopped_length(str)); return rb_str_subseq(str, 0, chopped_length(str));
} }
static long static long
chompped_length(VALUE str, VALUE rs) smart_chomp(VALUE str, const char *e, const char *p)
{ {
rb_encoding *enc; rb_encoding *enc = rb_enc_get(str);
int newline;
char *pp, *e, *rsptr;
long rslen;
char *const p = RSTRING_PTR(str);
long len = RSTRING_LEN(str);
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) { if (rb_enc_mbminlen(enc) > 1) {
pp = rb_enc_left_char_head(p, e-rb_enc_mbminlen(enc), e, enc); const char *pp = rb_enc_left_char_head(p, e-rb_enc_mbminlen(enc), e, enc);
if (rb_enc_is_newline(pp, e, enc)) { if (rb_enc_is_newline(pp, e, enc)) {
e = pp; e = pp;
} }
@ -9002,6 +8990,22 @@ chompped_length(VALUE str, VALUE rs)
} }
} }
return e - p; return e - p;
}
static long
chompped_length(VALUE str, VALUE rs)
{
rb_encoding *enc;
int newline;
char *pp, *e, *rsptr;
long rslen;
char *const p = RSTRING_PTR(str);
long len = RSTRING_LEN(str);
if (len == 0) return 0;
e = p + len;
if (rs == rb_default_rs) {
return smart_chomp(str, e, p);
} }
enc = rb_enc_get(str); enc = rb_enc_get(str);
@ -9037,11 +9041,11 @@ chompped_length(VALUE str, VALUE rs)
if (rslen == rb_enc_mbminlen(enc)) { if (rslen == rb_enc_mbminlen(enc)) {
if (rslen == 1) { if (rslen == 1) {
if (newline == '\n') if (newline == '\n')
goto smart_chomp; return smart_chomp(str, e, p);
} }
else { else {
if (rb_enc_is_newline(rsptr, rsptr+rslen, enc)) if (rb_enc_is_newline(rsptr, rsptr+rslen, enc))
goto smart_chomp; return smart_chomp(str, e, p);
} }
} }