| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 main() { | 5 main() { |
| 6 test(String s) { | 6 test(String s) { |
| 7 Iterable<int> units = s.codeUnits; | 7 Iterable<int> units = s.codeUnits; |
| 8 List<int> expectedUnits = <int>[]; | 8 List<int> expectedUnits = <int>[]; |
| 9 for (int i = 0; i < s.length; i++) { | 9 for (int i = 0; i < s.length; i++) { |
| 10 expectedUnits.add(s.codeUnitAt(i)); | 10 expectedUnits.add(s.codeUnitAt(i)); |
| 11 } | 11 } |
| 12 | 12 |
| 13 Expect.equals(s.length, units.length); | 13 Expect.equals(s.length, units.length); |
| 14 for (int i = 0; i < s.length; i++) { | 14 for (int i = 0; i < s.length; i++) { |
| 15 Expect.equals(s.codeUnitAt(i), units.elementAt(i)); | 15 Expect.equals(s.codeUnitAt(i), units.elementAt(i)); |
| 16 } | 16 } |
| 17 | 17 |
| 18 // for-in | 18 // for-in |
| 19 var res = []; | 19 var res = []; |
| 20 for (int unit in units) { | 20 for (int unit in units) { |
| 21 res.add(unit); | 21 res.add(unit); |
| 22 } | 22 } |
| 23 Expect.listEquals(expectedUnits, res); | 23 Expect.listEquals(expectedUnits, res); |
| 24 | 24 |
| 25 // .map | 25 // .map |
| 26 Expect.listEquals(expectedUnits.map((x) => x.toRadixString(16)).toList(), | 26 Expect.listEquals(expectedUnits.map((x) => x.toRadixString(16)).toList(), |
| 27 units.map((x) => x.toRadixString(16)).toList()); | 27 units.map((x) => x.toRadixString(16)).toList()); |
| 28 | |
| 29 if (s == "") { | |
| 30 Expect.throws(() => units.first, (e) => e is StateError); | |
| 31 Expect.throws(() => units.last, (e) => e is StateError); | |
| 32 Expect.throws(() => units[0], (e) => e is RangeError); | |
| 33 Expect.throws(() => units[0] = 499, (e) => e is UnsupportedError); | |
| 34 Expect.listEquals([], units.getRange(0, 0)); | |
| 35 Expect.equals(-1, units.indexOf(42)); | |
| 36 Expect.equals(-1, units.lastIndexOf(499)); | |
| 37 } else { | |
| 38 Expect.equals(s.codeUnitAt(0), units.first); | |
| 39 Expect.equals(s.codeUnitAt(s.length - 1), units.last); | |
| 40 Expect.equals(s.codeUnitAt(0), units[0]); | |
| 41 Expect.throws(() { units[0] = 499; }, (e) => e is UnsupportedError); | |
| 42 List<int> sub = units.getRange(1, units.length - 1); | |
| 43 Expect.listEquals(s.substring(1, s.length).codeUnits, sub); | |
| 44 Expect.equals(-1, units.indexOf(-1)); | |
| 45 Expect.equals(0, units.indexOf(units[0])); | |
| 46 Expect.equals(-1, units.lastIndexOf(-1)); | |
| 47 Expect.equals(units.length - 1, units.lastIndexOf(units[units.length - 1])
); | |
| 48 } | |
| 49 | |
| 50 Iterable reversed = units.reversed; | |
| 51 int i = units.length - 1; | |
| 52 for (int codeUnit in reversed) { | |
| 53 Expect.equals(units[i--], codeUnit); | |
| 54 } | |
| 55 } | 28 } |
| 56 | 29 |
| 57 test(""); | |
| 58 test("abc"); | 30 test("abc"); |
| 59 test("\x00\u0000\u{000000}"); | 31 test("\x00\u0000\u{000000}"); |
| 60 test("\u{ffff}\u{10000}\u{10ffff}"); | 32 test("\u{ffff}\u{10000}\u{10ffff}"); |
| 61 String string = new String.fromCharCodes( | 33 String string = new String.fromCharCodes( |
| 62 [0xdc00, 0xd800, 61, 0xd9ab, 0xd9ab, 0xddef, 0xddef, 62, 0xdc00, 0xd800]); | 34 [0xdc00, 0xd800, 61, 0xd9ab, 0xd9ab, 0xddef, 0xddef, 62, 0xdc00, 0xd800]); |
| 63 test(string); | 35 test(string); |
| 64 string = "\x00\x7f\xff\u0100\ufeff\uffef\uffff" | 36 string = "\x00\x7f\xff\u0100\ufeff\uffef\uffff" |
| 65 "\u{10000}\u{12345}\u{1d800}\u{1dc00}\u{1ffef}\u{1ffff}"; | 37 "\u{10000}\u{12345}\u{1d800}\u{1dc00}\u{1ffef}\u{1ffff}"; |
| 66 test(string); | 38 test(string); |
| 67 | 39 |
| 68 // Reading each unit of a surrogate pair works. | 40 // Reading each unit of a surrogate pair works. |
| 69 var r = "\u{10000}".codeUnits; | 41 var r = "\u{10000}".codeUnits; |
| 70 var it = r.iterator; | 42 var it = r.iterator; |
| 71 Expect.isTrue(it.moveNext()); | 43 Expect.isTrue(it.moveNext()); |
| 72 Expect.equals(0xD800, it.current); | 44 Expect.equals(0xD800, it.current); |
| 73 Expect.isTrue(it.moveNext()); | 45 Expect.isTrue(it.moveNext()); |
| 74 Expect.equals(0xDC00, it.current); | 46 Expect.equals(0xDC00, it.current); |
| 75 } | 47 } |
| OLD | NEW |