Skip to content

Commit

Permalink
Refactor some uses of the blockless String#split (crystal-lang#14001)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil authored Nov 25, 2023
1 parent c7202d4 commit 35eb340
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 15 deletions.
8 changes: 5 additions & 3 deletions src/crystal/system/unix/env.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ module Crystal::System::Env
while environ_ptr
environ_value = environ_ptr.value
if environ_value
key_value = String.new(environ_value).split('=', 2)
key = key_value[0]
value = key_value[1]? || ""
# this does `String.new(environ_value).partition('=')` without an intermediary string
key_value = Slice.new(environ_value, LibC.strlen(environ_value))
split_index = key_value.index!(0x3d_u8) # '='
key = String.new(key_value[0, split_index])
value = String.new(key_value[split_index + 1..])
yield key, value
environ_ptr += 1
else
Expand Down
8 changes: 3 additions & 5 deletions src/http/formdata.cr
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,10 @@ module HTTP::FormData
name = nil

parts = content_disposition.split(';')
type = parts[0]
type = parts.shift?
raise Error.new("Invalid Content-Disposition: not form-data") unless type == "form-data"
(1...parts.size).each do |i|
part = parts[i]

key, value = part.split('=', 2)
parts.each do |part|
key, _, value = part.partition('=')
key = key.strip
value = value.strip
if value[0] == '"'
Expand Down
4 changes: 2 additions & 2 deletions src/openssl/x509/name.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ module OpenSSL::X509
# ```
def self.parse(string : String) : Name
new.tap do |name|
string.split('/').each do |entry|
oid, value = entry.split('=')
string.split('/') do |entry|
oid, _, value = entry.partition('=')
name.add_entry(oid, value)
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/process/executable_path.cr
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Process
end

if path && !has_separator
path.split(PATH_DELIMITER).each do |path_entry|
path.split(PATH_DELIMITER) do |path_entry|
yield Path.new(path_entry, name)
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/semantic_version.cr
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ struct SemanticVersion
# ```
def self.parse(str : String) : self
identifiers = [] of String | Int32
str.split('.').each do |val|
str.split('.') do |val|
if number = val.to_i32?
identifiers << number
else
Expand Down
2 changes: 1 addition & 1 deletion src/spec/context.cr
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ module Spec
puts

message = ex.is_a?(SpecError) ? ex.to_s : ex.inspect_with_backtrace
message.split('\n').each do |line|
message.split('\n') do |line|
print " "
puts Spec.color(line, :error)
end
Expand Down
4 changes: 2 additions & 2 deletions src/spec/dsl.cr
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ module Spec

def self.add_split_filter(filter)
if filter
r, m = filter.split('%').map &.to_i
@@split_filter = SplitFilter.new(remainder: r, quotient: m)
r, _, m = filter.partition('%')
@@split_filter = SplitFilter.new(remainder: r.to_i, quotient: m.to_i)
else
@@split_filter = nil
end
Expand Down

0 comments on commit 35eb340

Please sign in to comment.