OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 6 // for details. All rights reserved. Use of this source code is governed by a |
| 7 // BSD-style license that can be found in the LICENSE file. |
| 8 |
| 9 import "package:expect/expect.dart"; |
| 10 import 'dart:convert'; |
| 11 |
| 12 String decode(List<int> bytes, int chunkSize) { |
| 13 StringBuffer buffer = new StringBuffer(); |
| 14 ChunkedConversionSink stringSink = |
| 15 new StringConversionSink.fromStringSink(buffer); |
| 16 var byteSink = new Utf8Decoder().startChunkedConversion(stringSink); |
| 17 int i = 0; |
| 18 while (i < bytes.length) { |
| 19 List nextChunk = []; |
| 20 for (int j = 0; j < chunkSize; j++) { |
| 21 if (i < bytes.length) { |
| 22 nextChunk.add(bytes[i]); |
| 23 i++; |
| 24 } |
| 25 } |
| 26 byteSink.add(nextChunk); |
| 27 } |
| 28 byteSink.close(); |
| 29 return buffer.toString(); |
| 30 } |
| 31 |
| 32 String decodeAllowMalformed(List<int> bytes, int chunkSize) { |
| 33 StringBuffer buffer = new StringBuffer(); |
| 34 ChunkedConversionSink stringSink = |
| 35 new StringConversionSink.fromStringSink(buffer); |
| 36 var decoder = new Utf8Decoder(allowMalformed: true); |
| 37 var byteSink = decoder.startChunkedConversion(stringSink); |
| 38 int i = 0; |
| 39 while (i < bytes.length) { |
| 40 List nextChunk = []; |
| 41 for (int j = 0; j < chunkSize; j++) { |
| 42 if (i < bytes.length) { |
| 43 nextChunk.add(bytes[i]); |
| 44 i++; |
| 45 } |
| 46 } |
| 47 byteSink.add(nextChunk); |
| 48 } |
| 49 byteSink.close(); |
| 50 return buffer.toString(); |
| 51 } |
| 52 |
| 53 main() { |
| 54 // Test that chunked UTF8-decoder removes leading BOM. |
| 55 Expect.equals("a", decode([0xEF, 0xBB, 0xBF, 0x61], 1)); |
| 56 Expect.equals("a", decode([0xEF, 0xBB, 0xBF, 0x61], 2)); |
| 57 Expect.equals("a", decode([0xEF, 0xBB, 0xBF, 0x61], 3)); |
| 58 Expect.equals("a", decode([0xEF, 0xBB, 0xBF, 0x61], 4)); |
| 59 Expect.equals("a", decodeAllowMalformed([0xEF, 0xBB, 0xBF, 0x61], 1)); |
| 60 Expect.equals("a", decodeAllowMalformed([0xEF, 0xBB, 0xBF, 0x61], 2)); |
| 61 Expect.equals("a", decodeAllowMalformed([0xEF, 0xBB, 0xBF, 0x61], 3)); |
| 62 Expect.equals("a", decodeAllowMalformed([0xEF, 0xBB, 0xBF, 0x61], 4)); |
| 63 Expect.equals("", decode([0xEF, 0xBB, 0xBF], 1)); |
| 64 Expect.equals("", decode([0xEF, 0xBB, 0xBF], 2)); |
| 65 Expect.equals("", decode([0xEF, 0xBB, 0xBF], 3)); |
| 66 Expect.equals("", decode([0xEF, 0xBB, 0xBF], 4)); |
| 67 Expect.equals("", decodeAllowMalformed([0xEF, 0xBB, 0xBF], 1)); |
| 68 Expect.equals("", decodeAllowMalformed([0xEF, 0xBB, 0xBF], 2)); |
| 69 Expect.equals("", decodeAllowMalformed([0xEF, 0xBB, 0xBF], 3)); |
| 70 Expect.equals("", decodeAllowMalformed([0xEF, 0xBB, 0xBF], 4)); |
| 71 Expect.equals("a\u{FEFF}", decode([0x61, 0xEF, 0xBB, 0xBF], 1)); |
| 72 Expect.equals("a\u{FEFF}", decode([0x61, 0xEF, 0xBB, 0xBF], 2)); |
| 73 Expect.equals("a\u{FEFF}", decode([0x61, 0xEF, 0xBB, 0xBF], 3)); |
| 74 Expect.equals("a\u{FEFF}", decode([0x61, 0xEF, 0xBB, 0xBF], 4)); |
| 75 Expect.equals("a\u{FEFF}", decodeAllowMalformed([0x61, 0xEF, 0xBB, 0xBF], 1)); |
| 76 Expect.equals("a\u{FEFF}", decodeAllowMalformed([0x61, 0xEF, 0xBB, 0xBF], 2)); |
| 77 Expect.equals("a\u{FEFF}", decodeAllowMalformed([0x61, 0xEF, 0xBB, 0xBF], 3)); |
| 78 Expect.equals("a\u{FEFF}", decodeAllowMalformed([0x61, 0xEF, 0xBB, 0xBF], 4)); |
| 79 } |
OLD | NEW |