Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_UTF_H_ |
| 4 | #define ART_SRC_UTF_H_ |
| 5 | |
| 6 | #include <stddef.h> |
| 7 | #include <stdint.h> |
| 8 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 9 | /* |
| 10 | * All UTF-8 in art is actually modified UTF-8. Mostly, this distinction |
| 11 | * doesn't matter. |
| 12 | * |
| 13 | * See http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8 for the details. |
| 14 | */ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 15 | namespace art { |
| 16 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 17 | template<class T> class PrimitiveArray; |
| 18 | typedef PrimitiveArray<uint16_t> CharArray; |
| 19 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 20 | /* |
| 21 | * Returns the number of UTF-16 characters in the given modified UTF-8 string. |
| 22 | */ |
| 23 | size_t CountModifiedUtf8Chars(const char* utf8); |
| 24 | |
| 25 | /* |
| 26 | * Returns the number of modified UTF-8 bytes needed to represent the given |
| 27 | * UTF-16 string. |
| 28 | */ |
| 29 | size_t CountUtf8Bytes(const uint16_t* chars, size_t char_count); |
| 30 | |
| 31 | /* |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 32 | * Convert from Modified UTF-8 to UTF-16. |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 33 | */ |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 34 | void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_out, const char* utf8_in); |
| 35 | |
| 36 | /* |
| 37 | * Convert from UTF-16 to Modified UTF-8. Note that the output is _not_ |
| 38 | * NUL-terminated. You probably need to call CountUtf8Bytes before calling |
| 39 | * this anyway, so if you want a NUL-terminated string, you know where to |
| 40 | * put the NUL byte. |
| 41 | */ |
| 42 | void ConvertUtf16ToModifiedUtf8(char* utf8_out, const uint16_t* utf16_in, size_t char_count); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * The java.lang.String hashCode() algorithm. |
| 46 | */ |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 47 | int32_t ComputeUtf16Hash(const CharArray* chars, int32_t offset, size_t char_count); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 48 | int32_t ComputeUtf16Hash(const uint16_t* chars, size_t char_count); |
| 49 | |
| 50 | /* |
| 51 | * Retrieve the next UTF-16 character from a UTF-8 string. |
| 52 | * |
| 53 | * Advances "*utf8_data_in" to the start of the next character. |
| 54 | * |
| 55 | * WARNING: If a string is corrupted by dropping a '\0' in the middle |
| 56 | * of a 3-byte sequence, you can end up overrunning the buffer with |
| 57 | * reads (and possibly with the writes if the length was computed and |
| 58 | * cached before the damage). For performance reasons, this function |
| 59 | * assumes that the string being parsed is known to be valid (e.g., by |
| 60 | * already being verified). Most strings we process here are coming |
| 61 | * out of dex files or other internal translations, so the only real |
| 62 | * risk comes from the JNI NewStringUTF call. |
| 63 | */ |
| 64 | uint16_t GetUtf16FromUtf8(const char** utf8_data_in); |
| 65 | |
| 66 | } // namespace art |
| 67 | |
| 68 | #endif // ART_SRC_UTF_H_ |