blob: bfdc31ac681deb1732e43413d08f9c0b7aa4e547 [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"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070019#include "scoped_thread_state_change.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070020
Elliott Hughesbf86d042011-08-31 17:53:14 -070021#ifdef HAVE__MEMCMP16
22// "count" is in 16-bit units.
23extern "C" uint32_t __memcmp16(const uint16_t* s0, const uint16_t* s1, size_t count);
24#define MemCmp16 __memcmp16
25#else
26uint32_t MemCmp16(const uint16_t* s0, const uint16_t* s1, size_t count) {
27 for (size_t i = 0; i < count; i++) {
28 if (s0[i] != s1[i]) {
29 return static_cast<int32_t>(s0[i]) - static_cast<int32_t>(s1[i]);
30 }
31 }
32 return 0;
33}
34#endif
35
36namespace art {
37
Elliott Hughes0512f022012-03-15 22:10:52 -070038static jint String_compareTo(JNIEnv* env, jobject javaThis, jobject javaRhs) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070039 ScopedObjectAccess soa(env);
40 String* lhs = soa.Decode<String*>(javaThis);
41 String* rhs = soa.Decode<String*>(javaRhs);
Elliott Hughesbf86d042011-08-31 17:53:14 -070042
43 if (rhs == NULL) {
44 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "rhs == null");
45 return -1;
46 }
47
48 // Quick test for comparison of a string with itself.
49 if (lhs == rhs) {
50 return 0;
51 }
52
53 // TODO: is this still true?
54 // The annoying part here is that 0x00e9 - 0xffff != 0x00ea,
55 // because the interpreter converts the characters to 32-bit integers
56 // *without* sign extension before it subtracts them (which makes some
57 // sense since "char" is unsigned). So what we get is the result of
58 // 0x000000e9 - 0x0000ffff, which is 0xffff00ea.
59 int lhsCount = lhs->GetLength();
60 int rhsCount = rhs->GetLength();
61 int countDiff = lhsCount - rhsCount;
62 int minCount = (countDiff < 0) ? lhsCount : rhsCount;
63 const uint16_t* lhsChars = lhs->GetCharArray()->GetData() + lhs->GetOffset();
64 const uint16_t* rhsChars = rhs->GetCharArray()->GetData() + rhs->GetOffset();
65 int otherRes = MemCmp16(lhsChars, rhsChars, minCount);
66 if (otherRes != 0) {
67 return otherRes;
68 }
69 return countDiff;
70}
71
Elliott Hughes529bfef2012-04-06 17:23:54 -070072static jint String_fastIndexOf(JNIEnv* env, jobject java_this, jint ch, jint start) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070073 ScopedObjectAccess soa(env);
Elliott Hughes529bfef2012-04-06 17:23:54 -070074 // 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
Ian Rogers00f7d0e2012-07-19 15:28:27 -070077 String* s = soa.Decode<String*>(java_this);
Elliott Hughes529bfef2012-04-06 17:23:54 -070078
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) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070099 ScopedObjectAccess soa(env);
100 String* s = soa.Decode<String*>(javaThis);
Brian Carlstromc74255f2011-09-11 22:47:39 -0700101 String* result = s->Intern();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700102 return soa.AddLocalReference<jstring>(result);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700103}
104
Elliott Hughes0512f022012-03-15 22:10:52 -0700105static JNINativeMethod gMethods[] = {
Elliott Hughesbf86d042011-08-31 17:53:14 -0700106 NATIVE_METHOD(String, compareTo, "(Ljava/lang/String;)I"),
Elliott Hughesbf86d042011-08-31 17:53:14 -0700107 NATIVE_METHOD(String, fastIndexOf, "(II)I"),
108 NATIVE_METHOD(String, intern, "()Ljava/lang/String;"),
109};
110
Elliott Hughesbf86d042011-08-31 17:53:14 -0700111void register_java_lang_String(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700112 REGISTER_NATIVE_METHODS("java/lang/String");
Elliott Hughesbf86d042011-08-31 17:53:14 -0700113}
114
115} // namespace art