Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Path#empty? #9137

Merged
merged 4 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions spec/std/path_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -727,4 +727,14 @@ describe Path do
assert_paths_raw("foo/bar", Path.posix("foo/bar"), &.to_posix)
assert_paths_raw("C:\\foo\\bar", Path.posix("C:\\foo\\bar"), Path.posix("C:/foo/bar"), &.to_posix)
end

describe "#empty?" do
assert_paths_raw("", true, &.empty?)
assert_paths_raw(".", true, &.empty?)
assert_paths_raw("./", false, &.empty?)
assert_paths_raw("a", false, &.empty?)
assert_paths_raw("a/b", false, &.empty?)
assert_paths_raw("..", false, &.empty?)
assert_paths_raw("/", false, &.empty?)
end
end
24 changes: 19 additions & 5 deletions src/path.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
# A `Path` can represent a root, a root and a sequence of names, or simply one or
# more name elements.
# A `Path` is considered to be an empty path if it consists solely of one name
# element that is empty. Accessing a file using an empty path is equivalent
# to accessing the default directory of the process.
# element that is empty or equal to `"."`. Accessing a file using an empty path
# is equivalent to accessing the default directory of the process.
#
# # Examples
#
Expand Down Expand Up @@ -230,7 +230,7 @@ struct Path

# Returns the parent path of this path.
#
# If the path is empty or `"."`, it returns `"."`. If the path is rooted
# If the path is empty, it returns `"."`. If the path is rooted
# and in the top-most hierarchy, the root path is returned.
#
# ```
Expand Down Expand Up @@ -267,7 +267,7 @@ struct Path
# # Path["foo/bar"]
# ```
def each_parent(&block : Path ->)
return if @name.empty? || @name == "."
return if empty?

first_char = @name.char_at(0)
unless separators.includes?(first_char) || (first_char == '.' && separators.includes?(@name.byte_at?(1).try &.unsafe_chr)) || (windows? && (windows_drive? || unc_share?))
Expand Down Expand Up @@ -398,7 +398,7 @@ struct Path
#
# See also Rob Pike: *[Lexical File Names in Plan 9 or Getting Dot-Dot Right](https://9p.io/sys/doc/lexnames.html)*
def normalize(*, remove_final_separator : Bool = true) : Path
return new_instance "." if @name.empty?
return new_instance "." if empty?

drive, root = drive_and_root
reader = Char::Reader.new(@name)
Expand Down Expand Up @@ -869,6 +869,20 @@ struct Path
end
end

# Returns `true` if this path is empty.
#
# `Path[""]` and `Path["."]` are considered empty.
#
# ```
# Path[""].empty? # => true
# Path["."].empty? # => true
# Path["a"].empty? # => false
# Path["/"].empty? # => false
# ```
def empty?
@name.empty? || @name == "."
end

# Compares this path to *other*.
#
# The comparison is performed strictly lexically: `foo` and `./foo` are *not*
Expand Down