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/native.h"
40 #include "native/include/java_lang_Object.h"
42 #include "native/include/java_lang_String.h"
44 #include "vm/stringlocal.h"
47 /* native methods implemented by this file ************************************/
49 static JNINativeMethod methods[] = {
50 { "hashCode", "()I", (void *) (ptrint) &Java_java_lang_String_hashCode },
51 { "indexOf", "(I)I", (void *) (ptrint) &Java_java_lang_String_indexOf__I },
52 { "indexOf", "(II)I", (void *) (ptrint) &Java_java_lang_String_indexOf__II },
53 { "lastIndexOf", "(I)I", (void *) (ptrint) &Java_java_lang_String_lastIndexOf__I },
54 { "lastIndexOf", "(II)I", (void *) (ptrint) &Java_java_lang_String_lastIndexOf__II },
56 { "equals", "(Ljava/lang/Object;)Z;", (void *) (ptrint) &Java_java_lang_String_equals },
58 { "intern", "()Ljava/lang/String;", (void *) (ptrint) &Java_java_lang_String_intern },
62 /* _Jv_java_lang_String_init ***************************************************
64 Register native functions.
66 *******************************************************************************/
68 void _Jv_java_lang_String_init(void)
72 u = utf_new_char("java/lang/String");
74 native_method_register(u, methods, NATIVE_METHODS_COUNT);
79 * Class: java/lang/String
83 JNIEXPORT s4 JNICALL Java_java_lang_String_hashCode(JNIEnv *env, java_lang_String *this)
85 java_chararray *value;
91 /* get values from Java object */
93 offset = this->offset;
99 for (i = 0; i < count; i++) {
100 hash = (31 * hash) + value->data[offset + i];
108 * Class: java/lang/String
112 JNIEXPORT s4 JNICALL Java_java_lang_String_indexOf__I(JNIEnv *env, java_lang_String *this, s4 ch)
114 java_chararray *value;
119 /* get values from Java object */
121 offset = this->offset;
125 for (i = 0; i < count; i++) {
126 if (value->data[offset + i] == ch) {
136 * Class: java/lang/String
140 JNIEXPORT s4 JNICALL Java_java_lang_String_indexOf__II(JNIEnv *env, java_lang_String *this, s4 ch, s4 fromIndex)
142 java_chararray *value;
147 /* get values from Java object */
149 offset = this->offset;
156 else if (fromIndex >= count) {
157 /* Note: fromIndex might be near -1>>>1. */
161 for (i = fromIndex ; i < count ; i++) {
162 if (value->data[offset + i] == ch) {
172 * Class: java/lang/String
173 * Method: lastIndexOf
176 JNIEXPORT s4 JNICALL Java_java_lang_String_lastIndexOf__I(JNIEnv *env, java_lang_String *this, s4 ch)
178 return Java_java_lang_String_lastIndexOf__II(env, this, ch, this->count - 1);
183 * Class: java/lang/String
184 * Method: lastIndexOf
187 JNIEXPORT s4 JNICALL Java_java_lang_String_lastIndexOf__II(JNIEnv *env, java_lang_String *this, s4 ch, s4 fromIndex)
189 java_chararray *value;
195 /* get values from Java object */
197 offset = this->offset;
201 start = ((fromIndex >= count) ? count - 1 : fromIndex);
203 for (i = start; i >= 0; i--) {
204 if (value->data[offset + i] == ch) {
215 * Class: java/lang/String
217 * Signature: (Ljava/lang/Object;)Z;
219 JNIEXPORT s4 JNICALL Java_java_lang_String_equals(JNIEnv *env, java_lang_String* this, java_lang_Object *o)
223 /* TODO: is this the correct implementation for short-circuiting on object identity? */
224 if ((java_lang_Object*)this == o)
227 if (o->header.vftbl->class != class_java_lang_String)
230 s = (java_lang_String *) o;
232 if (this->count != s->count)
235 return ( 0 == memcmp((void*)(this->value->data + this->offset),
236 (void*)(s->value->data + s->offset),
243 * Class: java/lang/String
245 * Signature: ()Ljava/lang/String;
247 JNIEXPORT java_lang_String* JNICALL Java_java_lang_String_intern(JNIEnv *env, java_lang_String *this)
249 java_objectheader *o;
254 /* search table so identical strings will get identical pointers */
256 o = literalstring_u2(this->value, this->count, this->offset, true);
258 return (java_lang_String *) o;
263 * These are local overrides for various environment variables in Emacs.
264 * Please do not remove this and leave it at the end of the file, where
265 * Emacs will automagically detect them.
266 * ---------------------------------------------------------------------
269 * indent-tabs-mode: t