@@ -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,17 +72,17 @@ 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
@@ -95,24 +95,24 @@ def valid_encoding?(string)
95
95
raise ArgumentError , "invalid byte sequence in #{ string . encoding . name } " unless string . valid_encoding?
96
96
end
97
97
98
- def split_type_chars ( string , limit , block )
98
+ def split_type_chars ( string , limit , callable )
99
99
if limit > 0
100
100
last = string . size > ( limit - 1 ) ? string [ ( limit - 1 ) ..-1 ] : empty_string ( string )
101
101
102
102
string . each_char . each_with_index do |char , index |
103
103
break if index == limit - 1
104
- block . call ( char )
104
+ callable . call ( char )
105
105
end
106
106
107
- block . call ( last )
107
+ callable . call ( last )
108
108
else
109
- string . each_char ( &block )
109
+ string . each_char ( &callable )
110
110
111
- block . call ( empty_string ( string ) ) if tail_empty? ( limit )
111
+ callable . call ( empty_string ( string ) ) if tail_empty? ( limit )
112
112
end
113
113
end
114
114
115
- def split_type_string ( string , pattern , limit , block )
115
+ def split_type_string ( string , pattern , limit , callable )
116
116
pos = 0
117
117
empty_count = 0
118
118
limited = limit > 0
@@ -128,24 +128,24 @@ def split_type_string(string, pattern, limit, block)
128
128
break unless nxt
129
129
130
130
match_size = nxt - pos
131
- empty_count = add_substring ( string , ret , string . byteslice ( pos , match_size ) , empty_count , block )
131
+ empty_count = add_substring ( string , ret , string . byteslice ( pos , match_size ) , empty_count , callable )
132
132
133
133
pos = nxt + pat_size
134
134
count += 1
135
135
end
136
136
137
137
# 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 )
138
+ empty_count = add_substring ( string , ret , string . byteslice ( pos , str_size - pos ) , empty_count , callable )
139
139
140
140
if tail_empty? ( limit )
141
- add_empty ( string , ret , empty_count , block )
141
+ add_empty ( string , ret , empty_count , callable )
142
142
end
143
143
end
144
144
145
- def split_type_regexp ( string , pattern , limit , block )
145
+ def split_type_regexp ( string , pattern , limit , callable )
146
146
# Handle // as a special case.
147
147
if pattern . source . empty?
148
- return split_type_chars ( string , limit , block )
148
+ return split_type_chars ( string , limit , callable )
149
149
end
150
150
151
151
start = 0
@@ -164,12 +164,12 @@ def split_type_regexp(string, pattern, limit, block)
164
164
165
165
unless collapsed && ( Primitive . match_data_byte_begin ( match , 0 ) == last_match_end )
166
166
substring = Truffle ::RegexpOperations . pre_match_from ( match , last_match_end )
167
- empty_count = add_substring ( string , ret , substring , empty_count , block )
167
+ empty_count = add_substring ( string , ret , substring , empty_count , callable )
168
168
169
169
# length > 1 means there are captures
170
170
if match . length > 1
171
171
match . captures . compact . each do |capture |
172
- empty_count = add_substring ( string , ret , capture , empty_count , block )
172
+ empty_count = add_substring ( string , ret , capture , empty_count , callable )
173
173
end
174
174
end
175
175
@@ -186,31 +186,31 @@ def split_type_regexp(string, pattern, limit, block)
186
186
end
187
187
188
188
if last_match
189
- empty_count = add_substring ( string , ret , last_match . post_match , empty_count , block )
189
+ empty_count = add_substring ( string , ret , last_match . post_match , empty_count , callable )
190
190
elsif ret . empty?
191
- empty_count = add_substring ( string , ret , string . dup , empty_count , block )
191
+ empty_count = add_substring ( string , ret , string . dup , empty_count , callable )
192
192
end
193
193
194
194
if tail_empty? ( limit )
195
- add_empty ( string , ret , empty_count , block )
195
+ add_empty ( string , ret , empty_count , callable )
196
196
end
197
197
198
- block ? string : ret
198
+ callable ? string : ret
199
199
end
200
200
201
201
202
- def add_substring ( string , array , substring , empty_count , block )
202
+ def add_substring ( string , array , substring , empty_count , callable )
203
203
return empty_count + 1 if substring . length == 0 # remember another one empty match
204
204
205
- add_empty ( string , array , empty_count , block )
205
+ add_empty ( string , array , empty_count , callable )
206
206
207
- block . call ( substring )
207
+ callable . call ( substring )
208
208
209
209
0 # always release all empties when we get non empty substring
210
210
end
211
211
212
- def add_empty ( string , array , count , block )
213
- count . times { block . call ( empty_string ( string ) ) }
212
+ def add_empty ( string , array , count , callable )
213
+ count . times { callable . call ( empty_string ( string ) ) }
214
214
end
215
215
216
216
def empty_string ( original )
0 commit comments