Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library utf8_test; | 5 library utf8_test; |
| 6 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
| 7 import 'dart:convert'; | 7 import 'dart:convert'; |
| 8 | 8 |
| 9 String decode(List<int> bytes) => new Utf8Decoder().convert(bytes); | 9 String decode(List<int> bytes) { |
| 10 String decodeAllowMalformed(List<int> bytes) { | 10 StringBuffer buffer = new StringBuffer(); |
| 11 return new Utf8Decoder(allowMalformed: true).convert(bytes); | 11 ChunkedConversionSink stringSink = |
| 12 new StringConversionSink.fromStringSink(buffer); | |
| 13 var byteSink = new Utf8Decoder().startChunkedConversion(stringSink); | |
| 14 bytes.forEach((byte) { byteSink.add([byte]); }); | |
|
Søren Gjesse
2013/07/24 09:26:41
Consider also testing with adding 2, 3, ... bytes
floitsch
2013/07/24 18:31:15
Done.
| |
| 15 byteSink.close(); | |
| 16 return buffer.toString(); | |
| 12 } | 17 } |
| 13 | 18 |
| 14 String decode2(List<int> bytes) => UTF8.decode(bytes); | 19 String decodeAllowMalformed(List<int> bytes) { |
| 15 String decodeAllowMalformed2(List<int> bytes) { | 20 StringBuffer buffer = new StringBuffer(); |
| 16 return UTF8.decode(bytes, allowMalformed: true); | 21 ChunkedConversionSink stringSink = |
| 17 } | 22 new StringConversionSink.fromStringSink(buffer); |
| 18 | 23 var decoder = new Utf8Decoder(allowMalformed: true); |
| 19 String decode3(List<int> bytes) => new Utf8Codec().decode(bytes); | 24 var byteSink = decoder.startChunkedConversion(stringSink); |
| 20 String decodeAllowMalformed3(List<int> bytes) { | 25 bytes.forEach((byte) { byteSink.add([byte]); }); |
| 21 return new Utf8Codec(allowMalformed: true).decode(bytes); | 26 byteSink.close(); |
| 22 } | 27 return buffer.toString(); |
| 23 | |
| 24 String decode4(List<int> bytes) => new Utf8Codec().decoder.convert(bytes); | |
| 25 String decodeAllowMalformed4(List<int> bytes) { | |
| 26 return new Utf8Codec(allowMalformed: true).decoder.convert(bytes); | |
| 27 } | 28 } |
| 28 | 29 |
| 29 final TESTS = [ | 30 final TESTS = [ |
| 30 // Unfinished UTF-8 sequences. | 31 // Unfinished UTF-8 sequences. |
| 31 [ 0xc3 ], | 32 [ 0xc3 ], |
| 32 [ 0xE2, 0x82 ], | 33 [ 0xE2, 0x82 ], |
| 33 [ 0xF0, 0xA4, 0xAD ], | 34 [ 0xF0, 0xA4, 0xAD ], |
| 34 // Overlong encoding of euro-sign. | 35 // Overlong encoding of euro-sign. |
| 35 [ 0xF0, 0x82, 0x82, 0xAC ], | 36 [ 0xF0, 0x82, 0x82, 0xAC ], |
| 36 // Other overlong/unfinished sequences. | 37 // Other overlong/unfinished sequences. |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 114 var allTests2 = TESTS2.map((test) { | 115 var allTests2 = TESTS2.map((test) { |
| 115 // Pairs of test and expected string output when malformed strings are | 116 // Pairs of test and expected string output when malformed strings are |
| 116 // allowed. Replacement character: U+FFFD | 117 // allowed. Replacement character: U+FFFD |
| 117 String expected = test[1].replaceAll("X", "\u{FFFD}"); | 118 String expected = test[1].replaceAll("X", "\u{FFFD}"); |
| 118 return [test[0], expected]; | 119 return [test[0], expected]; |
| 119 }); | 120 }); |
| 120 | 121 |
| 121 for (var test in []..addAll(allTests)..addAll(allTests2)) { | 122 for (var test in []..addAll(allTests)..addAll(allTests2)) { |
| 122 List<int> bytes = test[0]; | 123 List<int> bytes = test[0]; |
| 123 Expect.throws(() => decode(bytes), (e) => e is FormatException); | 124 Expect.throws(() => decode(bytes), (e) => e is FormatException); |
| 124 Expect.throws(() => decode2(bytes), (e) => e is FormatException); | |
| 125 Expect.throws(() => decode3(bytes), (e) => e is FormatException); | |
| 126 Expect.throws(() => decode4(bytes), (e) => e is FormatException); | |
| 127 | 125 |
| 128 String expected = test[1]; | 126 String expected = test[1]; |
| 129 Expect.equals(expected, decodeAllowMalformed(bytes)); | 127 Expect.equals(expected, decodeAllowMalformed(bytes)); |
| 130 Expect.equals(expected, decodeAllowMalformed2(bytes)); | |
| 131 Expect.equals(expected, decodeAllowMalformed3(bytes)); | |
| 132 Expect.equals(expected, decodeAllowMalformed4(bytes)); | |
| 133 } | 128 } |
| 134 } | 129 } |
| OLD | NEW |