Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 16 | |
| 17 | #include "utf.h" |
| 18 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 19 | #include "base/logging.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 20 | #include "mirror/array.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 21 | #include "mirror/object-inl.h" |
Ian Rogers | a672490 | 2013-09-23 09:23:37 -0700 | [diff] [blame] | 22 | #include "utf-inl.h" |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 23 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 24 | namespace art { |
| 25 | |
| 26 | size_t CountModifiedUtf8Chars(const char* utf8) { |
| 27 | size_t len = 0; |
| 28 | int ic; |
| 29 | while ((ic = *utf8++) != '\0') { |
| 30 | len++; |
| 31 | if ((ic & 0x80) == 0) { |
| 32 | // one-byte encoding |
| 33 | continue; |
| 34 | } |
| 35 | // two- or three-byte encoding |
| 36 | utf8++; |
| 37 | if ((ic & 0x20) == 0) { |
| 38 | // two-byte encoding |
| 39 | continue; |
| 40 | } |
| 41 | // three-byte encoding |
| 42 | utf8++; |
| 43 | } |
| 44 | return len; |
| 45 | } |
| 46 | |
| 47 | void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_data_out, const char* utf8_data_in) { |
| 48 | while (*utf8_data_in != '\0') { |
| 49 | *utf16_data_out++ = GetUtf16FromUtf8(&utf8_data_in); |
| 50 | } |
| 51 | } |
| 52 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 53 | void ConvertUtf16ToModifiedUtf8(char* utf8_out, const uint16_t* utf16_in, size_t char_count) { |
| 54 | while (char_count--) { |
| 55 | uint16_t ch = *utf16_in++; |
| 56 | if (ch > 0 && ch <= 0x7f) { |
| 57 | *utf8_out++ = ch; |
| 58 | } else { |
| 59 | if (ch > 0x07ff) { |
| 60 | *utf8_out++ = (ch >> 12) | 0xe0; |
| 61 | *utf8_out++ = ((ch >> 6) & 0x3f) | 0x80; |
| 62 | *utf8_out++ = (ch & 0x3f) | 0x80; |
| 63 | } else /*(ch > 0x7f || ch == 0)*/ { |
| 64 | *utf8_out++ = (ch >> 6) | 0xc0; |
| 65 | *utf8_out++ = (ch & 0x3f) | 0x80; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 71 | int32_t ComputeUtf16Hash(mirror::CharArray* chars, int32_t offset, |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 72 | size_t char_count) { |
Ian Rogers | 8f41dc3 | 2014-10-30 15:16:16 -0700 | [diff] [blame] | 73 | uint32_t hash = 0; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 74 | for (size_t i = 0; i < char_count; i++) { |
| 75 | hash = hash * 31 + chars->Get(offset + i); |
| 76 | } |
Ian Rogers | 8f41dc3 | 2014-10-30 15:16:16 -0700 | [diff] [blame] | 77 | return static_cast<int32_t>(hash); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 80 | int32_t ComputeUtf16Hash(const uint16_t* chars, size_t char_count) { |
Ian Rogers | 8f41dc3 | 2014-10-30 15:16:16 -0700 | [diff] [blame] | 81 | uint32_t hash = 0; |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 82 | while (char_count--) { |
| 83 | hash = hash * 31 + *chars++; |
| 84 | } |
Ian Rogers | 8f41dc3 | 2014-10-30 15:16:16 -0700 | [diff] [blame] | 85 | return static_cast<int32_t>(hash); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 88 | size_t ComputeModifiedUtf8Hash(const char* chars) { |
| 89 | size_t hash = 0; |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 90 | while (*chars != '\0') { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 91 | hash = hash * 31 + *chars++; |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 92 | } |
Ian Rogers | 8f41dc3 | 2014-10-30 15:16:16 -0700 | [diff] [blame] | 93 | return static_cast<int32_t>(hash); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 96 | int CompareModifiedUtf8ToUtf16AsCodePointValues(const char* utf8_1, const uint16_t* utf8_2) { |
| 97 | for (;;) { |
| 98 | if (*utf8_1 == '\0') { |
| 99 | return (*utf8_2 == '\0') ? 0 : -1; |
| 100 | } else if (*utf8_2 == '\0') { |
| 101 | return 1; |
| 102 | } |
| 103 | |
| 104 | int c1 = GetUtf16FromUtf8(&utf8_1); |
| 105 | int c2 = *utf8_2; |
| 106 | |
| 107 | if (c1 != c2) { |
| 108 | return c1 > c2 ? 1 : -1; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 113 | size_t CountUtf8Bytes(const uint16_t* chars, size_t char_count) { |
| 114 | size_t result = 0; |
| 115 | while (char_count--) { |
| 116 | uint16_t ch = *chars++; |
| 117 | if (ch > 0 && ch <= 0x7f) { |
| 118 | ++result; |
| 119 | } else { |
| 120 | if (ch > 0x7ff) { |
| 121 | result += 3; |
| 122 | } else { |
| 123 | result += 2; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | return result; |
| 128 | } |
| 129 | |
| 130 | } // namespace art |