summaryrefslogtreecommitdiff
path: root/ext/strscan
diff options
context:
space:
mode:
Diffstat (limited to 'ext/strscan')
-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 d5221eaeb2..838f0e7f86 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);
}