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/array.hpp"
40 #include "vm/javaobjects.hpp"
41 #include "vm/string.hpp"
44 // Native functions are exported as C functions.
48 * Class: java/lang/String
52 JNIEXPORT jint JNICALL Java_java_lang_String_hashCode(JNIEnv *env, jstring _this)
54 java_lang_String jls(_this);
56 CharArray 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) + value.get_element(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 CharArray value(jls.get_value());
82 int32_t offset = jls.get_offset();
83 int32_t count = jls.get_count();
85 for (int32_t i = 0; i < count; i++) {
86 if (value.get_element(offset + i) == ch) {
96 * Class: java/lang/String
100 JNIEXPORT jint JNICALL Java_java_lang_String_indexOf__II(JNIEnv *env, jstring _this, jint ch, jint fromIndex)
102 java_lang_String jls(_this);
104 CharArray value(jls.get_value());
106 int32_t offset = jls.get_offset();
107 int32_t count = jls.get_count();
112 else if (fromIndex >= count) {
113 // Note: fromIndex might be near -1>>>1.
117 for (int32_t i = fromIndex ; i < count ; i++) {
118 if (value.get_element(offset + i) == ch) {
128 * Class: java/lang/String
129 * Method: lastIndexOf
132 JNIEXPORT jint JNICALL Java_java_lang_String_lastIndexOf__II(JNIEnv *env, jstring _this, jint ch, jint fromIndex)
134 java_lang_String jls(_this);
136 CharArray value(jls.get_value());
138 int32_t offset = jls.get_offset();
139 int32_t count = jls.get_count();
141 int32_t start = ((fromIndex >= count) ? count - 1 : fromIndex);
143 for (int32_t i = start; i >= 0; i--) {
144 if (value.get_element(offset + i) == ch) {
154 * Class: java/lang/String
155 * Method: lastIndexOf
158 JNIEXPORT jint JNICALL Java_java_lang_String_lastIndexOf__I(JNIEnv *env, jstring _this, jint ch)
160 java_lang_String jls(_this);
162 return Java_java_lang_String_lastIndexOf__II(env, _this, ch, jls.get_count() - 1);
167 * Class: java/lang/String
169 * Signature: ()Ljava/lang/String;
171 JNIEXPORT jstring JNICALL Java_java_lang_String_intern(JNIEnv *env, jstring _this)
173 java_lang_String jls(_this);
178 return (jstring) javastring_intern(jls.get_handle());
184 /* native methods implemented by this file ************************************/
186 static JNINativeMethod methods[] = {
187 { (char*) "hashCode", (char*) "()I", (void*) (uintptr_t) &Java_java_lang_String_hashCode },
188 { (char*) "indexOf", (char*) "(I)I", (void*) (uintptr_t) &Java_java_lang_String_indexOf__I },
189 { (char*) "indexOf", (char*) "(II)I", (void*) (uintptr_t) &Java_java_lang_String_indexOf__II },
190 { (char*) "lastIndexOf", (char*) "(II)I", (void*) (uintptr_t) &Java_java_lang_String_lastIndexOf__II },
191 { (char*) "lastIndexOf", (char*) "(I)I", (void*) (uintptr_t) &Java_java_lang_String_lastIndexOf__I },
193 { (char*) "equals", (char*) "(Ljava/lang/Object;)Z;", (void*) (uintptr_t) &Java_java_lang_String_equals },
195 { (char*) "intern", (char*) "()Ljava/lang/String;", (void*) (uintptr_t) &Java_java_lang_String_intern },
199 /* _Jv_java_lang_String_init ***************************************************
201 Register native functions.
203 *******************************************************************************/
205 void _Jv_java_lang_String_init(void)
207 utf* u = utf_new_char("java/lang/String");
209 NativeMethods& nm = VM::get_current()->get_nativemethods();
210 nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
215 * These are local overrides for various environment variables in Emacs.
216 * Please do not remove this and leave it at the end of the file, where
217 * Emacs will automagically detect them.
218 * ---------------------------------------------------------------------
221 * indent-tabs-mode: t