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 | |
| 17 | #include "jni_internal.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 18 | #include "mirror/string.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 19 | #include "scoped_thread_state_change.h" |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 20 | #include "ScopedLocalRef.h" |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 21 | |
| 22 | namespace art { |
| 23 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 24 | static jint String_compareTo(JNIEnv* env, jobject javaThis, jobject javaRhs) { |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 25 | if (UNLIKELY(javaRhs == NULL)) { |
| 26 | ScopedLocalRef<jclass> npe(env, env->FindClass("java/lang/NullPointerException")); |
| 27 | env->ThrowNew(npe.get(), "rhs == null"); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 28 | return -1; |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 29 | } else { |
| 30 | ScopedObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 31 | return soa.Decode<mirror::String*>(javaThis)->CompareTo(soa.Decode<mirror::String*>(javaRhs)); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 32 | } |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Elliott Hughes | 529bfef | 2012-04-06 17:23:54 -0700 | [diff] [blame] | 35 | static jint String_fastIndexOf(JNIEnv* env, jobject java_this, jint ch, jint start) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 36 | ScopedObjectAccess soa(env); |
Elliott Hughes | 529bfef | 2012-04-06 17:23:54 -0700 | [diff] [blame] | 37 | // This method does not handle supplementary characters. They're dealt with in managed code. |
| 38 | DCHECK_LE(ch, 0xffff); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 39 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 40 | mirror::String* s = soa.Decode<mirror::String*>(java_this); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 41 | return s->FastIndexOf(ch, start); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 44 | static jstring String_intern(JNIEnv* env, jobject javaThis) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 45 | ScopedObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 46 | mirror::String* s = soa.Decode<mirror::String*>(javaThis); |
| 47 | mirror::String* result = s->Intern(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 48 | return soa.AddLocalReference<jstring>(result); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 51 | static JNINativeMethod gMethods[] = { |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 52 | NATIVE_METHOD(String, compareTo, "(Ljava/lang/String;)I"), |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 53 | NATIVE_METHOD(String, fastIndexOf, "(II)I"), |
| 54 | NATIVE_METHOD(String, intern, "()Ljava/lang/String;"), |
| 55 | }; |
| 56 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 57 | void register_java_lang_String(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 58 | REGISTER_NATIVE_METHODS("java/lang/String"); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | } // namespace art |