Skip to content

Commit

Permalink
use refine and using
Browse files Browse the repository at this point in the history
  • Loading branch information
shinokaro committed May 29, 2024
1 parent 6c5ecb8 commit 0454ada
Showing 1 changed file with 79 additions and 88 deletions.
167 changes: 79 additions & 88 deletions bin/ocran
Original file line number Diff line number Diff line change
Expand Up @@ -8,110 +8,101 @@ module Ocran
# is case sensitive and doesn't handle paths with mixed path
# separators.
module RefinePathname
# Compares two paths for equality based on the case sensitivity of the
# Ruby execution environment's file system.
# If the file system is case-insensitive, it performs a case-insensitive
# comparison. Otherwise, it performs a case-sensitive comparison.
def pathequal(a, b)
if File::FNM_SYSCASE.nonzero?
a.casecmp(b) == 0
else
a == b
refine Pathname do
# Compares two paths for equality based on the case sensitivity of the
# Ruby execution environment's file system.
# If the file system is case-insensitive, it performs a case-insensitive
# comparison. Otherwise, it performs a case-sensitive comparison.
def pathequal(a, b)
if File::FNM_SYSCASE.nonzero?
a.casecmp(b) == 0
else
a == b
end
end
end
private :pathequal
private :pathequal

def to_posix
if File::ALT_SEPARATOR
to_s.tr(File::ALT_SEPARATOR, File::SEPARATOR)
else
to_s
def to_posix
if File::ALT_SEPARATOR
to_s.tr(File::ALT_SEPARATOR, File::SEPARATOR)
else
to_s
end
end
end

# Checks if two Pathname objects are equal, considering the file system's
# case sensitivity and path separators. Returns false if the other object is not
# an Pathname.
# This method enables the use of the `uniq` method on arrays of Pathname objects.
def eql?(other)
return false unless other.is_a?(Pathname)
# Checks if two Pathname objects are equal, considering the file system's
# case sensitivity and path separators. Returns false if the other object is not
# an Pathname.
# This method enables the use of the `uniq` method on arrays of Pathname objects.
def eql?(other)
return false unless other.is_a?(Pathname)

a = to_posix
b = other.to_posix
pathequal(a, b)
end
a = to_posix
b = other.to_posix
pathequal(a, b)
end

alias == eql?
alias === eql?

# The drive_letter? method retrieves the drive letter from the current path
# in a Windows environment. This method returns the drive letter as a string
# only if File::ALT_SEPARATOR is present and the path is absolute.
# It returns nil if the path is not absolute, the environment is not Windows,
# or there is no drive letter present.
def drive_letter?
if File::ALT_SEPARATOR && absolute?
to_s[0, 2]
else
nil
alias == eql?
alias === eql?

# The drive_letter? method retrieves the drive letter from the current path
# in a Windows environment. This method returns the drive letter as a string
# only if File::ALT_SEPARATOR is present and the path is absolute.
# It returns nil if the path is not absolute, the environment is not Windows,
# or there is no drive letter present.
def drive_letter?
if File::ALT_SEPARATOR && absolute?
to_s[0, 2]
else
nil
end
end
end

# Compute the relative path from the 'src' path (directory) to 'tgt'
# (directory or file). Return the absolute path to 'tgt' if it can't
# be reached from 'src'.
def relative_path_from(other)
other = Pathname.new(other) unless other.is_a?(Pathname)
return other if drive_letter?&.casecmp(other.drive_letter?) != 0
# Compute the relative path from the 'src' path (directory) to 'tgt'
# (directory or file). Return the absolute path to 'tgt' if it can't
# be reached from 'src'.
def relative_path_from(other)
other = Pathname.new(other) unless other.is_a?(Pathname)
return other if drive_letter?&.casecmp(other.drive_letter?) != 0

if absolute? != other.absolute?
raise ArgumentError, "both paths must be either absolute or relative"
super
end
a = to_s.split(Pathname::SEPARATOR_PAT)
b = other.to_s.split(Pathname::SEPARATOR_PAT)
while a.first && b.first && pathequal(a.first, b.first)
a.shift
b.shift
end
b.size.times { a.unshift ".." }
Pathname.new(File.join(*a))
end

# Determines if 'src' is contained in 'tgt' (i.e. it is a subpath of
# 'tgt'). Both must be absolute paths and not contain '..'
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
end

# Determines if 'src' is contained in 'tgt' (i.e. it is a subpath of
# 'tgt'). Both must be absolute paths and not contain '..'
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
end

# Appends the given suffix to the filename, preserving the file extension.
# If the filename has an extension, the suffix is inserted before the extension.
# If the filename does not have an extension, the suffix is appended to the end.
# This method handles both directory and file paths correctly.
#
# Examples:
# pathname = Pathname("path.to/foo.tar.gz")
# pathname.append_to_filename("_bar") # => #<Pathname:path.to/foo_bar.tar.gz>
#
# pathname = Pathname("path.to/foo")
# pathname.append_to_filename("_bar") # => #<Pathname:path.to/foo_bar>
#
def append_to_filename(suffix)
sub(/(.*?#{Pathname::SEPARATOR_PAT})?(\.?[^.]+)?(\..*)?\z/, "\\1\\2#{suffix}\\3")
end
# Appends the given suffix to the filename, preserving the file extension.
# If the filename has an extension, the suffix is inserted before the extension.
# If the filename does not have an extension, the suffix is appended to the end.
# This method handles both directory and file paths correctly.
#
# Examples:
# pathname = Pathname("path.to/foo.tar.gz")
# pathname.append_to_filename("_bar") # => #<Pathname:path.to/foo_bar.tar.gz>
#
# pathname = Pathname("path.to/foo")
# pathname.append_to_filename("_bar") # => #<Pathname:path.to/foo_bar>
#
def append_to_filename(suffix)
sub(/(.*?#{Pathname::SEPARATOR_PAT})?(\.?[^.]+)?(\..*)?\z/, "\\1\\2#{suffix}\\3")
end

# Checks if the file's extension matches the expected extension.
# The comparison is case-insensitive.
# Example usage: ocran_pathname.extname?(".exe")
def extname?(expected_ext)
extname.casecmp(expected_ext) == 0
# Checks if the file's extension matches the expected extension.
# The comparison is case-insensitive.
# Example usage: ocran_pathname.extname?(".exe")
def extname?(expected_ext)
extname.casecmp(expected_ext) == 0
end
end
end

::Pathname.prepend(RefinePathname)
using RefinePathname

IGNORE_MODULE_NAMES = /\A(enumerator.so|rational.so|complex.so|fiber.so|thread.rb|ruby2_keywords.rb)\z/

Expand Down

0 comments on commit 0454ada

Please sign in to comment.