OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 part of dart.utf; | 5 part of dart.utf; |
6 | 6 |
7 const int _UTF8_ONE_BYTE_MAX = 0x7f; | 7 const int _UTF8_ONE_BYTE_MAX = 0x7f; |
8 const int _UTF8_TWO_BYTE_MAX = 0x7ff; | 8 const int _UTF8_TWO_BYTE_MAX = 0x7ff; |
9 const int _UTF8_THREE_BYTE_MAX = 0xffff; | 9 const int _UTF8_THREE_BYTE_MAX = 0xffff; |
10 | 10 |
(...skipping 25 matching lines...) Expand all Loading... |
36 | 36 |
37 /** | 37 /** |
38 * Produce a String from a List of UTF-8 encoded bytes. The parameters | 38 * Produce a String from a List of UTF-8 encoded bytes. The parameters |
39 * can set an offset into a list of bytes (as int), limit the length of the | 39 * can set an offset into a list of bytes (as int), limit the length of the |
40 * values to be decoded, and override the default Unicode replacement character. | 40 * values to be decoded, and override the default Unicode replacement character. |
41 * Set the replacementCharacter to null to throw an ArgumentError | 41 * Set the replacementCharacter to null to throw an ArgumentError |
42 * rather than replace the bad value. | 42 * rather than replace the bad value. |
43 */ | 43 */ |
44 String decodeUtf8(List<int> bytes, [int offset = 0, int length, | 44 String decodeUtf8(List<int> bytes, [int offset = 0, int length, |
45 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | 45 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { |
46 return codepointsToString( | 46 return new String.fromCharCodes( |
47 (new Utf8Decoder(bytes, offset, length, replacementCodepoint)) | 47 (new Utf8Decoder(bytes, offset, length, replacementCodepoint)) |
48 .decodeRest()); | 48 .decodeRest()); |
49 } | 49 } |
50 | 50 |
51 /** | 51 /** |
52 * Produce a sequence of UTF-8 encoded bytes from the provided string. | 52 * Produce a sequence of UTF-8 encoded bytes from the provided string. |
53 */ | 53 */ |
54 List<int> encodeUtf8(String str) => | 54 List<int> encodeUtf8(String str) => |
55 codepointsToUtf8(stringToCodepoints(str)); | 55 codepointsToUtf8(stringToCodepoints(str)); |
56 | 56 |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 return true; | 266 return true; |
267 } else if (replacementCodepoint != null) { | 267 } else if (replacementCodepoint != null) { |
268 _current = replacementCodepoint; | 268 _current = replacementCodepoint; |
269 return true; | 269 return true; |
270 } else { | 270 } else { |
271 throw new ArgumentError( | 271 throw new ArgumentError( |
272 "Invalid UTF8 at ${utf8EncodedBytesIterator.position - j}"); | 272 "Invalid UTF8 at ${utf8EncodedBytesIterator.position - j}"); |
273 } | 273 } |
274 } | 274 } |
275 } | 275 } |
OLD | NEW |