1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Extract update_coderange macro

Which restarts scanning the code range in unscanned part.
This commit is contained in:
Nobuyoshi Nakada 2022-08-30 14:16:02 +09:00
parent 22542a6fb0
commit 9dc60653db
Notes: git 2022-08-31 17:28:31 +09:00

View file

@ -221,7 +221,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
VALUE result; VALUE result;
long scanned = 0; long scanned = 0;
int coderange = ENC_CODERANGE_7BIT; enum ruby_coderange_type coderange = ENC_CODERANGE_7BIT;
int width, prec, flags = FNONE; int width, prec, flags = FNONE;
int nextarg = 1; int nextarg = 1;
int posarg = 0; int posarg = 0;
@ -246,6 +246,16 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
rb_raise(rb_eArgError, "flag after precision"); \ rb_raise(rb_eArgError, "flag after precision"); \
} }
#define update_coderange(partial) do { \
if (coderange != ENC_CODERANGE_BROKEN && scanned < blen \
&& rb_enc_to_index(enc) /* != ENCINDEX_ASCII_8BIT */) { \
int cr = coderange; \
scanned += rb_str_coderange_scan_restartable(buf+scanned, buf+blen, enc, &cr); \
ENC_CODERANGE_SET(result, \
(partial && cr == ENC_CODERANGE_UNKNOWN ? \
ENC_CODERANGE_BROKEN : (coderange = cr))); \
} \
} while (0)
++argc; ++argc;
--argv; --argv;
StringValue(fmt); StringValue(fmt);
@ -273,10 +283,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
rb_raise(rb_eArgError, "incomplete format specifier; use %%%% (double %%) instead"); rb_raise(rb_eArgError, "incomplete format specifier; use %%%% (double %%) instead");
} }
PUSH(p, t - p); PUSH(p, t - p);
if (coderange != ENC_CODERANGE_BROKEN && scanned < blen) { update_coderange(FALSE);
scanned += rb_str_coderange_scan_restartable(buf+scanned, buf+blen, enc, &coderange);
ENC_CODERANGE_SET(result, coderange);
}
if (t >= end) { if (t >= end) {
/* end of fmt string */ /* end of fmt string */
goto sprint_exit; goto sprint_exit;
@ -492,13 +499,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
format_s1: format_s1:
len = RSTRING_LEN(str); len = RSTRING_LEN(str);
rb_str_set_len(result, blen); rb_str_set_len(result, blen);
if (coderange != ENC_CODERANGE_BROKEN && scanned < blen) { update_coderange(TRUE);
int cr = coderange;
scanned += rb_str_coderange_scan_restartable(buf+scanned, buf+blen, enc, &cr);
ENC_CODERANGE_SET(result,
(cr == ENC_CODERANGE_UNKNOWN ?
ENC_CODERANGE_BROKEN : (coderange = cr)));
}
enc = rb_enc_check(result, str); enc = rb_enc_check(result, str);
if (flags&(FPREC|FWIDTH)) { if (flags&(FPREC|FWIDTH)) {
slen = rb_enc_strlen(RSTRING_PTR(str),RSTRING_END(str),enc); slen = rb_enc_strlen(RSTRING_PTR(str),RSTRING_END(str),enc);
@ -930,10 +931,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
flags = FNONE; flags = FNONE;
} }
if (coderange != ENC_CODERANGE_BROKEN && scanned < blen) { update_coderange(FALSE);
scanned += rb_str_coderange_scan_restartable(buf+scanned, buf+blen, enc, &coderange);
ENC_CODERANGE_SET(result, coderange);
}
sprint_exit: sprint_exit:
rb_str_tmp_frozen_release(orig, fmt); rb_str_tmp_frozen_release(orig, fmt);
/* XXX - We cannot validate the number of arguments if (digit)$ style used. /* XXX - We cannot validate the number of arguments if (digit)$ style used.