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 dart.convert; | 5 part of dart.convert; |
6 | 6 |
7 /** | 7 /** |
8 * An instance of the default implementation of the [Latin1Codec]. | 8 * An instance of the default implementation of the [Latin1Codec]. |
9 * | 9 * |
10 * This instance provides a convenient access to the most common ISO Latin 1 | 10 * This instance provides a convenient access to the most common ISO Latin 1 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 void _addSliceToSink(List<int> source, int start, int end, bool isLast) { | 125 void _addSliceToSink(List<int> source, int start, int end, bool isLast) { |
126 // If _sink was a UTF-16 conversion sink, just add the slice directly with | 126 // If _sink was a UTF-16 conversion sink, just add the slice directly with |
127 // _sink.addSlice(source, start, end, isLast). | 127 // _sink.addSlice(source, start, end, isLast). |
128 // The code below is an moderately stupid workaround until a real | 128 // The code below is an moderately stupid workaround until a real |
129 // solution can be made. | 129 // solution can be made. |
130 _sink.add(new String.fromCharCodes(source, start, end)); | 130 _sink.add(new String.fromCharCodes(source, start, end)); |
131 if (isLast) close(); | 131 if (isLast) close(); |
132 } | 132 } |
133 | 133 |
134 void addSlice(List<int> source, int start, int end, bool isLast) { | 134 void addSlice(List<int> source, int start, int end, bool isLast) { |
135 if (start < 0 || start > source.length) { | 135 RangeError.checkValidRange(start, end, source.length); |
136 throw new RangeError.range(start, 0, source.length); | |
137 } | |
138 if (end < start || end > source.length) { | |
139 throw new RangeError.range(end, start, source.length); | |
140 } | |
141 for (int i = start; i < end; i++) { | 136 for (int i = start; i < end; i++) { |
142 int char = source[i]; | 137 int char = source[i]; |
143 if (char > _LATIN1_MASK || char < 0) { | 138 if (char > _LATIN1_MASK || char < 0) { |
144 throw new FormatException("Source contains non-Latin-1 characters."); | 139 throw new FormatException("Source contains non-Latin-1 characters."); |
145 } | 140 } |
146 } | 141 } |
147 if (start < end) { | 142 if (start < end) { |
148 _addSliceToSink(source, start, end, isLast); | 143 _addSliceToSink(source, start, end, isLast); |
149 } | 144 } |
150 if (isLast) { | 145 if (isLast) { |
151 close(); | 146 close(); |
152 } | 147 } |
153 } | 148 } |
154 } | 149 } |
155 | 150 |
156 class _Latin1AllowInvalidDecoderSink extends _Latin1DecoderSink { | 151 class _Latin1AllowInvalidDecoderSink extends _Latin1DecoderSink { |
157 _Latin1AllowInvalidDecoderSink(StringConversionSink sink): super(sink); | 152 _Latin1AllowInvalidDecoderSink(StringConversionSink sink): super(sink); |
158 | 153 |
159 void addSlice(List<int> source, int start, int end, bool isLast) { | 154 void addSlice(List<int> source, int start, int end, bool isLast) { |
160 if (start < 0 || start > source.length) { | 155 RangeError.checkValidRange(start, end, source.length); |
161 throw new RangeError.range(start, 0, source.length); | |
162 } | |
163 if (end < start || end > source.length) { | |
164 throw new RangeError.range(end, start, source.length); | |
165 } | |
166 for (int i = start; i < end; i++) { | 156 for (int i = start; i < end; i++) { |
167 int char = source[i]; | 157 int char = source[i]; |
168 if (char > _LATIN1_MASK || char < 0) { | 158 if (char > _LATIN1_MASK || char < 0) { |
169 if (i > start) _addSliceToSink(source, start, i, false); | 159 if (i > start) _addSliceToSink(source, start, i, false); |
170 // Add UTF-8 encoding of U+FFFD. | 160 // Add UTF-8 encoding of U+FFFD. |
171 _addSliceToSink(const[0xFFFD], 0, 1, false); | 161 _addSliceToSink(const[0xFFFD], 0, 1, false); |
172 start = i + 1; | 162 start = i + 1; |
173 } | 163 } |
174 } | 164 } |
175 if (start < end) { | 165 if (start < end) { |
176 _addSliceToSink(source, start, end, isLast); | 166 _addSliceToSink(source, start, end, isLast); |
177 } | 167 } |
178 if (isLast) { | 168 if (isLast) { |
179 close(); | 169 close(); |
180 } | 170 } |
181 } | 171 } |
182 } | 172 } |
OLD | NEW |