Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 20 | #ifdef HAVE__MEMCMP16 |
| 21 | // "count" is in 16-bit units. |
| 22 | extern "C" uint32_t __memcmp16(const uint16_t* s0, const uint16_t* s1, size_t count); |
| 23 | #define MemCmp16 __memcmp16 |
| 24 | #else |
| 25 | uint32_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 | |
| 35 | namespace art { |
| 36 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 37 | static jint String_compareTo(JNIEnv* env, jobject javaThis, jobject javaRhs) { |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 38 | ScopedThreadStateChange tsc(Thread::Current(), kRunnable); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 39 | 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 Hughes | 529bfef | 2012-04-06 17:23:54 -0700 | [diff] [blame] | 71 | static 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 Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 74 | |
Elliott Hughes | 529bfef | 2012-04-06 17:23:54 -0700 | [diff] [blame] | 75 | String* s = Decode<String*>(env, java_this); |
| 76 | |
| 77 | jint count = s->GetLength(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 78 | if (start < 0) { |
| 79 | start = 0; |
Elliott Hughes | 529bfef | 2012-04-06 17:23:54 -0700 | [diff] [blame] | 80 | } else if (start > count) { |
| 81 | start = count; |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Elliott Hughes | 529bfef | 2012-04-06 17:23:54 -0700 | [diff] [blame] | 84 | 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 Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | return -1; |
| 94 | } |
| 95 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 96 | static jstring String_intern(JNIEnv* env, jobject javaThis) { |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 97 | String* s = Decode<String*>(env, javaThis); |
| 98 | String* result = s->Intern(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 99 | return AddLocalReference<jstring>(env, result); |
| 100 | } |
| 101 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 102 | static JNINativeMethod gMethods[] = { |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 103 | NATIVE_METHOD(String, compareTo, "(Ljava/lang/String;)I"), |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 104 | NATIVE_METHOD(String, fastIndexOf, "(II)I"), |
| 105 | NATIVE_METHOD(String, intern, "()Ljava/lang/String;"), |
| 106 | }; |
| 107 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 108 | void register_java_lang_String(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame^] | 109 | REGISTER_NATIVE_METHODS("java/lang/String"); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | } // namespace art |