| 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 #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 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 intptr_t Utf8::Length(const String& str) { | 120 intptr_t Utf8::Length(const String& str) { |
| 121 intptr_t length = 0; | 121 intptr_t length = 0; |
| 122 for (intptr_t i = 0; i < str.Length(); ++i) { | 122 for (intptr_t i = 0; i < str.Length(); ++i) { |
| 123 int32_t ch = str.CharAt(i); | 123 int32_t ch = str.CharAt(i); |
| 124 length += Utf8::Length(ch); | 124 length += Utf8::Length(ch); |
| 125 } | 125 } |
| 126 return length; | 126 return length; |
| 127 } | 127 } |
| 128 | 128 |
| 129 | 129 |
| 130 void Utf8::Encode(int32_t ch, char* dst) { | 130 intptr_t Utf8::Encode(int32_t ch, char* dst) { |
| 131 static const int kMask = ~(1 << 6); | 131 static const int kMask = ~(1 << 6); |
| 132 if (ch <= kMaxOneByteChar) { | 132 if (ch <= kMaxOneByteChar) { |
| 133 dst[0] = ch; | 133 dst[0] = ch; |
| 134 } else if (ch <= kMaxTwoByteChar) { | 134 return 1; |
| 135 } |
| 136 if (ch <= kMaxTwoByteChar) { |
| 135 dst[0] = 0xC0 | (ch >> 6); | 137 dst[0] = 0xC0 | (ch >> 6); |
| 136 dst[1] = 0x80 | (ch & kMask); | 138 dst[1] = 0x80 | (ch & kMask); |
| 137 } else if (ch <= kMaxThreeByteChar) { | 139 return 2; |
| 140 } |
| 141 if (ch <= kMaxThreeByteChar) { |
| 138 dst[0] = 0xE0 | (ch >> 12); | 142 dst[0] = 0xE0 | (ch >> 12); |
| 139 dst[1] = 0x80 | ((ch >> 6) & kMask); | 143 dst[1] = 0x80 | ((ch >> 6) & kMask); |
| 140 dst[2] = 0x80 | (ch & kMask); | 144 dst[2] = 0x80 | (ch & kMask); |
| 141 } else { | 145 return 3; |
| 142 ASSERT(ch <= kMaxFourByteChar); | |
| 143 dst[0] = 0xF0 | (ch >> 18); | |
| 144 dst[1] = 0x80 | ((ch >> 12) & kMask); | |
| 145 dst[2] = 0x80 | ((ch >> 6) & kMask); | |
| 146 dst[3] = 0x80 | (ch & kMask); | |
| 147 } | 146 } |
| 147 ASSERT(ch <= kMaxFourByteChar); |
| 148 dst[0] = 0xF0 | (ch >> 18); |
| 149 dst[1] = 0x80 | ((ch >> 12) & kMask); |
| 150 dst[2] = 0x80 | ((ch >> 6) & kMask); |
| 151 dst[3] = 0x80 | (ch & kMask); |
| 152 return 4; |
| 148 } | 153 } |
| 149 | 154 |
| 150 | 155 |
| 151 intptr_t Utf8::Encode(const String& src, char* dst, intptr_t len) { | 156 intptr_t Utf8::Encode(const String& src, char* dst, intptr_t len) { |
| 152 intptr_t pos = 0; | 157 intptr_t pos = 0; |
| 153 for (intptr_t i = 0; i < src.Length(); ++i) { | 158 for (intptr_t i = 0; i < src.Length(); ++i) { |
| 154 intptr_t ch = src.CharAt(i); | 159 intptr_t ch = src.CharAt(i); |
| 155 intptr_t num_bytes = Utf8::Length(ch); | 160 intptr_t num_bytes = Utf8::Length(ch); |
| 156 if (pos + num_bytes > len) { | 161 if (pos + num_bytes > len) { |
| 157 break; | 162 break; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 bool Utf8::Decode(const char* src, uint16_t* dst, intptr_t len) { | 227 bool Utf8::Decode(const char* src, uint16_t* dst, intptr_t len) { |
| 223 return DecodeImpl(src, dst, len); | 228 return DecodeImpl(src, dst, len); |
| 224 } | 229 } |
| 225 | 230 |
| 226 | 231 |
| 227 bool Utf8::Decode(const char* src, uint32_t* dst, intptr_t len) { | 232 bool Utf8::Decode(const char* src, uint32_t* dst, intptr_t len) { |
| 228 return DecodeImpl(src, dst, len); | 233 return DecodeImpl(src, dst, len); |
| 229 } | 234 } |
| 230 | 235 |
| 231 } // namespace dart | 236 } // namespace dart |
| OLD | NEW |