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 StringBuffer buffer = new StringBuffer(); |
| 11 // Use a non-chunked interface. |
| 12 String result; |
| 13 ChunkedConversionSink chunkedSink = |
| 14 new ChunkedConversionSink<String, dynamic>.withCallback((decoded) { |
| 15 result = decoded; |
| 16 }); |
| 17 var byteSink = new Utf8Decoder().startChunkedConversion(chunkedSink); |
| 18 bytes.forEach((byte) { byteSink.add([byte]); }); |
| 19 byteSink.close(); |
| 20 return result; |
| 21 } |
10 | 22 |
11 main() { | 23 main() { |
12 // Google favorite: "Îñţérñåţîöñåļîžåţîờñ". | 24 // Google favorite: "Îñţérñåţîöñåļîžåţîờñ". |
13 String string = decode([0xc3, 0x8e, 0xc3, 0xb1, 0xc5, 0xa3, 0xc3, 0xa9, 0x72, | 25 String string = decode([0xc3, 0x8e, 0xc3, 0xb1, 0xc5, 0xa3, 0xc3, 0xa9, 0x72, |
14 0xc3, 0xb1, 0xc3, 0xa5, 0xc5, 0xa3, 0xc3, 0xae, 0xc3, | 26 0xc3, 0xb1, 0xc3, 0xa5, 0xc5, 0xa3, 0xc3, 0xae, 0xc3, |
15 0xb6, 0xc3, 0xb1, 0xc3, 0xa5, 0xc4, 0xbc, 0xc3, 0xae, | 27 0xb6, 0xc3, 0xb1, 0xc3, 0xa5, 0xc4, 0xbc, 0xc3, 0xae, |
16 0xc5, 0xbe, 0xc3, 0xa5, 0xc5, 0xa3, 0xc3, 0xae, 0xe1, | 28 0xc5, 0xbe, 0xc3, 0xa5, 0xc5, 0xa3, 0xc3, 0xae, 0xe1, |
17 0xbb, 0x9d, 0xc3, 0xb1]); | 29 0xbb, 0x9d, 0xc3, 0xb1]); |
18 Expect.stringEquals("Îñţérñåţîöñåļîžåţîờñ", string); | 30 Expect.stringEquals("Îñţérñåţîöñåļîžåţîờñ", string); |
19 | 31 |
(...skipping 15 matching lines...) Expand all Loading... |
35 0xa4, 0xbe, 0xe0, 0xa4, 0xae, 0xe0, 0xa4, 0xbe, 0xe0, 0xa4, | 47 0xa4, 0xbe, 0xe0, 0xa4, 0xae, 0xe0, 0xa4, 0xbe, 0xe0, 0xa4, |
36 0xb2, 0xe0, 0xa5, 0x88]); | 48 0xb2, 0xe0, 0xa5, 0x88]); |
37 Expect.stringEquals("िसवा अणामालै", string); | 49 Expect.stringEquals("िसवा अणामालै", string); |
38 | 50 |
39 // DESERET CAPITAL LETTER BEE, unicode 0x10412(0xD801+0xDC12) | 51 // DESERET CAPITAL LETTER BEE, unicode 0x10412(0xD801+0xDC12) |
40 // UTF-8: F0 90 90 92 | 52 // UTF-8: F0 90 90 92 |
41 string = decode([0xf0, 0x90, 0x90, 0x92]); | 53 string = decode([0xf0, 0x90, 0x90, 0x92]); |
42 Expect.equals(string.length, 2); | 54 Expect.equals(string.length, 2); |
43 Expect.equals("𐐒".length, 2); | 55 Expect.equals("𐐒".length, 2); |
44 Expect.stringEquals("𐐒", string); | 56 Expect.stringEquals("𐐒", string); |
45 | |
46 // TODO(ahe): Add tests of bad input. | |
47 } | 57 } |
OLD | NEW |