From 162588dc064c9841a0b57d95c35221818b771f76 Mon Sep 17 00:00:00 2001 From: shinokaro Date: Wed, 29 May 2024 04:05:17 +0900 Subject: [PATCH] use refine and using --- bin/ocran | 176 +++++++++++++++++++++++++++--------------------------- 1 file changed, 89 insertions(+), 87 deletions(-) diff --git a/bin/ocran b/bin/ocran index 5fc3620..24cbd75 100644 --- a/bin/ocran +++ b/bin/ocran @@ -8,110 +8,112 @@ 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" - 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 + if absolute? != other.absolute? + raise ArgumentError, "both paths must be either absolute or relative" + 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 - 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 = Pathname("path.to/foo") - # pathname.append_to_filename("_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 = Pathname("path.to/foo") + # pathname.append_to_filename("_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/