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