| 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 | |
| Andreas Gampe | a14100c | 2017-04-24 15:09:56 -0700 | [diff] [blame] | 19 | #include "nativehelper/jni_macros.h" |
| 20 | |
| Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 21 | #include "common_throws.h" |
| Andreas Gampe | e15b9b1 | 2018-10-29 12:54:27 -0700 | [diff] [blame] | 22 | #include "handle_scope-inl.h" |
| Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 23 | #include "jni/jni_internal.h" |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 24 | #include "mirror/array.h" |
| 25 | #include "mirror/object-inl.h" |
| Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 26 | #include "mirror/string-inl.h" |
| Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 27 | #include "mirror/string.h" |
| Andreas Gampe | 87583b3 | 2017-05-25 11:22:18 -0700 | [diff] [blame] | 28 | #include "native_util.h" |
| Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 29 | #include "nativehelper/scoped_local_ref.h" |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 30 | #include "scoped_fast_native_object_access-inl.h" |
| 31 | #include "scoped_thread_state_change-inl.h" |
| Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 32 | #include "verify_object.h" |
| Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 36 | static jchar String_charAt(JNIEnv* env, jobject java_this, jint index) { |
| Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 37 | ScopedFastNativeObjectAccess soa(env); |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 38 | return soa.Decode<mirror::String>(java_this)->CharAt(index); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| Igor Murashkin | 06537f7 | 2018-02-22 15:03:05 -0800 | [diff] [blame] | 41 | static jint String_compareTo(JNIEnv* env, jobject java_this, jstring java_rhs) { |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 42 | ScopedFastNativeObjectAccess soa(env); |
| 43 | if (UNLIKELY(java_rhs == nullptr)) { |
| Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 44 | ThrowNullPointerException("rhs == null"); |
| Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 45 | return -1; |
| Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 46 | } else { |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 47 | return soa.Decode<mirror::String>(java_this)->CompareTo( |
| Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 48 | soa.Decode<mirror::String>(java_rhs).Ptr()); |
| Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 49 | } |
| Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| Igor Murashkin | 06537f7 | 2018-02-22 15:03:05 -0800 | [diff] [blame] | 52 | static jstring String_concat(JNIEnv* env, jobject java_this, jstring java_string_arg) { |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 53 | ScopedFastNativeObjectAccess soa(env); |
| 54 | if (UNLIKELY(java_string_arg == nullptr)) { |
| 55 | ThrowNullPointerException("string arg == null"); |
| 56 | return nullptr; |
| 57 | } |
| 58 | StackHandleScope<2> hs(soa.Self()); |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 59 | Handle<mirror::String> string_this(hs.NewHandle(soa.Decode<mirror::String>(java_this))); |
| 60 | Handle<mirror::String> string_arg(hs.NewHandle(soa.Decode<mirror::String>(java_string_arg))); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 61 | int32_t length_this = string_this->GetLength(); |
| 62 | int32_t length_arg = string_arg->GetLength(); |
| 63 | if (length_arg > 0 && length_this > 0) { |
| Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 64 | ObjPtr<mirror::String> result = |
| 65 | mirror::String::AllocFromStrings(soa.Self(), string_this, string_arg); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 66 | return soa.AddLocalReference<jstring>(result); |
| 67 | } |
| 68 | jobject string_original = (length_this == 0) ? java_string_arg : java_this; |
| 69 | return reinterpret_cast<jstring>(string_original); |
| 70 | } |
| 71 | |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 72 | 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] | 73 | ScopedFastNativeObjectAccess soa(env); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 74 | StackHandleScope<1> hs(soa.Self()); |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 75 | Handle<mirror::String> string_this(hs.NewHandle(soa.Decode<mirror::String>(java_this))); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 76 | gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator(); |
| Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 77 | ObjPtr<mirror::String> result = mirror::String::AllocFromString<true>(soa.Self(), |
| 78 | length, |
| 79 | string_this, |
| 80 | start, |
| 81 | allocator_type); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 82 | return soa.AddLocalReference<jstring>(result); |
| 83 | } |
| 84 | |
| 85 | static void String_getCharsNoCheck(JNIEnv* env, jobject java_this, jint start, jint end, |
| 86 | jcharArray buffer, jint index) { |
| 87 | ScopedFastNativeObjectAccess soa(env); |
| 88 | StackHandleScope<1> hs(soa.Self()); |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 89 | Handle<mirror::CharArray> char_array(hs.NewHandle(soa.Decode<mirror::CharArray>(buffer))); |
| 90 | soa.Decode<mirror::String>(java_this)->GetChars(start, end, char_array, index); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static jstring String_intern(JNIEnv* env, jobject java_this) { |
| 94 | ScopedFastNativeObjectAccess soa(env); |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 95 | ObjPtr<mirror::String> result = soa.Decode<mirror::String>(java_this)->Intern(); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 96 | return soa.AddLocalReference<jstring>(result); |
| Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| Vladimir Marko | 92907f3 | 2017-02-20 14:08:30 +0000 | [diff] [blame] | 99 | static jstring String_doReplace(JNIEnv* env, jobject java_this, jchar old_c, jchar new_c) { |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 100 | ScopedFastNativeObjectAccess soa(env); |
| Vladimir Marko | 9e57aba | 2017-03-16 10:45:40 +0000 | [diff] [blame] | 101 | StackHandleScope<1> hs(soa.Self()); |
| 102 | Handle<mirror::String> string = hs.NewHandle(soa.Decode<mirror::String>(java_this)); |
| 103 | ObjPtr<mirror::String> result = mirror::String::DoReplace(soa.Self(), string, old_c, new_c); |
| Vladimir Marko | 92907f3 | 2017-02-20 14:08:30 +0000 | [diff] [blame] | 104 | return soa.AddLocalReference<jstring>(result); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static jcharArray String_toCharArray(JNIEnv* env, jobject java_this) { |
| 108 | ScopedFastNativeObjectAccess soa(env); |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 109 | ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_this); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 110 | return soa.AddLocalReference<jcharArray>(s->ToCharArray(soa.Self())); |
| 111 | } |
| 112 | |
| Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 113 | static JNINativeMethod gMethods[] = { |
| Igor Murashkin | 3b6f440 | 2017-02-16 16:13:17 -0800 | [diff] [blame] | 114 | FAST_NATIVE_METHOD(String, charAt, "(I)C"), |
| 115 | FAST_NATIVE_METHOD(String, compareTo, "(Ljava/lang/String;)I"), |
| 116 | FAST_NATIVE_METHOD(String, concat, "(Ljava/lang/String;)Ljava/lang/String;"), |
| 117 | FAST_NATIVE_METHOD(String, doReplace, "(CC)Ljava/lang/String;"), |
| Igor Murashkin | 3b6f440 | 2017-02-16 16:13:17 -0800 | [diff] [blame] | 118 | FAST_NATIVE_METHOD(String, fastSubstring, "(II)Ljava/lang/String;"), |
| 119 | FAST_NATIVE_METHOD(String, getCharsNoCheck, "(II[CI)V"), |
| 120 | FAST_NATIVE_METHOD(String, intern, "()Ljava/lang/String;"), |
| 121 | FAST_NATIVE_METHOD(String, toCharArray, "()[C"), |
| Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
| Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 124 | void register_java_lang_String(JNIEnv* env) { |
| Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 125 | REGISTER_NATIVE_METHODS("java/lang/String"); |
| Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | } // namespace art |