@@ -44,7 +44,7 @@ def split(string, pattern, limit, &orig_block)
44
44
# To simplify the code, we present a single block
45
45
# that either calls user (origin) block or adds a substring to the resulting array
46
46
# See motivation: https://github.com/oracle/truffleruby/pull/2052#issuecomment-663449395
47
- block = orig_block || result . method ( :<< ) . to_proc
47
+ callable = orig_block || result . method ( :<< )
48
48
49
49
return ( orig_block ? string : result ) if string . empty?
50
50
@@ -57,7 +57,7 @@ def split(string, pattern, limit, &orig_block)
57
57
if limit == 1
58
58
dup_string = string . dup
59
59
60
- block . call ( dup_string )
60
+ callable . call ( dup_string )
61
61
return orig_block ? dup_string : result
62
62
end
63
63
@@ -72,47 +72,48 @@ def split(string, pattern, limit, &orig_block)
72
72
# See motivation: https://github.com/oracle/truffleruby/pull/2052#issuecomment-663494235
73
73
return Primitive . string_awk_split string , awk_limit , orig_block
74
74
elsif Primitive . is_a? ( pattern , Regexp )
75
- split_type_regexp ( string , pattern , limit , block )
75
+ split_type_regexp ( string , pattern , limit , callable )
76
76
else
77
77
pattern = StringValue ( pattern )
78
78
79
79
valid_encoding? ( string )
80
80
valid_encoding? ( pattern )
81
81
82
82
if pattern . empty?
83
- split_type_chars ( string , limit , block )
83
+ split_type_chars ( string , limit , callable )
84
84
else
85
- split_type_string ( string , pattern , limit , block )
85
+ split_type_string ( string , pattern , limit , callable )
86
86
end
87
87
end
88
88
89
89
orig_block ? string : result
90
90
end
91
+ Truffle ::Graal . always_split ( instance_method ( :split ) )
91
92
92
93
private
93
94
94
95
def valid_encoding? ( string )
95
96
raise ArgumentError , "invalid byte sequence in #{ string . encoding . name } " unless string . valid_encoding?
96
97
end
97
98
98
- def split_type_chars ( string , limit , block )
99
+ def split_type_chars ( string , limit , callable )
99
100
if limit > 0
100
101
last = string . size > ( limit - 1 ) ? string [ ( limit - 1 ) ..-1 ] : empty_string ( string )
101
102
102
103
string . each_char . each_with_index do |char , index |
103
104
break if index == limit - 1
104
- block . call ( char )
105
+ callable . call ( char )
105
106
end
106
107
107
- block . call ( last )
108
+ callable . call ( last )
108
109
else
109
- string . each_char ( & block )
110
+ string . each_char { | c | callable . call ( c ) }
110
111
111
- block . call ( empty_string ( string ) ) if tail_empty? ( limit )
112
+ callable . call ( empty_string ( string ) ) if tail_empty? ( limit )
112
113
end
113
114
end
114
115
115
- def split_type_string ( string , pattern , limit , block )
116
+ def split_type_string ( string , pattern , limit , callable )
116
117
pos = 0
117
118
empty_count = 0
118
119
limited = limit > 0
@@ -128,24 +129,24 @@ def split_type_string(string, pattern, limit, block)
128
129
break unless nxt
129
130
130
131
match_size = nxt - pos
131
- empty_count = add_substring ( string , ret , string . byteslice ( pos , match_size ) , empty_count , block )
132
+ empty_count = add_substring ( string , ret , string . byteslice ( pos , match_size ) , empty_count , callable )
132
133
133
134
pos = nxt + pat_size
134
135
count += 1
135
136
end
136
137
137
138
# No more separators, but we need to grab the last part still.
138
- empty_count = add_substring ( string , ret , string . byteslice ( pos , str_size - pos ) , empty_count , block )
139
+ empty_count = add_substring ( string , ret , string . byteslice ( pos , str_size - pos ) , empty_count , callable )
139
140
140
141
if tail_empty? ( limit )
141
- add_empty ( string , ret , empty_count , block )
142
+ add_empty ( string , ret , empty_count , callable )
142
143
end
143
144
end
144
145
145
- def split_type_regexp ( string , pattern , limit , block )
146
+ def split_type_regexp ( string , pattern , limit , callable )
146
147
# Handle // as a special case.
147
148
if pattern . source . empty?
148
- return split_type_chars ( string , limit , block )
149
+ return split_type_chars ( string , limit , callable )
149
150
end
150
151
151
152
start = 0
@@ -164,12 +165,12 @@ def split_type_regexp(string, pattern, limit, block)
164
165
165
166
unless collapsed && ( Primitive . match_data_byte_begin ( match , 0 ) == last_match_end )
166
167
substring = Truffle ::RegexpOperations . pre_match_from ( match , last_match_end )
167
- empty_count = add_substring ( string , ret , substring , empty_count , block )
168
+ empty_count = add_substring ( string , ret , substring , empty_count , callable )
168
169
169
170
# length > 1 means there are captures
170
171
if match . length > 1
171
172
match . captures . compact . each do |capture |
172
- empty_count = add_substring ( string , ret , capture , empty_count , block )
173
+ empty_count = add_substring ( string , ret , capture , empty_count , callable )
173
174
end
174
175
end
175
176
@@ -186,31 +187,31 @@ def split_type_regexp(string, pattern, limit, block)
186
187
end
187
188
188
189
if last_match
189
- empty_count = add_substring ( string , ret , last_match . post_match , empty_count , block )
190
+ empty_count = add_substring ( string , ret , last_match . post_match , empty_count , callable )
190
191
elsif ret . empty?
191
- empty_count = add_substring ( string , ret , string . dup , empty_count , block )
192
+ empty_count = add_substring ( string , ret , string . dup , empty_count , callable )
192
193
end
193
194
194
195
if tail_empty? ( limit )
195
- add_empty ( string , ret , empty_count , block )
196
+ add_empty ( string , ret , empty_count , callable )
196
197
end
197
198
198
- block ? string : ret
199
+ callable ? string : ret
199
200
end
200
201
201
202
202
- def add_substring ( string , array , substring , empty_count , block )
203
+ def add_substring ( string , array , substring , empty_count , callable )
203
204
return empty_count + 1 if substring . length == 0 # remember another one empty match
204
205
205
- add_empty ( string , array , empty_count , block )
206
+ add_empty ( string , array , empty_count , callable )
206
207
207
- block . call ( substring )
208
+ callable . call ( substring )
208
209
209
210
0 # always release all empties when we get non empty substring
210
211
end
211
212
212
- def add_empty ( string , array , count , block )
213
- count . times { block . call ( empty_string ( string ) ) }
213
+ def add_empty ( string , array , count , callable )
214
+ count . times { callable . call ( empty_string ( string ) ) }
214
215
end
215
216
216
217
def empty_string ( original )
0 commit comments