Skip to content

Commit 315fe21

Browse files
hsbtclaude
andcommitted
Use rb_len_t for String lengths and positions
It falls back to long on Ruby versions that do not define HAVE_RB_LEN_T, so the change also builds there. https://bugs.ruby-lang.org/issues/22400 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f829ead commit 315fe21

1 file changed

Lines changed: 47 additions & 40 deletions

File tree

‎ext/strscan/strscan.c‎

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
#include "ruby/re.h"
1313
#include "ruby/encoding.h"
1414

15+
#ifndef HAVE_RB_LEN_T
16+
typedef long rb_len_t;
17+
# define LEN2NUM LONG2NUM
18+
# define NUM2LEN NUM2LONG
19+
# define PRIdLEN "ld"
20+
#endif
21+
1522
#ifdef RUBY_EXTCONF_H
1623
# include RUBY_EXTCONF_H
1724
#endif
@@ -51,8 +58,8 @@ struct strscanner
5158
VALUE str;
5259

5360
/* scan pointers */
54-
long prev; /* legal only when MATCHED_P(s) */
55-
long curr; /* always legal */
61+
rb_len_t prev; /* legal only when MATCHED_P(s) */
62+
rb_len_t curr; /* always legal */
5663

5764
/* the regexp register; legal only when MATCHED_P(s) */
5865
struct re_registers regs;
@@ -90,9 +97,9 @@ struct strscanner
9097
Function Prototypes
9198
======================================================================= */
9299

93-
static inline long minl _((const long n, const long x));
94-
static VALUE extract_range _((struct strscanner *p, long beg_i, long end_i));
95-
static VALUE extract_beg_len _((struct strscanner *p, long beg_i, long len));
100+
static inline rb_len_t minl _((const rb_len_t n, const rb_len_t x));
101+
static VALUE extract_range _((struct strscanner *p, rb_len_t beg_i, rb_len_t end_i));
102+
static VALUE extract_beg_len _((struct strscanner *p, rb_len_t beg_i, rb_len_t len));
96103

97104
static struct strscanner *check_strscan _((VALUE obj));
98105
static void strscan_mark _((void *p));
@@ -149,29 +156,29 @@ static VALUE inspect2 _((struct strscanner *p));
149156
======================================================================= */
150157

151158
static VALUE
152-
str_new(struct strscanner *p, const char *ptr, long len)
159+
str_new(struct strscanner *p, const char *ptr, rb_len_t len)
153160
{
154161
VALUE str = rb_str_new(ptr, len);
155162
rb_enc_copy(str, p->str);
156163
return str;
157164
}
158165

159-
static inline long
160-
minl(const long x, const long y)
166+
static inline rb_len_t
167+
minl(const rb_len_t x, const rb_len_t y)
161168
{
162169
return (x < y) ? x : y;
163170
}
164171

165172
static VALUE
166-
extract_range(struct strscanner *p, long beg_i, long end_i)
173+
extract_range(struct strscanner *p, rb_len_t beg_i, rb_len_t end_i)
167174
{
168175
if (beg_i > S_LEN(p)) return Qnil;
169176
end_i = minl(end_i, S_LEN(p));
170177
return str_new(p, S_PBEG(p) + beg_i, end_i - beg_i);
171178
}
172179

173180
static VALUE
174-
extract_beg_len(struct strscanner *p, long beg_i, long len)
181+
extract_beg_len(struct strscanner *p, rb_len_t beg_i, rb_len_t len)
175182
{
176183
if (beg_i > S_LEN(p)) return Qnil;
177184
len = minl(len, S_LEN(p) - beg_i);
@@ -558,7 +565,7 @@ strscan_get_pos(VALUE self)
558565
struct strscanner *p;
559566

560567
GET_SCANNER(self, p);
561-
return LONG2NUM(p->curr);
568+
return LEN2NUM(p->curr);
562569
}
563570

