Skip to content

Commit

Permalink
[Truffle] Implement 0 arg String#split.
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvdrum committed Jan 8, 2015
1 parent e941b67 commit 8387bb4
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions core/src/main/java/org/jruby/truffle/nodes/core/StringNodes.java
Expand Up @@ -1157,7 +1157,7 @@ public int size(RubyString string) {
}
}

@CoreMethod(names = "split", required = 1)
@CoreMethod(names = "split", optional = 1)
public abstract static class SplitNode extends CoreMethodNode {

public SplitNode(RubyContext context, SourceSection sourceSection) {
Expand All @@ -1172,7 +1172,25 @@ public SplitNode(SplitNode prev) {
public RubyArray split(RubyString string, RubyString sep) {
notDesignedForCompilation();

final String[] components = string.toString().split(Pattern.quote(sep.toString()));
return splitHelper(string, sep.toString());
}

@Specialization
public RubyArray split(RubyString string, RubyRegexp sep) {
notDesignedForCompilation();

return RubyArray.fromObjects(getContext().getCoreLibrary().getArrayClass(), (Object[]) sep.split(string.toString()));
}

@Specialization
public RubyArray split(RubyString string, UndefinedPlaceholder sep) {
notDesignedForCompilation();

return splitHelper(string, " ");
}

private RubyArray splitHelper(RubyString string, String sep) {
final String[] components = string.toString().split(Pattern.quote(sep));

final Object[] objects = new Object[components.length];

Expand All @@ -1182,13 +1200,6 @@ public RubyArray split(RubyString string, RubyString sep) {

return RubyArray.fromObjects(getContext().getCoreLibrary().getArrayClass(), objects);
}

@Specialization
public RubyArray split(RubyString string, RubyRegexp sep) {
notDesignedForCompilation();

return RubyArray.fromObjects(getContext().getCoreLibrary().getArrayClass(), (Object[]) sep.split(string.toString()));
}
}

@CoreMethod(names = "start_with?", required = 1)
Expand Down

0 comments on commit 8387bb4

Please sign in to comment.