Skip to content

Commit 35eb340

Browse files
authored
Refactor some uses of the blockless String#split (#14001)
1 parent c7202d4 commit 35eb340

File tree

7 files changed

+15
-15
lines changed

7 files changed

+15
-15
lines changed

src/crystal/system/unix/env.cr

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ module Crystal::System::Env
4242
while environ_ptr
4343
environ_value = environ_ptr.value
4444
if environ_value
45-
key_value = String.new(environ_value).split('=', 2)
46-
key = key_value[0]
47-
value = key_value[1]? || ""
45+
# this does `String.new(environ_value).partition('=')` without an intermediary string
46+
key_value = Slice.new(environ_value, LibC.strlen(environ_value))
47+
split_index = key_value.index!(0x3d_u8) # '='
48+
key = String.new(key_value[0, split_index])
49+
value = String.new(key_value[split_index + 1..])
4850
yield key, value
4951
environ_ptr += 1
5052
else

src/http/formdata.cr

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,10 @@ module HTTP::FormData
139139
name = nil
140140

141141
parts = content_disposition.split(';')
142-
type = parts[0]
142+
type = parts.shift?
143143
raise Error.new("Invalid Content-Disposition: not form-data") unless type == "form-data"
144-
(1...parts.size).each do |i|
145-
part = parts[i]
146-
147-
key, value = part.split('=', 2)
144+
parts.each do |part|
145+
key, _, value = part.partition('=')
148146
key = key.strip
149147
value = value.strip
150148
if value[0] == '"'

src/openssl/x509/name.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ module OpenSSL::X509
1414
# ```
1515
def self.parse(string : String) : Name
1616
new.tap do |name|
17-
string.split('/').each do |entry|
18-
oid, value = entry.split('=')
17+
string.split('/') do |entry|
18+
oid, _, value = entry.partition('=')
1919
name.add_entry(oid, value)
2020
end
2121
end

src/process/executable_path.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class Process
9595
end
9696

9797
if path && !has_separator
98-
path.split(PATH_DELIMITER).each do |path_entry|
98+
path.split(PATH_DELIMITER) do |path_entry|
9999
yield Path.new(path_entry, name)
100100
end
101101
end

src/semantic_version.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ struct SemanticVersion
178178
# ```
179179
def self.parse(str : String) : self
180180
identifiers = [] of String | Int32
181-
str.split('.').each do |val|
181+
str.split('.') do |val|
182182
if number = val.to_i32?
183183
identifiers << number
184184
else

src/spec/context.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ module Spec
210210
puts
211211

212212
message = ex.is_a?(SpecError) ? ex.to_s : ex.inspect_with_backtrace
213-
message.split('\n').each do |line|
213+
message.split('\n') do |line|
214214
print " "
215215
puts Spec.color(line, :error)
216216
end

src/spec/dsl.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ module Spec
9696

9797
def self.add_split_filter(filter)
9898
if filter
99-
r, m = filter.split('%').map &.to_i
100-
@@split_filter = SplitFilter.new(remainder: r, quotient: m)
99+
r, _, m = filter.partition('%')
100+
@@split_filter = SplitFilter.new(remainder: r.to_i, quotient: m.to_i)
101101
else
102102
@@split_filter = nil
103103
end

0 commit comments

Comments
 (0)