blob: 44ab1ca8be785d4aff04a83cd6dc19ba6cf1eed0 [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
17#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080018#include "mirror/string.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070019#include "scoped_thread_state_change.h"
Ian Rogers64b6d142012-10-29 16:34:15 -070020#include "ScopedLocalRef.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070021
22namespace art {
23
Elliott Hughes0512f022012-03-15 22:10:52 -070024static jint String_compareTo(JNIEnv* env, jobject javaThis, jobject javaRhs) {
Ian Rogers64b6d142012-10-29 16:34:15 -070025 if (UNLIKELY(javaRhs == NULL)) {
26 ScopedLocalRef<jclass> npe(env, env->FindClass("java/lang/NullPointerException"));
27 env->ThrowNew(npe.get(), "rhs == null");
Elliott Hughesbf86d042011-08-31 17:53:14 -070028 return -1;
Ian Rogers64b6d142012-10-29 16:34:15 -070029 } else {
30 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031 return soa.Decode<mirror::String*>(javaThis)->CompareTo(soa.Decode<mirror::String*>(javaRhs));
Elliott Hughesbf86d042011-08-31 17:53:14 -070032 }
Elliott Hughesbf86d042011-08-31 17:53:14 -070033}
34
Elliott Hughes529bfef2012-04-06 17:23:54 -070035static jint String_fastIndexOf(JNIEnv* env, jobject java_this, jint ch, jint start) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070036 ScopedObjectAccess soa(env);
Elliott Hughes529bfef2012-04-06 17:23:54 -070037 // This method does not handle supplementary characters. They're dealt with in managed code.
38 DCHECK_LE(ch, 0xffff);
Elliott Hughesbf86d042011-08-31 17:53:14 -070039
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040 mirror::String* s = soa.Decode<mirror::String*>(java_this);
Ian Rogers64b6d142012-10-29 16:34:15 -070041 return s->FastIndexOf(ch, start);
Elliott Hughesbf86d042011-08-31 17:53:14 -070042}
43
Elliott Hughes0512f022012-03-15 22:10:52 -070044static jstring String_intern(JNIEnv* env, jobject javaThis) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070045 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046 mirror::String* s = soa.Decode<mirror::String*>(javaThis);
47 mirror::String* result = s->Intern();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070048 return soa.AddLocalReference<jstring>(result);
Elliott Hughesbf86d042011-08-31 17:53:14 -070049}
50
Elliott Hughes0512f022012-03-15 22:10:52 -070051static JNINativeMethod gMethods[] = {
Elliott Hughesbf86d042011-08-31 17:53:14 -070052 NATIVE_METHOD(String, compareTo, "(Ljava/lang/String;)I"),
Elliott Hughesbf86d042011-08-31 17:53:14 -070053 NATIVE_METHOD(String, fastIndexOf, "(II)I"),
54 NATIVE_METHOD(String, intern, "()Ljava/lang/String;"),
55};
56
Elliott Hughesbf86d042011-08-31 17:53:14 -070057void register_java_lang_String(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -070058 REGISTER_NATIVE_METHODS("java/lang/String");
Elliott Hughesbf86d042011-08-31 17:53:14 -070059}
60
61} // namespace art