blob: 9aa5221b0e6d397e6b6dff8634d654d97f070710 [file] [log] [blame]
Elliott Hughesbf86d042011-08-31 17:53:14 -07001/*
2 * Copyright (C) 2008 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 */
16
17#include "jni_internal.h"
18#include "object.h"
19
20#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
21
22#ifdef HAVE__MEMCMP16
23// "count" is in 16-bit units.
24extern "C" uint32_t __memcmp16(const uint16_t* s0, const uint16_t* s1, size_t count);
25#define MemCmp16 __memcmp16
26#else
27uint32_t MemCmp16(const uint16_t* s0, const uint16_t* s1, size_t count) {
28 for (size_t i = 0; i < count; i++) {
29 if (s0[i] != s1[i]) {
30 return static_cast<int32_t>(s0[i]) - static_cast<int32_t>(s1[i]);
31 }
32 }
33 return 0;
34}
35#endif
36
37namespace art {
38
39namespace {
40
41jint String_compareTo(JNIEnv* env, jobject javaThis, jobject javaRhs) {
42 String* lhs = Decode<String*>(env, javaThis);
43 String* rhs = Decode<String*>(env, javaRhs);
44
45 if (rhs == NULL) {
46 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "rhs == null");
47 return -1;
48 }
49
50 // Quick test for comparison of a string with itself.
51 if (lhs == rhs) {
52 return 0;
53 }
54
55 // TODO: is this still true?
56 // The annoying part here is that 0x00e9 - 0xffff != 0x00ea,
57 // because the interpreter converts the characters to 32-bit integers
58 // *without* sign extension before it subtracts them (which makes some
59 // sense since "char" is unsigned). So what we get is the result of
60 // 0x000000e9 - 0x0000ffff, which is 0xffff00ea.
61 int lhsCount = lhs->GetLength();
62 int rhsCount = rhs->GetLength();
63 int countDiff = lhsCount - rhsCount;
64 int minCount = (countDiff < 0) ? lhsCount : rhsCount;
65 const uint16_t* lhsChars = lhs->GetCharArray()->GetData() + lhs->GetOffset();
66 const uint16_t* rhsChars = rhs->GetCharArray()->GetData() + rhs->GetOffset();
67 int otherRes = MemCmp16(lhsChars, rhsChars, minCount);
68 if (otherRes != 0) {
69 return otherRes;
70 }
71 return countDiff;
72}
73
74jboolean String_equals(JNIEnv* env, jobject javaThis, jobject javaRhs) {
75 String* lhs = Decode<String*>(env, javaThis);
76 String* rhs = Decode<String*>(env, javaRhs);
77
78 // Quick test for comparison of a string with itself.
79 if (lhs == rhs) {
80 return JNI_TRUE;
81 }
82
83 // if (!(rhs instanceof String)) return false.
84 if (rhs == NULL || lhs->GetClass() != rhs->GetClass()) {
85 return JNI_FALSE;
86 }
87
88 // Quick length check.
89 int lhsCount = lhs->GetLength();
90 int rhsCount = rhs->GetLength();
91 if (lhsCount != rhsCount) {
92 return JNI_FALSE;
93 }
94
95 // You may, at this point, be tempted to pull out the hashCode fields
96 // and compare them. If both fields have been initialized, and they
97 // are not equal, we can return false immediately.
98 //
99 // However, the hashCode field is often not set. If it is set,
100 // there's an excellent chance that the String is being used as a key
101 // in a hashed data structure (e.g. HashMap). That data structure has
102 // already made the comparison and determined that the hashes are equal,
103 // making a check here redundant.
104 //
105 // It's not clear that checking the hashes will be a win in "typical"
106 // use cases. We err on the side of simplicity and ignore them.
107
108 const uint16_t* lhsChars = lhs->GetCharArray()->GetData() + lhs->GetOffset();
109 const uint16_t* rhsChars = rhs->GetCharArray()->GetData() + rhs->GetOffset();
110 return (MemCmp16(lhsChars, rhsChars, lhsCount) == 0) ? JNI_TRUE : JNI_FALSE;
111}
112
113/*
114 * public int indexOf(int c, int start)
115 *
116 * Scan forward through the string for a matching character.
117 * The character must be <= 0xffff; this method does not handle supplementary
118 * characters.
119 *
120 * Determine the index of the first character matching "ch". The string
121 * to search is described by "chars", "offset", and "count".
122 *
123 * The character must be <= 0xffff. Supplementary characters are handled in
124 * Java.
125 *
126 * The "start" parameter must be clamped to [0..count].
127 *
128 * Returns -1 if no match is found.
129 */
130jint String_fastIndexOf(JNIEnv* env, jobject javaThis, jint ch, jint start) {
131 String* s = Decode<String*>(env, javaThis);
132 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
133
134 if (start < 0) {
135 start = 0;
136 }
137
138 /* 16-bit loop, slightly better on ARM */
139 const uint16_t* ptr = chars + start;
140 const uint16_t* endPtr = chars + s->GetLength();
141 while (ptr < endPtr) {
142 if (*ptr++ == ch) {
143 return (ptr-1) - chars;
144 }
145 }
146
147 return -1;
148}
149
150jstring String_intern(JNIEnv* env, jobject javaThis) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700151 String* s = Decode<String*>(env, javaThis);
152 String* result = s->Intern();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700153 return AddLocalReference<jstring>(env, result);
154}
155
156JNINativeMethod gMethods[] = {
157 NATIVE_METHOD(String, compareTo, "(Ljava/lang/String;)I"),
158 NATIVE_METHOD(String, equals, "(Ljava/lang/Object;)Z"),
159 NATIVE_METHOD(String, fastIndexOf, "(II)I"),
160 NATIVE_METHOD(String, intern, "()Ljava/lang/String;"),
161};
162
163} // namespace
164
165void register_java_lang_String(JNIEnv* env) {
166 jniRegisterNativeMethods(env, "java/lang/String", gMethods, NELEM(gMethods));
167}
168
169} // namespace art