blob: f95664b9c3f495c0644ca74f2b44348aa7507b96 [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
Ian Rogers62d6c772013-02-27 08:32:07 -080017#include "common_throws.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070018#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019#include "mirror/string.h"
Ian Rogers1eb512d2013-10-18 15:42:20 -070020#include "scoped_fast_native_object_access.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070021#include "scoped_thread_state_change.h"
Ian Rogers64b6d142012-10-29 16:34:15 -070022#include "ScopedLocalRef.h"
Mathieu Chartier4e305412014-02-19 10:54:44 -080023#include "verify_object-inl.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070024
25namespace art {
26
Elliott Hughes0512f022012-03-15 22:10:52 -070027static jint String_compareTo(JNIEnv* env, jobject javaThis, jobject javaRhs) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070028 ScopedFastNativeObjectAccess soa(env);
Ian Rogers64b6d142012-10-29 16:34:15 -070029 if (UNLIKELY(javaRhs == NULL)) {
Ian Rogers62d6c772013-02-27 08:32:07 -080030 ThrowNullPointerException(NULL, "rhs == null");
Elliott Hughesbf86d042011-08-31 17:53:14 -070031 return -1;
Ian Rogers64b6d142012-10-29 16:34:15 -070032 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033 return soa.Decode<mirror::String*>(javaThis)->CompareTo(soa.Decode<mirror::String*>(javaRhs));
Elliott Hughesbf86d042011-08-31 17:53:14 -070034 }
Elliott Hughesbf86d042011-08-31 17:53:14 -070035}
36
Elliott Hughes529bfef2012-04-06 17:23:54 -070037static jint String_fastIndexOf(JNIEnv* env, jobject java_this, jint ch, jint start) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070038 ScopedFastNativeObjectAccess soa(env);
Elliott Hughes529bfef2012-04-06 17:23:54 -070039 // This method does not handle supplementary characters. They're dealt with in managed code.
40 DCHECK_LE(ch, 0xffff);
Elliott Hughesbf86d042011-08-31 17:53:14 -070041
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042 mirror::String* s = soa.Decode<mirror::String*>(java_this);
Ian Rogers64b6d142012-10-29 16:34:15 -070043 return s->FastIndexOf(ch, start);
Elliott Hughesbf86d042011-08-31 17:53:14 -070044}
45
Elliott Hughes0512f022012-03-15 22:10:52 -070046static jstring String_intern(JNIEnv* env, jobject javaThis) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070047 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048 mirror::String* s = soa.Decode<mirror::String*>(javaThis);
49 mirror::String* result = s->Intern();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070050 return soa.AddLocalReference<jstring>(result);
Elliott Hughesbf86d042011-08-31 17:53:14 -070051}
52
Elliott Hughes0512f022012-03-15 22:10:52 -070053static JNINativeMethod gMethods[] = {
Ian Rogers1eb512d2013-10-18 15:42:20 -070054 NATIVE_METHOD(String, compareTo, "!(Ljava/lang/String;)I"),
55 NATIVE_METHOD(String, fastIndexOf, "!(II)I"),
56 NATIVE_METHOD(String, intern, "!()Ljava/lang/String;"),
Elliott Hughesbf86d042011-08-31 17:53:14 -070057};
58
Elliott Hughesbf86d042011-08-31 17:53:14 -070059void register_java_lang_String(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -070060 REGISTER_NATIVE_METHODS("java/lang/String");
Elliott Hughesbf86d042011-08-31 17:53:14 -070061}
62
63} // namespace art