| 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 |
| 6 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
| 7 import 'dart:convert'; | 8 import 'dart:convert'; |
| 8 | 9 |
| 9 List<int> encode(String str) { | 10 List<int> encode(String str) { |
| 10 List<int> bytes; | 11 List<int> bytes; |
| 11 ChunkedConversionSink byteSink = | 12 ChunkedConversionSink byteSink = |
| 12 new ByteConversionSink.withCallback((result) => bytes = result); | 13 new ByteConversionSink.withCallback((result) => bytes = result); |
| 13 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); | 14 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); |
| 14 stringConversionSink.add(str); | 15 stringConversionSink.add(str); |
| 15 stringConversionSink.close(); | 16 stringConversionSink.close(); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 List<int> encode7(String str) { | 77 List<int> encode7(String str) { |
| 77 List<int> bytes; | 78 List<int> bytes; |
| 78 ChunkedConversionSink byteSink = | 79 ChunkedConversionSink byteSink = |
| 79 new ByteConversionSink.withCallback((result) => bytes = result); | 80 new ByteConversionSink.withCallback((result) => bytes = result); |
| 80 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); | 81 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); |
| 81 stringConversionSink.addSlice("1" + str + "2", 1, str.length + 1, false); | 82 stringConversionSink.addSlice("1" + str + "2", 1, str.length + 1, false); |
| 82 stringConversionSink.close(); | 83 stringConversionSink.close(); |
| 83 return bytes; | 84 return bytes; |
| 84 } | 85 } |
| 85 | 86 |
| 86 | |
| 87 int _nextPowerOf2(v) { | 87 int _nextPowerOf2(v) { |
| 88 assert(v > 0); | 88 assert(v > 0); |
| 89 v--; | 89 v--; |
| 90 v |= v >> 1; | 90 v |= v >> 1; |
| 91 v |= v >> 2; | 91 v |= v >> 2; |
| 92 v |= v >> 4; | 92 v |= v >> 4; |
| 93 v |= v >> 8; | 93 v |= v >> 8; |
| 94 v |= v >> 16; | 94 v |= v >> 16; |
| 95 v++; | 95 v++; |
| 96 return v; | 96 return v; |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 bytes[i + 1] = UTF8_TRAILING[1]; | 186 bytes[i + 1] = UTF8_TRAILING[1]; |
| 187 bytes[i + 2] = UTF8_TRAILING[2]; | 187 bytes[i + 2] = UTF8_TRAILING[2]; |
| 188 bytes[i + 3] = UTF8_LEADING[0]; | 188 bytes[i + 3] = UTF8_LEADING[0]; |
| 189 bytes[i + 4] = UTF8_LEADING[1]; | 189 bytes[i + 4] = UTF8_LEADING[1]; |
| 190 bytes[i + 5] = UTF8_LEADING[2]; | 190 bytes[i + 5] = UTF8_LEADING[2]; |
| 191 runTest([bytes, str]); | 191 runTest([bytes, str]); |
| 192 | 192 |
| 193 codeUnits.length = i + 3; | 193 codeUnits.length = i + 3; |
| 194 codeUnits[i] = LEADING_SURROGATE; | 194 codeUnits[i] = LEADING_SURROGATE; |
| 195 codeUnits[i + 1] = TRAILING_SURROGATE; | 195 codeUnits[i + 1] = TRAILING_SURROGATE; |
| 196 codeUnits[i + 2] = CHAR_A; // Add trailing 'a'. | 196 codeUnits[i + 2] = CHAR_A; // Add trailing 'a'. |
| 197 str = new String.fromCharCodes(codeUnits); | 197 str = new String.fromCharCodes(codeUnits); |
| 198 bytes = new List.filled(i + 5, CHAR_A); | 198 bytes = new List.filled(i + 5, CHAR_A); |
| 199 bytes[i] = UTF8_ENCODING[0]; | 199 bytes[i] = UTF8_ENCODING[0]; |
| 200 bytes[i + 1] = UTF8_ENCODING[1]; | 200 bytes[i + 1] = UTF8_ENCODING[1]; |
| 201 bytes[i + 2] = UTF8_ENCODING[2]; | 201 bytes[i + 2] = UTF8_ENCODING[2]; |
| 202 bytes[i + 3] = UTF8_ENCODING[3]; | 202 bytes[i + 3] = UTF8_ENCODING[3]; |
| 203 // No need to assign the 'a' character. The whole list is already filled | 203 // No need to assign the 'a' character. The whole list is already filled |
| 204 // with it. | 204 // with it. |
| 205 runTest([bytes, str]); | 205 runTest([bytes, str]); |
| 206 | 206 |
| 207 codeUnits[i] = TRAILING_SURROGATE; | 207 codeUnits[i] = TRAILING_SURROGATE; |
| 208 codeUnits[i + 1] = TRAILING_SURROGATE; | 208 codeUnits[i + 1] = TRAILING_SURROGATE; |
| 209 codeUnits[i + 2] = CHAR_A; // Add trailing 'a'. | 209 codeUnits[i + 2] = CHAR_A; // Add trailing 'a'. |
| 210 str = new String.fromCharCodes(codeUnits); | 210 str = new String.fromCharCodes(codeUnits); |
| 211 bytes = new List.filled(i + 7, CHAR_A); | 211 bytes = new List.filled(i + 7, CHAR_A); |
| 212 bytes[i] = UTF8_TRAILING[0]; | 212 bytes[i] = UTF8_TRAILING[0]; |
| 213 bytes[i + 1] = UTF8_TRAILING[1]; | 213 bytes[i + 1] = UTF8_TRAILING[1]; |
| 214 bytes[i + 2] = UTF8_TRAILING[2]; | 214 bytes[i + 2] = UTF8_TRAILING[2]; |
| 215 bytes[i + 3] = UTF8_TRAILING[0]; | 215 bytes[i + 3] = UTF8_TRAILING[0]; |
| 216 bytes[i + 4] = UTF8_TRAILING[1]; | 216 bytes[i + 4] = UTF8_TRAILING[1]; |
| 217 bytes[i + 5] = UTF8_TRAILING[2]; | 217 bytes[i + 5] = UTF8_TRAILING[2]; |
| 218 runTest([bytes, str]); | 218 runTest([bytes, str]); |
| 219 | 219 |
| 220 codeUnits[i] = LEADING_SURROGATE; | 220 codeUnits[i] = LEADING_SURROGATE; |
| 221 codeUnits[i + 1] = LEADING_SURROGATE; | 221 codeUnits[i + 1] = LEADING_SURROGATE; |
| 222 codeUnits[i + 2] = CHAR_A; // Add trailing 'a'. | 222 codeUnits[i + 2] = CHAR_A; // Add trailing 'a'. |
| 223 str = new String.fromCharCodes(codeUnits); | 223 str = new String.fromCharCodes(codeUnits); |
| 224 bytes = new List.filled(i + 7, CHAR_A); | 224 bytes = new List.filled(i + 7, CHAR_A); |
| 225 bytes[i] = UTF8_LEADING[0]; | 225 bytes[i] = UTF8_LEADING[0]; |
| 226 bytes[i + 1] = UTF8_LEADING[1]; | 226 bytes[i + 1] = UTF8_LEADING[1]; |
| 227 bytes[i + 2] = UTF8_LEADING[2]; | 227 bytes[i + 2] = UTF8_LEADING[2]; |
| 228 bytes[i + 3] = UTF8_LEADING[0]; | 228 bytes[i + 3] = UTF8_LEADING[0]; |
| 229 bytes[i + 4] = UTF8_LEADING[1]; | 229 bytes[i + 4] = UTF8_LEADING[1]; |
| 230 bytes[i + 5] = UTF8_LEADING[2]; | 230 bytes[i + 5] = UTF8_LEADING[2]; |
| 231 runTest([bytes, str]); | 231 runTest([bytes, str]); |
| 232 | 232 |
| 233 codeUnits[i] = TRAILING_SURROGATE; | 233 codeUnits[i] = TRAILING_SURROGATE; |
| 234 codeUnits[i + 1] = LEADING_SURROGATE; | 234 codeUnits[i + 1] = LEADING_SURROGATE; |
| 235 codeUnits[i + 2] = CHAR_A; // Add trailing 'a'. | 235 codeUnits[i + 2] = CHAR_A; // Add trailing 'a'. |
| 236 str = new String.fromCharCodes(codeUnits); | 236 str = new String.fromCharCodes(codeUnits); |
| 237 bytes = new List.filled(i + 7, CHAR_A); | 237 bytes = new List.filled(i + 7, CHAR_A); |
| 238 bytes[i] = UTF8_TRAILING[0]; | 238 bytes[i] = UTF8_TRAILING[0]; |
| 239 bytes[i + 1] = UTF8_TRAILING[1]; | 239 bytes[i + 1] = UTF8_TRAILING[1]; |
| 240 bytes[i + 2] = UTF8_TRAILING[2]; | 240 bytes[i + 2] = UTF8_TRAILING[2]; |
| 241 bytes[i + 3] = UTF8_LEADING[0]; | 241 bytes[i + 3] = UTF8_LEADING[0]; |
| 242 bytes[i + 4] = UTF8_LEADING[1]; | 242 bytes[i + 4] = UTF8_LEADING[1]; |
| 243 bytes[i + 5] = UTF8_LEADING[2]; | 243 bytes[i + 5] = UTF8_LEADING[2]; |
| 244 runTest([bytes, str]); | 244 runTest([bytes, str]); |
| 245 | 245 |
| 246 // Make sure the invariant is correct. | 246 // Make sure the invariant is correct. |
| 247 codeUnits[i] = CHAR_A; | 247 codeUnits[i] = CHAR_A; |
| 248 } | 248 } |
| 249 } | 249 } |
| OLD | NEW |