aboutsummaryrefslogtreecommitdiffstats
path: root/string.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-05-15 09:06:25 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-05-15 09:06:25 +0000
commitd70228d1053f4a2400995e6b4d7a97f3eaac2026 (patch)
tree17da735cea99c08e431773d529b0449cbac821c8 /string.c
parent04c10b8af30161843f3e147deb3f94b684ec8036 (diff)
downloadruby-d70228d1053f4a2400995e6b4d7a97f3eaac2026.tar.gz
string.c: all_digits_p
* string.c (all_digits_p): extract duplicate code from rb_str_upto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50500 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r--string.c27
1 files changed, 13 insertions, 14 deletions
diff --git a/string.c b/string.c
index 7e18f69027..80541e4b8b 100644
--- a/string.c
+++ b/string.c
@@ -3449,6 +3449,15 @@ rb_str_succ_bang(VALUE str)
return str;
}
+static int
+all_digits_p(const char *s, long len)
+{
+ while (len-- > 0) {
+ if (!ISDIGIT(*s)) return 0;
+ s++;
+ }
+ return 1;
+}
/*
* call-seq:
@@ -3513,22 +3522,13 @@ rb_str_upto(int argc, VALUE *argv, VALUE beg)
return beg;
}
/* both edges are all digits */
- if (ascii && ISDIGIT(RSTRING_PTR(beg)[0]) && ISDIGIT(RSTRING_PTR(end)[0])) {
- char *s, *send;
+ if (ascii && ISDIGIT(RSTRING_PTR(beg)[0]) && ISDIGIT(RSTRING_PTR(end)[0]) &&
+ all_digits_p(RSTRING_PTR(beg), RSTRING_LEN(beg)) &&
+ all_digits_p(RSTRING_PTR(end), RSTRING_LEN(end))) {
VALUE b, e;
int width;
- s = RSTRING_PTR(beg); send = RSTRING_END(beg);
- width = rb_long2int(send - s);
- while (s < send) {
- if (!ISDIGIT(*s)) goto no_digits;
- s++;
- }
- s = RSTRING_PTR(end); send = RSTRING_END(end);
- while (s < send) {
- if (!ISDIGIT(*s)) goto no_digits;
- s++;
- }
+ width = RSTRING_LENINT(beg);
b = rb_str_to_inum(beg, 10, FALSE);
e = rb_str_to_inum(end, 10, FALSE);
if (FIXNUM_P(b) && FIXNUM_P(e)) {
@@ -3556,7 +3556,6 @@ rb_str_upto(int argc, VALUE *argv, VALUE beg)
return beg;
}
/* normal case */
- no_digits:
n = rb_str_cmp(beg, end);
if (n > 0 || (excl && n == 0)) return beg;