blob: 075889612537a5764743fce5d12f13a860beb872 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Hughes814e4032011-08-23 12:07:56 -070016
17#ifndef ART_SRC_UTF_H_
18#define ART_SRC_UTF_H_
19
20#include <stddef.h>
21#include <stdint.h>
22
Elliott Hughesb465ab02011-08-24 11:21:21 -070023/*
24 * All UTF-8 in art is actually modified UTF-8. Mostly, this distinction
25 * doesn't matter.
26 *
27 * See http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8 for the details.
28 */
Elliott Hughes814e4032011-08-23 12:07:56 -070029namespace art {
30
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070031template<class T> class PrimitiveArray;
32typedef PrimitiveArray<uint16_t> CharArray;
33
Elliott Hughes814e4032011-08-23 12:07:56 -070034/*
35 * Returns the number of UTF-16 characters in the given modified UTF-8 string.
36 */
37size_t CountModifiedUtf8Chars(const char* utf8);
38
39/*
40 * Returns the number of modified UTF-8 bytes needed to represent the given
41 * UTF-16 string.
42 */
43size_t CountUtf8Bytes(const uint16_t* chars, size_t char_count);
44
45/*
Elliott Hughesb465ab02011-08-24 11:21:21 -070046 * Convert from Modified UTF-8 to UTF-16.
Elliott Hughes814e4032011-08-23 12:07:56 -070047 */
Elliott Hughesb465ab02011-08-24 11:21:21 -070048void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_out, const char* utf8_in);
49
50/*
Ian Rogers0571d352011-11-03 19:51:38 -070051 * Compare two modified UTF-8 strings as UTF-16 code point values in a non-locale sensitive manner
52 */
53int CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(const char* utf8_1, const char* utf8_2);
54
55/*
Elliott Hughesb465ab02011-08-24 11:21:21 -070056 * Convert from UTF-16 to Modified UTF-8. Note that the output is _not_
57 * NUL-terminated. You probably need to call CountUtf8Bytes before calling
58 * this anyway, so if you want a NUL-terminated string, you know where to
59 * put the NUL byte.
60 */
61void ConvertUtf16ToModifiedUtf8(char* utf8_out, const uint16_t* utf16_in, size_t char_count);
Elliott Hughes814e4032011-08-23 12:07:56 -070062
63/*
64 * The java.lang.String hashCode() algorithm.
65 */
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070066int32_t ComputeUtf16Hash(const CharArray* chars, int32_t offset, size_t char_count);
Elliott Hughes814e4032011-08-23 12:07:56 -070067int32_t ComputeUtf16Hash(const uint16_t* chars, size_t char_count);
68
69/*
70 * Retrieve the next UTF-16 character from a UTF-8 string.
71 *
72 * Advances "*utf8_data_in" to the start of the next character.
73 *
74 * WARNING: If a string is corrupted by dropping a '\0' in the middle
75 * of a 3-byte sequence, you can end up overrunning the buffer with
76 * reads (and possibly with the writes if the length was computed and
77 * cached before the damage). For performance reasons, this function
78 * assumes that the string being parsed is known to be valid (e.g., by
79 * already being verified). Most strings we process here are coming
80 * out of dex files or other internal translations, so the only real
81 * risk comes from the JNI NewStringUTF call.
82 */
83uint16_t GetUtf16FromUtf8(const char** utf8_data_in);
84
85} // namespace art
86
87#endif // ART_SRC_UTF_H_