Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Utf : AllStatic { | |
| 16 public: | |
| 17 static bool IsLatin1(int32_t ch) { | |
|
siva
2012/11/30 02:42:53
for consistency can we call the parameter
"int32_t
cshapiro
2012/11/30 03:21:24
Absolutely. Done.
| |
| 18 return (ch >= 0) && (ch <= 0xFF); | |
| 19 } | |
| 20 | |
| 21 static bool IsBmp(int32_t ch) { | |
| 22 return (ch >= 0) && (ch <= 0xFFFF); | |
| 23 } | |
| 24 | |
| 25 static bool IsSupplementary(int32_t ch) { | |
| 26 return (ch > 0xFFFF) && (ch <= 0x10FFFF); | |
| 27 } | |
| 28 | |
| 29 // Returns true if the code point value is above Plane 17. | |
| 30 static bool IsOutOfRange(int32_t code_point) { | |
| 31 return (code_point > 0x10FFFF); | |
|
siva
2012/11/30 02:42:53
can we make this:
return (code_point < 0 || code_p
cshapiro
2012/11/30 03:21:24
Done.
| |
| 32 } | |
| 33 }; | |
| 34 | |
| 35 | |
| 15 class Utf8 : AllStatic { | 36 class Utf8 : AllStatic { |
| 16 public: | 37 public: |
| 17 enum Type { | 38 enum Type { |
| 18 kLatin1 = 0, // Latin-1 code point [U+0000, U+00FF]. | 39 kLatin1 = 0, // Latin-1 code point [U+0000, U+00FF]. |
| 19 kBMP, // Basic Multilingual Plane code point [U+0000, U+FFFF]. | 40 kBMP, // Basic Multilingual Plane code point [U+0000, U+FFFF]. |
| 20 kSupplementary, // Supplementary code point [U+010000, U+10FFFF]. | 41 kSupplementary, // Supplementary code point [U+010000, U+10FFFF]. |
| 21 }; | 42 }; |
| 22 | 43 |
| 23 static const intptr_t kMaxOneByteChar = 0x7F; | |
| 24 static const intptr_t kMaxTwoByteChar = 0x7FF; | |
| 25 static const intptr_t kMaxThreeByteChar = 0xFFFF; | |
| 26 static const intptr_t kMaxFourByteChar = 0x10FFFF; | |
| 27 | |
| 28 static intptr_t CodePointCount(const uint8_t* utf8_array, | 44 static intptr_t CodePointCount(const uint8_t* utf8_array, |
| 29 intptr_t array_len, | 45 intptr_t array_len, |
| 30 Type* type); | 46 Type* type); |
| 31 | 47 |
| 32 // Returns true if 'utf8_array' is a valid UTF-8 string. | 48 // Returns true if 'utf8_array' is a valid UTF-8 string. |
| 33 static bool IsValid(const uint8_t* utf8_array, intptr_t array_len); | 49 static bool IsValid(const uint8_t* utf8_array, intptr_t array_len); |
| 34 | 50 |
| 35 static intptr_t Length(int32_t ch); | 51 static intptr_t Length(int32_t ch); |
| 36 static intptr_t Length(const String& str); | 52 static intptr_t Length(const String& str); |
| 37 | 53 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 49 static bool DecodeToUTF16(const uint8_t* utf8_array, | 65 static bool DecodeToUTF16(const uint8_t* utf8_array, |
| 50 intptr_t array_len, | 66 intptr_t array_len, |
| 51 uint16_t* dst, | 67 uint16_t* dst, |
| 52 intptr_t len); | 68 intptr_t len); |
| 53 static bool DecodeToUTF32(const uint8_t* utf8_array, | 69 static bool DecodeToUTF32(const uint8_t* utf8_array, |
| 54 intptr_t array_len, | 70 intptr_t array_len, |
| 55 int32_t* dst, | 71 int32_t* dst, |
| 56 intptr_t len); | 72 intptr_t len); |
| 57 static bool DecodeCStringToUTF32(const char* str, | 73 static bool DecodeCStringToUTF32(const char* str, |
| 58 int32_t* dst, | 74 int32_t* dst, |
| 59 intptr_t len) { | 75 intptr_t len); |
| 60 ASSERT(str != NULL); | 76 |
| 61 intptr_t array_len = strlen(str); | 77 private: |
| 62 const uint8_t* utf8_array = reinterpret_cast<const uint8_t*>(str); | 78 static const int32_t kMaxOneByteChar = 0x7F; |
| 63 return DecodeToUTF32(utf8_array, array_len, dst, len); | 79 static const int32_t kMaxTwoByteChar = 0x7FF; |
| 80 static const int32_t kMaxThreeByteChar = 0xFFFF; | |
| 81 static const int32_t kMaxFourByteChar = 0x10FFFF; | |
|
siva
2012/11/30 02:42:53
we could probably have a constant in class Utf whi
cshapiro
2012/11/30 03:21:24
Another great idea. Done.
| |
| 82 | |
| 83 static bool IsTrailByte(uint8_t code_unit) { | |
| 84 return (code_unit & 0xc0) == 0x80; | |
| 64 } | 85 } |
| 86 | |
| 87 static bool IsNonShortestForm(uint32_t code_point, size_t num_code_units) { | |
| 88 return code_point < kOverlongMinimum[num_code_units]; | |
| 89 } | |
| 90 | |
| 91 static bool IsLatin1SequenceStart(uint8_t code_unit) { | |
| 92 // Check is codepoint is <= U+00FF | |
| 93 return (code_unit <= Utf8::kMaxOneByteChar); | |
| 94 } | |
| 95 | |
| 96 static bool IsSupplementarySequenceStart(uint8_t code_unit) { | |
| 97 // Check is codepoint is >= U+10000. | |
| 98 return (code_unit >= 0xF0); | |
| 99 } | |
| 100 | |
| 101 static const int8_t kTrailBytes[]; | |
| 102 static const uint32_t kMagicBits[]; | |
| 103 static const uint32_t kOverlongMinimum[]; | |
| 65 }; | 104 }; |
| 66 | 105 |
| 67 | 106 |
| 68 class Utf16 : AllStatic { | 107 class Utf16 : AllStatic { |
| 108 private: | |
| 109 static const int32_t kMaxCodeUnit = 0xFFFF; | |
|
siva
2012/11/30 02:42:53
Why is this on the top here and not clubbed in wit
cshapiro
2012/11/30 03:21:24
I thought there was a scoping issue, but I was wro
| |
| 110 | |
| 69 public: | 111 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. | 112 // Returns the length of the code point in UTF-16 code units. |
| 77 static intptr_t Length(int32_t ch) { | 113 static intptr_t Length(int32_t ch) { |
| 78 return (ch <= kMaxBmpCodepoint) ? 1 : 2; | 114 return (ch <= Utf16::kMaxCodeUnit) ? 1 : 2; |
| 79 } | 115 } |
| 80 | 116 |
| 81 // Returns true if ch is a lead or trail surrogate. | 117 // Returns true if ch is a lead or trail surrogate. |
| 82 static bool IsSurrogate(int32_t ch) { | 118 static bool IsSurrogate(int32_t ch) { |
| 83 return (ch & 0xFFFFF800) == 0xD800; | 119 return (ch & 0xFFFFF800) == 0xD800; |
| 84 } | 120 } |
| 85 | 121 |
| 86 // Returns true if ch is a lead surrogate. | 122 // Returns true if ch is a lead surrogate. |
| 87 static bool IsLeadSurrogate(int32_t ch) { | 123 static bool IsLeadSurrogate(int32_t ch) { |
| 88 return (ch & 0xFFFFFC00) == 0xD800; | 124 return (ch & 0xFFFFFC00) == 0xD800; |
| 89 } | 125 } |
| 90 | 126 |
| 91 // Returns true if ch is a low surrogate. | 127 // Returns true if ch is a low surrogate. |
| 92 static bool IsTrailSurrogate(int32_t ch) { | 128 static bool IsTrailSurrogate(int32_t ch) { |
| 93 return (ch & 0xFFFFFC00) == 0xDC00; | 129 return (ch & 0xFFFFFC00) == 0xDC00; |
| 94 } | 130 } |
| 95 | 131 |
| 96 // Decodes a surrogate pair into a supplementary code point. | 132 // Decodes a surrogate pair into a supplementary code point. |
| 97 static int32_t Decode(int32_t lead, int32_t trail) { | 133 static int32_t Decode(int32_t lead, int32_t trail) { |
| 98 return 0x10000 + ((lead & 0x3FF) << 10) + (trail & 0x3FF); | 134 return 0x10000 + ((lead & 0x3FF) << 10) + (trail & 0x3FF); |
| 99 } | 135 } |
| 100 | 136 |
| 101 // Encodes a single code point. | 137 // Encodes a single code point. |
| 102 static void Encode(int32_t codepoint, uint16_t* dst); | 138 static void Encode(int32_t codepoint, uint16_t* dst); |
| 139 | |
| 140 private: | |
| 141 static const int32_t kLeadSurrogateOffset = (0xD800 - (0x10000 >> 10)); | |
| 142 | |
| 143 static const int32_t kSurrogateOffset = (0x10000 - (0xD800 << 10) - 0xDC00); | |
| 103 }; | 144 }; |
| 104 | 145 |
| 105 | 146 |
| 106 class CaseMapping : AllStatic { | 147 class CaseMapping : AllStatic { |
| 107 public: | 148 public: |
| 108 // Maps a code point to uppercase. | 149 // Maps a code point to uppercase. |
| 109 static int32_t ToUpper(int32_t code_point) { | 150 static int32_t ToUpper(int32_t code_point) { |
| 110 return Convert(code_point, kUppercase); | 151 return Convert(code_point, kUppercase); |
| 111 } | 152 } |
| 112 | 153 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 131 | 172 |
| 132 // The size of the stage 1 index. | 173 // The size of the stage 1 index. |
| 133 // TODO(cshapiro): improve indexing so this value is unnecessary. | 174 // TODO(cshapiro): improve indexing so this value is unnecessary. |
| 134 static const int kStage1Size = 261; | 175 static const int kStage1Size = 261; |
| 135 | 176 |
| 136 // The size of a stage 2 block in bytes. | 177 // The size of a stage 2 block in bytes. |
| 137 static const int kBlockSizeLog2 = 8; | 178 static const int kBlockSizeLog2 = 8; |
| 138 static const int kBlockSize = 1 << kBlockSizeLog2; | 179 static const int kBlockSize = 1 << kBlockSizeLog2; |
| 139 | 180 |
| 140 static int32_t Convert(int32_t ch, int32_t mapping) { | 181 static int32_t Convert(int32_t ch, int32_t mapping) { |
| 141 if (ch <= 0xFF) { | 182 if (Utf::IsLatin1(ch)) { |
| 142 int32_t info = stage2_[ch]; | 183 int32_t info = stage2_[ch]; |
| 143 if ((info & kTypeMask) == mapping) { | 184 if ((info & kTypeMask) == mapping) { |
| 144 ch += info >> kTypeShift; | 185 ch += info >> kTypeShift; |
| 145 } | 186 } |
| 146 } else if (ch <= (kStage1Size << kBlockSizeLog2)) { | 187 } else if (ch <= (kStage1Size << kBlockSizeLog2)) { |
| 147 int16_t offset = stage1_[ch >> kBlockSizeLog2] << kBlockSizeLog2; | 188 int16_t offset = stage1_[ch >> kBlockSizeLog2] << kBlockSizeLog2; |
| 148 int32_t info = stage2_[offset + (ch & (kBlockSize - 1))]; | 189 int32_t info = stage2_[offset + (ch & (kBlockSize - 1))]; |
| 149 int32_t type = info & kTypeMask; | 190 int32_t type = info & kTypeMask; |
| 150 if (type == mapping) { | 191 if (type == mapping) { |
| 151 ch += (info >> kTypeShift); | 192 ch += (info >> kTypeShift); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 162 // Data for small code points with one mapping | 203 // Data for small code points with one mapping |
| 163 static const int16_t stage2_[]; | 204 static const int16_t stage2_[]; |
| 164 | 205 |
| 165 // Data for large code points or code points with both mappings. | 206 // Data for large code points or code points with both mappings. |
| 166 static const int32_t stage2_exception_[][2]; | 207 static const int32_t stage2_exception_[][2]; |
| 167 }; | 208 }; |
| 168 | 209 |
| 169 } // namespace dart | 210 } // namespace dart |
| 170 | 211 |
| 171 #endif // VM_UNICODE_H_ | 212 #endif // VM_UNICODE_H_ |
| OLD | NEW |