| 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 // Interface for decoders decoding binary data into string data. The | 5 // Interface for decoders decoding binary data into string data. The |
| 6 // decoder keeps track of line breaks during decoding. | 6 // decoder keeps track of line breaks during decoding. |
| 7 abstract class _StringDecoder { | 7 abstract class _StringDecoder { |
| 8 // Add more binary data to be decoded. The ownership of the buffer | 8 // Add more binary data to be decoded. The ownership of the buffer |
| 9 // is transfered to the decoder and the caller most not modify it any more. | 9 // is transfered to the decoder and the caller most not modify it any more. |
| 10 int write(List<int> buffer); | 10 int write(List<int> buffer); |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 List<int> result = new Uint8List(size); | 263 List<int> result = new Uint8List(size); |
| 264 _encodeString(string, result); | 264 _encodeString(string, result); |
| 265 return result; | 265 return result; |
| 266 } | 266 } |
| 267 | 267 |
| 268 static int _encodingSize(String string) => _encodeString(string, null); | 268 static int _encodingSize(String string) => _encodeString(string, null); |
| 269 | 269 |
| 270 static int _encodeString(String string, List<int> buffer) { | 270 static int _encodeString(String string, List<int> buffer) { |
| 271 int pos = 0; | 271 int pos = 0; |
| 272 int length = string.length; | 272 int length = string.length; |
| 273 // TODO(erikcorry): Use new iterator over charcodes. |
| 273 for (int i = 0; i < length; i++) { | 274 for (int i = 0; i < length; i++) { |
| 274 int additionalBytes; | 275 int additionalBytes; |
| 275 int charCode = string.charCodeAt(i); | 276 int charCode = string.charCodeAt(i); |
| 276 if (charCode <= 0x007F) { | 277 if (charCode <= 0x007F) { |
| 277 additionalBytes = 0; | 278 additionalBytes = 0; |
| 278 if (buffer != null) buffer[pos] = charCode; | 279 if (buffer != null) buffer[pos] = charCode; |
| 279 } else if (charCode <= 0x07FF) { | 280 } else if (charCode <= 0x07FF) { |
| 280 // 110xxxxx (xxxxx is top 5 bits). | 281 // 110xxxxx (xxxxx is top 5 bits). |
| 281 if (buffer != null) buffer[pos] = ((charCode >> 6) & 0x1F) | 0xC0; | 282 if (buffer != null) buffer[pos] = ((charCode >> 6) & 0x1F) | 0xC0; |
| 282 additionalBytes = 1; | 283 additionalBytes = 1; |
| 283 } else if (charCode <= 0xFFFF) { | 284 } else if (charCode <= 0xFFFF) { |
| 284 // 1110xxxx (xxxx is top 4 bits) | 285 // 1110xxxx (xxxx is top 4 bits) |
| 285 if (buffer != null) buffer[pos] = ((charCode >> 12) & 0x0F)| 0xE0; | 286 if (buffer != null) buffer[pos] = ((charCode >> 12) & 0x0F)| 0xE0; |
| 286 additionalBytes = 2; | 287 additionalBytes = 2; |
| 287 } else { | 288 } else { |
| 289 i++; // Skip surrogate pair. |
| 288 // 11110xxx (xxx is top 3 bits) | 290 // 11110xxx (xxx is top 3 bits) |
| 289 if (buffer != null) buffer[pos] = ((charCode >> 18) & 0x07) | 0xF0; | 291 if (buffer != null) buffer[pos] = ((charCode >> 18) & 0x07) | 0xF0; |
| 290 additionalBytes = 3; | 292 additionalBytes = 3; |
| 291 } | 293 } |
| 292 pos++; | 294 pos++; |
| 293 if (buffer != null) { | 295 if (buffer != null) { |
| 294 for (int i = additionalBytes; i > 0; i--) { | 296 for (int i = additionalBytes; i > 0; i--) { |
| 295 // 10xxxxxx (xxxxxx is next 6 bits from the top). | 297 // 10xxxxxx (xxxxxx is next 6 bits from the top). |
| 296 buffer[pos++] = ((charCode >> (6 * (i - 1))) & 0x3F) | 0x80; | 298 buffer[pos++] = ((charCode >> (6 * (i - 1))) & 0x3F) | 0x80; |
| 297 } | 299 } |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 533 bool _inputClosed = false; // Is the underlying input stream closed? | 535 bool _inputClosed = false; // Is the underlying input stream closed? |
| 534 bool _closed = false; // Is this stream closed. | 536 bool _closed = false; // Is this stream closed. |
| 535 bool _eof = false; // Has all data been read from the decoder? | 537 bool _eof = false; // Has all data been read from the decoder? |
| 536 Timer _scheduledDataCallback; | 538 Timer _scheduledDataCallback; |
| 537 Timer _scheduledLineCallback; | 539 Timer _scheduledLineCallback; |
| 538 Timer _scheduledCloseCallback; | 540 Timer _scheduledCloseCallback; |
| 539 Function _clientDataHandler; | 541 Function _clientDataHandler; |
| 540 Function _clientLineHandler; | 542 Function _clientLineHandler; |
| 541 Function _clientCloseHandler; | 543 Function _clientCloseHandler; |
| 542 } | 544 } |
| OLD | NEW |