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