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