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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_UTF_H_ |
| 18 | #define ART_RUNTIME_UTF_H_ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 19 | |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 20 | #include "base/macros.h" |
| 21 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 22 | #include <stddef.h> |
| 23 | #include <stdint.h> |
| 24 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 25 | /* |
| 26 | * All UTF-8 in art is actually modified UTF-8. Mostly, this distinction |
| 27 | * doesn't matter. |
| 28 | * |
| 29 | * See http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8 for the details. |
| 30 | */ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 31 | namespace art { |
Ian Rogers | a672490 | 2013-09-23 09:23:37 -0700 | [diff] [blame] | 32 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 33 | /* |
| 34 | * Returns the number of UTF-16 characters in the given modified UTF-8 string. |
| 35 | */ |
| 36 | size_t CountModifiedUtf8Chars(const char* utf8); |
Bruce Hoult | 1646d7a | 2015-10-28 15:06:12 +0300 | [diff] [blame] | 37 | size_t CountModifiedUtf8Chars(const char* utf8, size_t byte_count); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * Returns the number of modified UTF-8 bytes needed to represent the given |
| 41 | * UTF-16 string. |
| 42 | */ |
| 43 | size_t CountUtf8Bytes(const uint16_t* chars, size_t char_count); |
| 44 | |
| 45 | /* |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 46 | * Convert from Modified UTF-8 to UTF-16. |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 47 | */ |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 48 | void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_out, const char* utf8_in); |
Bruce Hoult | 1646d7a | 2015-10-28 15:06:12 +0300 | [diff] [blame] | 49 | void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_out, size_t out_chars, |
| 50 | const char* utf8_in, size_t in_bytes); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 51 | |
| 52 | /* |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 53 | * Compare two modified UTF-8 strings as UTF-16 code point values in a non-locale sensitive manner |
| 54 | */ |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 55 | ALWAYS_INLINE int CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(const char* utf8_1, |
| 56 | const char* utf8_2); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 57 | |
| 58 | /* |
Vladimir Marko | a48aef4 | 2014-12-03 17:53:53 +0000 | [diff] [blame] | 59 | * Compare a null-terminated modified UTF-8 string with a UTF-16 string (not null-terminated) |
| 60 | * as code point values in a non-locale sensitive manner. |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 61 | */ |
Vladimir Marko | a48aef4 | 2014-12-03 17:53:53 +0000 | [diff] [blame] | 62 | int CompareModifiedUtf8ToUtf16AsCodePointValues(const char* utf8, const uint16_t* utf16, |
| 63 | size_t utf16_length); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 64 | |
| 65 | /* |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 66 | * Convert from UTF-16 to Modified UTF-8. Note that the output is _not_ |
| 67 | * NUL-terminated. You probably need to call CountUtf8Bytes before calling |
| 68 | * this anyway, so if you want a NUL-terminated string, you know where to |
| 69 | * put the NUL byte. |
| 70 | */ |
Bruce Hoult | 1646d7a | 2015-10-28 15:06:12 +0300 | [diff] [blame] | 71 | void ConvertUtf16ToModifiedUtf8(char* utf8_out, size_t byte_count, |
| 72 | const uint16_t* utf16_in, size_t char_count); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | * The java.lang.String hashCode() algorithm. |
| 76 | */ |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 77 | template<typename MemoryType> |
| 78 | int32_t ComputeUtf16Hash(const MemoryType* chars, size_t char_count) { |
| 79 | uint32_t hash = 0; |
| 80 | while (char_count--) { |
| 81 | hash = hash * 31 + *chars++; |
| 82 | } |
| 83 | return static_cast<int32_t>(hash); |
| 84 | } |
| 85 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 86 | int32_t ComputeUtf16HashFromModifiedUtf8(const char* utf8, size_t utf16_length); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 87 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 88 | // Compute a hash code of a modified UTF-8 string. Not the standard java hash since it returns a |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 89 | // uint32_t and hashes individual chars instead of codepoint words. |
| 90 | uint32_t ComputeModifiedUtf8Hash(const char* chars); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 91 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 92 | /* |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 93 | * Retrieve the next UTF-16 character or surrogate pair from a UTF-8 string. |
| 94 | * single byte, 2-byte and 3-byte UTF-8 sequences result in a single UTF-16 |
Narayan Kamath | 8508e37 | 2015-05-06 14:55:43 +0100 | [diff] [blame] | 95 | * character (possibly one half of a surrogate) whereas 4-byte UTF-8 sequences |
| 96 | * result in a surrogate pair. Use GetLeadingUtf16Char and GetTrailingUtf16Char |
| 97 | * to process the return value of this function. |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 98 | * |
| 99 | * Advances "*utf8_data_in" to the start of the next character. |
| 100 | * |
| 101 | * WARNING: If a string is corrupted by dropping a '\0' in the middle |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 102 | * of a multi byte sequence, you can end up overrunning the buffer with |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 103 | * reads (and possibly with the writes if the length was computed and |
| 104 | * cached before the damage). For performance reasons, this function |
| 105 | * assumes that the string being parsed is known to be valid (e.g., by |
| 106 | * already being verified). Most strings we process here are coming |
| 107 | * out of dex files or other internal translations, so the only real |
| 108 | * risk comes from the JNI NewStringUTF call. |
| 109 | */ |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 110 | uint32_t GetUtf16FromUtf8(const char** utf8_data_in); |
| 111 | |
| 112 | /** |
| 113 | * Gets the leading UTF-16 character from a surrogate pair, or the sole |
| 114 | * UTF-16 character from the return value of GetUtf16FromUtf8. |
| 115 | */ |
| 116 | ALWAYS_INLINE uint16_t GetLeadingUtf16Char(uint32_t maybe_pair); |
| 117 | |
| 118 | /** |
| 119 | * Gets the trailing UTF-16 character from a surrogate pair, or 0 otherwise |
| 120 | * from the return value of GetUtf16FromUtf8. |
| 121 | */ |
| 122 | ALWAYS_INLINE uint16_t GetTrailingUtf16Char(uint32_t maybe_pair); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 123 | |
| 124 | } // namespace art |
| 125 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 126 | #endif // ART_RUNTIME_UTF_H_ |