blob: b3a967d1679b62487581e8c4120ef021cea1904a [file] [log] [blame]
Elliott Hughesbf86d042011-08-31 17:53:14 -07001/*
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 Gampe277ccbd2014-11-03 21:36:10 -080017#include "java_lang_String.h"
18
Ian Rogers62d6c772013-02-27 08:32:07 -080019#include "common_throws.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070020#include "jni_internal.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080021#include "mirror/array.h"
22#include "mirror/object-inl.h"
23#include "mirror/string.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070024#include "mirror/string-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070025#include "scoped_fast_native_object_access-inl.h"
26#include "scoped_thread_state_change-inl.h"
Ian Rogers64b6d142012-10-29 16:34:15 -070027#include "ScopedLocalRef.h"
Mathieu Chartier4e305412014-02-19 10:54:44 -080028#include "verify_object-inl.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070029
30namespace art {
31
Jeff Hao848f70a2014-01-15 13:49:50 -080032static jchar String_charAt(JNIEnv* env, jobject java_this, jint index) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070033 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -070034 return soa.Decode<mirror::String>(java_this)->CharAt(index);
Jeff Hao848f70a2014-01-15 13:49:50 -080035}
36
37static jint String_compareTo(JNIEnv* env, jobject java_this, jobject java_rhs) {
38 ScopedFastNativeObjectAccess soa(env);
39 if (UNLIKELY(java_rhs == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000040 ThrowNullPointerException("rhs == null");
Elliott Hughesbf86d042011-08-31 17:53:14 -070041 return -1;
Ian Rogers64b6d142012-10-29 16:34:15 -070042 } else {
Mathieu Chartier0795f232016-09-27 18:43:30 -070043 return soa.Decode<mirror::String>(java_this)->CompareTo(
44 soa.Decode<mirror::String>(java_rhs).Decode());
Elliott Hughesbf86d042011-08-31 17:53:14 -070045 }
Elliott Hughesbf86d042011-08-31 17:53:14 -070046}
47
Jeff Hao848f70a2014-01-15 13:49:50 -080048static jstring String_concat(JNIEnv* env, jobject java_this, jobject java_string_arg) {
49 ScopedFastNativeObjectAccess soa(env);
50 if (UNLIKELY(java_string_arg == nullptr)) {
51 ThrowNullPointerException("string arg == null");
52 return nullptr;
53 }
54 StackHandleScope<2> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -070055 Handle<mirror::String> string_this(hs.NewHandle(soa.Decode<mirror::String>(java_this)));
56 Handle<mirror::String> string_arg(hs.NewHandle(soa.Decode<mirror::String>(java_string_arg)));
Jeff Hao848f70a2014-01-15 13:49:50 -080057 int32_t length_this = string_this->GetLength();
58 int32_t length_arg = string_arg->GetLength();
59 if (length_arg > 0 && length_this > 0) {
60 mirror::String* result = mirror::String::AllocFromStrings(soa.Self(), string_this, string_arg);
61 return soa.AddLocalReference<jstring>(result);
62 }
63 jobject string_original = (length_this == 0) ? java_string_arg : java_this;
64 return reinterpret_cast<jstring>(string_original);
65}
66
Elliott Hughes529bfef2012-04-06 17:23:54 -070067static jint String_fastIndexOf(JNIEnv* env, jobject java_this, jint ch, jint start) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070068 ScopedFastNativeObjectAccess soa(env);
Elliott Hughes529bfef2012-04-06 17:23:54 -070069 // This method does not handle supplementary characters. They're dealt with in managed code.
70 DCHECK_LE(ch, 0xffff);
Mathieu Chartier0795f232016-09-27 18:43:30 -070071 return soa.Decode<mirror::String>(java_this)->FastIndexOf(ch, start);
Elliott Hughesbf86d042011-08-31 17:53:14 -070072}
73
Jeff Hao848f70a2014-01-15 13:49:50 -080074static jstring String_fastSubstring(JNIEnv* env, jobject java_this, jint start, jint length) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070075 ScopedFastNativeObjectAccess soa(env);
Jeff Hao848f70a2014-01-15 13:49:50 -080076 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -070077 Handle<mirror::String> string_this(hs.NewHandle(soa.Decode<mirror::String>(java_this)));
Jeff Hao848f70a2014-01-15 13:49:50 -080078 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
79 mirror::String* result = mirror::String::AllocFromString<true>(soa.Self(), length, string_this,
80 start, allocator_type);
81 return soa.AddLocalReference<jstring>(result);
82}
83
84static void String_getCharsNoCheck(JNIEnv* env, jobject java_this, jint start, jint end,
85 jcharArray buffer, jint index) {
86 ScopedFastNativeObjectAccess soa(env);
87 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -070088 Handle<mirror::CharArray> char_array(hs.NewHandle(soa.Decode<mirror::CharArray>(buffer)));
89 soa.Decode<mirror::String>(java_this)->GetChars(start, end, char_array, index);
Jeff Hao848f70a2014-01-15 13:49:50 -080090}
91
92static jstring String_intern(JNIEnv* env, jobject java_this) {
93 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -070094 ObjPtr<mirror::String> result = soa.Decode<mirror::String>(java_this)->Intern();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070095 return soa.AddLocalReference<jstring>(result);
Elliott Hughesbf86d042011-08-31 17:53:14 -070096}
97
Jeff Hao848f70a2014-01-15 13:49:50 -080098static void String_setCharAt(JNIEnv* env, jobject java_this, jint index, jchar c) {
99 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700100 soa.Decode<mirror::String>(java_this)->SetCharAt(index, c);
Jeff Hao848f70a2014-01-15 13:49:50 -0800101}
102
103static jcharArray String_toCharArray(JNIEnv* env, jobject java_this) {
104 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700105 ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_this);
Jeff Hao848f70a2014-01-15 13:49:50 -0800106 return soa.AddLocalReference<jcharArray>(s->ToCharArray(soa.Self()));
107}
108
Elliott Hughes0512f022012-03-15 22:10:52 -0700109static JNINativeMethod gMethods[] = {
Jeff Hao848f70a2014-01-15 13:49:50 -0800110 NATIVE_METHOD(String, charAt, "!(I)C"),
Ian Rogers1eb512d2013-10-18 15:42:20 -0700111 NATIVE_METHOD(String, compareTo, "!(Ljava/lang/String;)I"),
Jeff Hao848f70a2014-01-15 13:49:50 -0800112 NATIVE_METHOD(String, concat, "!(Ljava/lang/String;)Ljava/lang/String;"),
Ian Rogers1eb512d2013-10-18 15:42:20 -0700113 NATIVE_METHOD(String, fastIndexOf, "!(II)I"),
Jeff Hao848f70a2014-01-15 13:49:50 -0800114 NATIVE_METHOD(String, fastSubstring, "!(II)Ljava/lang/String;"),
115 NATIVE_METHOD(String, getCharsNoCheck, "!(II[CI)V"),
Ian Rogers1eb512d2013-10-18 15:42:20 -0700116 NATIVE_METHOD(String, intern, "!()Ljava/lang/String;"),
Jeff Hao848f70a2014-01-15 13:49:50 -0800117 NATIVE_METHOD(String, setCharAt, "!(IC)V"),
118 NATIVE_METHOD(String, toCharArray, "!()[C"),
Elliott Hughesbf86d042011-08-31 17:53:14 -0700119};
120
Elliott Hughesbf86d042011-08-31 17:53:14 -0700121void register_java_lang_String(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700122 REGISTER_NATIVE_METHODS("java/lang/String");
Elliott Hughesbf86d042011-08-31 17:53:14 -0700123}
124
125} // namespace art