Skip to content

Commit

Permalink
RefinePathname#subpath? implementation for strict path comparison
Browse files Browse the repository at this point in the history
Updated to use Pathname#relative_path_from for strict path comparison. This allows comparison of relative paths as well.
Additionally, it returns false when comparing an absolute path with a relative path.
  • Loading branch information
shinokaro committed May 31, 2024
1 parent 7b43e9a commit e20ab48
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions bin/ocran
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ module Ocran
alias == eql?
alias === eql?

# Determines if 'src' is contained in 'tgt' (i.e. it is a subpath of
# 'tgt'). Both must be absolute paths and not contain '..'
# Checks if the current path is a sub path of the specified base_directory.
# Both paths must be either absolute paths or relative paths; otherwise, this
# method returns false.
def subpath?(base_directory)
base_directory = Pathname.new(base_directory) unless base_directory.is_a?(Pathname)
src_normalized = to_posix
tgt_normalized = base_directory.to_posix
src_normalized =~ /^#{Regexp.escape tgt_normalized}#{Pathname::SEPARATOR_PAT}/i
s = relative_path_from(base_directory).each_filename.first
s != '.' && s != ".."
rescue ArgumentError
false
end

# Appends the given suffix to the filename, preserving the file extension.
Expand Down

0 comments on commit e20ab48

Please sign in to comment.