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

Side by Side Diff: runtime/vm/unicode.cc

Issue 11416054: Provide a code point iterator to the String class to simplify iteration. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: presubmit Created 8 years, 1 month 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
« no previous file with comments | « runtime/vm/unicode.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 #include "vm/unicode.h" 5 #include "vm/unicode.h"
6 6
7 #include "vm/allocation.h" 7 #include "vm/allocation.h"
8 #include "vm/globals.h" 8 #include "vm/globals.h"
9 #include "vm/object.h" 9 #include "vm/object.h"
10 10
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 return (code_unit <= Utf8::kMaxOneByteChar); 63 return (code_unit <= Utf8::kMaxOneByteChar);
64 } 64 }
65 65
66 66
67 static bool IsSmpSequenceStart(uint8_t code_unit) { 67 static bool IsSmpSequenceStart(uint8_t code_unit) {
68 // Check is codepoint is >= U+10000. 68 // Check is codepoint is >= U+10000.
69 return (code_unit >= 0xF0); 69 return (code_unit >= 0xF0);
70 } 70 }
71 71
72 72
73 // Returns true if the code point is a high- or low-surrogate.
74 static bool IsSurrogate(uint32_t code_point) {
75 return (code_point & 0xfffff800) == 0xd800;
76 }
77
78
79 // Returns true if the code point value is above Plane 17. 73 // Returns true if the code point value is above Plane 17.
80 static bool IsOutOfRange(uint32_t code_point) { 74 static bool IsOutOfRange(uint32_t code_point) {
81 return (code_point > 0x10FFFF); 75 return (code_point > 0x10FFFF);
82 } 76 }
83 77
84 78
85 // Returns true if the byte sequence is ill-formed. 79 // Returns true if the byte sequence is ill-formed.
86 static bool IsNonShortestForm(uint32_t code_point, size_t num_bytes) { 80 static bool IsNonShortestForm(uint32_t code_point, size_t num_bytes) {
87 return code_point < kOverlongMinimum[num_bytes]; 81 return code_point < kOverlongMinimum[num_bytes];
88 } 82 }
89 83
90 84
91 void Utf8::ConvertUTF32ToUTF16(int32_t codepoint, uint16_t* dst) {
92 ASSERT(codepoint > kMaxBmpCodepoint);
93 ASSERT(dst != NULL);
94 dst[0] = (Utf8::kLeadOffset + (codepoint >> 10));
95 dst[1] = (0xDC00 + (codepoint & 0x3FF));
96 }
97
98
99 // Returns a count of the number of UTF-8 trail bytes. 85 // Returns a count of the number of UTF-8 trail bytes.
100 intptr_t Utf8::CodePointCount(const uint8_t* utf8_array, 86 intptr_t Utf8::CodePointCount(const uint8_t* utf8_array,
101 intptr_t array_len, 87 intptr_t array_len,
102 Type* type) { 88 Type* type) {
103 intptr_t len = 0; 89 intptr_t len = 0;
104 Type char_type = kLatin1; 90 Type char_type = kLatin1;
105 for (intptr_t i = 0; i < array_len; i++) { 91 for (intptr_t i = 0; i < array_len; i++) {
106 uint8_t code_unit = utf8_array[i]; 92 uint8_t code_unit = utf8_array[i];
107 if (!IsTrailByte(code_unit)) { 93 if (!IsTrailByte(code_unit)) {
108 ++len; 94 ++len;
(...skipping 28 matching lines...) Expand all
137 ch = (ch << 6) + code_unit; 123 ch = (ch << 6) + code_unit;
138 } else { 124 } else {
139 return false; 125 return false;
140 } 126 }
141 } 127 }
142 ch -= kMagicBits[num_trail_bytes]; 128 ch -= kMagicBits[num_trail_bytes];
143 if (!((is_malformed == false) && 129 if (!((is_malformed == false) &&
144 (j == num_trail_bytes) && 130 (j == num_trail_bytes) &&
145 !IsOutOfRange(ch) && 131 !IsOutOfRange(ch) &&
146 !IsNonShortestForm(ch, j) && 132 !IsNonShortestForm(ch, j) &&
147 !IsSurrogate(ch))) { 133 !Utf16::IsSurrogate(ch))) {
148 return false; 134 return false;
149 } 135 }
150 } 136 }
151 i += j; 137 i += j;
152 } 138 }
153 return true; 139 return true;
154 } 140 }
155 141
156 142
157 intptr_t Utf8::Length(int32_t ch) { 143 intptr_t Utf8::Length(int32_t ch) {
158 if (ch <= kMaxOneByteChar) { 144 if (ch <= kMaxOneByteChar) {
159 return 1; 145 return 1;
160 } else if (ch <= kMaxTwoByteChar) { 146 } else if (ch <= kMaxTwoByteChar) {
161 return 2; 147 return 2;
162 } else if (ch <= kMaxThreeByteChar) { 148 } else if (ch <= kMaxThreeByteChar) {
163 return 3; 149 return 3;
164 } 150 }
165 ASSERT(ch <= kMaxFourByteChar); 151 ASSERT(ch <= kMaxFourByteChar);
166 return 4; 152 return 4;
167 } 153 }
168 154
169 155
170 intptr_t Utf8::Length(const String& str) { 156 intptr_t Utf8::Length(const String& str) {
171 intptr_t length = 0; 157 intptr_t length = 0;
172 for (intptr_t i = 0; i < str.Length(); ++i) { 158 String::CodePointIterator it(str);
173 int32_t ch = str.CharAt(i); 159 while (it.Next()) {
160 int32_t ch = it.Current();
174 length += Utf8::Length(ch); 161 length += Utf8::Length(ch);
175 } 162 }
176 return length; 163 return length;
177 } 164 }
178 165
179 166
180 intptr_t Utf8::Encode(int32_t ch, char* dst) { 167 intptr_t Utf8::Encode(int32_t ch, char* dst) {
181 static const int kMask = ~(1 << 6); 168 static const int kMask = ~(1 << 6);
182 if (ch <= kMaxOneByteChar) { 169 if (ch <= kMaxOneByteChar) {
183 dst[0] = ch; 170 dst[0] = ch;
(...skipping 14 matching lines...) Expand all
198 dst[0] = 0xF0 | (ch >> 18); 185 dst[0] = 0xF0 | (ch >> 18);
199 dst[1] = 0x80 | ((ch >> 12) & kMask); 186 dst[1] = 0x80 | ((ch >> 12) & kMask);
200 dst[2] = 0x80 | ((ch >> 6) & kMask); 187 dst[2] = 0x80 | ((ch >> 6) & kMask);
201 dst[3] = 0x80 | (ch & kMask); 188 dst[3] = 0x80 | (ch & kMask);
202 return 4; 189 return 4;
203 } 190 }
204 191
205 192
206 intptr_t Utf8::Encode(const String& src, char* dst, intptr_t len) { 193 intptr_t Utf8::Encode(const String& src, char* dst, intptr_t len) {
207 intptr_t pos = 0; 194 intptr_t pos = 0;
208 for (intptr_t i = 0; i < src.Length(); ++i) { 195 String::CodePointIterator it(src);
209 intptr_t ch = src.CharAt(i); 196 while (it.Next()) {
197 int32_t ch = it.Current();
210 intptr_t num_bytes = Utf8::Length(ch); 198 intptr_t num_bytes = Utf8::Length(ch);
211 if (pos + num_bytes > len) { 199 if (pos + num_bytes > len) {
212 break; 200 break;
213 } 201 }
214 Utf8::Encode(ch, &dst[pos]); 202 Utf8::Encode(ch, &dst[pos]);
215 pos += num_bytes; 203 pos += num_bytes;
216 } 204 }
217 return pos; 205 return pos;
218 } 206 }
219 207
220 208
221 intptr_t Utf8::Decode(const uint8_t* utf8_array, 209 intptr_t Utf8::Decode(const uint8_t* utf8_array,
222 intptr_t array_len, 210 intptr_t array_len,
223 int32_t* dst) { 211 int32_t* dst) {
224 uint32_t ch = utf8_array[0] & 0xFF; 212 uint32_t ch = utf8_array[0] & 0xFF;
225 intptr_t i = 1; 213 intptr_t i = 1;
226 if (ch >= 0x80) { 214 if (ch >= 0x80) {
227 int32_t num_trail_bytes = kTrailBytes[ch]; 215 intptr_t num_trail_bytes = kTrailBytes[ch];
228 bool is_malformed = false; 216 bool is_malformed = false;
229 for (; i < num_trail_bytes; ++i) { 217 for (; i < num_trail_bytes; ++i) {
230 if (i < array_len) { 218 if (i < array_len) {
231 uint8_t code_unit = utf8_array[i]; 219 uint8_t code_unit = utf8_array[i];
232 is_malformed |= !IsTrailByte(code_unit); 220 is_malformed |= !IsTrailByte(code_unit);
233 ch = (ch << 6) + code_unit; 221 ch = (ch << 6) + code_unit;
234 } else { 222 } else {
235 *dst = -1; 223 *dst = -1;
236 return 0; 224 return 0;
237 } 225 }
238 } 226 }
239 ch -= kMagicBits[num_trail_bytes]; 227 ch -= kMagicBits[num_trail_bytes];
240 if (!((is_malformed == false) && 228 if (!((is_malformed == false) &&
241 (i == num_trail_bytes) && 229 (i == num_trail_bytes) &&
242 !IsOutOfRange(ch) && 230 !IsOutOfRange(ch) &&
243 !IsNonShortestForm(ch, i) && 231 !IsNonShortestForm(ch, i) &&
244 !IsSurrogate(ch))) { 232 !Utf16::IsSurrogate(ch))) {
245 *dst = -1; 233 *dst = -1;
246 return 0; 234 return 0;
247 } 235 }
248 } 236 }
249 *dst = ch; 237 *dst = ch;
250 return i; 238 return i;
251 } 239 }
252 240
253 241
254 bool Utf8::DecodeToLatin1(const uint8_t* utf8_array, 242 bool Utf8::DecodeToLatin1(const uint8_t* utf8_array,
(...skipping 28 matching lines...) Expand all
283 intptr_t j = 0; 271 intptr_t j = 0;
284 intptr_t num_bytes; 272 intptr_t num_bytes;
285 for (; (i < array_len) && (j < len); i += num_bytes, ++j) { 273 for (; (i < array_len) && (j < len); i += num_bytes, ++j) {
286 int32_t ch; 274 int32_t ch;
287 bool is_smp = IsSmpSequenceStart(utf8_array[i]); 275 bool is_smp = IsSmpSequenceStart(utf8_array[i]);
288 num_bytes = Utf8::Decode(&utf8_array[i], (array_len - i), &ch); 276 num_bytes = Utf8::Decode(&utf8_array[i], (array_len - i), &ch);
289 if (ch == -1) { 277 if (ch == -1) {
290 return false; // invalid input 278 return false; // invalid input
291 } 279 }
292 if (is_smp) { 280 if (is_smp) {
293 ConvertUTF32ToUTF16(ch, &(dst[j])); 281 Utf16::Encode(ch, &dst[j]);
294 j = j + 1; 282 j = j + 1;
295 } else { 283 } else {
296 dst[j] = ch; 284 dst[j] = ch;
297 } 285 }
298 } 286 }
299 if ((i < array_len) && (j == len)) { 287 if ((i < array_len) && (j == len)) {
300 return false; // output overflow 288 return false; // output overflow
301 } 289 }
302 return true; // success 290 return true; // success
303 } 291 }
(...skipping 13 matching lines...) Expand all
317 return false; // invalid input 305 return false; // invalid input
318 } 306 }
319 dst[j] = ch; 307 dst[j] = ch;
320 } 308 }
321 if ((i < array_len) && (j == len)) { 309 if ((i < array_len) && (j == len)) {
322 return false; // output overflow 310 return false; // output overflow
323 } 311 }
324 return true; // success 312 return true; // success
325 } 313 }
326 314
315
316 void Utf16::Encode(int32_t codepoint, uint16_t* dst) {
317 ASSERT(codepoint > kMaxBmpCodepoint);
318 ASSERT(dst != NULL);
319 dst[0] = (Utf16::kLeadSurrogateOffset + (codepoint >> 10));
320 dst[1] = (0xDC00 + (codepoint & 0x3FF));
321 }
322
327 } // namespace dart 323 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/unicode.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698