Skip to content

Commit

Permalink
[Truffle] File.basename from Rubinius.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Apr 14, 2015
1 parent 65881af commit af7b18a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 42 deletions.
4 changes: 0 additions & 4 deletions spec/truffle/tags/core/file/basename_tags.txt
@@ -1,7 +1,3 @@
fails:File.basename returns the basename of a path (basic cases)
fails:File.basename returns the last component of the filename
fails:File.basename accepts an object that has a #to_path method
fails:File.basename returns the extension for a multibyte filename
fails(windows):File.basename returns the basename for windows
fails(windows):File.basename returns basename windows unc
fails(windows):File.basename returns basename windows forward slash
Expand Down
38 changes: 0 additions & 38 deletions truffle/src/main/java/org/jruby/truffle/nodes/core/FileNodes.java
Expand Up @@ -62,44 +62,6 @@ public RubyString absolutePath(RubyString path) {

}

@CoreMethod(names = "basename", onSingleton = true, required = 1, optional = 1)
public abstract static class BasenameNode extends CoreMethodNode {

public BasenameNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public BasenameNode(BasenameNode prev) {
super(prev);
}

@Specialization
public RubyString basename(RubyString path, @SuppressWarnings("unused") UndefinedPlaceholder extension) {
notDesignedForCompilation();

return getContext().makeString(new File(path.toString()).getName());
}

@Specialization
public RubyString basename(RubyString path, RubyString extension) {
notDesignedForCompilation();

final String extensionAsString = extension.toString();
final String name = new File(path.toString()).getName();
final String basename;

if (extensionAsString.equals(".*") && name.indexOf('.') != -1) {
basename = name.substring(0, name.lastIndexOf('.'));
} else if (name.endsWith(extensionAsString)) {
basename = name.substring(0, name.lastIndexOf(extensionAsString));
} else {
basename = name;
}

return getContext().makeString(basename);
}
}

@CoreMethod(names = "close")
public abstract static class CloseNode extends CoreMethodNode {

Expand Down
65 changes: 65 additions & 0 deletions truffle/src/main/ruby/core/rubinius/common/file.rb
Expand Up @@ -121,6 +121,71 @@ def self.path(obj)
StringValue(obj)
end

##
# Returns the last component of the filename given
# in file_name, which must be formed using forward
# slashes (``/’’) regardless of the separator used
# on the local file system. If suffix is given and
# present at the end of file_name, it is removed.
#
# File.basename("/home/gumby/work/ruby.rb") #=> "ruby.rb"
# File.basename("/home/gumby/work/ruby.rb", ".rb") #=> "ruby"
def self.basename(path, ext=undefined)
path = Rubinius::Type.coerce_to_path(path)

slash = "/"

ext_not_present = undefined.equal?(ext)

if pos = path.find_string_reverse(slash, path.bytesize)
# special case. If the string ends with a /, ignore it.
if pos == path.bytesize - 1

# Find the first non-/ from the right
data = path.data
found = false
pos.downto(0) do |i|
if data[i] != 47 # ?/
path = path.byteslice(0, i+1)
found = true
break
end
end

# edge case, it's all /'s, return "/"
return slash unless found

# Now that we've trimmed the /'s at the end, search again
pos = path.find_string_reverse(slash, path.bytesize)
if ext_not_present and !pos
# No /'s found and ext not present, return path.
return path
end
end

path = path.byteslice(pos + 1, path.bytesize - pos) if pos
end

return path if ext_not_present

# special case. if ext is ".*", remove any extension

ext = StringValue(ext)

if ext == ".*"
if pos = path.find_string_reverse(".", path.bytesize)
return path.byteslice(0, pos)
end
elsif pos = path.find_string_reverse(ext, path.bytesize)
# Check that ext is the last thing in the string
if pos == path.bytesize - ext.size
return path.byteslice(0, pos)
end
end

return path
end

end

File::Stat = Rubinius::Stat
6 changes: 6 additions & 0 deletions truffle/src/main/ruby/core/shims.rb
Expand Up @@ -209,3 +209,9 @@ def initialize(*args)
end
end

class Rubinius::ByteArray

alias_method :[], :get_byte

end

0 comments on commit af7b18a

Please sign in to comment.