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: 51e9606cc48b
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 23e9b953de69
Choose a head ref
  • 2 commits
  • 1 file changed
  • 1 contributor

Commits on Mar 21, 2015

  1. Copy the full SHA
    d245906 View commit details
  2. Copy the full SHA
    23e9b95 View commit details
Showing with 24 additions and 19 deletions.
  1. +24 −19 truffle/src/main/java/org/jruby/truffle/nodes/rubinius/StringPrimitiveNodes.java
Original file line number Diff line number Diff line change
@@ -318,29 +318,36 @@ public StringChrAtPrimitiveNode(StringChrAtPrimitiveNode prev) {
@CompilerDirectives.TruffleBoundary
@Specialization
public Object stringChrAt(RubyString string, int byteIndex) {
if (stringByteSubstringNode == null) {
CompilerDirectives.transferToInterpreter();
// Taken from Rubinius's Character::create_from.

stringByteSubstringNode = insert(
StringPrimitiveNodesFactory.StringByteSubstringPrimitiveNodeFactory.create(
getContext(),
getSourceSection(),
new RubyNode[]{})
);
final ByteList bytes = string.getByteList();

if (byteIndex < 0 || byteIndex >= bytes.getRealSize()) {
return nil();
}

final ByteList bytes = string.getByteList();
final int p = bytes.getBegin();
final int end = p + bytes.getRealSize();
final int c = StringSupport.preciseLength(bytes.getEncoding(), bytes.getUnsafeBytes(), p, end);

if (! StringSupport.MBCLEN_CHARFOUND_P(c)) {
return getContext().getCoreLibrary().getNilObject();
return nil();
}

final int n = StringSupport.MBCLEN_CHARFOUND_LEN(c);
if (n + byteIndex > end) {
return getContext().getCoreLibrary().getNilObject();
return nil();
}

if (stringByteSubstringNode == null) {
CompilerDirectives.transferToInterpreter();

stringByteSubstringNode = insert(
StringPrimitiveNodesFactory.StringByteSubstringPrimitiveNodeFactory.create(
getContext(),
getSourceSection(),
new RubyNode[]{})
);
}

return stringByteSubstringNode.stringByteSubstring(string, byteIndex, n);
@@ -691,6 +698,8 @@ public StringPreviousByteIndexPrimitiveNode(StringPreviousByteIndexPrimitiveNode

@Specialization
public Object stringPreviousByteIndex(RubyString string, int index) {
// Port of Rubinius's String::previous_byte_index.

if (index < 0) {
CompilerDirectives.transferToInterpreter();
throw new RaiseException(getContext().getCoreLibrary().argumentError("negative index given", this));
@@ -700,17 +709,13 @@ public Object stringPreviousByteIndex(RubyString string, int index) {
final int p = bytes.getBegin();
final int end = p + bytes.getRealSize();

if (p > end) {
return 0;
}

final int s = bytes.getEncoding().prevCharHead(bytes.getUnsafeBytes(), p, p + index, end);
final int b = bytes.getEncoding().prevCharHead(bytes.getUnsafeBytes(), p, p + index, end);

if (s == -1) {
return 0;
if (b == -1) {
return nil();
}

return s - p;
return b - p;
}

}