Skip to content

Commit

Permalink
[Truffle] Break dependency on RubyEncoding in StringOperations.
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvdrum committed Nov 8, 2016
1 parent ca5e762 commit 4d4a170
Showing 1 changed file with 8 additions and 10 deletions.
Expand Up @@ -37,7 +37,6 @@
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.object.DynamicObject;
import org.jcodings.Encoding;
import org.jruby.RubyEncoding;
import org.jruby.truffle.Layouts;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.array.ArrayOperations;
Expand All @@ -50,6 +49,8 @@
import org.jruby.truffle.util.StringUtils;
import org.jruby.util.ByteList;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

public abstract class StringOperations {
Expand Down Expand Up @@ -153,17 +154,14 @@ public static Rope encodeRope(CharSequence value, Encoding encoding, CodeRange c
Charset charset = encoding.getCharset();

// if null charset, fall back on Java default charset
if (charset == null) charset = Charset.defaultCharset();

byte[] bytes;
if (charset == RubyEncoding.UTF8) {
bytes = RubyEncoding.encodeUTF8(value);
} else if (charset == RubyEncoding.UTF16) {
bytes = RubyEncoding.encodeUTF16(value);
} else {
bytes = RubyEncoding.encode(value, charset);
if (charset == null) {
charset = Charset.defaultCharset();
}

final ByteBuffer buffer = charset.encode(CharBuffer.wrap(value));
final byte[] bytes = new byte[buffer.limit()];
buffer.get(bytes);

return RopeOperations.create(bytes, encoding, codeRange);
}

Expand Down

0 comments on commit 4d4a170

Please sign in to comment.