Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: utils/string_encoding/utf8_impl.dart

Issue 9323077: Optimize special cases in utf8 and utf16 where input can be copied to output. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Optimize special cases in utf8 and utf16 where input can be copied to output. Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 final int _UTF8_ONE_BYTE_MAX = 0x7f; 5 final int _UTF8_ONE_BYTE_MAX = 0x7f;
6 final int _UTF8_TWO_BYTE_MAX = 0x7ff; 6 final int _UTF8_TWO_BYTE_MAX = 0x7ff;
7 final int _UTF8_THREE_BYTE_MAX = 0xffff; 7 final int _UTF8_THREE_BYTE_MAX = 0xffff;
8 8
9 final int _UTF8_LO_SIX_BIT_MASK = 0x3f; 9 final int _UTF8_LO_SIX_BIT_MASK = 0x3f;
10 10
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 } 123 }
124 124
125 if (!(length == null || length >= 0)) { 125 if (!(length == null || length >= 0)) {
126 throw new IllegalArgumentException("length"); 126 throw new IllegalArgumentException("length");
127 } 127 }
128 128
129 int end = length != null ? 129 int end = length != null ?
130 Math.min(utf8EncodedBytes.length, offset + length) : 130 Math.min(utf8EncodedBytes.length, offset + length) :
131 utf8EncodedBytes.length; 131 utf8EncodedBytes.length;
132 132
133 void addReplacementCodepoint(void f(int v), int replacementCodepoint) { 133 void decode(void f(int v)) {
134 if(replacementCodepoint != null) {
135 f(replacementCodepoint);
136 } else {
137 throw new IllegalArgumentException("Invalid encoding");
138 }
139 }
140
141 void apply(void f(int v)) {
142 int i = offset; 134 int i = offset;
143 while (i < end) { 135 while (i < end) {
144 int value = utf8EncodedBytes[i++]; 136 int value = utf8EncodedBytes[i++];
145 if (value >= 0) { 137 if (value < 0) {
146 if (value <= _UTF8_ONE_BYTE_MAX) { 138 f(null);
147 f(value); 139 continue;
148 } else if (value < _UTF8_FIRST_BYTE_OF_TWO_BASE) { 140 }
149 addReplacementCodepoint(f, replacementCodepoint); 141
142 if (value <= _UTF8_ONE_BYTE_MAX) {
143 f(value);
144 } else if (value < _UTF8_FIRST_BYTE_OF_TWO_BASE) {
145 f(null);
146 continue;
147 } else {
148 int additionalBytes = 0;
149 if (value < _UTF8_FIRST_BYTE_OF_THREE_BASE) {
150 value -= _UTF8_FIRST_BYTE_OF_TWO_BASE;
151 additionalBytes = 1;
152 } else if (value < _UTF8_FIRST_BYTE_OF_FOUR_BASE) {
153 value -= _UTF8_FIRST_BYTE_OF_THREE_BASE;
154 additionalBytes = 2;
155 } else if (value < _UTF8_FIRST_BYTE_OF_FIVE_BASE) {
156 value -= _UTF8_FIRST_BYTE_OF_FOUR_BASE;
157 additionalBytes = 3;
158 } else if (value < _UTF8_FIRST_BYTE_OF_SIX_BASE) {
159 value -= _UTF8_FIRST_BYTE_OF_FIVE_BASE;
160 additionalBytes = 4;
161 } else if (value < _UTF8_FIRST_BYTE_BOUND_EXCL) {
162 value -= _UTF8_FIRST_BYTE_OF_SIX_BASE;
163 additionalBytes = 5;
164 } else {
165 f(null);
150 continue; 166 continue;
167 }
168 int j = 0;
169 while (j < additionalBytes && i < end) {
170 int nextValue = utf8EncodedBytes[i++];
171 if (nextValue > _UTF8_ONE_BYTE_MAX &&
172 nextValue < _UTF8_FIRST_BYTE_OF_TWO_BASE) {
173 value = (value << 6) | (nextValue & _UTF8_LO_SIX_BIT_MASK);
174 } else {
175 // if sequence-starting code unit, reposition cursor to start here
176 if (nextValue >= _UTF8_FIRST_BYTE_OF_TWO_BASE) {
177 i--;
178 }
179 break;
180 }
181 j++;
182 }
183 if (j == additionalBytes && (
184 value < UNICODE_UTF16_RESERVED_LO ||
185 value > UNICODE_UTF16_RESERVED_HI)) {
186 if ((additionalBytes == 1 && value > _UTF8_ONE_BYTE_MAX) ||
187 (additionalBytes == 2 && value > _UTF8_TWO_BYTE_MAX) ||
188 (additionalBytes == 3 && value > _UTF8_THREE_BYTE_MAX &&
189 value <= UNICODE_VALID_RANGE_MAX)) {
190 f(value);
191 } else {
192 f(null);
193 }
151 } else { 194 } else {
152 int additionalBytes = 0; 195 f(null);
153 if (value < _UTF8_FIRST_BYTE_OF_THREE_BASE) { 196 continue;
154 value -= _UTF8_FIRST_BYTE_OF_TWO_BASE;
155 additionalBytes = 1;
156 } else if (value < _UTF8_FIRST_BYTE_OF_FOUR_BASE) {
157 value -= _UTF8_FIRST_BYTE_OF_THREE_BASE;
158 additionalBytes = 2;
159 } else if (value < _UTF8_FIRST_BYTE_OF_FIVE_BASE) {
160 value -= _UTF8_FIRST_BYTE_OF_FOUR_BASE;
161 additionalBytes = 3;
162 } else if (value < _UTF8_FIRST_BYTE_OF_SIX_BASE) {
163 value -= _UTF8_FIRST_BYTE_OF_FIVE_BASE;
164 additionalBytes = 4;
165 } else if (value < _UTF8_FIRST_BYTE_BOUND_EXCL) {
166 value -= _UTF8_FIRST_BYTE_OF_SIX_BASE;
167 additionalBytes = 5;
168 } else {
169 addReplacementCodepoint(f, replacementCodepoint);
170 continue;
171 }
172 int j = 0;
173 while (j < additionalBytes && i < end) {
174 int nextValue = utf8EncodedBytes[i++];
175 if (nextValue > _UTF8_ONE_BYTE_MAX &&
176 nextValue < _UTF8_FIRST_BYTE_OF_TWO_BASE) {
177 value = (value << 6) | (nextValue & _UTF8_LO_SIX_BIT_MASK);
178 } else {
179 // if sequence-starting code unit, reposition cursor to start here
180 if (nextValue >= _UTF8_FIRST_BYTE_OF_TWO_BASE) {
181 i--;
182 }
183 break;
184 }
185 j++;
186 }
187 if (j == additionalBytes && (
188 value < UNICODE_UTF16_RESERVED_LO ||
189 value > UNICODE_UTF16_RESERVED_HI)) {
190 if ((additionalBytes == 1 && value > _UTF8_ONE_BYTE_MAX) ||
191 (additionalBytes == 2 && value > _UTF8_TWO_BYTE_MAX) ||
192 (additionalBytes == 3 && value > _UTF8_THREE_BYTE_MAX &&
193 value <= UNICODE_VALID_RANGE_MAX)) {
194 f(value);
195 } else {
196 addReplacementCodepoint(f, replacementCodepoint);
197 }
198 } else {
199 addReplacementCodepoint(f, replacementCodepoint);
200 continue;
201 }
202 } 197 }
203 } else {
204 addReplacementCodepoint(f, replacementCodepoint);
205 continue;
206 } 198 }
207 } 199 }
208 } 200 }
209 201
210 int codepointBufferLength = 0; 202 int codepointBufferLength = 0;
Søren Gjesse 2012/02/06 15:45:44 Please add a comment here regarding the function p
dcarlson 2012/02/06 22:11:29 Done.
211 apply(void _(int value) { 203 bool hasReplacements = false;
204 decode(void _(int value) {
212 codepointBufferLength++; 205 codepointBufferLength++;
206 if (value == null) {
207 hasReplacements = true;
208 }
213 }); 209 });
214 210
211 int _length = end - offset;
215 List<int> codepointBuffer = new List<int>(codepointBufferLength); 212 List<int> codepointBuffer = new List<int>(codepointBufferLength);
216 int i = 0; 213 if (_length == codepointBufferLength && !hasReplacements) {
217 apply(void _(int value) { 214 codepointBuffer.setRange(0, _length, utf8EncodedBytes, offset);
218 codepointBuffer[i++] = value; 215 } else {
219 }); 216 int i = 0;
217 decode(
218 void _(int value) {
219 if (value != null) {
220 codepointBuffer[i++] = value;
221 } else {
222 if (replacementCodepoint != null) {
223 codepointBuffer[i++] = replacementCodepoint;
224 } else {
225 throw new IllegalArgumentException("Invalid encoding");
226 }
227 }
228 }
229 );
230 }
220 return codepointBuffer; 231 return codepointBuffer;
221 } 232 }
OLDNEW
« utils/string_encoding/unicode_core.dart ('K') | « utils/string_encoding/unicode_core.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698