blob: f8fb4a745e07318298aa3c96351c34cd1886eddb [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
Elliott Hughesbf86d042011-08-31 17:53:14 -070020#ifdef HAVE__MEMCMP16
21// "count" is in 16-bit units.
22extern "C" uint32_t __memcmp16(const uint16_t* s0, const uint16_t* s1, size_t count);
23#define MemCmp16 __memcmp16
24#else
25uint32_t MemCmp16(const uint16_t* s0, const uint16_t* s1, size_t count) {
26 for (size_t i = 0; i < count; i++) {
27 if (s0[i] != s1[i]) {
28 return static_cast<int32_t>(s0[i]) - static_cast<int32_t>(s1[i]);
29 }
30 }
31 return 0;
32}
33#endif
34
35namespace art {
36
Elliott Hughes0512f022012-03-15 22:10:52 -070037static jint String_compareTo(JNIEnv* env, jobject javaThis, jobject javaRhs) {
Elliott Hughes34e06962012-04-09 13:55:55 -070038 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
Elliott Hughesbf86d042011-08-31 17:53:14 -070039 String* lhs = Decode<String*>(env, javaThis);
40 String* rhs = Decode<String*>(env, javaRhs);
41
42 if (rhs == NULL) {
43 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "rhs == null");
44 return -1;
45 }
46
47 // Quick test for comparison of a string with itself.
48 if (lhs == rhs) {
49 return 0;
50 }
51
52 // TODO: is this still true?
53 // The annoying part here is that 0x00e9 - 0xffff != 0x00ea,
54 // because the interpreter converts the characters to 32-bit integers
55 // *without* sign extension before it subtracts them (which makes some
56 // sense since "char" is unsigned). So what we get is the result of
57 // 0x000000e9 - 0x0000ffff, which is 0xffff00ea.
58 int lhsCount = lhs->GetLength();
59 int rhsCount = rhs->GetLength();
60 int countDiff = lhsCount - rhsCount;
61 int minCount = (countDiff < 0) ? lhsCount : rhsCount;
62 const uint16_t* lhsChars = lhs->GetCharArray()->GetData() + lhs->GetOffset();
63 const uint16_t* rhsChars = rhs->GetCharArray()->GetData() + rhs->GetOffset();
64 int otherRes = MemCmp16(lhsChars, rhsChars, minCount);
65 if (otherRes != 0) {
66 return otherRes;
67 }
68 return countDiff;
69}
70
Elliott Hughes529bfef2012-04-06 17:23:54 -070071static jint String_fastIndexOf(JNIEnv* env, jobject java_this, jint ch, jint start) {
72 // This method does not handle supplementary characters. They're dealt with in managed code.
73 DCHECK_LE(ch, 0xffff);
Elliott Hughesbf86d042011-08-31 17:53:14 -070074
Elliott Hughes529bfef2012-04-06 17:23:54 -070075 String* s = Decode<String*>(env, java_this);
76
77 jint count = s->GetLength();
Elliott Hughesbf86d042011-08-31 17:53:14 -070078 if (start < 0) {
79 start = 0;
Elliott Hughes529bfef2012-04-06 17:23:54 -070080 } else if (start > count) {
81 start = count;
Elliott Hughesbf86d042011-08-31 17:53:14 -070082 }
83
Elliott Hughes529bfef2012-04-06 17:23:54 -070084 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
85 const uint16_t* p = chars + start;
86 const uint16_t* end = chars + count;
87 while (p < end) {
88 if (*p++ == ch) {
89 return (p - 1) - chars;
Elliott Hughesbf86d042011-08-31 17:53:14 -070090 }
91 }
92
93 return -1;
94}
95
Elliott Hughes0512f022012-03-15 22:10:52 -070096static jstring String_intern(JNIEnv* env, jobject javaThis) {
Brian Carlstromc74255f2011-09-11 22:47:39 -070097 String* s = Decode<String*>(env, javaThis);
98 String* result = s->Intern();
Elliott Hughesbf86d042011-08-31 17:53:14 -070099 return AddLocalReference<jstring>(env, result);
100}
101
Elliott Hughes0512f022012-03-15 22:10:52 -0700102static JNINativeMethod gMethods[] = {
Elliott Hughesbf86d042011-08-31 17:53:14 -0700103 NATIVE_METHOD(String, compareTo, "(Ljava/lang/String;)I"),
Elliott Hughesbf86d042011-08-31 17:53:14 -0700104 NATIVE_METHOD(String, fastIndexOf, "(II)I"),
105 NATIVE_METHOD(String, intern, "()Ljava/lang/String;"),
106};
107
Elliott Hughesbf86d042011-08-31 17:53:14 -0700108void register_java_lang_String(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700109 REGISTER_NATIVE_METHODS("java/lang/String");
Elliott Hughesbf86d042011-08-31 17:53:14 -0700110}
111
112} // namespace art