Skip to content

Commit

Permalink
Showing 3 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -34,18 +34,17 @@
import org.jruby.truffle.core.encoding.EncodingManager;
import org.jruby.truffle.core.string.StringOperations;
import org.jruby.truffle.language.RubyGuards;
import org.jruby.truffle.language.control.RaiseException;
import org.jruby.truffle.util.StringUtils;
import org.jruby.util.ByteList;
import org.jruby.util.Memo;
import org.jruby.util.StringSupport;
import org.jruby.util.io.EncodingUtils;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.concurrent.ConcurrentHashMap;

import static org.jruby.truffle.core.rope.CodeRange.CR_7BIT;
import static org.jruby.truffle.core.rope.CodeRange.CR_BROKEN;
import static org.jruby.truffle.core.rope.CodeRange.CR_UNKNOWN;
@@ -646,4 +645,19 @@ private static CodeRange commonCodeRange(CodeRange first, CodeRange second) {
return CR_VALID;
}

@TruffleBoundary
public static int codePoint(RubyContext context, Rope rope, int start) {
byte[] bytes = rope.getBytes();
int p = start;
int end = rope.byteLength();
Encoding enc = rope.getEncoding();

assert p < end : "empty string";
int cl = StringSupport.preciseLength(enc, bytes, p, end);
if (cl <= 0) {
throw new RaiseException(context.getCoreExceptions().argumentError("invalid byte sequence in " + enc, null));
}
return enc.mbcToCode(bytes, p, end);
}

}
Original file line number Diff line number Diff line change
@@ -137,7 +137,6 @@
import org.jruby.truffle.language.dispatch.CallDispatchHeadNode;
import org.jruby.truffle.language.dispatch.DispatchHeadNodeFactory;
import org.jruby.truffle.language.objects.AllocateObjectNode;
import org.jruby.truffle.language.objects.AllocateObjectNodeGen;
import org.jruby.truffle.language.objects.IsFrozenNode;
import org.jruby.truffle.language.objects.IsFrozenNodeGen;
import org.jruby.truffle.language.objects.TaintNode;
@@ -1481,12 +1480,10 @@ public Object lstripBang(DynamicObject string) {
final Encoding enc = RopeOperations.STR_ENC_GET(rope);
final int s = 0;
final int end = s + rope.byteLength();
final byte[] bytes = rope.getBytes();

int p = s;

while (p < end) {
int c = StringSupport.codePoint(getContext().getJRubyRuntime(), enc, bytes, p, end);
int c = RopeOperations.codePoint(getContext(), rope, p);
if (!ASCIIEncoding.INSTANCE.isSpace(c)) break;
p += StringSupport.codeLength(enc, c);
}
@@ -1529,18 +1526,13 @@ public int ord(DynamicObject string) {
final Rope rope = rope(string);

try {
return codePoint(rope.getEncoding(), rope.getBytes(), 0, rope.byteLength());
return RopeOperations.codePoint(getContext(), rope, 0);
} catch (IllegalArgumentException e) {
CompilerDirectives.transferToInterpreterAndInvalidate();
throw new RaiseException(coreExceptions().argumentError(e.getMessage(), this));
}
}

@TruffleBoundary
private int codePoint(Encoding encoding, byte[] bytes, int p, int end) {
return StringSupport.codePoint(encoding, bytes, p, end);
}

}

@CoreMethod(names = "replace", required = 1, raiseIfFrozenSelf = true, taintFrom = 1)
@@ -1621,7 +1613,7 @@ public Object rstripBang(DynamicObject string) {
int endp = end;
int prev;
while ((prev = prevCharHead(enc, bytes, start, endp, end)) != -1) {
int point = StringSupport.codePoint(getContext().getJRubyRuntime(), enc, bytes, prev, end);
int point = RopeOperations.codePoint(getContext(), rope, prev);
if (point != 0 && !ASCIIEncoding.INSTANCE.isSpace(point)) break;
endp = prev;
}
@@ -2474,15 +2466,15 @@ public DynamicObject capitalizeBang(DynamicObject string) {
byte[] bytes = rope.getBytesCopy();
boolean modify = false;

int c = StringSupport.codePoint(getContext().getJRubyRuntime(), enc, bytes, s, end);
int c = RopeOperations.codePoint(getContext(), rope, s);
if (enc.isLower(c)) {
enc.codeToMbc(StringSupport.toUpper(enc, c), bytes, s);
modify = true;
}

s += StringSupport.codeLength(enc, c);
while (s < end) {
c = StringSupport.codePoint(getContext().getJRubyRuntime(), enc, bytes, s, end);
c = RopeOperations.codePoint(getContext(), rope, s);
if (enc.isUpper(c)) {
enc.codeToMbc(StringSupport.toLower(enc, c), bytes, s);
modify = true;
@@ -2492,7 +2484,6 @@ public DynamicObject capitalizeBang(DynamicObject string) {

if (modify) {
StringOperations.setRope(string, makeLeafRopeNode.executeMake(bytes, rope.getEncoding(), rope.getCodeRange(), rope.characterLength()));

return string;
}

@@ -2694,7 +2685,7 @@ public DynamicObject stringAwkSplit(DynamicObject string, int lim) {
c = bytes[p++] & 0xff;
} else {
try {
c = StringSupport.codePoint(getContext().getJRubyRuntime(), enc, bytes, p, end);
c = RopeOperations.codePoint(getContext(), rope, p);
} catch (org.jruby.exceptions.RaiseException ex) {
throw new RaiseException(getContext().getJRubyInterop().toTruffle(ex.getException(), this));
}
Original file line number Diff line number Diff line change
@@ -37,6 +37,7 @@
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.object.DynamicObject;
import org.jcodings.Encoding;
import org.jruby.Ruby;
import org.jruby.RubyEncoding;
import org.jruby.truffle.Layouts;
import org.jruby.truffle.RubyContext;
@@ -46,10 +47,11 @@
import org.jruby.truffle.core.rope.Rope;
import org.jruby.truffle.core.rope.RopeOperations;
import org.jruby.truffle.language.RubyGuards;
import org.jruby.truffle.language.control.RaiseException;
import org.jruby.truffle.util.ByteListUtils;
import org.jruby.truffle.util.StringUtils;
import org.jruby.util.ByteList;

import org.jruby.util.StringSupport;
import java.nio.charset.Charset;

public abstract class StringOperations {

0 comments on commit a76ff66

Please sign in to comment.