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 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 17 | #include "java_lang_String.h" |
| 18 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 19 | #include "common_throws.h" |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 20 | #include "jni_internal.h" |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 21 | #include "mirror/array.h" |
| 22 | #include "mirror/object-inl.h" |
| 23 | #include "mirror/string.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 24 | #include "mirror/string-inl.h" |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 25 | #include "scoped_fast_native_object_access.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 26 | #include "scoped_thread_state_change.h" |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 27 | #include "ScopedLocalRef.h" |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 28 | #include "verify_object-inl.h" |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 32 | static jchar String_charAt(JNIEnv* env, jobject java_this, jint index) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 33 | ScopedFastNativeObjectAccess soa(env); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 34 | return soa.Decode<mirror::String*>(java_this)->CharAt(index); |
| 35 | } |
| 36 | |
| 37 | static jint String_compareTo(JNIEnv* env, jobject java_this, jobject java_rhs) { |
| 38 | ScopedFastNativeObjectAccess soa(env); |
| 39 | if (UNLIKELY(java_rhs == nullptr)) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 40 | ThrowNullPointerException("rhs == null"); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 41 | return -1; |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 42 | } else { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 43 | return soa.Decode<mirror::String*>(java_this)->CompareTo(soa.Decode<mirror::String*>(java_rhs)); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 44 | } |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 47 | static jstring String_concat(JNIEnv* env, jobject java_this, jobject java_string_arg) { |
| 48 | ScopedFastNativeObjectAccess soa(env); |
| 49 | if (UNLIKELY(java_string_arg == nullptr)) { |
| 50 | ThrowNullPointerException("string arg == null"); |
| 51 | return nullptr; |
| 52 | } |
| 53 | StackHandleScope<2> hs(soa.Self()); |
| 54 | Handle<mirror::String> string_this(hs.NewHandle(soa.Decode<mirror::String*>(java_this))); |
| 55 | Handle<mirror::String> string_arg(hs.NewHandle(soa.Decode<mirror::String*>(java_string_arg))); |
| 56 | int32_t length_this = string_this->GetLength(); |
| 57 | int32_t length_arg = string_arg->GetLength(); |
| 58 | if (length_arg > 0 && length_this > 0) { |
| 59 | mirror::String* result = mirror::String::AllocFromStrings(soa.Self(), string_this, string_arg); |
| 60 | return soa.AddLocalReference<jstring>(result); |
| 61 | } |
| 62 | jobject string_original = (length_this == 0) ? java_string_arg : java_this; |
| 63 | return reinterpret_cast<jstring>(string_original); |
| 64 | } |
| 65 | |
Elliott Hughes | 529bfef | 2012-04-06 17:23:54 -0700 | [diff] [blame] | 66 | static jint String_fastIndexOf(JNIEnv* env, jobject java_this, jint ch, jint start) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 67 | ScopedFastNativeObjectAccess soa(env); |
Elliott Hughes | 529bfef | 2012-04-06 17:23:54 -0700 | [diff] [blame] | 68 | // This method does not handle supplementary characters. They're dealt with in managed code. |
| 69 | DCHECK_LE(ch, 0xffff); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 70 | return soa.Decode<mirror::String*>(java_this)->FastIndexOf(ch, start); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 73 | static jstring String_fastSubstring(JNIEnv* env, jobject java_this, jint start, jint length) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 74 | ScopedFastNativeObjectAccess soa(env); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 75 | StackHandleScope<1> hs(soa.Self()); |
| 76 | Handle<mirror::String> string_this(hs.NewHandle(soa.Decode<mirror::String*>(java_this))); |
| 77 | gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator(); |
| 78 | mirror::String* result = mirror::String::AllocFromString<true>(soa.Self(), length, string_this, |
| 79 | start, allocator_type); |
| 80 | return soa.AddLocalReference<jstring>(result); |
| 81 | } |
| 82 | |
| 83 | static void String_getCharsNoCheck(JNIEnv* env, jobject java_this, jint start, jint end, |
| 84 | jcharArray buffer, jint index) { |
| 85 | ScopedFastNativeObjectAccess soa(env); |
| 86 | StackHandleScope<1> hs(soa.Self()); |
| 87 | Handle<mirror::CharArray> char_array(hs.NewHandle(soa.Decode<mirror::CharArray*>(buffer))); |
| 88 | soa.Decode<mirror::String*>(java_this)->GetChars(start, end, char_array, index); |
| 89 | } |
| 90 | |
| 91 | static jstring String_intern(JNIEnv* env, jobject java_this) { |
| 92 | ScopedFastNativeObjectAccess soa(env); |
| 93 | mirror::String* s = soa.Decode<mirror::String*>(java_this); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 94 | mirror::String* result = s->Intern(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 95 | return soa.AddLocalReference<jstring>(result); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 98 | static void String_setCharAt(JNIEnv* env, jobject java_this, jint index, jchar c) { |
| 99 | ScopedFastNativeObjectAccess soa(env); |
| 100 | soa.Decode<mirror::String*>(java_this)->SetCharAt(index, c); |
| 101 | } |
| 102 | |
| 103 | static jcharArray String_toCharArray(JNIEnv* env, jobject java_this) { |
| 104 | ScopedFastNativeObjectAccess soa(env); |
| 105 | mirror::String* s = soa.Decode<mirror::String*>(java_this); |
| 106 | return soa.AddLocalReference<jcharArray>(s->ToCharArray(soa.Self())); |
| 107 | } |
| 108 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 109 | static JNINativeMethod gMethods[] = { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 110 | NATIVE_METHOD(String, charAt, "!(I)C"), |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 111 | NATIVE_METHOD(String, compareTo, "!(Ljava/lang/String;)I"), |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 112 | NATIVE_METHOD(String, concat, "!(Ljava/lang/String;)Ljava/lang/String;"), |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 113 | NATIVE_METHOD(String, fastIndexOf, "!(II)I"), |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 114 | NATIVE_METHOD(String, fastSubstring, "!(II)Ljava/lang/String;"), |
| 115 | NATIVE_METHOD(String, getCharsNoCheck, "!(II[CI)V"), |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 116 | NATIVE_METHOD(String, intern, "!()Ljava/lang/String;"), |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 117 | NATIVE_METHOD(String, setCharAt, "!(IC)V"), |
| 118 | NATIVE_METHOD(String, toCharArray, "!()[C"), |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 121 | void register_java_lang_String(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 122 | REGISTER_NATIVE_METHODS("java/lang/String"); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | } // namespace art |