summaryrefslogtreecommitdiff
path: root/ext/pathname
diff options
context:
space:
mode:
authorMarc-Andre Lafortune <github@marc-andre.ca>2019-04-03 15:22:18 -0400
committerMarc-André Lafortune <github@marc-andre.ca>2020-09-14 14:18:23 -0400
commit39312cf4d6c2ab3f07d688ad1a467c8f84b58db0 (patch)
treed9d880a04c60437451c7aad7dead0945d832eb70 /ext/pathname
parent28e60b0045b5732bca11012d81a5223001faa6b2 (diff)
Optimize Pathname#relative? / absolute?
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/2107
Diffstat (limited to 'ext/pathname')
-rw-r--r--ext/pathname/lib/pathname.rb15
1 files changed, 9 insertions, 6 deletions
diff --git a/ext/pathname/lib/pathname.rb b/ext/pathname/lib/pathname.rb
index 5274286358..e6fb90277d 100644
--- a/ext/pathname/lib/pathname.rb
+++ b/ext/pathname/lib/pathname.rb
@@ -35,6 +35,13 @@ class Pathname
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
# p.absolute?
# #=> false
def absolute?
- !relative?
+ ABSOLUTE_PATH.match? @path
end
# The opposite of Pathname#absolute?
@@ -237,11 +244,7 @@ class Pathname
# p.relative?
# #=> true
def relative?
- path = @path
- while r = chop_basename(path)
- path, = r
- end
- path == ''
+ !absolute?
end
#