OLD | NEW |
1 // Copyright (c) 2013, 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, int chunkSize) { | 9 String decode(List<int> bytes, int chunkSize) { |
10 StringBuffer buffer = new StringBuffer(); | 10 StringBuffer buffer = new StringBuffer(); |
11 ChunkedConversionSink stringSink = | 11 var stringSink = new StringConversionSink.fromStringSink(buffer); |
12 new StringConversionSink.fromStringSink(buffer); | |
13 var byteSink = new Utf8Decoder().startChunkedConversion(stringSink); | 12 var byteSink = new Utf8Decoder().startChunkedConversion(stringSink); |
14 int i = 0; | 13 int i = 0; |
15 while (i < bytes.length) { | 14 while (i < bytes.length) { |
16 List nextChunk = []; | 15 List nextChunk = []; |
17 for (int j = 0; j < chunkSize; j++) { | 16 for (int j = 0; j < chunkSize; j++) { |
18 if (i < bytes.length) { | 17 if (i < bytes.length) { |
19 nextChunk.add(bytes[i]); | 18 nextChunk.add(bytes[i]); |
20 i++; | 19 i++; |
21 } | 20 } |
22 } | 21 } |
23 byteSink.add(nextChunk); | 22 byteSink.add(nextChunk); |
24 } | 23 } |
25 byteSink.close(); | 24 byteSink.close(); |
26 return buffer.toString(); | 25 return buffer.toString(); |
27 } | 26 } |
28 | 27 |
29 String decodeAllowMalformed(List<int> bytes, int chunkSize) { | 28 String decodeAllowMalformed(List<int> bytes, int chunkSize) { |
30 StringBuffer buffer = new StringBuffer(); | 29 StringBuffer buffer = new StringBuffer(); |
31 ChunkedConversionSink stringSink = | 30 var stringSink = new StringConversionSink.fromStringSink(buffer); |
32 new StringConversionSink.fromStringSink(buffer); | |
33 var decoder = new Utf8Decoder(allowMalformed: true); | 31 var decoder = new Utf8Decoder(allowMalformed: true); |
34 var byteSink = decoder.startChunkedConversion(stringSink); | 32 var byteSink = decoder.startChunkedConversion(stringSink); |
35 int i = 0; | 33 int i = 0; |
36 while (i < bytes.length) { | 34 while (i < bytes.length) { |
37 List nextChunk = []; | 35 List nextChunk = []; |
38 for (int j = 0; j < chunkSize; j++) { | 36 for (int j = 0; j < chunkSize; j++) { |
39 if (i < bytes.length) { | 37 if (i < bytes.length) { |
40 nextChunk.add(bytes[i]); | 38 nextChunk.add(bytes[i]); |
41 i++; | 39 i++; |
42 } | 40 } |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 Expect.throws(() => decode(bytes, 3), (e) => e is FormatException); | 144 Expect.throws(() => decode(bytes, 3), (e) => e is FormatException); |
147 Expect.throws(() => decode(bytes, 4), (e) => e is FormatException); | 145 Expect.throws(() => decode(bytes, 4), (e) => e is FormatException); |
148 | 146 |
149 String expected = test[1]; | 147 String expected = test[1]; |
150 Expect.equals(expected, decodeAllowMalformed(bytes, 1)); | 148 Expect.equals(expected, decodeAllowMalformed(bytes, 1)); |
151 Expect.equals(expected, decodeAllowMalformed(bytes, 2)); | 149 Expect.equals(expected, decodeAllowMalformed(bytes, 2)); |
152 Expect.equals(expected, decodeAllowMalformed(bytes, 3)); | 150 Expect.equals(expected, decodeAllowMalformed(bytes, 3)); |
153 Expect.equals(expected, decodeAllowMalformed(bytes, 4)); | 151 Expect.equals(expected, decodeAllowMalformed(bytes, 4)); |
154 } | 152 } |
155 } | 153 } |
OLD | NEW |