* Removed all Id tags.
[cacao.git] / src / native / vm / cldc1.1 / java_lang_String.c
1 /* src/native/vm/cldc1.1/java_lang_String.c
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02110-1301, USA.
24
25 */
26
27
28 #include "config.h"
29
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "vm/types.h"
34
35 #include "native/jni.h"
36 #include "native/llni.h"
37 #include "native/native.h"
38
39 #include "native/include/java_lang_Object.h"
40
41 #include "native/include/java_lang_String.h"
42
43 #include "vm/stringlocal.h"
44
45
46 /* native methods implemented by this file ************************************/
47  
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 },
54 #if 0
55         { "equals",      "(Ljava/lang/Object;)Z;", (void *) (ptrint) &Java_java_lang_String_equals          },
56 #endif
57         { "intern",      "()Ljava/lang/String;",   (void *) (ptrint) &Java_java_lang_String_intern          },
58 };
59
60
61 /* _Jv_java_lang_String_init ***************************************************
62  
63    Register native functions.
64  
65 *******************************************************************************/
66  
67 void _Jv_java_lang_String_init(void)
68 {
69         utf *u;
70  
71         u = utf_new_char("java/lang/String");
72  
73         native_method_register(u, methods, NATIVE_METHODS_COUNT);
74 }
75
76
77 /*
78  * Class:     java/lang/String
79  * Method:    hashCode
80  * Signature: ()I
81  */
82 JNIEXPORT s4 JNICALL Java_java_lang_String_hashCode(JNIEnv *env, java_lang_String *this)
83 {
84         java_handle_chararray_t *value;
85         int32_t                 offset;
86         int32_t                 count;
87         s4                              hash;
88         s4                              i;
89
90         /* get values from Java object */
91         
92         LLNI_field_get_val(this, offset, offset);
93         LLNI_field_get_val(this, count, count);
94         LLNI_field_get_ref(this, value, value);
95
96         hash = 0;
97
98         for (i = 0; i < count; i++) {
99                 hash = (31 * hash) + LLNI_array_direct(value, offset + i);
100         }
101
102         return hash;
103 }
104
105
106 /*
107  * Class:     java/lang/String
108  * Method:    indexOf
109  * Signature: (I)I
110  */
111 JNIEXPORT s4 JNICALL Java_java_lang_String_indexOf__I(JNIEnv *env, java_lang_String *this, s4 ch)
112 {
113         java_handle_chararray_t *value;
114         int32_t                 offset;
115         int32_t                 count;
116         s4                              i;
117
118         /* get values from Java object */
119
120         LLNI_field_get_val(this, offset, offset);
121         LLNI_field_get_val(this, count, count);
122         LLNI_field_get_ref(this, value, value);
123
124         for (i = 0; i < count; i++) {
125                 if (LLNI_array_direct(value, offset + i) == ch) {
126                         return i;
127                 }
128         }
129
130         return -1;
131 }
132
133
134 /*
135  * Class:     java/lang/String
136  * Method:    indexOf
137  * Signature: (II)I
138  */
139 JNIEXPORT s4 JNICALL Java_java_lang_String_indexOf__II(JNIEnv *env, java_lang_String *this, s4 ch, s4 fromIndex)
140 {
141         java_handle_chararray_t *value;
142         int32_t                 offset;
143         int32_t                 count;
144         s4                              i;
145
146         /* get values from Java object */
147
148         LLNI_field_get_val(this, offset, offset);
149         LLNI_field_get_val(this, count, count);
150         LLNI_field_get_ref(this, value, value);
151
152         if (fromIndex < 0) {
153                 fromIndex = 0;
154         }
155         else if (fromIndex >= count) {
156                 /* Note: fromIndex might be near -1>>>1. */
157                 return -1;
158         }
159
160         for (i = fromIndex ; i < count ; i++) {
161                 if (LLNI_array_direct(value, offset + i) == ch) {
162                         return i;
163                 }
164         }
165
166         return -1;
167 }
168
169
170 /*
171  * Class:     java/lang/String
172  * Method:    lastIndexOf
173  * Signature: (I)I
174  */
175 JNIEXPORT s4 JNICALL Java_java_lang_String_lastIndexOf__I(JNIEnv *env, java_lang_String *this, s4 ch)
176 {
177         int32_t count;
178         
179         LLNI_field_get_val(this, count, count);
180         
181         return Java_java_lang_String_lastIndexOf__II(env, this, ch, count - 1);
182 }
183
184
185 /*
186  * Class:     java/lang/String
187  * Method:    lastIndexOf
188  * Signature: (II)I
189  */
190 JNIEXPORT s4 JNICALL Java_java_lang_String_lastIndexOf__II(JNIEnv *env, java_lang_String *this, s4 ch, s4 fromIndex)
191 {
192         java_handle_chararray_t *value;
193         int32_t                 offset;
194         int32_t                 count;
195         s4                              start;
196         s4                              i;
197
198         /* get values from Java object */
199
200         LLNI_field_get_val(this, offset, offset);
201         LLNI_field_get_val(this, count, count);
202         LLNI_field_get_ref(this, value, value);
203
204         start = ((fromIndex >= count) ? count - 1 : fromIndex);
205
206         for (i = start; i >= 0; i--) {
207                 if (LLNI_array_direct(value, offset + i) == ch) {
208                         return i;
209                 }
210         }
211
212         return -1;
213 }
214
215
216 #if 0
217 /*
218  * Class:     java/lang/String
219  * Method:    equals
220  * Signature: (Ljava/lang/Object;)Z;
221  */
222 JNIEXPORT s4 JNICALL Java_java_lang_String_equals(JNIEnv *env, java_lang_String* this, java_lang_Object *o)
223 {
224         java_lang_String*               s;
225         java_handle_chararray_t *value;
226         int32_t                 offset;
227         int32_t                 count;
228         java_handle_chararray_t *dvalue;
229         int32_t                 doffset;
230         int32_t                 dcount;
231         classinfo                       *c;
232         
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);
237
238         /* TODO: is this the correct implementation for short-circuiting on object identity? */
239         if ((java_lang_Object*)this == o)
240                 return 1;
241         
242         if (c != class_java_lang_String) 
243                 return 0;
244
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);
249
250         if (count != dcount)
251                 return 0;
252
253         return ( 0 == memcmp((void*)(LLNI_array_direct(value, offset)),
254                                                  (void*)(LLNI_array_direct(dvalue, doffset),
255                                                  count) );
256
257 }
258 #endif
259
260
261 /*
262  * Class:     java/lang/String
263  * Method:    intern
264  * Signature: ()Ljava/lang/String;
265  */
266 JNIEXPORT java_lang_String* JNICALL Java_java_lang_String_intern(JNIEnv *env, java_lang_String *this)
267 {
268         if (this == NULL)
269                 return NULL;
270
271         return (java_lang_String *) javastring_intern((java_handle_t *) this);
272 }
273
274
275 /*
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  * ---------------------------------------------------------------------
280  * Local variables:
281  * mode: c
282  * indent-tabs-mode: t
283  * c-basic-offset: 4
284  * tab-width: 4
285  * End:
286  */