[前][次][番号順一覧][スレッド一覧]

ruby-changes:62955

From: Marc-Andre <ko1@a...>
Date: Tue, 15 Sep 2020 03:18:43 +0900 (JST)
Subject: [ruby-changes:62955] 39312cf4d6 (master): Optimize Pathname#relative? / absolute?

https://git.ruby-lang.org/ruby.git/commit/?id=39312cf4d6

From 39312cf4d6c2ab3f07d688ad1a467c8f84b58db0 Mon Sep 17 00:00:00 2001
From: Marc-Andre Lafortune <github@m...>
Date: Wed, 3 Apr 2019 15:22:18 -0400
Subject: Optimize Pathname#relative? / absolute?


diff --git a/ext/pathname/lib/pathname.rb b/ext/pathname/lib/pathname.rb
index 5274286..e6fb902 100644
--- a/ext/pathname/lib/pathname.rb
+++ b/ext/pathname/lib/pathname.rb
@@ -35,6 +35,13 @@ class Pathname https://github.com/ruby/ruby/blob/trunk/ext/pathname/lib/pathname.rb#L35
     SEPARATOR_PAT = /#{Regexp.quote File::SEPARATOR}/
   end
 
+  if File.dirname('A:') == 'A:.' # DOSish drive letter
+    ABSOLUTE_PATH = /\A(?:[A-Za-z]:|#{SEPARATOR_PAT})/o
+  else
+    ABSOLUTE_PATH = /\A#{SEPARATOR_PAT}/o
+  end
+  private_constant :ABSOLUTE_PATH
+
   # :startdoc:
 
   # chop_basename(path) -> [pre-basename, basename] or nil
@@ -222,7 +229,7 @@ class Pathname https://github.com/ruby/ruby/blob/trunk/ext/pathname/lib/pathname.rb#L229
   #   p.absolute?
   #       #=> false
   def absolute?
-    !relative?
+    ABSOLUTE_PATH.match? @path
   end
 
   # The opposite of Pathname#absolute?
@@ -237,11 +244,7 @@ class Pathname https://github.com/ruby/ruby/blob/trunk/ext/pathname/lib/pathname.rb#L244
   #   p.relative?
   #       #=> true
   def relative?
-    path = @path
-    while r = chop_basename(path)
-      path, = r
-    end
-    path == ''
+    !absolute?
   end
 
   #
diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb
index 77958b6..dadcd13 100644
--- a/test/pathname/test_pathname.rb
+++ b/test/pathname/test_pathname.rb
@@ -269,17 +269,17 @@ class TestPathname < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/pathname/test_pathname.rb#L269
     Pathname.new(path).relative?
   end
 
+  defassert(:relative?, true, '')
   defassert(:relative?, false, '/')
   defassert(:relative?, false, '/a')
   defassert(:relative?, false, '/..')
   defassert(:relative?, true, 'a')
   defassert(:relative?, true, 'a/b')
 
-  if DOSISH_DRIVE_LETTER
-    defassert(:relative?, false, 'A:')
-    defassert(:relative?, false, 'A:/')
-    defassert(:relative?, false, 'A:/a')
-  end
+  defassert(:relative?, !DOSISH_DRIVE_LETTER, 'A:.')
+  defassert(:relative?, !DOSISH_DRIVE_LETTER, 'A:')
+  defassert(:relative?, !DOSISH_DRIVE_LETTER, 'A:/')
+  defassert(:relative?, !DOSISH_DRIVE_LETTER, 'A:/a')
 
   if File.dirname('//') == '//'
     defassert(:relative?, false, '//')
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]