564571
/*
@@ -578,7 +585,7 @@ strscan_get_charpos(VALUE self)
578585
GET_SCANNER(self, p);
579586

580587
s = EOS_P(p) ? S_PEND(p) : CURPTR(p);
581-
return LONG2NUM(rb_enc_strlen(S_PBEG(p), s, rb_enc_get(p->str)));
588+
return LEN2NUM(rb_enc_strlen(S_PBEG(p), s, rb_enc_get(p->str)));
582589
}
583590

584591
/*
@@ -594,15 +601,15 @@ static VALUE
594601
strscan_set_pos(VALUE self, VALUE v)
595602
{
596603
struct strscanner *p;
597-
long i;
604+
rb_len_t i;
598605

599606
GET_SCANNER(self, p);
600-
i = NUM2LONG(v);
607+
i = NUM2LEN(v);
601608
if (i < 0) i += S_LEN(p);
602609
if (i < 0) rb_raise(rb_eRangeError, "index out of range");
603610
if (i > S_LEN(p)) rb_raise(rb_eRangeError, "index out of range");
604611
p->curr = i;
605-
return LONG2NUM(i);
612+
return LEN2NUM(i);
606613
}
607614

608615
static inline UChar *
@@ -658,7 +665,7 @@ succ(struct strscanner *p)
658665
}
659666
}
660667

661-
static inline long
668+
static inline rb_len_t
662669
last_match_length(struct strscanner *p)
663670
{
664671
if (p->fixed_anchor_p) {
@@ -670,8 +677,8 @@ last_match_length(struct strscanner *p)
670677
}
671678
}
672679

673-
static inline long
674-
adjust_register_position(struct strscanner *p, long position)
680+
static inline rb_len_t
681+
adjust_register_position(struct strscanner *p, rb_len_t position)
675682
{
676683
if (p->fixed_anchor_p) {
677684
return position;
@@ -798,8 +805,8 @@ strscan_do_scan(VALUE self, VALUE pattern, int succptr, int getstr, int headonly
798805
}
799806
else {
800807
rb_encoding *enc = rb_enc_check(p->str, pattern);
801-
long pos = rb_memsearch(RSTRING_PTR(pattern), RSTRING_LEN(pattern),
802-
CURPTR(p), S_RESTLEN(p), enc);
808+
rb_len_t pos = rb_memsearch(RSTRING_PTR(pattern), RSTRING_LEN(pattern),
809+
CURPTR(p), S_RESTLEN(p), enc);
803810
if (pos == -1) {
804811
return Qnil;
805812
}
@@ -814,7 +821,7 @@ strscan_do_scan(VALUE self, VALUE pattern, int succptr, int getstr, int headonly
814821
succ(p);
815822
}
816823
{
817-
const long length = last_match_length(p);
824+
const rb_len_t length = last_match_length(p);
818825
if (getstr) {
819826
return extract_beg_len(p, p->prev, length);
820827
}
@@ -1208,7 +1215,7 @@ static VALUE
12081215
strscan_getch(VALUE self)
12091216
{
12101217
struct strscanner *p;
1211-
long len;
1218+
rb_len_t len;
12121219

12131220
GET_SCANNER(self, p);
12141221
CLEAR_MATCH_STATUS(p);
@@ -1322,11 +1329,11 @@ static VALUE
13221329
strscan_peek(VALUE self, VALUE vlen)
13231330
{
13241331
struct strscanner *p;
1325-
long len;
1332+
rb_len_t len;
13261333

13271334
GET_SCANNER(self, p);
13281335

1329-
len = NUM2LONG(vlen);
1336+
len = NUM2LEN(vlen);
13301337
if (EOS_P(p))
13311338
return str_new(p, "", 0);
13321339

@@ -1335,7 +1342,7 @@ strscan_peek(VALUE self, VALUE vlen)
13351342
}
13361343

13371344
static VALUE
1338-
strscan_parse_integer(struct strscanner *p, int base, long len)
1345+
strscan_parse_integer(struct strscanner *p, int base, rb_len_t len)
13391346
{
13401347
VALUE buffer_v, integer;
13411348

@@ -1377,7 +1384,7 @@ static VALUE
13771384
strscan_scan_base10_integer(VALUE self)
13781385
{
13791386
char *ptr;
1380-
long len = 0, remaining_len;
1387+
rb_len_t len = 0, remaining_len;
13811388
struct strscanner *p;
13821389

13831390
GET_SCANNER(self, p);
@@ -1415,7 +1422,7 @@ static VALUE
14151422
strscan_scan_base16_integer(VALUE self)
14161423
{
14171424
char *ptr;
1418-
long len = 0, remaining_len;
1425+
rb_len_t len = 0, remaining_len;
14191426
struct strscanner *p;
14201427

14211428
GET_SCANNER(self, p);
@@ -1693,14 +1700,14 @@ static VALUE
16931700
strscan_matched_size(VALUE self)
16941701
{
16951702
struct strscanner *p;
1696-
long beg, end;
1703+
rb_len_t beg, end;
16971704

16981705
GET_SCANNER(self, p);
16991706
if (! MATCHED_P(p)) return Qnil;
17001707
beg = adjust_register_position(p, p->regs.beg[0]);
17011708
if (beg > S_LEN(p)) return Qnil;
17021709
end = minl(adjust_register_position(p, p->regs.end[0]), S_LEN(p));
1703-
return LONG2NUM(end - beg);
1710+
return LEN2NUM(end - beg);
17041711
}
17051712

17061713
static int
@@ -1725,11 +1732,11 @@ name_to_backref_number(struct re_registers *regs, VALUE regexp, const char* name
17251732
* For Symbol/String specifiers, raises IndexError if the named group
17261733
* does not exist.
17271734
*/
1728-
static long
1735+
static rb_len_t
17291736
resolve_capture_index(struct strscanner *p, VALUE specifier)
17301737
{
17311738
const char *name;
1732-
long i;
1739+
rb_len_t i;
17331740
if (! MATCHED_P(p)) return -1;
17341741
switch (TYPE(specifier)) {
17351742
case T_SYMBOL:
@@ -1741,7 +1748,7 @@ resolve_capture_index(struct strscanner *p, VALUE specifier)
17411748
rb_enc_get(specifier));
17421749
break;
17431750
default:
1744-
i = NUM2LONG(specifier);
1751+
i = NUM2LEN(specifier);
17451752
}
17461753
if (i < 0)
17471754
i += p->regs.num_regs;
@@ -1826,7 +1833,7 @@ static VALUE
18261833
strscan_aref(VALUE self, VALUE idx)
18271834
{
18281835
struct strscanner *p;
1829-
long i;
1836+
rb_len_t i;
18301837

18311838
GET_SCANNER(self, p);
18321839
i = resolve_capture_index(p, idx);
@@ -1872,8 +1879,8 @@ static VALUE
18721879
strscan_integer_at(int argc, VALUE *argv, VALUE self)
18731880
{
18741881
struct strscanner *p;
1875-
long i;
1876-
long beg, end, len;
1882+
rb_len_t i;
1883+
rb_len_t beg, end, len;
18771884
const char *ptr;
18781885
VALUE rb_specifier;
18791886
VALUE rb_base;
@@ -2176,7 +2183,7 @@ static VALUE
21762183
strscan_rest_size(VALUE self)
21772184
{
21782185
struct strscanner *p;
2179-
long i;
2186+
rb_len_t i;
21802187

21812188
GET_SCANNER(self, p);
21822189
if (EOS_P(p)) {
@@ -2240,15 +2247,15 @@ strscan_inspect(VALUE self)
22402247
}
22412248
if (p->curr == 0) {
22422249
b = inspect2(p);
2243-
a = rb_sprintf("#<%"PRIsVALUE" %ld/%ld @ %"PRIsVALUE">",
2250+
a = rb_sprintf("#<%"PRIsVALUE" %"PRIdLEN"/%"PRIdLEN" @ %"PRIsVALUE">",
22442251
rb_obj_class(self),
22452252
p->curr, S_LEN(p),
22462253
b);
22472254
return a;
22482255
}
22492256
a = inspect1(p);
22502257
b = inspect2(p);
2251-
a = rb_sprintf("#<%"PRIsVALUE" %ld/%ld %"PRIsVALUE" @ %"PRIsVALUE">",
2258+
a = rb_sprintf("#<%"PRIsVALUE" %"PRIdLEN"/%"PRIdLEN" %"PRIsVALUE" @ %"PRIsVALUE">",
22522259
rb_obj_class(self),
22532260
p->curr, S_LEN(p),
22542261
a, b);
@@ -2259,7 +2266,7 @@ static VALUE
22592266
inspect1(struct strscanner *p)
22602267
{
22612268
VALUE str;
2262-
long len;
2269+
rb_len_t len;
22632270

22642271
if (p->curr == 0) return rb_str_new2("");
22652272
if (p->curr > INSPECT_LENGTH) {
@@ -2278,7 +2285,7 @@ static VALUE
22782285
inspect2(struct strscanner *p)
22792286
{
22802287
VALUE str;
2281-
long len;
2288+
rb_len_t len;
22822289

22832290
if (EOS_P(p)) return rb_str_new2("");
22842291
len = S_RESTLEN(p);

0 commit comments

Comments
 (0)