blob: 4abd605f5ad30dc9755c1dac8cc701e19e7756a2 [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_UTF_H_
18#define ART_RUNTIME_UTF_H_
Elliott Hughes814e4032011-08-23 12:07:56 -070019
Elliott Hughes76160052012-12-12 16:31:20 -080020#include "base/macros.h"
Ian Rogers719d1a32014-03-06 12:13:39 -080021#include "base/mutex.h"
Elliott Hughes76160052012-12-12 16:31:20 -080022
Elliott Hughes814e4032011-08-23 12:07:56 -070023#include <stddef.h>
24#include <stdint.h>
25
Elliott Hughesb465ab02011-08-24 11:21:21 -070026/*
27 * All UTF-8 in art is actually modified UTF-8. Mostly, this distinction
28 * doesn't matter.
29 *
30 * See http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8 for the details.
31 */
Elliott Hughes814e4032011-08-23 12:07:56 -070032namespace art {
Ian Rogersa6724902013-09-23 09:23:37 -070033
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034namespace mirror {
Ian Rogersa6724902013-09-23 09:23:37 -070035 template<class T> class PrimitiveArray;
36 typedef PrimitiveArray<uint16_t> CharArray;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037} // namespace mirror
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070038
Elliott Hughes814e4032011-08-23 12:07:56 -070039/*
40 * Returns the number of UTF-16 characters in the given modified UTF-8 string.
41 */
42size_t CountModifiedUtf8Chars(const char* utf8);
Bruce Hoult1646d7a2015-10-28 15:06:12 +030043size_t CountModifiedUtf8Chars(const char* utf8, size_t byte_count);
Elliott Hughes814e4032011-08-23 12:07:56 -070044
45/*
46 * Returns the number of modified UTF-8 bytes needed to represent the given
47 * UTF-16 string.
48 */
49size_t CountUtf8Bytes(const uint16_t* chars, size_t char_count);
50
51/*
Elliott Hughesb465ab02011-08-24 11:21:21 -070052 * Convert from Modified UTF-8 to UTF-16.
Elliott Hughes814e4032011-08-23 12:07:56 -070053 */
Elliott Hughesb465ab02011-08-24 11:21:21 -070054void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_out, const char* utf8_in);
Bruce Hoult1646d7a2015-10-28 15:06:12 +030055void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_out, size_t out_chars,
56 const char* utf8_in, size_t in_bytes);
Elliott Hughesb465ab02011-08-24 11:21:21 -070057
58/*
Ian Rogers0571d352011-11-03 19:51:38 -070059 * Compare two modified UTF-8 strings as UTF-16 code point values in a non-locale sensitive manner
60 */
Ian Rogers1ff3c982014-08-12 02:30:58 -070061ALWAYS_INLINE int CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(const char* utf8_1,
62 const char* utf8_2);
Ian Rogers0571d352011-11-03 19:51:38 -070063
64/*
Vladimir Markoa48aef42014-12-03 17:53:53 +000065 * Compare a null-terminated modified UTF-8 string with a UTF-16 string (not null-terminated)
66 * as code point values in a non-locale sensitive manner.
Ian Rogers637c65b2013-05-31 11:46:00 -070067 */
Vladimir Markoa48aef42014-12-03 17:53:53 +000068int CompareModifiedUtf8ToUtf16AsCodePointValues(const char* utf8, const uint16_t* utf16,
69 size_t utf16_length);
Ian Rogers637c65b2013-05-31 11:46:00 -070070
71/*
Elliott Hughesb465ab02011-08-24 11:21:21 -070072 * Convert from UTF-16 to Modified UTF-8. Note that the output is _not_
73 * NUL-terminated. You probably need to call CountUtf8Bytes before calling
74 * this anyway, so if you want a NUL-terminated string, you know where to
75 * put the NUL byte.
76 */
Bruce Hoult1646d7a2015-10-28 15:06:12 +030077void ConvertUtf16ToModifiedUtf8(char* utf8_out, size_t byte_count,
78 const uint16_t* utf16_in, size_t char_count);
Elliott Hughes814e4032011-08-23 12:07:56 -070079
80/*
81 * The java.lang.String hashCode() algorithm.
82 */
Ian Rogersef7d42f2014-01-06 12:55:46 -080083int32_t ComputeUtf16Hash(mirror::CharArray* chars, int32_t offset, size_t char_count)
Mathieu Chartier90443472015-07-16 20:32:27 -070084 SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes814e4032011-08-23 12:07:56 -070085int32_t ComputeUtf16Hash(const uint16_t* chars, size_t char_count);
86
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -080087// Compute a hash code of a modified UTF-8 string. Not the standard java hash since it returns a
Mathieu Chartier208a5cb2015-12-02 15:44:07 -080088// uint32_t and hashes individual chars instead of codepoint words.
89uint32_t ComputeModifiedUtf8Hash(const char* chars);
Ian Rogers68b56852014-08-29 20:19:11 -070090
Elliott Hughes814e4032011-08-23 12:07:56 -070091/*
Narayan Kamatha5afcfc2015-01-29 20:06:46 +000092 * Retrieve the next UTF-16 character or surrogate pair from a UTF-8 string.
93 * single byte, 2-byte and 3-byte UTF-8 sequences result in a single UTF-16
Narayan Kamath8508e372015-05-06 14:55:43 +010094 * character (possibly one half of a surrogate) whereas 4-byte UTF-8 sequences
95 * result in a surrogate pair. Use GetLeadingUtf16Char and GetTrailingUtf16Char
96 * to process the return value of this function.
Elliott Hughes814e4032011-08-23 12:07:56 -070097 *
98 * Advances "*utf8_data_in" to the start of the next character.
99 *
100 * WARNING: If a string is corrupted by dropping a '\0' in the middle
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000101 * of a multi byte sequence, you can end up overrunning the buffer with
Elliott Hughes814e4032011-08-23 12:07:56 -0700102 * reads (and possibly with the writes if the length was computed and
103 * cached before the damage). For performance reasons, this function
104 * assumes that the string being parsed is known to be valid (e.g., by
105 * already being verified). Most strings we process here are coming
106 * out of dex files or other internal translations, so the only real
107 * risk comes from the JNI NewStringUTF call.
108 */
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000109uint32_t GetUtf16FromUtf8(const char** utf8_data_in);
110
111/**
112 * Gets the leading UTF-16 character from a surrogate pair, or the sole
113 * UTF-16 character from the return value of GetUtf16FromUtf8.
114 */
115ALWAYS_INLINE uint16_t GetLeadingUtf16Char(uint32_t maybe_pair);
116
117/**
118 * Gets the trailing UTF-16 character from a surrogate pair, or 0 otherwise
119 * from the return value of GetUtf16FromUtf8.
120 */
121ALWAYS_INLINE uint16_t GetTrailingUtf16Char(uint32_t maybe_pair);
Elliott Hughes814e4032011-08-23 12:07:56 -0700122
123} // namespace art
124
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700125#endif // ART_RUNTIME_UTF_H_