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.hpp"
33 #include "native/native.hpp"
35 #if defined(ENABLE_JNI_HEADERS)
36 # include "native/include/java_lang_String.h"
39 #include "vm/javaobjects.hpp"
40 #include "vm/string.hpp"
43 // Native functions are exported as C functions.
47 * Class: java/lang/String
51 JNIEXPORT jint JNICALL Java_java_lang_String_hashCode(JNIEnv *env, jstring _this)
53 java_lang_String jls(_this);
55 CharArray value(jls.get_value());
57 int32_t offset = jls.get_offset();
58 int32_t count = jls.get_count();
62 for (int32_t i = 0; i < count; i++) {
63 hash = (31 * hash) + value.get_element(offset + i);
71 * Class: java/lang/String
75 JNIEXPORT jint JNICALL Java_java_lang_String_indexOf__I(JNIEnv *env, jstring _this, jint ch)
77 java_lang_String jls(_this);
79 CharArray 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 (value.get_element(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 CharArray value(jls.get_value());
105 int32_t offset = jls.get_offset();
106 int32_t count = jls.get_count();
111 else if (fromIndex >= count) {
112 // Note: fromIndex might be near -1>>>1.
116 for (int32_t i = fromIndex ; i < count ; i++) {
117 if (value.get_element(offset + i) == ch) {
127 * Class: java/lang/String
128 * Method: lastIndexOf
131 JNIEXPORT jint JNICALL Java_java_lang_String_lastIndexOf__II(JNIEnv *env, jstring _this, jint ch, jint fromIndex)
133 java_lang_String jls(_this);
135 CharArray value(jls.get_value());
137 int32_t offset = jls.get_offset();
138 int32_t count = jls.get_count();
140 int32_t start = ((fromIndex >= count) ? count - 1 : fromIndex);
142 for (int32_t i = start; i >= 0; i--) {
143 if (value.get_element(offset + i) == ch) {
153 * Class: java/lang/String
154 * Method: lastIndexOf
157 JNIEXPORT jint JNICALL Java_java_lang_String_lastIndexOf__I(JNIEnv *env, jstring _this, jint ch)
159 java_lang_String jls(_this);
161 return Java_java_lang_String_lastIndexOf__II(env, _this, ch, jls.get_count() - 1);
166 * Class: java/lang/String
168 * Signature: ()Ljava/lang/String;
170 JNIEXPORT jstring JNICALL Java_java_lang_String_intern(JNIEnv *env, jstring _this)
172 java_lang_String jls(_this);
177 return (jstring) javastring_intern(jls.get_handle());
183 /* native methods implemented by this file ************************************/
185 static JNINativeMethod methods[] = {
186 { (char*) "hashCode", (char*) "()I", (void*) (uintptr_t) &Java_java_lang_String_hashCode },
187 { (char*) "indexOf", (char*) "(I)I", (void*) (uintptr_t) &Java_java_lang_String_indexOf__I },
188 { (char*) "indexOf", (char*) "(II)I", (void*) (uintptr_t) &Java_java_lang_String_indexOf__II },
189 { (char*) "lastIndexOf", (char*) "(II)I", (void*) (uintptr_t) &Java_java_lang_String_lastIndexOf__II },
190 { (char*) "lastIndexOf", (char*) "(I)I", (void*) (uintptr_t) &Java_java_lang_String_lastIndexOf__I },
192 { (char*) "equals", (char*) "(Ljava/lang/Object;)Z;", (void*) (uintptr_t) &Java_java_lang_String_equals },
194 { (char*) "intern", (char*) "()Ljava/lang/String;", (void*) (uintptr_t) &Java_java_lang_String_intern },
198 /* _Jv_java_lang_String_init ***************************************************
200 Register native functions.
202 *******************************************************************************/
204 void _Jv_java_lang_String_init(void)
206 utf* u = utf_new_char("java/lang/String");
208 NativeMethods& nm = VM::get_current()->get_nativemethods();
209 nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
214 * These are local overrides for various environment variables in Emacs.
215 * Please do not remove this and leave it at the end of the file, where
216 * Emacs will automagically detect them.
217 * ---------------------------------------------------------------------
220 * indent-tabs-mode: t