From 8ec62f5a68561c9aae182a97f0efc46801500ffc Mon Sep 17 00:00:00 2001 From: shinokaro Date: Mon, 3 Jun 2024 00:23:49 +0900 Subject: [PATCH] Refactor path normalization logic in RefinePathname module - Extract path separator normalization logic from to_posix method to a private constant NORMALIZE_FILE_SEPARATOR. - Apply NORMALIZE_FILE_SEPARATOR in the to_posix method and eql? method for consistent path normalization. - Ensure NORMALIZE_FILE_SEPARATOR is private to the RefinePathname module to encapsulate the implementation details. --- bin/ocran | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/bin/ocran b/bin/ocran index 7245f58..ce6d45b 100644 --- a/bin/ocran +++ b/bin/ocran @@ -7,6 +7,13 @@ module Ocran # The Pathname class in Ruby is modified to handle mixed path separators and # to be case-insensitive. module RefinePathname + NORMALIZE_FILE_SEPARATOR = if File::ALT_SEPARATOR + proc { |s| s.tr(File::ALT_SEPARATOR, File::SEPARATOR) } + else + proc { |s| s } + end + private_constant :NORMALIZE_FILE_SEPARATOR + # 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 @@ -21,11 +28,7 @@ module Ocran private :pathequal def to_posix - if File::ALT_SEPARATOR - to_s.tr(File::ALT_SEPARATOR, File::SEPARATOR) - else - to_s - end + NORMALIZE_FILE_SEPARATOR[to_s] end # Checks if two Pathname objects are equal, considering the file system's @@ -35,8 +38,8 @@ module Ocran def eql?(other) return false unless other.is_a?(Pathname) - a = to_posix - b = other.to_posix + a = NORMALIZE_FILE_SEPARATOR[to_s] + b = NORMALIZE_FILE_SEPARATOR[other.to_s] pathequal(a, b) end