Skip to content

Commit

Permalink
adding sort=true keyword to readdir() according to JuliaLang#24626 (J…
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanKarpinski committed Oct 14, 2019
1 parent 06cdb91 commit 73cfacb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ New library functions
* The `tempname` function now takes an optional `parent::AbstractString` argument to give it a directory in which to attempt to produce a temporary path name ([#33090]).
* The `tempname` function now takes a `cleanup::Bool` keyword argument defaulting to `true`, which causes the process to try to ensure that any file or directory at the path returned by `tempname` is deleted upon process exit ([#33090]).
* The `readdir` function now takes a `join::Bool` keyword argument defaulting to `false`, which when set causes `readdir` to join its directory argument with each listed name ([#33113]).
* `readdir` output is now guaranteed to be sorted. The `sort` keyword allows opting out of sorting to get names in OS-native order ([#33542]).
* The new `only(x)` function returns the one-and-only element of a collection `x`, and throws an `ArgumentError` if `x` contains zero or multiple elements. ([#33129])
* `takewhile` and `dropwhile` have been added to the Iterators submodule ([#33437]).

Expand Down
19 changes: 15 additions & 4 deletions base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -678,16 +678,23 @@ struct uv_dirent_t
end

"""
readdir(dir::AbstractString=pwd(); join::Bool=false) -> Vector{String}
readdir(dir::AbstractString=pwd();
join::Bool = false,
sort::Bool = true,
) -> Vector{String}
Return the names in the directory `dir` or the current working directory if not
given. When `join` is false, `readdir` returns just the names in the directory
as is; when `join` is true, it returns `joinpath(dir, name)` for each `name` so
that the returned strings are full paths. If you want to get absolute paths
back, call `readdir` with an absolute directory path and `join` set to true.
By default, `readdir` sorts the list of names it returns. If you want to skip
sorting the names and get them in the order that the file system lists them,
you can use `readir(dir, sort=false)` to opt out of sorting.
!!! compat "Julia 1.4"
The `join` keyword argument requires at least Julia 1.4.
The `join` and `sort` keyword arguments require at least Julia 1.4.
# Examples
```julia-repl
Expand Down Expand Up @@ -744,7 +751,7 @@ julia> readdir(abspath("base"), join=true)
"/home/JuliaUser/dev/julia/base/weakkeydict.jl"
```
"""
function readdir(dir::AbstractString; join::Bool=false)
function readdir(dir::AbstractString; join::Bool=false, sort::Bool=true)
# Allocate space for uv_fs_t struct
uv_readdir_req = zeros(UInt8, ccall(:jl_sizeof_uv_fs_t, Int32, ()))

Expand All @@ -764,9 +771,13 @@ function readdir(dir::AbstractString; join::Bool=false)
# Clean up the request string
ccall(:uv_fs_req_cleanup, Cvoid, (Ptr{UInt8},), uv_readdir_req)

# sort entries unless opted out
sort && sort!(entries)

return entries
end
readdir(; join::Bool=false) = readdir(join ? pwd() : ".", join=join)
readdir(; join::Bool=false, sort::Bool=true) =
readdir(join ? pwd() : ".", join=join, sort=sort)

"""
walkdir(dir; topdown=true, follow_symlinks=false, onerror=throw)
Expand Down
14 changes: 14 additions & 0 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,20 @@ cd(dirwalk) do
end
rm(dirwalk, recursive=true)

###################
# readdir #
###################
@testset "readdir is sorted" begin
mktempdir() do dir
cd(dir) do
for k in 1:10
touch(randstring())
end
@test issorted(readdir())
end
end
end

############
# Clean up #
############
Expand Down

0 comments on commit 73cfacb

Please sign in to comment.