blob: 0cde0f2e4dc0b1d00721c7997f900e84d0b62370 [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 Hughesbf86d042011-08-31 17:53:14 -070073/*
74 * public int indexOf(int c, int start)
75 *
76 * Scan forward through the string for a matching character.
77 * The character must be <= 0xffff; this method does not handle supplementary
78 * characters.
79 *
80 * Determine the index of the first character matching "ch". The string
81 * to search is described by "chars", "offset", and "count".
82 *
83 * The character must be <= 0xffff. Supplementary characters are handled in
84 * Java.
85 *
86 * The "start" parameter must be clamped to [0..count].
87 *
88 * Returns -1 if no match is found.
89 */
Elliott Hughes0512f022012-03-15 22:10:52 -070090static jint String_fastIndexOf(JNIEnv* env, jobject javaThis, jint ch, jint start) {
Elliott Hughesbf86d042011-08-31 17:53:14 -070091 String* s = Decode<String*>(env, javaThis);
92 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
93
94 if (start < 0) {
95 start = 0;
96 }
97
98 /* 16-bit loop, slightly better on ARM */
99 const uint16_t* ptr = chars + start;
100 const uint16_t* endPtr = chars + s->GetLength();
101 while (ptr < endPtr) {
102 if (*ptr++ == ch) {
103 return (ptr-1) - chars;
104 }
105 }
106
107 return -1;
108}
109
Elliott Hughes0512f022012-03-15 22:10:52 -0700110static jstring String_intern(JNIEnv* env, jobject javaThis) {
Brian Carlstromc74255f2011-09-11 22:47:39 -0700111 String* s = Decode<String*>(env, javaThis);
112 String* result = s->Intern();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700113 return AddLocalReference<jstring>(env, result);
114}
115
Elliott Hughes0512f022012-03-15 22:10:52 -0700116static JNINativeMethod gMethods[] = {
Elliott Hughesbf86d042011-08-31 17:53:14 -0700117 NATIVE_METHOD(String, compareTo, "(Ljava/lang/String;)I"),
Elliott Hughesbf86d042011-08-31 17:53:14 -0700118 NATIVE_METHOD(String, fastIndexOf, "(II)I"),
119 NATIVE_METHOD(String, intern, "()Ljava/lang/String;"),
120};
121
Elliott Hughesbf86d042011-08-31 17:53:14 -0700122void register_java_lang_String(JNIEnv* env) {
123 jniRegisterNativeMethods(env, "java/lang/String", gMethods, NELEM(gMethods));
124}
125
126} // namespace art