* src/native/vm/cldc1.1/com_sun_cldc_io_ResourceInputStream.c,
[cacao.git] / src / native / vm / cldc1.1 / java_lang_String.cpp
1 /* src/native/vm/cldc1.1/java_lang_String.cpp
2
3    Copyright (C) 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "native/jni.h"
33 #include "native/llni.h"
34 #include "native/native.h"
35
36 #include "native/include/java_lang_Object.h"
37
38 // FIXME
39 extern "C" {
40 #include "native/include/java_lang_String.h"
41 }
42
43 #include "vm/string.hpp"
44
45
46 /* native methods implemented by this file ************************************/
47  
48 static JNINativeMethod methods[] = {
49         { (char*) "hashCode",    (char*) "()I",                    (void*) (uintptr_t) &Java_java_lang_String_hashCode        },
50         { (char*) "indexOf",     (char*) "(I)I",                   (void*) (uintptr_t) &Java_java_lang_String_indexOf__I      },
51         { (char*) "indexOf",     (char*) "(II)I",                  (void*) (uintptr_t) &Java_java_lang_String_indexOf__II     },
52         { (char*) "lastIndexOf", (char*) "(I)I",                   (void*) (uintptr_t) &Java_java_lang_String_lastIndexOf__I  },
53         { (char*) "lastIndexOf", (char*) "(II)I",                  (void*) (uintptr_t) &Java_java_lang_String_lastIndexOf__II },
54 #if 0
55         { (char*) "equals",      (char*) "(Ljava/lang/Object;)Z;", (void*) (uintptr_t) &Java_java_lang_String_equals          },
56 #endif
57         { (char*) "intern",      (char*) "()Ljava/lang/String;",   (void*) (uintptr_t) &Java_java_lang_String_intern          },
58 };
59
60
61 /* _Jv_java_lang_String_init ***************************************************
62  
63    Register native functions.
64  
65 *******************************************************************************/
66  
67 // FIXME
68 extern "C" {
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 // Native functions are exported as C functions.
81 extern "C" {
82
83 /*
84  * Class:     java/lang/String
85  * Method:    hashCode
86  * Signature: ()I
87  */
88 JNIEXPORT s4 JNICALL Java_java_lang_String_hashCode(JNIEnv *env, java_lang_String *_this)
89 {
90         java_handle_chararray_t *value;
91         int32_t                 offset;
92         int32_t                 count;
93         s4                              hash;
94         s4                              i;
95
96         /* get values from Java object */
97         
98         LLNI_field_get_val(_this, offset, offset);
99         LLNI_field_get_val(_this, count, count);
100         LLNI_field_get_ref(_this, value, value);
101
102         hash = 0;
103
104         for (i = 0; i < count; i++) {
105                 hash = (31 * hash) + LLNI_array_direct(value, offset + i);
106         }
107
108         return hash;
109 }
110
111
112 /*
113  * Class:     java/lang/String
114  * Method:    indexOf
115  * Signature: (I)I
116  */
117 JNIEXPORT s4 JNICALL Java_java_lang_String_indexOf__I(JNIEnv *env, java_lang_String *_this, s4 ch)
118 {
119         java_handle_chararray_t *value;
120         int32_t                 offset;
121         int32_t                 count;
122         s4                              i;
123
124         /* get values from Java object */
125
126         LLNI_field_get_val(_this, offset, offset);
127         LLNI_field_get_val(_this, count, count);
128         LLNI_field_get_ref(_this, value, value);
129
130         for (i = 0; i < count; i++) {
131                 if (LLNI_array_direct(value, offset + i) == ch) {
132                         return i;
133                 }
134         }
135
136         return -1;
137 }
138
139
140 /*
141  * Class:     java/lang/String
142  * Method:    indexOf
143  * Signature: (II)I
144  */
145 JNIEXPORT s4 JNICALL Java_java_lang_String_indexOf__II(JNIEnv *env, java_lang_String *_this, s4 ch, s4 fromIndex)
146 {
147         java_handle_chararray_t *value;
148         int32_t                 offset;
149         int32_t                 count;
150         s4                              i;
151
152         /* get values from Java object */
153
154         LLNI_field_get_val(_this, offset, offset);
155         LLNI_field_get_val(_this, count, count);
156         LLNI_field_get_ref(_this, value, value);
157
158         if (fromIndex < 0) {
159                 fromIndex = 0;
160         }
161         else if (fromIndex >= count) {
162                 /* Note: fromIndex might be near -1>>>1. */
163                 return -1;
164         }
165
166         for (i = fromIndex ; i < count ; i++) {
167                 if (LLNI_array_direct(value, offset + i) == ch) {
168                         return i;
169                 }
170         }
171
172         return -1;
173 }
174
175
176 /*
177  * Class:     java/lang/String
178  * Method:    lastIndexOf
179  * Signature: (I)I
180  */
181 JNIEXPORT s4 JNICALL Java_java_lang_String_lastIndexOf__I(JNIEnv *env, java_lang_String *_this, s4 ch)
182 {
183         int32_t count;
184         
185         LLNI_field_get_val(_this, count, count);
186         
187         return Java_java_lang_String_lastIndexOf__II(env, _this, ch, count - 1);
188 }
189
190
191 /*
192  * Class:     java/lang/String
193  * Method:    lastIndexOf
194  * Signature: (II)I
195  */
196 JNIEXPORT s4 JNICALL Java_java_lang_String_lastIndexOf__II(JNIEnv *env, java_lang_String *_this, s4 ch, s4 fromIndex)
197 {
198         java_handle_chararray_t *value;
199         int32_t                 offset;
200         int32_t                 count;
201         s4                              start;
202         s4                              i;
203
204         /* get values from Java object */
205
206         LLNI_field_get_val(_this, offset, offset);
207         LLNI_field_get_val(_this, count, count);
208         LLNI_field_get_ref(_this, value, value);
209
210         start = ((fromIndex >= count) ? count - 1 : fromIndex);
211
212         for (i = start; i >= 0; i--) {
213                 if (LLNI_array_direct(value, offset + i) == ch) {
214                         return i;
215                 }
216         }
217
218         return -1;
219 }
220
221
222 #if 0
223 /*
224  * Class:     java/lang/String
225  * Method:    equals
226  * Signature: (Ljava/lang/Object;)Z;
227  */
228 JNIEXPORT s4 JNICALL Java_java_lang_String_equals(JNIEnv *env, java_lang_String* _this, java_lang_Object *o)
229 {
230         java_lang_String*               s;
231         java_handle_chararray_t *value;
232         int32_t                 offset;
233         int32_t                 count;
234         java_handle_chararray_t *dvalue;
235         int32_t                 doffset;
236         int32_t                 dcount;
237         classinfo                       *c;
238         
239         LLNI_field_get_val(_this, offset, offset);
240         LLNI_field_get_val(_this, count, count);
241         LLNI_field_get_ref(_this, value, value);
242         LLNI_class_get(o, c);
243
244         /* TODO: is this the correct implementation for short-circuiting on object identity? */
245         if ((java_lang_Object*)_this == o)
246                 return 1;
247         
248         if (c != class_java_lang_String) 
249                 return 0;
250
251         s = (java_lang_String *) o;
252         LLNI_field_get_val(_this, offset, doffset);
253         LLNI_field_get_val(_this, count, dcount);
254         LLNI_field_get_ref(_this, value, dvalue);
255
256         if (count != dcount)
257                 return 0;
258
259         return ( 0 == memcmp((void*)(LLNI_array_direct(value, offset)),
260                                                  (void*)(LLNI_array_direct(dvalue, doffset),
261                                                  count) );
262
263 }
264 #endif
265
266
267 /*
268  * Class:     java/lang/String
269  * Method:    intern
270  * Signature: ()Ljava/lang/String;
271  */
272 JNIEXPORT java_lang_String* JNICALL Java_java_lang_String_intern(JNIEnv *env, java_lang_String *_this)
273 {
274         if (_this == NULL)
275                 return NULL;
276
277         return (java_lang_String *) javastring_intern((java_handle_t *) _this);
278 }
279
280 } // extern "C"
281
282
283 /*
284  * These are local overrides for various environment variables in Emacs.
285  * Please do not remove this and leave it at the end of the file, where
286  * Emacs will automagically detect them.
287  * ---------------------------------------------------------------------
288  * Local variables:
289  * mode: c++
290  * indent-tabs-mode: t
291  * c-basic-offset: 4
292  * tab-width: 4
293  * End:
294  */