Skip to content

Instantly share code, notes, and snippets.

@flavio
Created July 26, 2012 10:12
Show Gist options
  • Save flavio/3181352 to your computer and use it in GitHub Desktop.
Save flavio/3181352 to your computer and use it in GitHub Desktop.
diff --git a/kernel/bootstrap/rubinius18.rb b/kernel/bootstrap/rubinius18.rb
index 10fe27e..7433eab 100644
--- a/kernel/bootstrap/rubinius18.rb
+++ b/kernel/bootstrap/rubinius18.rb
@@ -13,4 +13,8 @@ module Rubinius
def self.binary_string(string)
string
end
+
+ def self.external_encoding_string(string)
+ string
+ end
end
diff --git a/kernel/bootstrap/rubinius19.rb b/kernel/bootstrap/rubinius19.rb
index 8279ed7..35af72c 100644
--- a/kernel/bootstrap/rubinius19.rb
+++ b/kernel/bootstrap/rubinius19.rb
@@ -12,4 +12,8 @@ module Rubinius
def self.binary_string(string)
string.force_encoding(Encoding::BINARY)
end
+
+ def self.external_encoding_string(string)
+ string.force_encoding(Encoding.default_external)
+ end
end
diff --git a/kernel/common/file.rb b/kernel/common/file.rb
index b3b70b3..6e02796 100644
--- a/kernel/common/file.rb
+++ b/kernel/common/file.rb
@@ -416,6 +416,89 @@ class File < IO
end
##
+ # Converts a pathname to an absolute pathname. Relative
+ # paths are referenced from the current working directory
+ # of the process unless dir_string is given, in which case
+ # it will be used as the starting point. The given pathname
+ # may start with a ``~’’, which expands to the process owner‘s
+ # home directory (the environment variable HOME must be set
+ # correctly). "~user" expands to the named user‘s home directory.
+ #
+ # File.expand_path("~oracle/bin") #=> "/home/oracle/bin"
+ # File.expand_path("../../bin", "/tmp/x") #=> "/bin"
+ def self.expand_path(path, dir=nil)
+ path = Rubinius::Type.coerce_to_path(path)
+
+ first = path[0]
+ if first == ?~
+ case path[1]
+ when ?/
+ unless home = ENV["HOME"]
+ raise ArgumentError, "couldn't find HOME environment variable when expanding '~'"
+ end
+
+ path = ENV["HOME"] + path.byteslice(1, path.size - 1)
+ when nil
+ unless home = ENV["HOME"]
+ raise ArgumentError, "couldn't find HOME environment variable when expanding '~'"
+ end
+
+ if home.empty?
+ raise ArgumentError, "HOME environment variable is empty expanding '~'"
+ end
+
+ return home
+ else
+ unless length = path.index("/", 1)
+ length = path.size
+ end
+
+ name = path.byteslice 1, length - 1
+ unless dir = Rubinius.get_user_home(name)
+ raise ArgumentError, "user #{name} does not exist"
+ end
+
+ path = dir + path.byteslice(length, path.size - length)
+ end
+ elsif first != ?/
+ if dir
+ dir = expand_path dir
+ else
+ dir = PrivateDir.pwd
+ end
+
+ path = "#{dir}/#{path}"
+ end
+
+ items = []
+ start = 0
+ size = path.size
+
+ while index = path.index("/", start) or (start < size and index = size)
+ length = index - start
+
+ if length > 0
+ item = path.byteslice start, length
+
+ if item == ".."
+ items.pop
+ elsif item != "."
+ items << item
+ end
+ end
+
+ start = index + 1
+ end
+
+ return Rubinius.external_encoding_string("/") if items.empty?
+
+ str = ""
+ items.each { |x| str.append "/#{x}" }
+
+ return Rubinius.external_encoding_string(str)
+ end
+
+ ##
# Returns true if the named file exists and is a regular file.
def self.file?(path)
st = Stat.stat path
diff --git a/kernel/common/file18.rb b/kernel/common/file18.rb
index b30700e..448ac90 100644
--- a/kernel/common/file18.rb
+++ b/kernel/common/file18.rb
@@ -26,87 +26,4 @@ class File
private :initialize
- ##
- # Converts a pathname to an absolute pathname. Relative
- # paths are referenced from the current working directory
- # of the process unless dir_string is given, in which case
- # it will be used as the starting point. The given pathname
- # may start with a ``~’’, which expands to the process owner‘s
- # home directory (the environment variable HOME must be set
- # correctly). "~user" expands to the named user‘s home directory.
- #
- # File.expand_path("~oracle/bin") #=> "/home/oracle/bin"
- # File.expand_path("../../bin", "/tmp/x") #=> "/bin"
- def self.expand_path(path, dir=nil)
- path = Rubinius::Type.coerce_to_path(path)
-
- first = path[0]
- if first == ?~
- case path[1]
- when ?/
- unless home = ENV["HOME"]
- raise ArgumentError, "couldn't find HOME environment variable when expanding '~'"
- end
-
- path = ENV["HOME"] + path.byteslice(1, path.size - 1)
- when nil
- unless home = ENV["HOME"]
- raise ArgumentError, "couldn't find HOME environment variable when expanding '~'"
- end
-
- if home.empty?
- raise ArgumentError, "HOME environment variable is empty expanding '~'"
- end
-
- return home
- else
- unless length = path.index("/", 1)
- length = path.size
- end
-
- name = path.byteslice 1, length - 1
- unless dir = Rubinius.get_user_home(name)
- raise ArgumentError, "user #{name} does not exist"
- end
-
- path = dir + path.byteslice(length, path.size - length)
- end
- elsif first != ?/
- if dir
- dir = expand_path dir
- else
- dir = PrivateDir.pwd
- end
-
- path = "#{dir}/#{path}"
- end
-
- items = []
- start = 0
- size = path.size
-
- while index = path.index("/", start) or (start < size and index = size)
- length = index - start
-
- if length > 0
- item = path.byteslice start, length
-
- if item == ".."
- items.pop
- elsif item != "."
- items << item
- end
- end
-
- start = index + 1
- end
-
- return "/" if items.empty?
-
- str = ""
- items.each { |x| str.append "/#{x}" }
-
- return str
- end
-
end
diff --git a/kernel/common/file19.rb b/kernel/common/file19.rb
index 47443f3..4e61920 100644
--- a/kernel/common/file19.rb
+++ b/kernel/common/file19.rb
@@ -112,89 +112,6 @@ class File
real
end
- ##
- # Converts a pathname to an absolute pathname. Relative
- # paths are referenced from the current working directory
- # of the process unless dir_string is given, in which case
- # it will be used as the starting point. The given pathname
- # may start with a ``~’’, which expands to the process owner‘s
- # home directory (the environment variable HOME must be set
- # correctly). "~user" expands to the named user‘s home directory.
- #
- # File.expand_path("~oracle/bin") #=> "/home/oracle/bin"
- # File.expand_path("../../bin", "/tmp/x") #=> "/bin"
- def self.expand_path(path, dir=nil)
- path = Rubinius::Type.coerce_to_path(path)
-
- first = path[0]
- if first == ?~
- case path[1]
- when ?/
- unless home = ENV["HOME"]
- raise ArgumentError, "couldn't find HOME environment variable when expanding '~'"
- end
-
- path = ENV["HOME"] + path.byteslice(1, path.size - 1)
- when nil
- unless home = ENV["HOME"]
- raise ArgumentError, "couldn't find HOME environment variable when expanding '~'"
- end
-
- if home.empty?
- raise ArgumentError, "HOME environment variable is empty expanding '~'"
- end
-
- return home
- else
- unless length = path.index("/", 1)
- length = path.size
- end
-
- name = path.byteslice 1, length - 1
- unless dir = Rubinius.get_user_home(name)
- raise ArgumentError, "user #{name} does not exist"
- end
-
- path = dir + path.byteslice(length, path.size - length)
- end
- elsif first != ?/
- if dir
- dir = expand_path dir
- else
- dir = PrivateDir.pwd
- end
-
- path = "#{dir}/#{path}"
- end
-
- items = []
- start = 0
- size = path.size
-
- while index = path.index("/", start) or (start < size and index = size)
- length = index - start
-
- if length > 0
- item = path.byteslice start, length
-
- if item == ".."
- items.pop
- elsif item != "."
- items << item
- end
- end
-
- start = index + 1
- end
-
- return "/" if items.empty?
-
- str = ""
- items.each { |x| str.append "/#{x}" }
-
- return str.encode(Encoding.default_external)
- end
-
end
class File::Stat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment