Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: b483e10e0727
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 4a39b20710aa
Choose a head ref
  • 2 commits
  • 3 files changed
  • 2 contributors

Commits on Mar 19, 2015

  1. Copy the full SHA
    2753f16 View commit details
  2. Merge pull request #2731 from bjfish/truffle_array_join

    [Truffle] Moving Array#join to array.rb.
    chrisseaton committed Mar 19, 2015
    Copy the full SHA
    4a39b20 View commit details
19 changes: 0 additions & 19 deletions spec/truffle/tags/core/array/join_tags.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1 @@
fails:Array#join returns a string formed by concatenating each element.to_str separated by separator
fails:Array#join uses the same separator with nested arrays
fails:Array#join attempts coercion via #to_str first
fails:Array#join attempts coercion via #to_ary second
fails:Array#join attempts coercion via #to_s third
fails:Array#join raises a NoMethodError if an element does not respond to #to_str, #to_ary, or #to_s
fails:Array#join taints the result if the Array is tainted and non-empty
fails:Array#join taints the result if the result of coercing an element is tainted
fails:Array#join untrusts the result if the Array is untrusted and non-empty
fails:Array#join untrusts the result if the result of coercing an element is untrusted
fails:Array#join uses the first encoding when other strings are compatible
fails:Array#join fails for arrays with incompatibly-encoded strings
fails:Array#join does not separate elements when the passed separator is nil
fails:Array#join calls #to_str to convert the separator to a String
fails:Array#join does not call #to_str on the separator if the array is empty
fails:Array#join with a tainted separator taints the result if the array has two or more elements
fails:Array#join with an untrusted separator untrusts the result if the array has two or more elements
fails:Array#join with $, separates elements with default separator when the passed separator is nil
fails:Array#join raises an ArgumentError when the Array is recursive
fails:Array#join uses the widest common encoding when other strings are incompatible
46 changes: 0 additions & 46 deletions truffle/src/main/java/org/jruby/truffle/nodes/core/ArrayNodes.java
Original file line number Diff line number Diff line change
@@ -1722,52 +1722,6 @@ public Object insert(RubyArray array, int index, int value) {

}

@CoreMethod(names = "join", optional = 1)
public abstract static class JoinNode extends ArrayCoreMethodNode {

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

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

@Specialization
public RubyString join(RubyArray array, UndefinedPlaceholder unused) {
Object separator = getContext().getCoreLibrary().getGlobalVariablesObject().getInstanceVariable("$,");
if (separator == nil()) {
separator = getContext().makeString("");
}

if (separator instanceof RubyString) {
return join(array, (RubyString) separator);
} else {
throw new UnsupportedOperationException();
}
}

@Specialization
public RubyString join(RubyArray array, RubyString separator) {
notDesignedForCompilation();

final StringBuilder builder = new StringBuilder();

final Object[] objects = array.slowToArray();

for (int n = 0; n < objects.length; n++) {
if (n > 0) {
builder.append(separator);
}

builder.append(objects[n]);
}

return getContext().makeString(builder.toString());
}

}

@CoreMethod(names = {"map", "collect"}, needsBlock = true, returnsEnumeratorIfNoBlock = true)
@ImportGuards(ArrayGuards.class)
public abstract static class MapNode extends YieldingCoreMethodNode {
46 changes: 46 additions & 0 deletions truffle/src/main/ruby/core/rubinius/common/array.rb
Original file line number Diff line number Diff line change
@@ -634,6 +634,52 @@ def inspect

alias_method :to_s, :inspect

def join(sep=nil)
return "".force_encoding(Encoding::US_ASCII) if @total == 0

out = ""
raise ArgumentError, "recursive array join" if Thread.detect_recursion self do
sep = sep.nil? ? $, : StringValue(sep)

# We've manually unwound the first loop entry for performance
# reasons.
x = @tuple[@start]

if str = String.try_convert(x)
x = str
elsif ary = Array.try_convert(x)
x = ary.join(sep)
else
x = x.to_s
end

out.force_encoding(x.encoding)
out << x

total = @start + size()
i = @start + 1

while i < total
out << sep if sep

x = @tuple[i]

if str = String.try_convert(x)
x = str
elsif ary = Array.try_convert(x)
x = ary.join(sep)
else
x = x.to_s
end

out << x
i += 1
end
end

Rubinius::Type.infect(out, self)
end

def keep_if(&block)
return to_enum :keep_if unless block_given?