java_lang_String.cc revision 00f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abac
1bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes/* 2bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * Copyright (C) 2008 The Android Open Source Project 3bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * 4bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 5bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * you may not use this file except in compliance with the License. 6bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * You may obtain a copy of the License at 7bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * 8bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 9bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * 10bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * Unless required by applicable law or agreed to in writing, software 11bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 12bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * See the License for the specific language governing permissions and 14bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * limitations under the License. 15bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes */ 16bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes 17bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes#include "jni_internal.h" 18bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes#include "object.h" 1900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h" 20bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes 21bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes#ifdef HAVE__MEMCMP16 22bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes// "count" is in 16-bit units. 23bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughesextern "C" uint32_t __memcmp16(const uint16_t* s0, const uint16_t* s1, size_t count); 24bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes#define MemCmp16 __memcmp16 25bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes#else 26bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughesuint32_t MemCmp16(const uint16_t* s0, const uint16_t* s1, size_t count) { 27bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes for (size_t i = 0; i < count; i++) { 28bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes if (s0[i] != s1[i]) { 29bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes return static_cast<int32_t>(s0[i]) - static_cast<int32_t>(s1[i]); 30bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes } 31bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes } 32bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes return 0; 33bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes} 34bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes#endif 35bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes 36bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughesnamespace art { 37bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes 380512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jint String_compareTo(JNIEnv* env, jobject javaThis, jobject javaRhs) { 3900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(env); 4000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers String* lhs = soa.Decode<String*>(javaThis); 4100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers String* rhs = soa.Decode<String*>(javaRhs); 42bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes 43bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes if (rhs == NULL) { 44bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "rhs == null"); 45bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes return -1; 46bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes } 47bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes 48bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes // Quick test for comparison of a string with itself. 49bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes if (lhs == rhs) { 50bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes return 0; 51bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes } 52bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes 53bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes // TODO: is this still true? 54bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes // The annoying part here is that 0x00e9 - 0xffff != 0x00ea, 55bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes // because the interpreter converts the characters to 32-bit integers 56bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes // *without* sign extension before it subtracts them (which makes some 57bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes // sense since "char" is unsigned). So what we get is the result of 58bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes // 0x000000e9 - 0x0000ffff, which is 0xffff00ea. 59bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes int lhsCount = lhs->GetLength(); 60bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes int rhsCount = rhs->GetLength(); 61bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes int countDiff = lhsCount - rhsCount; 62bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes int minCount = (countDiff < 0) ? lhsCount : rhsCount; 63bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes const uint16_t* lhsChars = lhs->GetCharArray()->GetData() + lhs->GetOffset(); 64bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes const uint16_t* rhsChars = rhs->GetCharArray()->GetData() + rhs->GetOffset(); 65bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes int otherRes = MemCmp16(lhsChars, rhsChars, minCount); 66bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes if (otherRes != 0) { 67bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes return otherRes; 68bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes } 69bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes return countDiff; 70bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes} 71bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes 72529bfef6b3152fcf244e806ea6822ee96ff3eb4fElliott Hughesstatic jint String_fastIndexOf(JNIEnv* env, jobject java_this, jint ch, jint start) { 7300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(env); 74529bfef6b3152fcf244e806ea6822ee96ff3eb4fElliott Hughes // This method does not handle supplementary characters. They're dealt with in managed code. 75529bfef6b3152fcf244e806ea6822ee96ff3eb4fElliott Hughes DCHECK_LE(ch, 0xffff); 76bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes 7700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers String* s = soa.Decode<String*>(java_this); 78529bfef6b3152fcf244e806ea6822ee96ff3eb4fElliott Hughes 79529bfef6b3152fcf244e806ea6822ee96ff3eb4fElliott Hughes jint count = s->GetLength(); 80bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes if (start < 0) { 81bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes start = 0; 82529bfef6b3152fcf244e806ea6822ee96ff3eb4fElliott Hughes } else if (start > count) { 83529bfef6b3152fcf244e806ea6822ee96ff3eb4fElliott Hughes start = count; 84bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes } 85bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes 86529bfef6b3152fcf244e806ea6822ee96ff3eb4fElliott Hughes const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset(); 87529bfef6b3152fcf244e806ea6822ee96ff3eb4fElliott Hughes const uint16_t* p = chars + start; 88529bfef6b3152fcf244e806ea6822ee96ff3eb4fElliott Hughes const uint16_t* end = chars + count; 89529bfef6b3152fcf244e806ea6822ee96ff3eb4fElliott Hughes while (p < end) { 90529bfef6b3152fcf244e806ea6822ee96ff3eb4fElliott Hughes if (*p++ == ch) { 91529bfef6b3152fcf244e806ea6822ee96ff3eb4fElliott Hughes return (p - 1) - chars; 92bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes } 93bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes } 94bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes 95bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes return -1; 96bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes} 97bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes 980512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jstring String_intern(JNIEnv* env, jobject javaThis) { 9900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(env); 10000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers String* s = soa.Decode<String*>(javaThis); 101c74255fffb035001304c9a058a2e730a5a1a9604Brian Carlstrom String* result = s->Intern(); 10200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return soa.AddLocalReference<jstring>(result); 103bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes} 104bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes 1050512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic JNINativeMethod gMethods[] = { 106bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes NATIVE_METHOD(String, compareTo, "(Ljava/lang/String;)I"), 107bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes NATIVE_METHOD(String, fastIndexOf, "(II)I"), 108bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes NATIVE_METHOD(String, intern, "()Ljava/lang/String;"), 109bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes}; 110bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes 111bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughesvoid register_java_lang_String(JNIEnv* env) { 112eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes REGISTER_NATIVE_METHODS("java/lang/String"); 113bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes} 114bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes 115bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes} // namespace art 116