Skip to content

Commit 0641951

Browse files
committed
2digits macros
1 parent cf8388f commit 0641951

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

time.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,6 +2153,9 @@ invalid_utc_offset(VALUE zone)
21532153
zone);
21542154
}
21552155

2156+
#define have_2digits(ptr) (ISDIGIT((ptr)[0]) && ISDIGIT((ptr)[1]))
2157+
#define num_from_2digits(ptr) ((ptr)[0] * 10 + (ptr)[1] - '0' * 11)
2158+
21562159
static VALUE
21572160
utc_offset_arg(VALUE arg)
21582161
{
@@ -2207,18 +2210,18 @@ utc_offset_arg(VALUE arg)
22072210
goto invalid_utc_offset;
22082211
}
22092212
if (sec) {
2210-
if (!ISDIGIT(sec[0]) || !ISDIGIT(sec[1])) goto invalid_utc_offset;
2211-
n += (sec[0] * 10 + sec[1] - '0' * 11);
2213+
if (!have_2digits(sec)) goto invalid_utc_offset;
2214+
n += num_from_2digits(sec);
22122215
ASSUME(min);
22132216
}
22142217
if (min) {
2215-
if (!ISDIGIT(min[0]) || !ISDIGIT(min[1])) goto invalid_utc_offset;
2218+
if (!have_2digits(min)) goto invalid_utc_offset;
22162219
if (min[0] > '5') goto invalid_utc_offset;
2217-
n += (min[0] * 10 + min[1] - '0' * 11) * 60;
2220+
n += num_from_2digits(min) * 60;
22182221
}
22192222
if (s[0] != '+' && s[0] != '-') goto invalid_utc_offset;
2220-
if (!ISDIGIT(s[1]) || !ISDIGIT(s[2])) goto invalid_utc_offset;
2221-
n += (s[1] * 10 + s[2] - '0' * 11) * 3600;
2223+
if (!have_2digits(s+1)) goto invalid_utc_offset;
2224+
n += num_from_2digits(s+1) * 3600;
22222225
if (s[0] == '-') {
22232226
if (n == 0) return UTC_ZONE;
22242227
n = -n;
@@ -2528,8 +2531,7 @@ static int
25282531
two_digits(const char *ptr, const char *end, const char **endp, const char *name)
25292532
{
25302533
ssize_t len = end - ptr;
2531-
if (len < 2 || (!ISDIGIT(ptr[0]) || !ISDIGIT(ptr[1])) ||
2532-
((len > 2) && ISDIGIT(ptr[2]))) {
2534+
if (len < 2 || !have_2digits(ptr) || ((len > 2) && ISDIGIT(ptr[2]))) {
25332535
VALUE mesg = rb_sprintf("two digits %s is expected", name);
25342536
if (ptr[-1] == '-' || ptr[-1] == ':') {
25352537
rb_str_catf(mesg, " after '%c'", ptr[-1]);
@@ -2538,7 +2540,7 @@ two_digits(const char *ptr, const char *end, const char **endp, const char *name
25382540
rb_exc_raise(rb_exc_new_str(rb_eArgError, mesg));
25392541
}
25402542
*endp = ptr + 2;
2541-
return (ptr[0] - '0') * 10 + (ptr[1] - '0');
2543+
return num_from_2digits(ptr);
25422544
}
25432545

25442546
static VALUE

0 commit comments

Comments
 (0)