1 /* src/native/vm/cldc1.1/java_lang_String.c
3 Copyright (C) 2006, 2007 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
35 #include "native/jni.h"
36 #include "native/llni.h"
37 #include "native/native.h"
39 #include "native/include/java_lang_Object.h"
41 #include "native/include/java_lang_String.h"
43 #include "vm/stringlocal.h"
46 /* native methods implemented by this file ************************************/
48 static JNINativeMethod methods[] = {
49 { "hashCode", "()I", (void *) (ptrint) &Java_java_lang_String_hashCode },
50 { "indexOf", "(I)I", (void *) (ptrint) &Java_java_lang_String_indexOf__I },
51 { "indexOf", "(II)I", (void *) (ptrint) &Java_java_lang_String_indexOf__II },
52 { "lastIndexOf", "(I)I", (void *) (ptrint) &Java_java_lang_String_lastIndexOf__I },
53 { "lastIndexOf", "(II)I", (void *) (ptrint) &Java_java_lang_String_lastIndexOf__II },
55 { "equals", "(Ljava/lang/Object;)Z;", (void *) (ptrint) &Java_java_lang_String_equals },
57 { "intern", "()Ljava/lang/String;", (void *) (ptrint) &Java_java_lang_String_intern },
61 /* _Jv_java_lang_String_init ***************************************************
63 Register native functions.
65 *******************************************************************************/
67 void _Jv_java_lang_String_init(void)
71 u = utf_new_char("java/lang/String");
73 native_method_register(u, methods, NATIVE_METHODS_COUNT);
78 * Class: java/lang/String
82 JNIEXPORT s4 JNICALL Java_java_lang_String_hashCode(JNIEnv *env, java_lang_String *this)
84 java_handle_chararray_t *value;
90 /* get values from Java object */
92 LLNI_field_get_val(this, offset, offset);
93 LLNI_field_get_val(this, count, count);
94 LLNI_field_get_ref(this, value, value);
98 for (i = 0; i < count; i++) {
99 hash = (31 * hash) + LLNI_array_direct(value, offset + i);
107 * Class: java/lang/String
111 JNIEXPORT s4 JNICALL Java_java_lang_String_indexOf__I(JNIEnv *env, java_lang_String *this, s4 ch)
113 java_handle_chararray_t *value;
118 /* get values from Java object */
120 LLNI_field_get_val(this, offset, offset);
121 LLNI_field_get_val(this, count, count);
122 LLNI_field_get_ref(this, value, value);
124 for (i = 0; i < count; i++) {
125 if (LLNI_array_direct(value, offset + i) == ch) {
135 * Class: java/lang/String
139 JNIEXPORT s4 JNICALL Java_java_lang_String_indexOf__II(JNIEnv *env, java_lang_String *this, s4 ch, s4 fromIndex)
141 java_handle_chararray_t *value;
146 /* get values from Java object */
148 LLNI_field_get_val(this, offset, offset);
149 LLNI_field_get_val(this, count, count);
150 LLNI_field_get_ref(this, value, value);
155 else if (fromIndex >= count) {
156 /* Note: fromIndex might be near -1>>>1. */
160 for (i = fromIndex ; i < count ; i++) {
161 if (LLNI_array_direct(value, offset + i) == ch) {
171 * Class: java/lang/String
172 * Method: lastIndexOf
175 JNIEXPORT s4 JNICALL Java_java_lang_String_lastIndexOf__I(JNIEnv *env, java_lang_String *this, s4 ch)
179 LLNI_field_get_val(this, count, count);
181 return Java_java_lang_String_lastIndexOf__II(env, this, ch, count - 1);
186 * Class: java/lang/String
187 * Method: lastIndexOf
190 JNIEXPORT s4 JNICALL Java_java_lang_String_lastIndexOf__II(JNIEnv *env, java_lang_String *this, s4 ch, s4 fromIndex)
192 java_handle_chararray_t *value;
198 /* get values from Java object */
200 LLNI_field_get_val(this, offset, offset);
201 LLNI_field_get_val(this, count, count);
202 LLNI_field_get_ref(this, value, value);
204 start = ((fromIndex >= count) ? count - 1 : fromIndex);
206 for (i = start; i >= 0; i--) {
207 if (LLNI_array_direct(value, offset + i) == ch) {
218 * Class: java/lang/String
220 * Signature: (Ljava/lang/Object;)Z;
222 JNIEXPORT s4 JNICALL Java_java_lang_String_equals(JNIEnv *env, java_lang_String* this, java_lang_Object *o)
225 java_handle_chararray_t *value;
228 java_handle_chararray_t *dvalue;
233 LLNI_field_get_val(this, offset, offset);
234 LLNI_field_get_val(this, count, count);
235 LLNI_field_get_ref(this, value, value);
236 LLNI_class_get(o, c);
238 /* TODO: is this the correct implementation for short-circuiting on object identity? */
239 if ((java_lang_Object*)this == o)
242 if (c != class_java_lang_String)
245 s = (java_lang_String *) o;
246 LLNI_field_get_val(this, offset, doffset);
247 LLNI_field_get_val(this, count, dcount);
248 LLNI_field_get_ref(this, value, dvalue);
253 return ( 0 == memcmp((void*)(LLNI_array_direct(value, offset)),
254 (void*)(LLNI_array_direct(dvalue, doffset),
262 * Class: java/lang/String
264 * Signature: ()Ljava/lang/String;
266 JNIEXPORT java_lang_String* JNICALL Java_java_lang_String_intern(JNIEnv *env, java_lang_String *this)
271 return (java_lang_String *) javastring_intern((java_handle_t *) this);
276 * These are local overrides for various environment variables in Emacs.
277 * Please do not remove this and leave it at the end of the file, where
278 * Emacs will automagically detect them.
279 * ---------------------------------------------------------------------
282 * indent-tabs-mode: t