blob: 2c22f8765abab6dbbb7059664f10c6a786cbb861 [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
17/*
18 * Returns the number of UTF-16 characters in the given modified UTF-8 string.
19 */
20size_t CountModifiedUtf8Chars(const char* utf8);
21
22/*
23 * Returns the number of modified UTF-8 bytes needed to represent the given
24 * UTF-16 string.
25 */
26size_t CountUtf8Bytes(const uint16_t* chars, size_t char_count);
27
28/*
Elliott Hughesb465ab02011-08-24 11:21:21 -070029 * Convert from Modified UTF-8 to UTF-16.
Elliott Hughes814e4032011-08-23 12:07:56 -070030 */
Elliott Hughesb465ab02011-08-24 11:21:21 -070031void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_out, const char* utf8_in);
32
33/*
34 * Convert from UTF-16 to Modified UTF-8. Note that the output is _not_
35 * NUL-terminated. You probably need to call CountUtf8Bytes before calling
36 * this anyway, so if you want a NUL-terminated string, you know where to
37 * put the NUL byte.
38 */
39void ConvertUtf16ToModifiedUtf8(char* utf8_out, const uint16_t* utf16_in, size_t char_count);
Elliott Hughes814e4032011-08-23 12:07:56 -070040
41/*
42 * The java.lang.String hashCode() algorithm.
43 */
44int32_t ComputeUtf16Hash(const uint16_t* chars, size_t char_count);
45
46/*
47 * Retrieve the next UTF-16 character from a UTF-8 string.
48 *
49 * Advances "*utf8_data_in" to the start of the next character.
50 *
51 * WARNING: If a string is corrupted by dropping a '\0' in the middle
52 * of a 3-byte sequence, you can end up overrunning the buffer with
53 * reads (and possibly with the writes if the length was computed and
54 * cached before the damage). For performance reasons, this function
55 * assumes that the string being parsed is known to be valid (e.g., by
56 * already being verified). Most strings we process here are coming
57 * out of dex files or other internal translations, so the only real
58 * risk comes from the JNI NewStringUTF call.
59 */
60uint16_t GetUtf16FromUtf8(const char** utf8_data_in);
61
62} // namespace art
63
64#endif // ART_SRC_UTF_H_