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

Deprecate Path#dirname #7762

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion spec/std/path_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ describe Path do
end

describe "#dirname" do
assert_paths_raw("/Users/foo/bar.cr", "/Users/foo", &.dirname)
assert_paths("/Users/foo/bar.cr", "/Users/foo", &.dirname)
end

describe "#basename" do
Expand Down
2 changes: 1 addition & 1 deletion src/file.cr
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ class File < IO::FileDescriptor
# File.dirname("/foo/bar/file.cr") # => "/foo/bar"
# ```
def self.dirname(path) : String
Path.new(path).dirname
Path.new(path).dirname.to_s
end

# Returns the last component of the given *path*.
Expand Down
12 changes: 6 additions & 6 deletions src/path.cr
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ struct Path
# Returns all components of this path except the last one.
#
# ```
# Path["/foo/bar/file.cr"].dirname # => "/foo/bar"
# Path["/foo/bar/file.cr"].dirname # => Path["/foo/bar]"
# ```
def dirname : String
def dirname : Path
reader = Char::Reader.new(at_end: @name)
separators = self.separators

Expand All @@ -199,22 +199,22 @@ struct Path
current = reader.current_char

if separators.includes?(current)
return current.to_s
return Path.new current.to_s
else
# skip windows here for next condition regarding anchor
if windows? && reader.has_next? && reader.peek_next_char == ':'
reader.next_char
else
return "."
return Path.new "."
end
end
end

if windows? && reader.current_char == ':' && reader.pos == 1 && (anchor = self.anchor)
return anchor.to_s
return anchor
end

@name.byte_slice(0, reader.pos + 1)
Path.new @name.byte_slice(0, reader.pos + 1)
end

# Returns the parent path of this path.
Expand Down