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