Skip to content

Commit aa2a428

Browse files
committed
Refactor str_substr and str_subseq
This commit extracts common code between str_substr and rb_str_subseq into a function called str_subseq. This commit also applies optimizations in commit 2e88bca to rb_str_subseq.
1 parent 830b5b5 commit aa2a428

File tree

1 file changed

+21
-37
lines changed

1 file changed

+21
-37
lines changed

string.c

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2797,26 +2797,32 @@ rb_str_sublen(VALUE str, long pos)
27972797
}
27982798
}
27992799

2800-
VALUE
2801-
rb_str_subseq(VALUE str, long beg, long len)
2800+
static VALUE
2801+
str_subseq(VALUE str, long beg, long len)
28022802
{
28032803
VALUE str2;
28042804

28052805
if (!STR_EMBEDDABLE_P(len, TERM_LEN(str)) &&
2806-
SHARABLE_SUBSTRING_P(beg, len, RSTRING_LEN(str))) {
2807-
long olen;
2808-
str2 = rb_str_new_shared(rb_str_new_frozen_String(str));
2806+
SHARABLE_SUBSTRING_P(beg, len, RSTRING_LEN(str))) {
2807+
str2 = rb_str_new_shared(str);
28092808
RSTRING(str2)->as.heap.ptr += beg;
2810-
olen = RSTRING(str2)->as.heap.len;
2811-
if (olen > len) RSTRING(str2)->as.heap.len = len;
2809+
if (RSTRING(str2)->as.heap.len > len) {
2810+
RSTRING(str2)->as.heap.len = len;
2811+
}
28122812
}
28132813
else {
2814-
str2 = rb_str_new(RSTRING_PTR(str)+beg, len);
2814+
str2 = rb_str_new(RSTRING_PTR(str) + beg, len);
28152815
RB_GC_GUARD(str);
28162816
}
28172817

2818-
rb_enc_cr_str_copy_for_substr(str2, str);
2818+
return str2;
2819+
}
28192820

2821+
VALUE
2822+
rb_str_subseq(VALUE str, long beg, long len)
2823+
{
2824+
VALUE str2 = str_subseq(str, beg, len);
2825+
rb_enc_cr_str_copy_for_substr(str2, str);
28202826
return str2;
28212827
}
28222828

@@ -2916,25 +2922,15 @@ rb_str_substr(VALUE str, long beg, long len)
29162922
static VALUE
29172923
str_substr(VALUE str, long beg, long len, int empty)
29182924
{
2919-
VALUE str2;
29202925
char *p = rb_str_subpos(str, beg, &len);
29212926

29222927
if (!p) return Qnil;
2923-
if (!STR_EMBEDDABLE_P(len, TERM_LEN(str)) &&
2924-
SHARABLE_SUBSTRING_P(p, len, RSTRING_END(str))) {
2925-
long ofs = p - RSTRING_PTR(str);
2926-
str2 = str_new_shared(rb_cString, str);
2927-
RSTRING(str2)->as.heap.ptr += ofs;
2928-
RSTRING(str2)->as.heap.len = len;
2929-
ENC_CODERANGE_CLEAR(str2);
2930-
}
2931-
else {
2932-
if (!len && !empty) return Qnil;
2933-
str2 = rb_str_new(p, len);
2934-
RB_GC_GUARD(str);
2935-
}
2936-
rb_enc_cr_str_copy_for_substr(str2, str);
2928+
if (!len && !empty) return Qnil;
29372929

2930+
beg = p - RSTRING_PTR(str);
2931+
2932+
VALUE str2 = str_subseq(str, beg, len);
2933+
rb_enc_cr_str_copy_for_substr(str2, str);
29382934
return str2;
29392935
}
29402936

@@ -6141,9 +6137,7 @@ rb_str_setbyte(VALUE str, VALUE index, VALUE value)
61416137
static VALUE
61426138
str_byte_substr(VALUE str, long beg, long len, int empty)
61436139
{
6144-
char *p, *s = RSTRING_PTR(str);
61456140
long n = RSTRING_LEN(str);
6146-
VALUE str2;
61476141

61486142
if (beg > n || len < 0) return Qnil;
61496143
if (beg < 0) {
@@ -6155,19 +6149,9 @@ str_byte_substr(VALUE str, long beg, long len, int empty)
61556149
if (len <= 0) {
61566150
if (!empty) return Qnil;
61576151
len = 0;
6158-
p = 0;
61596152
}
6160-
else
6161-
p = s + beg;
61626153

6163-
if (!STR_EMBEDDABLE_P(len, TERM_LEN(str)) && SHARABLE_SUBSTRING_P(beg, len, n)) {
6164-
str2 = str_new_shared(rb_cString, str);
6165-
RSTRING(str2)->as.heap.ptr += beg;
6166-
RSTRING(str2)->as.heap.len = len;
6167-
}
6168-
else {
6169-
str2 = rb_str_new(p, len);
6170-
}
6154+
VALUE str2 = str_subseq(str, beg, len);
61716155

61726156
str_enc_copy(str2, str);
61736157

0 commit comments

Comments
 (0)