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 #include "native/include/java_lang_Object.h"
40 #include "native/include/java_lang_String.h"
43 #include "vm/string.hpp"
46 /* native methods implemented by this file ************************************/
48 static JNINativeMethod methods[] = {
49 { (char*) "hashCode", (char*) "()I", (void*) (uintptr_t) &Java_java_lang_String_hashCode },
50 { (char*) "indexOf", (char*) "(I)I", (void*) (uintptr_t) &Java_java_lang_String_indexOf__I },
51 { (char*) "indexOf", (char*) "(II)I", (void*) (uintptr_t) &Java_java_lang_String_indexOf__II },
52 { (char*) "lastIndexOf", (char*) "(I)I", (void*) (uintptr_t) &Java_java_lang_String_lastIndexOf__I },
53 { (char*) "lastIndexOf", (char*) "(II)I", (void*) (uintptr_t) &Java_java_lang_String_lastIndexOf__II },
55 { (char*) "equals", (char*) "(Ljava/lang/Object;)Z;", (void*) (uintptr_t) &Java_java_lang_String_equals },
57 { (char*) "intern", (char*) "()Ljava/lang/String;", (void*) (uintptr_t) &Java_java_lang_String_intern },
61 /* _Jv_java_lang_String_init ***************************************************
63 Register native functions.
65 *******************************************************************************/
69 void _Jv_java_lang_String_init(void)
73 u = utf_new_char("java/lang/String");
75 native_method_register(u, methods, NATIVE_METHODS_COUNT);
80 // Native functions are exported as C functions.
84 * Class: java/lang/String
88 JNIEXPORT s4 JNICALL Java_java_lang_String_hashCode(JNIEnv *env, java_lang_String *_this)
90 java_handle_chararray_t *value;
96 /* get values from Java object */
98 LLNI_field_get_val(_this, offset, offset);
99 LLNI_field_get_val(_this, count, count);
100 LLNI_field_get_ref(_this, value, value);
104 for (i = 0; i < count; i++) {
105 hash = (31 * hash) + LLNI_array_direct(value, offset + i);
113 * Class: java/lang/String
117 JNIEXPORT s4 JNICALL Java_java_lang_String_indexOf__I(JNIEnv *env, java_lang_String *_this, s4 ch)
119 java_handle_chararray_t *value;
124 /* get values from Java object */
126 LLNI_field_get_val(_this, offset, offset);
127 LLNI_field_get_val(_this, count, count);
128 LLNI_field_get_ref(_this, value, value);
130 for (i = 0; i < count; i++) {
131 if (LLNI_array_direct(value, offset + i) == ch) {
141 * Class: java/lang/String
145 JNIEXPORT s4 JNICALL Java_java_lang_String_indexOf__II(JNIEnv *env, java_lang_String *_this, s4 ch, s4 fromIndex)
147 java_handle_chararray_t *value;
152 /* get values from Java object */
154 LLNI_field_get_val(_this, offset, offset);
155 LLNI_field_get_val(_this, count, count);
156 LLNI_field_get_ref(_this, value, value);
161 else if (fromIndex >= count) {
162 /* Note: fromIndex might be near -1>>>1. */
166 for (i = fromIndex ; i < count ; i++) {
167 if (LLNI_array_direct(value, offset + i) == ch) {
177 * Class: java/lang/String
178 * Method: lastIndexOf
181 JNIEXPORT s4 JNICALL Java_java_lang_String_lastIndexOf__I(JNIEnv *env, java_lang_String *_this, s4 ch)
185 LLNI_field_get_val(_this, count, count);
187 return Java_java_lang_String_lastIndexOf__II(env, _this, ch, count - 1);
192 * Class: java/lang/String
193 * Method: lastIndexOf
196 JNIEXPORT s4 JNICALL Java_java_lang_String_lastIndexOf__II(JNIEnv *env, java_lang_String *_this, s4 ch, s4 fromIndex)
198 java_handle_chararray_t *value;
204 /* get values from Java object */
206 LLNI_field_get_val(_this, offset, offset);
207 LLNI_field_get_val(_this, count, count);
208 LLNI_field_get_ref(_this, value, value);
210 start = ((fromIndex >= count) ? count - 1 : fromIndex);
212 for (i = start; i >= 0; i--) {
213 if (LLNI_array_direct(value, offset + i) == ch) {
224 * Class: java/lang/String
226 * Signature: (Ljava/lang/Object;)Z;
228 JNIEXPORT s4 JNICALL Java_java_lang_String_equals(JNIEnv *env, java_lang_String* _this, java_lang_Object *o)
231 java_handle_chararray_t *value;
234 java_handle_chararray_t *dvalue;
239 LLNI_field_get_val(_this, offset, offset);
240 LLNI_field_get_val(_this, count, count);
241 LLNI_field_get_ref(_this, value, value);
242 LLNI_class_get(o, c);
244 /* TODO: is this the correct implementation for short-circuiting on object identity? */
245 if ((java_lang_Object*)_this == o)
248 if (c != class_java_lang_String)
251 s = (java_lang_String *) o;
252 LLNI_field_get_val(_this, offset, doffset);
253 LLNI_field_get_val(_this, count, dcount);
254 LLNI_field_get_ref(_this, value, dvalue);
259 return ( 0 == memcmp((void*)(LLNI_array_direct(value, offset)),
260 (void*)(LLNI_array_direct(dvalue, doffset),
268 * Class: java/lang/String
270 * Signature: ()Ljava/lang/String;
272 JNIEXPORT java_lang_String* JNICALL Java_java_lang_String_intern(JNIEnv *env, java_lang_String *_this)
277 return (java_lang_String *) javastring_intern((java_handle_t *) _this);
284 * These are local overrides for various environment variables in Emacs.
285 * Please do not remove this and leave it at the end of the file, where
286 * Emacs will automagically detect them.
287 * ---------------------------------------------------------------------
290 * indent-tabs-mode: t