Chromium Code Reviews| Index: sdk/lib/convert/ascii.dart |
| diff --git a/sdk/lib/convert/ascii.dart b/sdk/lib/convert/ascii.dart |
| index 40ff5a8fdc14f87afd028987d67d6cfa5fc4c816..1d84e63f537d60d3ddcfbe2ff848f7e7b2060457 100644 |
| --- a/sdk/lib/convert/ascii.dart |
| +++ b/sdk/lib/convert/ascii.dart |
| @@ -60,9 +60,9 @@ class AsciiCodec extends Encoding { |
| } |
| } |
| - Converter<String, List<int>> get encoder => const AsciiEncoder(); |
| + AsciiEncoder get encoder => const AsciiEncoder(); |
| - Converter<List<int>, String> get decoder => |
| + AsciiDecoder get decoder => |
| _allowInvalid ? const AsciiDecoder(allowInvalid: true) |
| : const AsciiDecoder(allowInvalid: false); |
| } |
| @@ -74,10 +74,22 @@ class _UnicodeSubsetEncoder extends Converter<String, List<int>> { |
| const _UnicodeSubsetEncoder(this._subsetMask); |
| - List<int> convert(String string) { |
| - List result = new Uint8List(string.length); |
| - for (int i = 0; i < string.length; i++) { |
| - var codeUnit = string.codeUnitAt(i); |
| + List<int> convert(String string, [int start = 0, int end]) { |
|
Søren Gjesse
2014/11/20 10:41:41
Where are the new arguments documented?
Lasse Reichstein Nielsen
2014/11/20 12:46:22
Ack, documenting.
|
| + int stringLength = string.length; |
|
Søren Gjesse
2014/11/20 10:41:41
Maybe factor out argument checking.
Lasse Reichstein Nielsen
2014/11/20 12:46:22
Funny you should say that - I'm already doing that
|
| + if (start < 0 || start > stringLength) { |
| + throw new RangeError.range(start, 0, stringLength, "start"); |
| + } |
| + if (end == null) { |
| + end = stringLength; |
| + } else { |
| + if (end < start || end > stringLength) { |
| + throw new RangeError.range(end, start, stringLength, "end"); |
| + } |
| + } |
| + int length = end - start; |
| + List result = new Uint8List(length); |
| + for (int i = 0; i < length; i++) { |
| + var codeUnit = string.codeUnitAt(start + i); |
| if ((codeUnit & ~_subsetMask) != 0) { |
| throw new ArgumentError("String contains invalid characters."); |
| } |
| @@ -173,22 +185,35 @@ abstract class _UnicodeSubsetDecoder extends Converter<List<int>, String> { |
| * Converts the [bytes] (a list of unsigned 7- or 8-bit integers) to the |
| * corresponding string. |
|
Søren Gjesse
2014/11/20 10:41:41
Add documentation.
|
| */ |
| - String convert(List<int> bytes) { |
| - for (int i = 0; i < bytes.length; i++) { |
| + String convert(List<int> bytes, [int start = 0, int end]) { |
| + int byteCount = bytes.length; |
| + if (start < 0 || start > byteCount) { |
| + throw new RangeError.range(start, 0, byteCount, "start"); |
| + } |
| + if (end == null) { |
| + end = byteCount; |
| + } else { |
| + if (end < start || end > byteCount) { |
| + throw new RangeError.range(end, start, byteCount, "end"); |
| + } |
| + } |
| + int length = end - start; |
| + |
| + for (int i = start; i < end; i++) { |
| int byte = bytes[i]; |
| if ((byte & ~_subsetMask) != 0) { |
| if (!_allowInvalid) { |
| throw new FormatException("Invalid value in input: $byte"); |
| } |
| - return _convertInvalid(bytes); |
| + return _convertInvalid(bytes, start, end); |
| } |
| } |
| - return new String.fromCharCodes(bytes); |
| + return new String.fromCharCodes(bytes, start, end); |
| } |
| - String _convertInvalid(List<int> bytes) { |
| + String _convertInvalid(List<int> bytes, int start, int end) { |
| StringBuffer buffer = new StringBuffer(); |
| - for (int i = 0; i < bytes.length; i++) { |
| + for (int i = start; i < end; i++) { |
| int value = bytes[i]; |
| if ((value & ~_subsetMask) != 0) value = 0xFFFD; |
| buffer.writeCharCode(value); |