1 /* src/native/vm/cldc1.1/java_lang_String.cpp
3 Copyright (C) 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32 #include "native/jni.h"
33 #include "native/llni.h"
34 #include "native/native.h"
36 #if defined(ENABLE_JNI_HEADERS)
37 # include "native/include/java_lang_String.h"
40 #include "vm/string.hpp"
42 #include "vmcore/javaobjects.hpp"
45 // Native functions are exported as C functions.
49 * Class: java/lang/String
53 JNIEXPORT jint JNICALL Java_java_lang_String_hashCode(JNIEnv *env, jstring _this)
55 java_lang_String jls(_this);
57 java_handle_chararray_t* value = jls.get_value();
58 int32_t offset = jls.get_offset();
59 int32_t count = jls.get_count();
63 for (int32_t i = 0; i < count; i++) {
64 hash = (31 * hash) + LLNI_array_direct(value, offset + i);
72 * Class: java/lang/String
76 JNIEXPORT jint JNICALL Java_java_lang_String_indexOf__I(JNIEnv *env, jstring _this, jint ch)
78 java_lang_String jls(_this);
80 java_handle_chararray_t* value = jls.get_value();
81 int32_t offset = jls.get_offset();
82 int32_t count = jls.get_count();
84 for (int32_t i = 0; i < count; i++) {
85 if (LLNI_array_direct(value, offset + i) == ch) {
95 * Class: java/lang/String
99 JNIEXPORT jint JNICALL Java_java_lang_String_indexOf__II(JNIEnv *env, jstring _this, jint ch, jint fromIndex)
101 java_lang_String jls(_this);
103 java_handle_chararray_t* value = jls.get_value();
104 int32_t offset = jls.get_offset();
105 int32_t count = jls.get_count();
110 else if (fromIndex >= count) {
111 // Note: fromIndex might be near -1>>>1.
115 for (int32_t i = fromIndex ; i < count ; i++) {
116 if (LLNI_array_direct(value, offset + i) == ch) {
126 * Class: java/lang/String
127 * Method: lastIndexOf
130 JNIEXPORT jint JNICALL Java_java_lang_String_lastIndexOf__II(JNIEnv *env, jstring _this, jint ch, jint fromIndex)
132 java_lang_String jls(_this);
134 java_handle_chararray_t* value = jls.get_value();
135 int32_t offset = jls.get_offset();
136 int32_t count = jls.get_count();
138 int32_t start = ((fromIndex >= count) ? count - 1 : fromIndex);
140 for (int32_t i = start; i >= 0; i--) {
141 if (LLNI_array_direct(value, offset + i) == ch) {
151 * Class: java/lang/String
152 * Method: lastIndexOf
155 JNIEXPORT jint JNICALL Java_java_lang_String_lastIndexOf__I(JNIEnv *env, jstring _this, jint ch)
157 java_lang_String jls(_this);
159 return Java_java_lang_String_lastIndexOf__II(env, _this, ch, jls.get_count() - 1);
164 * Class: java/lang/String
166 * Signature: ()Ljava/lang/String;
168 JNIEXPORT jstring JNICALL Java_java_lang_String_intern(JNIEnv *env, jstring _this)
170 java_lang_String jls(_this);
175 return (jstring) javastring_intern(jls.get_handle());
181 /* native methods implemented by this file ************************************/
183 static JNINativeMethod methods[] = {
184 { (char*) "hashCode", (char*) "()I", (void*) (uintptr_t) &Java_java_lang_String_hashCode },
185 { (char*) "indexOf", (char*) "(I)I", (void*) (uintptr_t) &Java_java_lang_String_indexOf__I },
186 { (char*) "indexOf", (char*) "(II)I", (void*) (uintptr_t) &Java_java_lang_String_indexOf__II },
187 { (char*) "lastIndexOf", (char*) "(II)I", (void*) (uintptr_t) &Java_java_lang_String_lastIndexOf__II },
188 { (char*) "lastIndexOf", (char*) "(I)I", (void*) (uintptr_t) &Java_java_lang_String_lastIndexOf__I },
190 { (char*) "equals", (char*) "(Ljava/lang/Object;)Z;", (void*) (uintptr_t) &Java_java_lang_String_equals },
192 { (char*) "intern", (char*) "()Ljava/lang/String;", (void*) (uintptr_t) &Java_java_lang_String_intern },
196 /* _Jv_java_lang_String_init ***************************************************
198 Register native functions.
200 *******************************************************************************/
204 void _Jv_java_lang_String_init(void)
208 u = utf_new_char("java/lang/String");
210 native_method_register(u, methods, NATIVE_METHODS_COUNT);
216 * These are local overrides for various environment variables in Emacs.
217 * Please do not remove this and leave it at the end of the file, where
218 * Emacs will automagically detect them.
219 * ---------------------------------------------------------------------
222 * indent-tabs-mode: t