| 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 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
| 6 import 'dart:convert'; | 6 import 'dart:convert'; |
| 7 import 'unicode_tests.dart'; | 7 import 'unicode_tests.dart'; |
| 8 | 8 |
| 9 List<int> encode(String str) { | 9 List<int> encode(String str) { |
| 10 List<int> bytes; | 10 List<int> bytes; |
| 11 ChunkedConversionSink byteSink = | 11 var byteSink = |
| 12 new ByteConversionSink.withCallback((result) => bytes = result); | 12 new ByteConversionSink.withCallback((result) => bytes = result); |
| 13 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); | 13 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); |
| 14 stringConversionSink.add(str); | 14 stringConversionSink.add(str); |
| 15 stringConversionSink.close(); | 15 stringConversionSink.close(); |
| 16 return bytes; | 16 return bytes; |
| 17 } | 17 } |
| 18 | 18 |
| 19 List<int> encode2(String str) { | 19 List<int> encode2(String str) { |
| 20 List<int> bytes; | 20 List<int> bytes; |
| 21 ChunkedConversionSink byteSink = | 21 var byteSink = |
| 22 new ByteConversionSink.withCallback((result) => bytes = result); | 22 new ByteConversionSink.withCallback((result) => bytes = result); |
| 23 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); | 23 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); |
| 24 ClosableStringSink stringSink = stringConversionSink.asStringSink(); | 24 ClosableStringSink stringSink = stringConversionSink.asStringSink(); |
| 25 stringSink.write(str); | 25 stringSink.write(str); |
| 26 stringSink.close(); | 26 stringSink.close(); |
| 27 return bytes; | 27 return bytes; |
| 28 } | 28 } |
| 29 | 29 |
| 30 List<int> encode3(String str) { | 30 List<int> encode3(String str) { |
| 31 List<int> bytes; | 31 List<int> bytes; |
| 32 ChunkedConversionSink byteSink = | 32 var byteSink = |
| 33 new ByteConversionSink.withCallback((result) => bytes = result); | 33 new ByteConversionSink.withCallback((result) => bytes = result); |
| 34 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); | 34 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); |
| 35 ClosableStringSink stringSink = stringConversionSink.asStringSink(); | 35 ClosableStringSink stringSink = stringConversionSink.asStringSink(); |
| 36 str.codeUnits.forEach(stringSink.writeCharCode); | 36 str.codeUnits.forEach(stringSink.writeCharCode); |
| 37 stringSink.close(); | 37 stringSink.close(); |
| 38 return bytes; | 38 return bytes; |
| 39 } | 39 } |
| 40 | 40 |
| 41 List<int> encode4(String str) { | 41 List<int> encode4(String str) { |
| 42 List<int> bytes; | 42 List<int> bytes; |
| 43 ChunkedConversionSink byteSink = | 43 var byteSink = |
| 44 new ByteConversionSink.withCallback((result) => bytes = result); | 44 new ByteConversionSink.withCallback((result) => bytes = result); |
| 45 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); | 45 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); |
| 46 ClosableStringSink stringSink = stringConversionSink.asStringSink(); | 46 ClosableStringSink stringSink = stringConversionSink.asStringSink(); |
| 47 str.runes.forEach(stringSink.writeCharCode); | 47 str.runes.forEach(stringSink.writeCharCode); |
| 48 stringSink.close(); | 48 stringSink.close(); |
| 49 return bytes; | 49 return bytes; |
| 50 } | 50 } |
| 51 | 51 |
| 52 List<int> encode5(String str) { | 52 List<int> encode5(String str) { |
| 53 List<int> bytes; | 53 List<int> bytes; |
| 54 ChunkedConversionSink byteSink = | 54 var byteSink = |
| 55 new ByteConversionSink.withCallback((result) => bytes = result); | 55 new ByteConversionSink.withCallback((result) => bytes = result); |
| 56 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); | 56 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); |
| 57 ByteConversionSink inputByteSink = stringConversionSink.asUtf8Sink(false); | 57 ByteConversionSink inputByteSink = stringConversionSink.asUtf8Sink(false); |
| 58 List<int> tmpBytes = UTF8.encode(str); | 58 List<int> tmpBytes = UTF8.encode(str); |
| 59 inputByteSink.add(tmpBytes); | 59 inputByteSink.add(tmpBytes); |
| 60 inputByteSink.close(); | 60 inputByteSink.close(); |
| 61 return bytes; | 61 return bytes; |
| 62 } | 62 } |
| 63 | 63 |
| 64 List<int> encode6(String str) { | 64 List<int> encode6(String str) { |
| 65 List<int> bytes; | 65 List<int> bytes; |
| 66 ChunkedConversionSink byteSink = | 66 var byteSink = |
| 67 new ByteConversionSink.withCallback((result) => bytes = result); | 67 new ByteConversionSink.withCallback((result) => bytes = result); |
| 68 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); | 68 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); |
| 69 ByteConversionSink inputByteSink = stringConversionSink.asUtf8Sink(false); | 69 ByteConversionSink inputByteSink = stringConversionSink.asUtf8Sink(false); |
| 70 List<int> tmpBytes = UTF8.encode(str); | 70 List<int> tmpBytes = UTF8.encode(str); |
| 71 tmpBytes.forEach((b) => inputByteSink.addSlice([0, b, 1], 1, 2, false)); | 71 tmpBytes.forEach((b) => inputByteSink.addSlice([0, b, 1], 1, 2, false)); |
| 72 inputByteSink.close(); | 72 inputByteSink.close(); |
| 73 return bytes; | 73 return bytes; |
| 74 } | 74 } |
| 75 | 75 |
| 76 List<int> encode7(String str) { | 76 List<int> encode7(String str) { |
| 77 List<int> bytes; | 77 List<int> bytes; |
| 78 ChunkedConversionSink byteSink = | 78 var byteSink = |
| 79 new ByteConversionSink.withCallback((result) => bytes = result); | 79 new ByteConversionSink.withCallback((result) => bytes = result); |
| 80 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); | 80 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); |
| 81 stringConversionSink.addSlice("1" + str + "2", 1, str.length + 1, false); | 81 stringConversionSink.addSlice("1" + str + "2", 1, str.length + 1, false); |
| 82 stringConversionSink.close(); | 82 stringConversionSink.close(); |
| 83 return bytes; | 83 return bytes; |
| 84 } | 84 } |
| 85 | 85 |
| 86 | 86 |
| 87 main() { | 87 main() { |
| 88 for (var test in UNICODE_TESTS) { | 88 for (var test in UNICODE_TESTS) { |
| 89 List<int> bytes = test[0]; | 89 List<int> bytes = test[0]; |
| 90 String string = test[1]; | 90 String string = test[1]; |
| 91 Expect.listEquals(bytes, encode(string)); | 91 Expect.listEquals(bytes, encode(string)); |
| 92 Expect.listEquals(bytes, encode2(string)); | 92 Expect.listEquals(bytes, encode2(string)); |
| 93 Expect.listEquals(bytes, encode3(string)); | 93 Expect.listEquals(bytes, encode3(string)); |
| 94 Expect.listEquals(bytes, encode4(string)); | 94 Expect.listEquals(bytes, encode4(string)); |
| 95 Expect.listEquals(bytes, encode5(string)); | 95 Expect.listEquals(bytes, encode5(string)); |
| 96 Expect.listEquals(bytes, encode6(string)); | 96 Expect.listEquals(bytes, encode6(string)); |
| 97 Expect.listEquals(bytes, encode7(string)); | 97 Expect.listEquals(bytes, encode7(string)); |
| 98 } | 98 } |
| 99 } | 99 } |
| OLD | NEW |