Skip to content

Commit

Permalink
[Truffle] Initial implementation of String#gsub! and rewrote String#g…
Browse files Browse the repository at this point in the history
…sub in terms of String#gsub.
  • Loading branch information
nirvdrum committed Feb 4, 2015
1 parent f83da0a commit dfd1a6f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
35 changes: 19 additions & 16 deletions truffle/src/main/java/org/jruby/truffle/nodes/core/StringNodes.java
Expand Up @@ -830,61 +830,63 @@ public RubyString forceEncoding(RubyString string, RubyEncoding encoding) {

}

@CoreMethod(names = "gsub", required = 1, optional = 1, needsBlock = true)
public abstract static class GsubNode extends RegexpNodes.EscapingYieldingNode {
@CoreMethod(names = "gsub!", required = 1, optional = 1, needsBlock = true)
public abstract static class GsubBangNode extends RegexpNodes.EscapingYieldingNode {

@Child private CallDispatchHeadNode toS;

public GsubNode(RubyContext context, SourceSection sourceSection) {
public GsubBangNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
toS = DispatchHeadNodeFactory.createMethodCall(context);
}

public GsubNode(GsubNode prev) {
public GsubBangNode(GsubBangNode prev) {
super(prev);
toS = prev.toS;
}

@Specialization
public RubyString gsub(VirtualFrame frame, RubyString string, RubyString regexpString, RubyString replacement, UndefinedPlaceholder block) {
public RubyString gsubBang(VirtualFrame frame, RubyString string, RubyString regexpString, RubyString replacement, UndefinedPlaceholder block) {
notDesignedForCompilation();

final RubyRegexp regexp = new RubyRegexp(this, getContext().getCoreLibrary().getRegexpClass(), escape(frame, regexpString).getBytes(), Option.DEFAULT);
return gsub(string, regexp, replacement, block);
return gsubBang(string, regexp, replacement, block);
}

@Specialization
public RubyString gsub(RubyString string, RubyRegexp regexp, RubyString replacement, @SuppressWarnings("unused") UndefinedPlaceholder block) {
public RubyString gsubBang(RubyString string, RubyRegexp regexp, RubyString replacement, @SuppressWarnings("unused") UndefinedPlaceholder block) {
notDesignedForCompilation();

return regexp.gsub(string, replacement.toString());
string.set(regexp.gsub(string, replacement.toString()).getBytes());
return string;
}

@Specialization
public RubyString gsub(VirtualFrame frame, RubyString string, RubyString regexpString, RubyString replacement, RubyProc block) {
public RubyString gsubBang(VirtualFrame frame, RubyString string, RubyString regexpString, RubyString replacement, RubyProc block) {
notDesignedForCompilation();

final RubyRegexp regexp = new RubyRegexp(this, getContext().getCoreLibrary().getRegexpClass(), escape(frame, regexpString).getBytes(), Option.DEFAULT);
return gsub(string, regexp, replacement, block);
return gsubBang(string, regexp, replacement, block);
}

@Specialization
public RubyString gsub(RubyString string, RubyRegexp regexp, RubyString replacement, @SuppressWarnings("unused") RubyProc block) {
public RubyString gsubBang(RubyString string, RubyRegexp regexp, RubyString replacement, @SuppressWarnings("unused") RubyProc block) {
notDesignedForCompilation();

return regexp.gsub(string, replacement.toString());
string.set(regexp.gsub(string, replacement.toString()).getBytes());
return string;
}

@Specialization
public RubyString gsub(VirtualFrame frame, RubyString string, RubyString regexpString, @SuppressWarnings("unused") UndefinedPlaceholder replacement, RubyProc block) {
public RubyString gsubBang(VirtualFrame frame, RubyString string, RubyString regexpString, @SuppressWarnings("unused") UndefinedPlaceholder replacement, RubyProc block) {
notDesignedForCompilation();

final RubyRegexp regexp = new RubyRegexp(this, getContext().getCoreLibrary().getRegexpClass(), escape(frame, regexpString).getBytes(), Option.DEFAULT);
return gsub(frame, string, regexp, replacement, block);
return gsubBang(frame, string, regexp, replacement, block);
}

@Specialization
public RubyString gsub(VirtualFrame frame, RubyString string, RubyRegexp regexp, @SuppressWarnings("unused") UndefinedPlaceholder replacement, RubyProc block) {
public RubyString gsubBang(VirtualFrame frame, RubyString string, RubyRegexp regexp, @SuppressWarnings("unused") UndefinedPlaceholder replacement, RubyProc block) {
notDesignedForCompilation();

final RubyContext context = getContext();
Expand Down Expand Up @@ -930,7 +932,8 @@ public RubyString gsub(VirtualFrame frame, RubyString string, RubyRegexp regexp,
end = StringSupport.positionEndForScan(string.getBytes(), matcher, encoding, p, range);
}

return context.makeString(builder.toString());
string.set(context.makeString(builder.toString()).getBytes());
return string;
}
}

Expand Down
6 changes: 6 additions & 0 deletions truffle/src/main/ruby/jruby/truffle/core/shims.rb
Expand Up @@ -155,3 +155,9 @@ def extended_modules(object)
end

end

class String
def gsub(*args)
dup.gsub!(*args)
end
end

0 comments on commit dfd1a6f

Please sign in to comment.