summaryrefslogtreecommitdiff
path: root/ext/strscan/strscan.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-09-28 05:11:22 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-09-28 05:11:22 +0000
commit0b149ab5f350aea7b803f67c87f35e68d3bf8556 (patch)
treeeb6c8fdfb047c3f585dfd264967f623b92e5971f /ext/strscan/strscan.c
parent604ae2e1519259b1dcede91c0a5126d1a9d400e0 (diff)
strscan.c: minl
* ext/strscan/strscan.c (minl): extract to reduce repeated S_LEN. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56282 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/strscan/strscan.c')
-rw-r--r--ext/strscan/strscan.c20
1 files changed, 11 insertions, 9 deletions
diff --git a/ext/strscan/strscan.c b/ext/strscan/strscan.c
index d5221ea..838f0e7 100644
--- a/ext/strscan/strscan.c
+++ b/ext/strscan/strscan.c
@@ -64,6 +64,7 @@ struct strscanner
Function Prototypes
======================================================================= */
+static inline long minl _((const long n, const long x));
static VALUE infect _((VALUE str, struct strscanner *p));
static VALUE extract_range _((struct strscanner *p, long beg_i, long end_i));
static VALUE extract_beg_len _((struct strscanner *p, long beg_i, long len));
@@ -140,12 +141,17 @@ str_new(struct strscanner *p, const char *ptr, long len)
return str;
}
+static inline long
+minl(const long x, const long y)
+{
+ return (x < y) ? x : y;
+}
+
static VALUE
extract_range(struct strscanner *p, long beg_i, long end_i)
{
if (beg_i > S_LEN(p)) return Qnil;
- if (end_i > S_LEN(p))
- end_i = S_LEN(p);
+ end_i = minl(end_i, S_LEN(p));
return infect(str_new(p, S_PBEG(p) + beg_i, end_i - beg_i), p);
}
@@ -153,8 +159,7 @@ static VALUE
extract_beg_len(struct strscanner *p, long beg_i, long len)
{
if (beg_i > S_LEN(p)) return Qnil;
- if (beg_i + len > S_LEN(p))
- len = S_LEN(p) - beg_i;
+ len = minl(len, S_LEN(p) - beg_i);
return infect(str_new(p, S_PBEG(p) + beg_i, len), p);
}
@@ -728,9 +733,7 @@ strscan_getch(VALUE self)
return Qnil;
len = rb_enc_mbclen(CURPTR(p), S_PEND(p), rb_enc_get(p->str));
- if (len > S_RESTLEN(p)) {
- len = S_RESTLEN(p);
- }
+ len = minl(len, S_RESTLEN(p));
p->prev = p->curr;
p->curr += len;
MATCHED(p);
@@ -807,8 +810,7 @@ strscan_peek(VALUE self, VALUE vlen)
if (EOS_P(p))
return infect(str_new(p, "", 0), p);
- if (len > S_RESTLEN(p))
- len = S_RESTLEN(p);
+ len = minl(len, S_RESTLEN(p));
return extract_beg_len(p, p->curr, len);
}