Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include "utf.h" |
| 4 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 5 | #include "logging.h" |
| 6 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 7 | namespace art { |
| 8 | |
| 9 | size_t CountModifiedUtf8Chars(const char* utf8) { |
| 10 | size_t len = 0; |
| 11 | int ic; |
| 12 | while ((ic = *utf8++) != '\0') { |
| 13 | len++; |
| 14 | if ((ic & 0x80) == 0) { |
| 15 | // one-byte encoding |
| 16 | continue; |
| 17 | } |
| 18 | // two- or three-byte encoding |
| 19 | utf8++; |
| 20 | if ((ic & 0x20) == 0) { |
| 21 | // two-byte encoding |
| 22 | continue; |
| 23 | } |
| 24 | // three-byte encoding |
| 25 | utf8++; |
| 26 | } |
| 27 | return len; |
| 28 | } |
| 29 | |
| 30 | void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_data_out, const char* utf8_data_in) { |
| 31 | while (*utf8_data_in != '\0') { |
| 32 | *utf16_data_out++ = GetUtf16FromUtf8(&utf8_data_in); |
| 33 | } |
| 34 | } |
| 35 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 36 | void ConvertUtf16ToModifiedUtf8(char* utf8_out, const uint16_t* utf16_in, size_t char_count) { |
| 37 | while (char_count--) { |
| 38 | uint16_t ch = *utf16_in++; |
| 39 | if (ch > 0 && ch <= 0x7f) { |
| 40 | *utf8_out++ = ch; |
| 41 | } else { |
| 42 | if (ch > 0x07ff) { |
| 43 | *utf8_out++ = (ch >> 12) | 0xe0; |
| 44 | *utf8_out++ = ((ch >> 6) & 0x3f) | 0x80; |
| 45 | *utf8_out++ = (ch & 0x3f) | 0x80; |
| 46 | } else /*(ch > 0x7f || ch == 0)*/ { |
| 47 | *utf8_out++ = (ch >> 6) | 0xc0; |
| 48 | *utf8_out++ = (ch & 0x3f) | 0x80; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 54 | int32_t ComputeUtf16Hash(const uint16_t* chars, size_t char_count) { |
| 55 | int32_t hash = 0; |
| 56 | while (char_count--) { |
| 57 | hash = hash * 31 + *chars++; |
| 58 | } |
| 59 | return hash; |
| 60 | } |
| 61 | |
| 62 | uint16_t GetUtf16FromUtf8(const char** utf8_data_in) { |
| 63 | uint8_t one = *(*utf8_data_in)++; |
| 64 | if ((one & 0x80) == 0) { |
| 65 | // one-byte encoding |
| 66 | return one; |
| 67 | } |
| 68 | // two- or three-byte encoding |
| 69 | uint8_t two = *(*utf8_data_in)++; |
| 70 | if ((one & 0x20) == 0) { |
| 71 | // two-byte encoding |
| 72 | return ((one & 0x1f) << 6) | (two & 0x3f); |
| 73 | } |
| 74 | // three-byte encoding |
| 75 | uint8_t three = *(*utf8_data_in)++; |
| 76 | return ((one & 0x0f) << 12) | ((two & 0x3f) << 6) | (three & 0x3f); |
| 77 | } |
| 78 | |
| 79 | size_t CountUtf8Bytes(const uint16_t* chars, size_t char_count) { |
| 80 | size_t result = 0; |
| 81 | while (char_count--) { |
| 82 | uint16_t ch = *chars++; |
| 83 | if (ch > 0 && ch <= 0x7f) { |
| 84 | ++result; |
| 85 | } else { |
| 86 | if (ch > 0x7ff) { |
| 87 | result += 3; |
| 88 | } else { |
| 89 | result += 2; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | return result; |
| 94 | } |
| 95 | |
| 96 | } // namespace art |