| 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 part of utf; | 5 part of utf; |
| 6 | 6 |
| 7 // TODO(floitsch): make this transformer reusable. | 7 // TODO(floitsch): make this transformer reusable. |
| 8 abstract class _StringDecoder | 8 abstract class _StringDecoder |
| 9 implements StreamTransformer<List<int>, String>, EventSink<List<int>> { | 9 implements StreamTransformer<List<int>, String>, EventSink<List<int>> { |
| 10 List<int> _carry; | 10 List<int> _carry; |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 List<int> _processString(String string); | 198 List<int> _processString(String string); |
| 199 } | 199 } |
| 200 | 200 |
| 201 /** | 201 /** |
| 202 * StringTransformer that UTF-8 encodes a stream of strings. | 202 * StringTransformer that UTF-8 encodes a stream of strings. |
| 203 */ | 203 */ |
| 204 class Utf8EncoderTransformer extends _StringEncoder { | 204 class Utf8EncoderTransformer extends _StringEncoder { |
| 205 List<int> _processString(String string) { | 205 List<int> _processString(String string) { |
| 206 var bytes = []; | 206 var bytes = []; |
| 207 int pos = 0; | 207 int pos = 0; |
| 208 List<int> codepoints = _utf16CodeUnitsToCodepoints(string.codeUnits); | 208 List<int> codepoints = utf16CodeUnitsToCodepoints(string.codeUnits); |
| 209 int length = codepoints.length; | 209 int length = codepoints.length; |
| 210 for (int i = 0; i < length; i++) { | 210 for (int i = 0; i < length; i++) { |
| 211 int additionalBytes; | 211 int additionalBytes; |
| 212 int charCode = codepoints[i]; | 212 int charCode = codepoints[i]; |
| 213 if (charCode <= 0x007F) { | 213 if (charCode <= 0x007F) { |
| 214 additionalBytes = 0; | 214 additionalBytes = 0; |
| 215 bytes.add(charCode); | 215 bytes.add(charCode); |
| 216 } else if (charCode <= 0x07FF) { | 216 } else if (charCode <= 0x07FF) { |
| 217 // 110xxxxx (xxxxx is top 5 bits). | 217 // 110xxxxx (xxxxx is top 5 bits). |
| 218 bytes.add(((charCode >> 6) & 0x1F) | 0xC0); | 218 bytes.add(((charCode >> 6) & 0x1F) | 0xC0); |
| 219 additionalBytes = 1; | 219 additionalBytes = 1; |
| 220 } else if (charCode <= 0xFFFF) { | 220 } else if (charCode <= 0xFFFF) { |
| 221 // 1110xxxx (xxxx is top 4 bits) | 221 // 1110xxxx (xxxx is top 4 bits) |
| 222 bytes.add(((charCode >> 12) & 0x0F)| 0xE0); | 222 bytes.add(((charCode >> 12) & 0x0F)| 0xE0); |
| 223 additionalBytes = 2; | 223 additionalBytes = 2; |
| 224 } else { | 224 } else { |
| 225 // 11110xxx (xxx is top 3 bits) | 225 // 11110xxx (xxx is top 3 bits) |
| 226 bytes.add(((charCode >> 18) & 0x07) | 0xF0); | 226 bytes.add(((charCode >> 18) & 0x07) | 0xF0); |
| 227 additionalBytes = 3; | 227 additionalBytes = 3; |
| 228 } | 228 } |
| 229 for (int i = additionalBytes; i > 0; i--) { | 229 for (int i = additionalBytes; i > 0; i--) { |
| 230 // 10xxxxxx (xxxxxx is next 6 bits from the top). | 230 // 10xxxxxx (xxxxxx is next 6 bits from the top). |
| 231 bytes.add(((charCode >> (6 * (i - 1))) & 0x3F) | 0x80); | 231 bytes.add(((charCode >> (6 * (i - 1))) & 0x3F) | 0x80); |
| 232 } | 232 } |
| 233 pos += additionalBytes + 1; | 233 pos += additionalBytes + 1; |
| 234 } | 234 } |
| 235 return bytes; | 235 return bytes; |
| 236 } | 236 } |
| 237 } | 237 } |
| OLD | NEW |