|
79 | 79 | import org.perl6.nqp.sixmodel.reprs.VMArray;
|
80 | 80 | import org.perl6.nqp.sixmodel.reprs.VMArrayInstance;
|
81 | 81 | import org.perl6.nqp.sixmodel.reprs.VMArrayInstance_i16;
|
| 82 | +import org.perl6.nqp.sixmodel.reprs.VMArrayInstance_u16; |
82 | 83 | import org.perl6.nqp.sixmodel.reprs.VMArrayInstance_i32;
|
| 84 | +import org.perl6.nqp.sixmodel.reprs.VMArrayInstance_u32; |
83 | 85 | import org.perl6.nqp.sixmodel.reprs.VMArrayInstance_i8;
|
84 | 86 | import org.perl6.nqp.sixmodel.reprs.VMArrayInstance_u8;
|
85 | 87 | import org.perl6.nqp.sixmodel.reprs.VMExceptionInstance;
|
@@ -3356,21 +3358,40 @@ else if (encoding.equals("ascii")) {
|
3356 | 3358 | else if (encoding.equals("iso-8859-1")) {
|
3357 | 3359 | return decode8(buf, "ISO-8859-1", tc);
|
3358 | 3360 | }
|
3359 |
| - else if (encoding.equals("utf16")) { |
| 3361 | + else if (encoding.equals("utf16") || encoding.equals("utf32")) { |
3360 | 3362 | int n = (int)buf.elems(tc);
|
3361 | 3363 | StringBuilder sb = new StringBuilder(n);
|
3362 |
| - for (int i = 0; i < n; i++) { |
3363 |
| - buf.at_pos_native(tc, i); |
3364 |
| - sb.append((char)tc.native_i); |
| 3364 | + if (buf instanceof VMArrayInstance_u8 || buf instanceof VMArrayInstance_i8) { |
| 3365 | + if (encoding.equals("utf16") && n % 2 == 1) { |
| 3366 | + throw ExceptionHandling.dieInternal(tc, "Malformed UTF-16; odd number of bytes"); |
| 3367 | + } |
| 3368 | + if (encoding.equals("utf32") && n % 4 > 0) { |
| 3369 | + throw ExceptionHandling.dieInternal(tc, "Malformed UTF-32; number of bytes must be factor of four"); |
| 3370 | + } |
| 3371 | + for (int i = 0; i < n;) { |
| 3372 | + buf.at_pos_native(tc, i++); |
| 3373 | + int a = (int)tc.native_i; |
| 3374 | + buf.at_pos_native(tc, i++); |
| 3375 | + int b = (int)tc.native_i; |
| 3376 | + sb.appendCodePoint(a + (b << 8)); |
| 3377 | + } |
3365 | 3378 | }
|
3366 |
| - return sb.toString(); |
3367 |
| - } |
3368 |
| - else if (encoding.equals("utf32")) { |
3369 |
| - int n = (int)buf.elems(tc); |
3370 |
| - StringBuilder sb = new StringBuilder(n); |
3371 |
| - for (int i = 0; i < n; i++) { |
3372 |
| - buf.at_pos_native(tc, i); |
3373 |
| - sb.appendCodePoint((int)tc.native_i); |
| 3379 | + else if (buf instanceof VMArrayInstance_i16 || buf instanceof VMArrayInstance_u16) { |
| 3380 | + for (int i = 0; i < n; i++) { |
| 3381 | + buf.at_pos_native(tc, i); |
| 3382 | + sb.appendCodePoint((int)tc.native_i); |
| 3383 | + } |
| 3384 | + } |
| 3385 | + else if (buf instanceof VMArrayInstance_i32 || buf instanceof VMArrayInstance_u32) { |
| 3386 | + for (int i = 0; i < n; i++) { |
| 3387 | + buf.at_pos_native(tc, i); |
| 3388 | + int a = (int)tc.native_i; |
| 3389 | + sb.appendCodePoint(a & 0xFFFF); |
| 3390 | + sb.appendCodePoint(a >> 16); |
| 3391 | + } |
| 3392 | + } |
| 3393 | + else { |
| 3394 | + throw ExceptionHandling.dieInternal(tc, "Unknown buf type: " + buf.getClass() + "/" + Ops.typeName(buf, tc)); |
3374 | 3395 | }
|
3375 | 3396 | return sb.toString();
|
3376 | 3397 | }
|
|
0 commit comments