blob: 14f511c0730a1d4febfbca14fd5fceeed1f9ebb5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
// Copyright 2011 Google Inc. All Rights Reserved.
#include "utf.h"
#include "logging.h"
#include "object.h"
namespace art {
size_t CountModifiedUtf8Chars(const char* utf8) {
size_t len = 0;
int ic;
while ((ic = *utf8++) != '\0') {
len++;
if ((ic & 0x80) == 0) {
// one-byte encoding
continue;
}
// two- or three-byte encoding
utf8++;
if ((ic & 0x20) == 0) {
// two-byte encoding
continue;
}
// three-byte encoding
utf8++;
}
return len;
}
void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_data_out, const char* utf8_data_in) {
while (*utf8_data_in != '\0') {
*utf16_data_out++ = GetUtf16FromUtf8(&utf8_data_in);
}
}
void ConvertUtf16ToModifiedUtf8(char* utf8_out, const uint16_t* utf16_in, size_t char_count) {
while (char_count--) {
uint16_t ch = *utf16_in++;
if (ch > 0 && ch <= 0x7f) {
*utf8_out++ = ch;
} else {
if (ch > 0x07ff) {
*utf8_out++ = (ch >> 12) | 0xe0;
*utf8_out++ = ((ch >> 6) & 0x3f) | 0x80;
*utf8_out++ = (ch & 0x3f) | 0x80;
} else /*(ch > 0x7f || ch == 0)*/ {
*utf8_out++ = (ch >> 6) | 0xc0;
*utf8_out++ = (ch & 0x3f) | 0x80;
}
}
}
}
int32_t ComputeUtf16Hash(const CharArray* chars, int32_t offset,
size_t char_count) {
int32_t hash = 0;
for (size_t i = 0; i < char_count; i++) {
hash = hash * 31 + chars->Get(offset + i);
}
return hash;
}
int32_t ComputeUtf16Hash(const uint16_t* chars, size_t char_count) {
int32_t hash = 0;
while (char_count--) {
hash = hash * 31 + *chars++;
}
return hash;
}
uint16_t GetUtf16FromUtf8(const char** utf8_data_in) {
uint8_t one = *(*utf8_data_in)++;
if ((one & 0x80) == 0) {
// one-byte encoding
return one;
}
// two- or three-byte encoding
uint8_t two = *(*utf8_data_in)++;
if ((one & 0x20) == 0) {
// two-byte encoding
return ((one & 0x1f) << 6) | (two & 0x3f);
}
// three-byte encoding
uint8_t three = *(*utf8_data_in)++;
return ((one & 0x0f) << 12) | ((two & 0x3f) << 6) | (three & 0x3f);
}
int CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(const char* utf8_1, const char* utf8_2) {
for (;;) {
if (*utf8_1 == '\0') {
return (*utf8_2 == '\0') ? 0 : -1;
} else if (*utf8_2 == '\0') {
return 1;
}
int c1 = GetUtf16FromUtf8(&utf8_1);
int c2 = GetUtf16FromUtf8(&utf8_2);
if (c1 != c2) {
return c1 > c2 ? 1 : -1;
}
}
}
size_t CountUtf8Bytes(const uint16_t* chars, size_t char_count) {
size_t result = 0;
while (char_count--) {
uint16_t ch = *chars++;
if (ch > 0 && ch <= 0x7f) {
++result;
} else {
if (ch > 0x7ff) {
result += 3;
} else {
result += 2;
}
}
}
return result;
}
} // namespace art
|