ruby-changes:44209
From: nobu <ko1@a...>
Date: Wed, 28 Sep 2016 14:11:26 +0900 (JST)
Subject: [ruby-changes:44209] nobu:r56282 (trunk): strscan.c: minl
nobu 2016-09-28 14:11:22 +0900 (Wed, 28 Sep 2016) New Revision: 56282 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56282 Log: strscan.c: minl * ext/strscan/strscan.c (minl): extract to reduce repeated S_LEN. Modified files: trunk/ext/strscan/strscan.c Index: ext/strscan/strscan.c =================================================================== --- ext/strscan/strscan.c (revision 56281) +++ ext/strscan/strscan.c (revision 56282) @@ -64,6 +64,7 @@ struct strscanner https://github.com/ruby/ruby/blob/trunk/ext/strscan/strscan.c#L64 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 https://github.com/ruby/ruby/blob/trunk/ext/strscan/strscan.c#L141 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 https://github.com/ruby/ruby/blob/trunk/ext/strscan/strscan.c#L159 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) https://github.com/ruby/ruby/blob/trunk/ext/strscan/strscan.c#L733 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) https://github.com/ruby/ruby/blob/trunk/ext/strscan/strscan.c#L810 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); } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/