* 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
This commit is contained in:
nobu 2016-09-28 05:11:22 +00:00
Родитель 604ae2e151
Коммит 0b149ab5f3
1 изменённых файлов: 11 добавлений и 9 удалений

Просмотреть файл

@ -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);
}