blob: d8c258b5d91068542daf3bc0cd63cbb52cebef4d [file] [log] [blame]
Ian Rogersa6724902013-09-23 09:23:37 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#ifndef ART_RUNTIME_UTF_INL_H_
18#define ART_RUNTIME_UTF_INL_H_
19
20#include "utf.h"
21
22namespace art {
23
24inline uint16_t GetUtf16FromUtf8(const char** utf8_data_in) {
25 uint8_t one = *(*utf8_data_in)++;
26 if ((one & 0x80) == 0) {
27 // one-byte encoding
28 return one;
29 }
30 // two- or three-byte encoding
31 uint8_t two = *(*utf8_data_in)++;
32 if ((one & 0x20) == 0) {
33 // two-byte encoding
34 return ((one & 0x1f) << 6) | (two & 0x3f);
35 }
36 // three-byte encoding
37 uint8_t three = *(*utf8_data_in)++;
38 return ((one & 0x0f) << 12) | ((two & 0x3f) << 6) | (three & 0x3f);
39}
40
41inline int CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(const char* utf8_1,
42 const char* utf8_2) {
43 for (;;) {
44 if (*utf8_1 == '\0') {
45 return (*utf8_2 == '\0') ? 0 : -1;
46 } else if (*utf8_2 == '\0') {
47 return 1;
48 }
49
50 int c1 = GetUtf16FromUtf8(&utf8_1);
51 int c2 = GetUtf16FromUtf8(&utf8_2);
52
53 if (c1 != c2) {
54 return c1 > c2 ? 1 : -1;
55 }
56 }
57}
58
59} // namespace art
60
61#endif // ART_RUNTIME_UTF_INL_H_