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
25 $Id: java_lang_VMRuntime.c 5900 2006-11-04 17:30:44Z michi $
37 #include "native/jni.h"
38 #include "native/llni.h"
39 #include "native/native.h"
41 #include "native/include/java_lang_Object.h"
43 #include "native/include/java_lang_String.h"
45 #include "vm/stringlocal.h"
48 /* native methods implemented by this file ************************************/
50 static JNINativeMethod methods[] = {
51 { "hashCode", "()I", (void *) (ptrint) &Java_java_lang_String_hashCode },
52 { "indexOf", "(I)I", (void *) (ptrint) &Java_java_lang_String_indexOf__I },
53 { "indexOf", "(II)I", (void *) (ptrint) &Java_java_lang_String_indexOf__II },
54 { "lastIndexOf", "(I)I", (void *) (ptrint) &Java_java_lang_String_lastIndexOf__I },
55 { "lastIndexOf", "(II)I", (void *) (ptrint) &Java_java_lang_String_lastIndexOf__II },
57 { "equals", "(Ljava/lang/Object;)Z;", (void *) (ptrint) &Java_java_lang_String_equals },
59 { "intern", "()Ljava/lang/String;", (void *) (ptrint) &Java_java_lang_String_intern },
63 /* _Jv_java_lang_String_init ***************************************************
65 Register native functions.
67 *******************************************************************************/
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 * Class: java/lang/String
84 JNIEXPORT s4 JNICALL Java_java_lang_String_hashCode(JNIEnv *env, java_lang_String *this)
86 java_handle_chararray_t *value;
92 /* get values from Java object */
94 LLNI_field_get_val(this, offset, offset);
95 LLNI_field_get_val(this, count, count);
96 LLNI_field_get_ref(this, value, value);
100 for (i = 0; i < count; i++) {
101 hash = (31 * hash) + LLNI_array_direct(value, offset + i);
109 * Class: java/lang/String
113 JNIEXPORT s4 JNICALL Java_java_lang_String_indexOf__I(JNIEnv *env, java_lang_String *this, s4 ch)
115 java_handle_chararray_t *value;
120 /* get values from Java object */
122 LLNI_field_get_val(this, offset, offset);
123 LLNI_field_get_val(this, count, count);
124 LLNI_field_get_ref(this, value, value);
126 for (i = 0; i < count; i++) {
127 if (LLNI_array_direct(value, offset + i) == ch) {
137 * Class: java/lang/String
141 JNIEXPORT s4 JNICALL Java_java_lang_String_indexOf__II(JNIEnv *env, java_lang_String *this, s4 ch, s4 fromIndex)
143 java_handle_chararray_t *value;
148 /* get values from Java object */
150 LLNI_field_get_val(this, offset, offset);
151 LLNI_field_get_val(this, count, count);
152 LLNI_field_get_ref(this, value, value);
157 else if (fromIndex >= count) {
158 /* Note: fromIndex might be near -1>>>1. */
162 for (i = fromIndex ; i < count ; i++) {
163 if (LLNI_array_direct(value, offset + i) == ch) {
173 * Class: java/lang/String
174 * Method: lastIndexOf
177 JNIEXPORT s4 JNICALL Java_java_lang_String_lastIndexOf__I(JNIEnv *env, java_lang_String *this, s4 ch)
181 LLNI_field_get_val(this, count, count);
183 return Java_java_lang_String_lastIndexOf__II(env, this, ch, count - 1);
188 * Class: java/lang/String
189 * Method: lastIndexOf
192 JNIEXPORT s4 JNICALL Java_java_lang_String_lastIndexOf__II(JNIEnv *env, java_lang_String *this, s4 ch, s4 fromIndex)
194 java_handle_chararray_t *value;
200 /* get values from Java object */
202 LLNI_field_get_val(this, offset, offset);
203 LLNI_field_get_val(this, count, count);
204 LLNI_field_get_ref(this, value, value);
206 start = ((fromIndex >= count) ? count - 1 : fromIndex);
208 for (i = start; i >= 0; i--) {
209 if (LLNI_array_direct(value, offset + i) == ch) {
220 * Class: java/lang/String
222 * Signature: (Ljava/lang/Object;)Z;
224 JNIEXPORT s4 JNICALL Java_java_lang_String_equals(JNIEnv *env, java_lang_String* this, java_lang_Object *o)
227 java_handle_chararray_t *value;
230 java_handle_chararray_t *dvalue;
235 LLNI_field_get_val(this, offset, offset);
236 LLNI_field_get_val(this, count, count);
237 LLNI_field_get_ref(this, value, value);
238 LLNI_class_get(o, c);
240 /* TODO: is this the correct implementation for short-circuiting on object identity? */
241 if ((java_lang_Object*)this == o)
244 if (c != class_java_lang_String)
247 s = (java_lang_String *) o;
248 LLNI_field_get_val(this, offset, doffset);
249 LLNI_field_get_val(this, count, dcount);
250 LLNI_field_get_ref(this, value, dvalue);
255 return ( 0 == memcmp((void*)(LLNI_array_direct(value, offset)),
256 (void*)(LLNI_array_direct(dvalue, doffset),
264 * Class: java/lang/String
266 * Signature: ()Ljava/lang/String;
268 JNIEXPORT java_lang_String* JNICALL Java_java_lang_String_intern(JNIEnv *env, java_lang_String *this)
271 java_handle_chararray_t *value;
278 LLNI_field_get_val(this, offset, offset);
279 LLNI_field_get_val(this, count, count);
280 LLNI_field_get_ref(this, value, value);
282 /* search table so identical strings will get identical pointers */
284 o = literalstring_u2(value, count, offset, true);
286 return (java_lang_String *) o;
291 * These are local overrides for various environment variables in Emacs.
292 * Please do not remove this and leave it at the end of the file, where
293 * Emacs will automagically detect them.
294 * ---------------------------------------------------------------------
297 * indent-tabs-mode: t