Skip to content

Commit

Permalink
Showing 2 changed files with 49 additions and 5 deletions.
32 changes: 32 additions & 0 deletions spec/truffle/specs/truffle/string/truncate_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
# code is released under a tri EPL/GPL/LGPL license. You can use it,
# redistribute it and/or modify it under the terms of the:
#
# Eclipse Public License version 1.0
# GNU General Public License version 2
# GNU Lesser General Public License version 2.1
# OTHER DEALINGS IN THE SOFTWARE.

require_relative '../../../../ruby/spec_helper'

describe "Truffle::String.truncate" do
it "should truncate if the new byte length is shorter than the current length" do
str = "abcdef"

Truffle::String.truncate(str, 3)

str.should == "abc"
end

it "should raise an error if the new byte length is greater than the current length" do
lambda do
Truffle::String.truncate("abc", 10)
end.should raise_error(ArgumentError)
end

it "should raise an error if the new byte length is negative" do
lambda do
Truffle::String.truncate("abc", -1)
end.should raise_error(ArgumentError)
end
end
Original file line number Diff line number Diff line change
@@ -30,14 +30,21 @@ public class TruffleStringNodes {
@CoreMethod(names = "truncate", onSingleton = true, required = 2, lowerFixnumParameters = 1)
public abstract static class TruncateNode extends CoreMethodArrayArgumentsNode {

@Specialization(guards = { "isRubyString(string)", "isNewLengthTooLarge(string, newByteLength)" })
@Specialization(guards = { "newByteLength < 0" })
@TruffleBoundary
public DynamicObject truncateError(DynamicObject string, int newByteLength) {
public DynamicObject truncateLengthNegative(DynamicObject string, int newByteLength) {
throw new RaiseException(
getContext().getCoreExceptions().argumentError(formatError(newByteLength, rope(string)), this));
getContext().getCoreExceptions().argumentError(formatNegativeError(newByteLength), this));
}

@Specialization(guards = { "isRubyString(string)" })
@Specialization(guards = { "newByteLength > 0", "isRubyString(string)", "isNewLengthTooLarge(string, newByteLength)" })
@TruffleBoundary
public DynamicObject truncateLengthTooLong(DynamicObject string, int newByteLength) {
throw new RaiseException(
getContext().getCoreExceptions().argumentError(formatTooLongError(newByteLength, rope(string)), this));
}

@Specialization(guards = { "newByteLength > 0", "isRubyString(string)", "!isNewLengthTooLarge(string, newByteLength)" })
public DynamicObject stealStorage(DynamicObject string, int newByteLength,
@Cached("createX()") RopeNodes.MakeSubstringNode makeSubstringNode) {

@@ -53,7 +60,12 @@ protected static boolean isNewLengthTooLarge(DynamicObject string, int newByteLe
}

@TruffleBoundary
private String formatError(int count, final Rope rope) {
private String formatNegativeError(int count) {
return String.format("Invalid byte count: %d is negative", count);
}

@TruffleBoundary
private String formatTooLongError(int count, final Rope rope) {
return String.format("Invalid byte count: %d exceeds string size of %d bytes", count, rope.byteLength());
}

0 comments on commit 372d3e9

Please sign in to comment.