Skip to content

Commit

Permalink
Use String/Nil#presence in a few places throughout stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija committed Nov 22, 2019
1 parent ef7fcd7 commit 401e8c3
Show file tree
Hide file tree
Showing 12 changed files with 14 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/compiler/crystal/compiler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ module Crystal
object_name = %(%*)
end

if (link_flags = @link_flags) && !link_flags.empty?
if link_flags = @link_flags.presence
link_flags = "/link #{link_flags}"
end

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/call_error.cr
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ class Crystal::Call
str << '*' if a_def.splat_index == i

if arg.external_name != arg.name
str << (arg.external_name.empty? ? '_' : arg.external_name)
str << (arg.external_name.presence || '_')
str << ' '
end

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/tools/doc/macro.cr
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Crystal::Doc::Macro

def arg_to_s(arg : Arg, io : IO) : Nil
if arg.external_name != arg.name
name = arg.external_name.empty? ? "_" : arg.external_name
name = arg.external_name.presence || "_"
if Symbol.needs_quotes? name
HTML.escape name.inspect, io
else
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/tools/doc/method.cr
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class Crystal::Doc::Method

def arg_to_html(arg : Arg, io, links = true)
if arg.external_name != arg.name
name = arg.external_name.empty? ? "_" : arg.external_name
name = arg.external_name.presence || "_"
if Symbol.needs_quotes? name
HTML.escape name.inspect, io
else
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/tools/doc/type.cr
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ class Crystal::Doc::Type
methods.find { |method| method.name == name && method.args.size == args_size }
else
methods = methods.select { |method| method.name == name }
(methods.find { |method| method.args.empty? }) || methods.first?
methods.find(&.args.empty?) || methods.first?
end
end

Expand Down
4 changes: 2 additions & 2 deletions src/http/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -828,8 +828,8 @@ class HTTP::Client
{% end %}

protected def self.validate_host(uri)
host = uri.host
return host if host && !host.empty?
host = uri.host.presence
return host if host

raise ArgumentError.new %(Request URI must have host (URI is: #{uri}))
end
Expand Down
2 changes: 1 addition & 1 deletion src/http/request.cr
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class HTTP::Request

# Returns the request's path component.
def path
(path = uri.path).empty? ? "/" : path
uri.path.presence || "/"
end

# Sets request's path component.
Expand Down
6 changes: 1 addition & 5 deletions src/json/builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,7 @@ class JSON::Builder

# Sets the indent *string*.
def indent=(string : String)
if string.empty?
@indent = nil
else
@indent = string
end
@indent = string.presence
end

# Sets the indent *level* (number of spaces).
Expand Down
2 changes: 1 addition & 1 deletion src/llvm.cr
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ module LLVM

# Fix LLVM not replacing empty triple parts with "unknown"
# This was fixed in LLVM 8
normalized = normalized.split('-').map { |c| c.empty? ? "unknown" : c }.join('-')
normalized = normalized.split('-').map { |c| c.presence || "unknown" }.join('-')

normalized
end
Expand Down
6 changes: 1 addition & 5 deletions src/mime/multipart.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ module MIME::Multipart
type = MIME::MediaType.parse?(content_type)

if type && type.type == "multipart"
boundary = type["boundary"]?

if boundary && !boundary.empty?
boundary
end
type["boundary"]?.presence
end
end

Expand Down
3 changes: 1 addition & 2 deletions src/oauth/signature.cr
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ struct OAuth::Signature
str << "%3A"
str << port
end
uri_path = request.path || "/"
uri_path = "/" if uri_path.empty?
uri_path = request.path.presence || "/"
URI.encode_www_form(uri_path, str, space_to_plus: false)
str << '&'
str << params
Expand Down
4 changes: 2 additions & 2 deletions src/socket/address.cr
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class Socket
# ```
def self.parse(uri : URI) : IPAddress
host = uri.host
raise Socket::Error.new("Invalid IP address: missing host") if !host || host.empty?
raise Socket::Error.new("Invalid IP address: missing host") unless host.presence

port = uri.port || raise Socket::Error.new("Invalid IP address: missing port")

Expand Down Expand Up @@ -341,7 +341,7 @@ class Socket
if port = uri.port
io << ':' << port
end
if (path = uri.path) && !path.empty?
if path = uri.path.presence
io << path
end
end
Expand Down

0 comments on commit 401e8c3

Please sign in to comment.