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/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 java_handle_chararray_t* 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) + LLNI_array_direct(value, 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 java_handle_chararray_t* value = jls.get_value();
80 int32_t offset = jls.get_offset();
81 int32_t count = jls.get_count();
83 for (int32_t i = 0; i < count; i++) {
84 if (LLNI_array_direct(value, offset + i) == ch) {
94 * Class: java/lang/String
98 JNIEXPORT jint JNICALL Java_java_lang_String_indexOf__II(JNIEnv *env, jstring _this, jint ch, jint fromIndex)
100 java_lang_String jls(_this);
102 java_handle_chararray_t* value = jls.get_value();
103 int32_t offset = jls.get_offset();
104 int32_t count = jls.get_count();
109 else if (fromIndex >= count) {
110 // Note: fromIndex might be near -1>>>1.
114 for (int32_t i = fromIndex ; i < count ; i++) {
115 if (LLNI_array_direct(value, offset + i) == ch) {
125 * Class: java/lang/String
126 * Method: lastIndexOf
129 JNIEXPORT jint JNICALL Java_java_lang_String_lastIndexOf__II(JNIEnv *env, jstring _this, jint ch, jint fromIndex)
131 java_lang_String jls(_this);
133 java_handle_chararray_t* value = jls.get_value();
134 int32_t offset = jls.get_offset();
135 int32_t count = jls.get_count();
137 int32_t start = ((fromIndex >= count) ? count - 1 : fromIndex);
139 for (int32_t i = start; i >= 0; i--) {
140 if (LLNI_array_direct(value, offset + i) == ch) {
150 * Class: java/lang/String
151 * Method: lastIndexOf
154 JNIEXPORT jint JNICALL Java_java_lang_String_lastIndexOf__I(JNIEnv *env, jstring _this, jint ch)
156 java_lang_String jls(_this);
158 return Java_java_lang_String_lastIndexOf__II(env, _this, ch, jls.get_count() - 1);
163 * Class: java/lang/String
165 * Signature: ()Ljava/lang/String;
167 JNIEXPORT jstring JNICALL Java_java_lang_String_intern(JNIEnv *env, jstring _this)
169 java_lang_String jls(_this);
174 return (jstring) javastring_intern(jls.get_handle());
180 /* native methods implemented by this file ************************************/
182 static JNINativeMethod methods[] = {
183 { (char*) "hashCode", (char*) "()I", (void*) (uintptr_t) &Java_java_lang_String_hashCode },
184 { (char*) "indexOf", (char*) "(I)I", (void*) (uintptr_t) &Java_java_lang_String_indexOf__I },
185 { (char*) "indexOf", (char*) "(II)I", (void*) (uintptr_t) &Java_java_lang_String_indexOf__II },
186 { (char*) "lastIndexOf", (char*) "(II)I", (void*) (uintptr_t) &Java_java_lang_String_lastIndexOf__II },
187 { (char*) "lastIndexOf", (char*) "(I)I", (void*) (uintptr_t) &Java_java_lang_String_lastIndexOf__I },
189 { (char*) "equals", (char*) "(Ljava/lang/Object;)Z;", (void*) (uintptr_t) &Java_java_lang_String_equals },
191 { (char*) "intern", (char*) "()Ljava/lang/String;", (void*) (uintptr_t) &Java_java_lang_String_intern },
195 /* _Jv_java_lang_String_init ***************************************************
197 Register native functions.
199 *******************************************************************************/
203 void _Jv_java_lang_String_init(void)
207 u = utf_new_char("java/lang/String");
209 native_method_register(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