blob: 17dbbe94477867eba517d01efa8b30c6903f8089 [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 Hughes529bfef2012-04-06 17:23:54 -070073static jint String_fastIndexOf(JNIEnv* env, jobject java_this, jint ch, jint start) {
74 // This method does not handle supplementary characters. They're dealt with in managed code.
75 DCHECK_LE(ch, 0xffff);
Elliott Hughesbf86d042011-08-31 17:53:14 -070076
Elliott Hughes529bfef2012-04-06 17:23:54 -070077 String* s = Decode<String*>(env, java_this);
78
79 jint count = s->GetLength();
Elliott Hughesbf86d042011-08-31 17:53:14 -070080 if (start < 0) {
81 start = 0;
Elliott Hughes529bfef2012-04-06 17:23:54 -070082 } else if (start > count) {
83 start = count;
Elliott Hughesbf86d042011-08-31 17:53:14 -070084 }
85
Elliott Hughes529bfef2012-04-06 17:23:54 -070086 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
87 const uint16_t* p = chars + start;
88 const uint16_t* end = chars + count;
89 while (p < end) {
90 if (*p++ == ch) {
91 return (p - 1) - chars;
Elliott Hughesbf86d042011-08-31 17:53:14 -070092 }
93 }
94
95 return -1;
96}
97
Elliott Hughes0512f022012-03-15 22:10:52 -070098static jstring String_intern(JNIEnv* env, jobject javaThis) {
Brian Carlstromc74255f2011-09-11 22:47:39 -070099 String* s = Decode<String*>(env, javaThis);
100 String* result = s->Intern();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700101 return AddLocalReference<jstring>(env, result);
102}
103
Elliott Hughes0512f022012-03-15 22:10:52 -0700104static JNINativeMethod gMethods[] = {
Elliott Hughesbf86d042011-08-31 17:53:14 -0700105 NATIVE_METHOD(String, compareTo, "(Ljava/lang/String;)I"),
Elliott Hughesbf86d042011-08-31 17:53:14 -0700106 NATIVE_METHOD(String, fastIndexOf, "(II)I"),
107 NATIVE_METHOD(String, intern, "()Ljava/lang/String;"),
108};
109
Elliott Hughesbf86d042011-08-31 17:53:14 -0700110void register_java_lang_String(JNIEnv* env) {
111 jniRegisterNativeMethods(env, "java/lang/String", gMethods, NELEM(gMethods));
112}
113
114} // namespace art