Skip to content

Commit 3fde283

Browse files
committed
First pass at nqp::decode(...).
1 parent 92197db commit 3fde283

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/vm/jvm/QAST/Compiler.nqp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2074,6 +2074,7 @@ QAST::OperationsJAST.add_core_op('rindex', -> $qastcomp, $op {
20742074

20752075
QAST::OperationsJAST.map_classlib_core_op('codepointfromname', $TYPE_OPS, 'codepointfromname', [$RT_STR], $RT_INT);
20762076
QAST::OperationsJAST.map_classlib_core_op('encode', $TYPE_OPS, 'encode', [$RT_STR, $RT_STR, $RT_OBJ], $RT_OBJ, :tc);
2077+
QAST::OperationsJAST.map_classlib_core_op('decode', $TYPE_OPS, 'decode', [$RT_OBJ, $RT_STR], $RT_STR, :tc);
20772078

20782079
# serialization context opcodes
20792080
QAST::OperationsJAST.map_classlib_core_op('sha1', $TYPE_OPS, 'sha1', [$RT_STR], $RT_STR);

src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.math.BigInteger;
1717
import java.math.RoundingMode;
1818
import java.nio.ByteBuffer;
19+
import java.nio.charset.Charset;
1920
import java.nio.file.Files;
2021
import java.nio.file.LinkOption;
2122
import java.nio.file.Path;
@@ -2979,6 +2980,57 @@ else if (encoding.equals("utf32")) {
29792980
}
29802981
}
29812982

2983+
public static String decode8(SixModelObject buf, String csName, ThreadContext tc) {
2984+
ByteBuffer bb;
2985+
if (buf instanceof VMArrayInstance_i8) {
2986+
VMArrayInstance_i8 bufi8 = (VMArrayInstance_i8)buf;
2987+
bb = bufi8.slots != null
2988+
? ByteBuffer.wrap(bufi8.slots, bufi8.start, bufi8.elems)
2989+
: ByteBuffer.allocate(0);
2990+
}
2991+
else {
2992+
int n = (int)buf.elems(tc);
2993+
bb = ByteBuffer.allocate(n);
2994+
for (int i = 0; i < n; i++) {
2995+
buf.at_pos_native(tc, i);
2996+
bb.put((byte)tc.native_i);
2997+
}
2998+
}
2999+
return Charset.forName(csName).decode(bb).toString();
3000+
}
3001+
public static String decode(SixModelObject buf, String encoding, ThreadContext tc) {
3002+
if (encoding.equals("utf8")) {
3003+
return decode8(buf, "UTF-8", tc);
3004+
}
3005+
else if (encoding.equals("ascii")) {
3006+
return decode8(buf, "US-ASCII", tc);
3007+
}
3008+
else if (encoding.equals("iso-8859-1")) {
3009+
return decode8(buf, "ISO-8859-1", tc);
3010+
}
3011+
else if (encoding.equals("utf16")) {
3012+
int n = (int)buf.elems(tc);
3013+
StringBuilder sb = new StringBuilder(n);
3014+
for (int i = 0; i < n; i++) {
3015+
buf.at_pos_native(tc, i);
3016+
sb.append((char)tc.native_i);
3017+
}
3018+
return sb.toString();
3019+
}
3020+
else if (encoding.equals("utf32")) {
3021+
int n = (int)buf.elems(tc);
3022+
StringBuilder sb = new StringBuilder(n);
3023+
for (int i = 0; i < n; i++) {
3024+
buf.at_pos_native(tc, i);
3025+
sb.appendCodePoint((int)tc.native_i);
3026+
}
3027+
return sb.toString();
3028+
}
3029+
else {
3030+
throw ExceptionHandling.dieInternal(tc, "Unknown encoding '" + encoding + "'");
3031+
}
3032+
}
3033+
29823034
private static final int CCLASS_ANY = 65535;
29833035
private static final int CCLASS_UPPERCASE = 1;
29843036
private static final int CCLASS_LOWERCASE = 2;

0 commit comments

Comments
 (0)