summaryrefslogtreecommitdiffstats
path: root/vm/UtfString.h
blob: 352948c6ac8696d9cfcee246bcb91b3ff19ae3e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * UTF-8 and Unicode string manipulation functions, plus convenience
 * functions for working with java/lang/String.
 */
#ifndef DALVIK_STRING_H_
#define DALVIK_STRING_H_

#include <string>
#include <vector>

/*
 * (This is private to UtfString.c, but we cheat a bit and also use it
 * for InlineNative.c.  Not really worth creating a separate header.)
 *
 * We can avoid poking around in gDvm by hard-coding the expected values of
 * the String field offsets.  This will be annoying if String is in flux
 * or the VM field layout is changing, so we use defines here to make it
 * easy to switch back to the gDvm version.
 *
 * The values are checked for correctness during startup.
 */
//#define USE_GLOBAL_STRING_DEFS
#ifdef USE_GLOBAL_STRING_DEFS
# define STRING_FIELDOFF_VALUE      gDvm.offJavaLangString_value
# define STRING_FIELDOFF_OFFSET     gDvm.offJavaLangString_offset
# define STRING_FIELDOFF_COUNT      gDvm.offJavaLangString_count
# define STRING_FIELDOFF_HASHCODE   gDvm.offJavaLangString_hashCode
#else
# define STRING_FIELDOFF_VALUE      8
# define STRING_FIELDOFF_HASHCODE   12
# define STRING_FIELDOFF_OFFSET     16
# define STRING_FIELDOFF_COUNT      20
#endif

/*
 * Hash function for modified UTF-8 strings.
 */
u4 dvmComputeUtf8Hash(const char* str);

/*
 * Hash function for string objects. Ensures the hash code field is
 * populated and returns its value.
 */
u4 dvmComputeStringHash(StringObject* strObj);

/*
 * Create a java.lang.String[] from a vector of C++ strings.
 *
 * The caller must call dvmReleaseTrackedAlloc() on the returned array,
 * but not on the individual elements.
 *
 * Returns NULL and throws an exception on failure.
 */
ArrayObject* dvmCreateStringArray(const std::vector<std::string>& strings);

/*
 * Create a java/lang/String from a C string.
 *
 * The caller must call dvmReleaseTrackedAlloc() on the return value.
 *
 * Returns NULL and throws an exception on failure.
 */
StringObject* dvmCreateStringFromCstr(const char* utf8Str);

/*
 * Create a java/lang/String from a C++ string.
 *
 * The caller must call dvmReleaseTrackedAlloc() on the return value.
 *
 * Returns NULL and throws an exception on failure.
 */
StringObject* dvmCreateStringFromCstr(const std::string& utf8Str);

/*
 * Create a java/lang/String from a C string, given its UTF-16 length
 * (number of UTF-16 code points).
 *
 * The caller must call dvmReleaseTrackedAlloc() on the return value.
 *
 * Returns NULL and throws an exception on failure.
 */
StringObject* dvmCreateStringFromCstrAndLength(const char* utf8Str,
    u4 utf16Length);

/*
 * Compute the number of characters in a "modified UTF-8" string.  This will
 * match the result from strlen() so long as there are no multi-byte chars.
 */
size_t dvmUtf8Len(const char* utf8Str);

/*
 * Convert a UTF-8 string to UTF-16.  "utf16Str" must have enough room
 * to hold the output.
 */
void dvmConvertUtf8ToUtf16(u2* utf16Str, const char* utf8Str);

/*
 * Create a java/lang/String from a Unicode string.
 *
 * The caller must call dvmReleaseTrackedAlloc() on the return value.
 */
StringObject* dvmCreateStringFromUnicode(const u2* unichars, int len);

/*
 * Create a UTF-8 C string from a java/lang/String.  Caller must free
 * the result.
 *
 * Returns NULL if "jstr" is NULL.
 */
char* dvmCreateCstrFromString(const StringObject* jstr);

/*
 * Create a UTF-8 C string from a region of a java/lang/String.  (Used by
 * the JNI GetStringUTFRegion call.)
 */
void dvmGetStringUtfRegion(const StringObject* jstr,
        int start, int len, char* buf);

/*
 * Compare two string objects.  (This is a dvmHashTableLookup() callback.)
 */
int dvmHashcmpStrings(const void* vstrObj1, const void* vstrObj2);

#endif  // DALVIK_STRING_H_