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

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

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/object_test.cc ('k') | runtime/vm/unicode.cc » ('j') | 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 #ifndef VM_UNICODE_H_ 5 #ifndef VM_UNICODE_H_
6 #define VM_UNICODE_H_ 6 #define VM_UNICODE_H_
7 7
8 #include "vm/allocation.h" 8 #include "vm/allocation.h"
9 #include "vm/globals.h" 9 #include "vm/globals.h"
10 10
11 namespace dart { 11 namespace dart {
12 12
13 class String; 13 class String;
14 14
15 class Utf8 : AllStatic { 15 class Utf8 : AllStatic {
16 public: 16 public:
17 enum Type { 17 enum Type {
18 kLatin1 = 0, // Latin-1 character set. 18 kLatin1 = 0, // Latin-1 character set.
19 kBMP, // Basic Multilingual Plane. 19 kBMP, // Basic Multilingual Plane.
20 kSMP, // Supplementary Multilingual Plane. 20 kSMP, // Supplementary Multilingual Plane.
21 }; 21 };
22 22
23 static const intptr_t kMaxOneByteChar = 0x7F; 23 static const intptr_t kMaxOneByteChar = 0x7F;
24 static const intptr_t kMaxTwoByteChar = 0x7FF; 24 static const intptr_t kMaxTwoByteChar = 0x7FF;
25 static const intptr_t kMaxThreeByteChar = 0xFFFF; 25 static const intptr_t kMaxThreeByteChar = 0xFFFF;
26 static const intptr_t kMaxFourByteChar = 0x10FFFF; 26 static const intptr_t kMaxFourByteChar = 0x10FFFF;
27 static const intptr_t kMaxBmpCodepoint = 0xffff;
28 static const int32_t kLeadOffset = (0xD800 - (0x10000 >> 10));
29 static const int32_t kSurrogateOffset = (0x10000 - (0xD800 << 10) - 0xDC00);
30 27
31 static void ConvertUTF32ToUTF16(int32_t codepoint, uint16_t* dst);
32 static intptr_t CodePointCount(const uint8_t* utf8_array, 28 static intptr_t CodePointCount(const uint8_t* utf8_array,
33 intptr_t array_len, 29 intptr_t array_len,
34 Type* type); 30 Type* type);
35 31
36 // Returns true if 'utf8_array' is a valid UTF-8 string. 32 // Returns true if 'utf8_array' is a valid UTF-8 string.
37 static bool IsValid(const uint8_t* utf8_array, intptr_t array_len); 33 static bool IsValid(const uint8_t* utf8_array, intptr_t array_len);
38 34
39 static intptr_t Length(int32_t ch); 35 static intptr_t Length(int32_t ch);
40 static intptr_t Length(const String& str); 36 static intptr_t Length(const String& str);
41 37
(...skipping 20 matching lines...) Expand all
62 uint32_t* dst, 58 uint32_t* dst,
63 intptr_t len) { 59 intptr_t len) {
64 ASSERT(str != NULL); 60 ASSERT(str != NULL);
65 intptr_t array_len = strlen(str); 61 intptr_t array_len = strlen(str);
66 const uint8_t* utf8_array = reinterpret_cast<const uint8_t*>(str); 62 const uint8_t* utf8_array = reinterpret_cast<const uint8_t*>(str);
67 return DecodeToUTF32(utf8_array, array_len, dst, len); 63 return DecodeToUTF32(utf8_array, array_len, dst, len);
68 } 64 }
69 }; 65 };
70 66
71 67
68 class Utf16 : AllStatic {
69 public:
70 static const int32_t kMaxBmpCodepoint = 0xFFFF;
71
72 static const int32_t kLeadSurrogateOffset = (0xD800 - (0x10000 >> 10));
73
74 static const int32_t kSurrogateOffset = (0x10000 - (0xD800 << 10) - 0xDC00);
75
76 // Returns the length of the code point in UTF-16 code units.
77 static intptr_t Length(int32_t ch) {
78 return (ch <= kMaxBmpCodepoint) ? 1 : 2;
79 }
80
81 // Returns true if ch is a lead or trail surrogate.
82 static bool IsSurrogate(int32_t ch) {
83 return (ch & 0xFFFFF800) == 0xD800;
84 }
85
86 // Returns true if ch is a lead surrogate.
87 static bool IsLeadSurrogate(int32_t ch) {
88 return (ch & 0xFFFFFC00) == 0xD800;
89 }
90
91 // Returns true if ch is a low surrogate.
92 static bool IsTrailSurrogate(int32_t ch) {
93 return (ch & 0xFFFFFC00) == 0xDC00;
94 }
95
96 // Decodes a surrogate pair into a supplementary code point.
97 static int32_t Decode(int32_t lead, int32_t trail) {
98 return 0x10000 + ((lead & 0x3FF) << 10) + (trail & 0x3FF);
99 }
100
101 // Encodes a single code point.
102 static void Encode(int32_t codepoint, uint16_t* dst);
103 };
104
105
72 class CaseMapping : AllStatic { 106 class CaseMapping : AllStatic {
73 public: 107 public:
74 // Maps a code point to uppercase. 108 // Maps a code point to uppercase.
75 static int32_t ToUpper(int32_t code_point) { 109 static int32_t ToUpper(int32_t code_point) {
76 return Convert(code_point, kUppercase); 110 return Convert(code_point, kUppercase);
77 } 111 }
78 112
79 // Maps a code point to lowercase. 113 // Maps a code point to lowercase.
80 static int32_t ToLower(int32_t code_point) { 114 static int32_t ToLower(int32_t code_point) {
81 return Convert(code_point, kLowercase); 115 return Convert(code_point, kLowercase);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 // Data for small code points with one mapping 162 // Data for small code points with one mapping
129 static const int16_t stage2_[]; 163 static const int16_t stage2_[];
130 164
131 // Data for large code points or code points with both mappings. 165 // Data for large code points or code points with both mappings.
132 static const int32_t stage2_exception_[][2]; 166 static const int32_t stage2_exception_[][2];
133 }; 167 };
134 168
135 } // namespace dart 169 } // namespace dart
136 170
137 #endif // VM_UNICODE_H_ 171 #endif // VM_UNICODE_H_
OLDNEW
« no previous file with comments | « runtime/vm/object_test.cc ('k') | runtime/vm/unicode.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698