blob: 4ea2546e300b1198f3a2165011da0e80668a2d08 [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"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070021#include "mirror/string-inl.h"
Ian Rogers1eb512d2013-10-18 15:42:20 -070022#include "scoped_fast_native_object_access.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070023#include "scoped_thread_state_change.h"
Ian Rogers64b6d142012-10-29 16:34:15 -070024#include "ScopedLocalRef.h"
Mathieu Chartier4e305412014-02-19 10:54:44 -080025#include "verify_object-inl.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070026
27namespace art {
28
Elliott Hughes0512f022012-03-15 22:10:52 -070029static jint String_compareTo(JNIEnv* env, jobject javaThis, jobject javaRhs) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070030 ScopedFastNativeObjectAccess soa(env);
Ian Rogers64b6d142012-10-29 16:34:15 -070031 if (UNLIKELY(javaRhs == NULL)) {
Ian Rogers62d6c772013-02-27 08:32:07 -080032 ThrowNullPointerException(NULL, "rhs == null");
Elliott Hughesbf86d042011-08-31 17:53:14 -070033 return -1;
Ian Rogers64b6d142012-10-29 16:34:15 -070034 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035 return soa.Decode<mirror::String*>(javaThis)->CompareTo(soa.Decode<mirror::String*>(javaRhs));
Elliott Hughesbf86d042011-08-31 17:53:14 -070036 }
Elliott Hughesbf86d042011-08-31 17:53:14 -070037}
38
Elliott Hughes529bfef2012-04-06 17:23:54 -070039static jint String_fastIndexOf(JNIEnv* env, jobject java_this, jint ch, jint start) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070040 ScopedFastNativeObjectAccess soa(env);
Elliott Hughes529bfef2012-04-06 17:23:54 -070041 // This method does not handle supplementary characters. They're dealt with in managed code.
42 DCHECK_LE(ch, 0xffff);
Elliott Hughesbf86d042011-08-31 17:53:14 -070043
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044 mirror::String* s = soa.Decode<mirror::String*>(java_this);
Ian Rogers64b6d142012-10-29 16:34:15 -070045 return s->FastIndexOf(ch, start);
Elliott Hughesbf86d042011-08-31 17:53:14 -070046}
47
Elliott Hughes0512f022012-03-15 22:10:52 -070048static jstring String_intern(JNIEnv* env, jobject javaThis) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070049 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050 mirror::String* s = soa.Decode<mirror::String*>(javaThis);
51 mirror::String* result = s->Intern();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070052 return soa.AddLocalReference<jstring>(result);
Elliott Hughesbf86d042011-08-31 17:53:14 -070053}
54
Elliott Hughes0512f022012-03-15 22:10:52 -070055static JNINativeMethod gMethods[] = {
Ian Rogers1eb512d2013-10-18 15:42:20 -070056 NATIVE_METHOD(String, compareTo, "!(Ljava/lang/String;)I"),
57 NATIVE_METHOD(String, fastIndexOf, "!(II)I"),
58 NATIVE_METHOD(String, intern, "!()Ljava/lang/String;"),
Elliott Hughesbf86d042011-08-31 17:53:14 -070059};
60
Elliott Hughesbf86d042011-08-31 17:53:14 -070061void register_java_lang_String(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -070062 REGISTER_NATIVE_METHODS("java/lang/String");
Elliott Hughesbf86d042011-08-31 17:53:14 -070063}
64
65} // namespace art