2 * Copyright (C) 2011 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef ART_RUNTIME_UTF_INL_H_
18 #define ART_RUNTIME_UTF_INL_H_
24 inline uint16_t GetTrailingUtf16Char(uint32_t maybe_pair) {
25 return static_cast<uint16_t>(maybe_pair >> 16);
28 inline uint16_t GetLeadingUtf16Char(uint32_t maybe_pair) {
29 return static_cast<uint16_t>(maybe_pair & 0x0000FFFF);
32 inline uint32_t GetUtf16FromUtf8(const char** utf8_data_in) {
33 const uint8_t one = *(*utf8_data_in)++;
34 if ((one & 0x80) == 0) {
39 const uint8_t two = *(*utf8_data_in)++;
40 if ((one & 0x20) == 0) {
42 return ((one & 0x1f) << 6) | (two & 0x3f);
45 const uint8_t three = *(*utf8_data_in)++;
46 if ((one & 0x10) == 0) {
47 return ((one & 0x0f) << 12) | ((two & 0x3f) << 6) | (three & 0x3f);
50 // Four byte encodings need special handling. We'll have
51 // to convert them into a surrogate pair.
52 const uint8_t four = *(*utf8_data_in)++;
54 // Since this is a 4 byte UTF-8 sequence, it will lie between
55 // U+10000 and U+1FFFFF.
57 // TODO: What do we do about values in (U+10FFFF, U+1FFFFF) ? The
58 // spec says they're invalid but nobody appears to check for them.
59 const uint32_t code_point = ((one & 0x0f) << 18) | ((two & 0x3f) << 12)
60 | ((three & 0x3f) << 6) | (four & 0x3f);
62 uint32_t surrogate_pair = 0;
63 // Step two: Write out the high (leading) surrogate to the bottom 16 bits
64 // of the of the 32 bit type.
65 surrogate_pair |= ((code_point >> 10) + 0xd7c0) & 0xffff;
66 // Step three : Write out the low (trailing) surrogate to the top 16 bits.
67 surrogate_pair |= ((code_point & 0x03ff) + 0xdc00) << 16;
69 return surrogate_pair;
72 inline int CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(const char* utf8_1,
78 // Did we reach a terminating character?
80 return (c2 == 0) ? 0 : -1;
85 c1 = GetUtf16FromUtf8(&utf8_1);
86 c2 = GetUtf16FromUtf8(&utf8_2);
89 const uint32_t leading_surrogate_diff = GetLeadingUtf16Char(c1) - GetLeadingUtf16Char(c2);
90 if (leading_surrogate_diff != 0) {
91 return static_cast<int>(leading_surrogate_diff);
94 return GetTrailingUtf16Char(c1) - GetTrailingUtf16Char(c2);
99 #endif // ART_RUNTIME_UTF_INL_H_