blob: 7b9b90e11ea581f684d0190d200d75c9af0f0d2a [file] [log] [blame]
Elliott Hughes814e4032011-08-23 12:07:56 -07001// 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 Hughesb465ab02011-08-24 11:21:21 -07009/*
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 Hughes814e4032011-08-23 12:07:56 -070015namespace art {
16
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070017template<class T> class PrimitiveArray;
18typedef PrimitiveArray<uint16_t> CharArray;
19
Elliott Hughes814e4032011-08-23 12:07:56 -070020/*
21 * Returns the number of UTF-16 characters in the given modified UTF-8 string.
22 */
23size_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 */
29size_t CountUtf8Bytes(const uint16_t* chars, size_t char_count);
30
31/*
Elliott Hughesb465ab02011-08-24 11:21:21 -070032 * Convert from Modified UTF-8 to UTF-16.
Elliott Hughes814e4032011-08-23 12:07:56 -070033 */
Elliott Hughesb465ab02011-08-24 11:21:21 -070034void 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 */
42void ConvertUtf16ToModifiedUtf8(char* utf8_out, const uint16_t* utf16_in, size_t char_count);
Elliott Hughes814e4032011-08-23 12:07:56 -070043
44/*
45 * The java.lang.String hashCode() algorithm.
46 */
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070047int32_t ComputeUtf16Hash(const CharArray* chars, int32_t offset, size_t char_count);
Elliott Hughes814e4032011-08-23 12:07:56 -070048int32_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 */
64uint16_t GetUtf16FromUtf8(const char** utf8_data_in);
65
66} // namespace art
67
68#endif // ART_SRC_UTF_H_