| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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; | |
| 6 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
| 7 import 'dart:convert'; | 6 import 'dart:convert'; |
| 8 | 7 |
| 9 String decode(List<int> bytes) => new Utf8Decoder().convert(bytes); | 8 String decode(List<int> inputBytes) { |
| 10 String decodeAllowMalformed(List<int> bytes) { | 9 List<int> bytes; |
| 11 return new Utf8Decoder(allowMalformed: true).convert(bytes); | 10 ChunkedConversionSink byteSink = |
| 11 new ByteConversionSink.withCallback((result) => bytes = result); |
| 12 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); |
| 13 ByteConversionSink inputByteSink = stringConversionSink.asUtf8Sink(false); |
| 14 inputByteSink.add(inputBytes); |
| 15 inputByteSink.close(); |
| 16 return UTF8.decode(bytes); |
| 12 } | 17 } |
| 13 | 18 |
| 14 String decode2(List<int> bytes) => UTF8.decode(bytes); | 19 String decode2(List<int> inputBytes) { |
| 15 String decodeAllowMalformed2(List<int> bytes) { | 20 List<int> bytes; |
| 16 return UTF8.decode(bytes, allowMalformed: true); | 21 ChunkedConversionSink byteSink = |
| 22 new ByteConversionSink.withCallback((result) => bytes = result); |
| 23 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); |
| 24 ByteConversionSink inputByteSink = stringConversionSink.asUtf8Sink(false); |
| 25 inputBytes.forEach((b) => inputByteSink.addSlice([0, b, 1], 1, 2, false)); |
| 26 inputByteSink.close(); |
| 27 return UTF8.decode(bytes); |
| 17 } | 28 } |
| 18 | 29 |
| 19 String decode3(List<int> bytes) => new Utf8Codec().decode(bytes); | 30 String decodeAllowMalformed(List<int> inputBytes) { |
| 20 String decodeAllowMalformed3(List<int> bytes) { | 31 List<int> bytes; |
| 21 return new Utf8Codec(allowMalformed: true).decode(bytes); | 32 ChunkedConversionSink byteSink = |
| 33 new ByteConversionSink.withCallback((result) => bytes = result); |
| 34 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); |
| 35 ByteConversionSink inputByteSink = stringConversionSink.asUtf8Sink(true); |
| 36 inputByteSink.add(inputBytes); |
| 37 inputByteSink.close(); |
| 38 return UTF8.decode(bytes); |
| 22 } | 39 } |
| 23 | 40 |
| 24 String decode4(List<int> bytes) => new Utf8Codec().decoder.convert(bytes); | 41 String decodeAllowMalformed2(List<int> inputBytes) { |
| 25 String decodeAllowMalformed4(List<int> bytes) { | 42 List<int> bytes; |
| 26 return new Utf8Codec(allowMalformed: true).decoder.convert(bytes); | 43 ChunkedConversionSink byteSink = |
| 44 new ByteConversionSink.withCallback((result) => bytes = result); |
| 45 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); |
| 46 ByteConversionSink inputByteSink = stringConversionSink.asUtf8Sink(true); |
| 47 inputBytes.forEach((b) => inputByteSink.addSlice([0, b, 1], 1, 2, false)); |
| 48 inputByteSink.close(); |
| 49 return UTF8.decode(bytes); |
| 27 } | 50 } |
| 28 | 51 |
| 52 |
| 29 final TESTS = [ | 53 final TESTS = [ |
| 30 // Unfinished UTF-8 sequences. | 54 // Unfinished UTF-8 sequences. |
| 31 [ 0xc3 ], | 55 [ 0xc3 ], |
| 32 [ 0xE2, 0x82 ], | 56 [ 0xE2, 0x82 ], |
| 33 [ 0xF0, 0xA4, 0xAD ], | 57 [ 0xF0, 0xA4, 0xAD ], |
| 34 // Overlong encoding of euro-sign. | 58 // Overlong encoding of euro-sign. |
| 35 [ 0xF0, 0x82, 0x82, 0xAC ], | 59 [ 0xF0, 0x82, 0x82, 0xAC ], |
| 36 // Other overlong/unfinished sequences. | 60 // Other overlong/unfinished sequences. |
| 37 [ 0xC0 ], | 61 [ 0xC0 ], |
| 38 [ 0xC1 ], | 62 [ 0xC1 ], |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 // Pairs of test and expected string output when malformed strings are | 139 // Pairs of test and expected string output when malformed strings are |
| 116 // allowed. Replacement character: U+FFFD | 140 // allowed. Replacement character: U+FFFD |
| 117 String expected = test[1].replaceAll("X", "\u{FFFD}"); | 141 String expected = test[1].replaceAll("X", "\u{FFFD}"); |
| 118 return [test[0], expected]; | 142 return [test[0], expected]; |
| 119 }); | 143 }); |
| 120 | 144 |
| 121 for (var test in []..addAll(allTests)..addAll(allTests2)) { | 145 for (var test in []..addAll(allTests)..addAll(allTests2)) { |
| 122 List<int> bytes = test[0]; | 146 List<int> bytes = test[0]; |
| 123 Expect.throws(() => decode(bytes), (e) => e is FormatException); | 147 Expect.throws(() => decode(bytes), (e) => e is FormatException); |
| 124 Expect.throws(() => decode2(bytes), (e) => e is FormatException); | 148 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 | 149 |
| 128 String expected = test[1]; | 150 String expected = test[1]; |
| 129 Expect.equals(expected, decodeAllowMalformed(bytes)); | 151 Expect.equals(expected, decodeAllowMalformed(bytes)); |
| 130 Expect.equals(expected, decodeAllowMalformed2(bytes)); | 152 Expect.equals(expected, decodeAllowMalformed2(bytes)); |
| 131 Expect.equals(expected, decodeAllowMalformed3(bytes)); | |
| 132 Expect.equals(expected, decodeAllowMalformed4(bytes)); | |
| 133 } | 153 } |
| 134 } | 154 } |
| OLD | NEW |