Merged revisions 7797-7917 via svnmerge from
[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/native.h"
39
40 #include "native/include/java_lang_Object.h"
41
42 #include "native/include/java_lang_String.h"
43
44 #include "vm/stringlocal.h"
45
46
47 /* native methods implemented by this file ************************************/
48  
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 },
55 #if 0
56         { "equals",      "(Ljava/lang/Object;)Z;", (void *) (ptrint) &Java_java_lang_String_equals          },
57 #endif
58         { "intern",      "()Ljava/lang/String;",   (void *) (ptrint) &Java_java_lang_String_intern          },
59 };
60
61
62 /* _Jv_java_lang_String_init ***************************************************
63  
64    Register native functions.
65  
66 *******************************************************************************/
67  
68 void _Jv_java_lang_String_init(void)
69 {
70         utf *u;
71  
72         u = utf_new_char("java/lang/String");
73  
74         native_method_register(u, methods, NATIVE_METHODS_COUNT);
75 }
76
77
78 /*
79  * Class:     java/lang/String
80  * Method:    hashCode
81  * Signature: ()I
82  */
83 JNIEXPORT s4 JNICALL Java_java_lang_String_hashCode(JNIEnv *env, java_lang_String *this)
84 {
85         java_chararray *value;
86         s4              offset;
87         s4              count;
88         s4              hash;
89         s4              i;
90
91         /* get values from Java object */
92
93         offset = this->offset;
94         count  = this->count;
95         value  = this->value;
96
97         hash = 0;
98
99         for (i = 0; i < count; i++) {
100                 hash = (31 * hash) + value->data[offset + i];
101         }
102
103         return hash;
104 }
105
106
107 /*
108  * Class:     java/lang/String
109  * Method:    indexOf
110  * Signature: (I)I
111  */
112 JNIEXPORT s4 JNICALL Java_java_lang_String_indexOf__I(JNIEnv *env, java_lang_String *this, s4 ch)
113 {
114         java_chararray *value;
115         s4              offset;
116         s4              count;
117         s4              i;
118
119         /* get values from Java object */
120
121         offset = this->offset;
122         count  = this->count;
123         value  = this->value;
124
125         for (i = 0; i < count; i++) {
126                 if (value->data[offset + i] == ch) {
127                         return i;
128                 }
129         }
130
131         return -1;
132 }
133
134
135 /*
136  * Class:     java/lang/String
137  * Method:    indexOf
138  * Signature: (II)I
139  */
140 JNIEXPORT s4 JNICALL Java_java_lang_String_indexOf__II(JNIEnv *env, java_lang_String *this, s4 ch, s4 fromIndex)
141 {
142         java_chararray *value;
143         s4              offset;
144         s4              count;
145         s4              i;
146
147         /* get values from Java object */
148
149         offset = this->offset;
150         count  = this->count;
151         value  = this->value;
152
153         if (fromIndex < 0) {
154                 fromIndex = 0;
155         }
156         else if (fromIndex >= count) {
157                 /* Note: fromIndex might be near -1>>>1. */
158                 return -1;
159         }
160
161         for (i = fromIndex ; i < count ; i++) {
162                 if (value->data[offset + i] == ch) {
163                         return i;
164                 }
165         }
166
167         return -1;
168 }
169
170
171 /*
172  * Class:     java/lang/String
173  * Method:    lastIndexOf
174  * Signature: (I)I
175  */
176 JNIEXPORT s4 JNICALL Java_java_lang_String_lastIndexOf__I(JNIEnv *env, java_lang_String *this, s4 ch)
177 {
178         return Java_java_lang_String_lastIndexOf__II(env, this, ch, this->count - 1);
179 }
180
181
182 /*
183  * Class:     java/lang/String
184  * Method:    lastIndexOf
185  * Signature: (II)I
186  */
187 JNIEXPORT s4 JNICALL Java_java_lang_String_lastIndexOf__II(JNIEnv *env, java_lang_String *this, s4 ch, s4 fromIndex)
188 {
189         java_chararray *value;
190         s4              offset;
191         s4              count;
192         s4              start;
193         s4              i;
194
195         /* get values from Java object */
196
197         offset = this->offset;
198         count  = this->count;
199         value  = this->value;
200
201         start = ((fromIndex >= count) ? count - 1 : fromIndex);
202
203         for (i = start; i >= 0; i--) {
204                 if (value->data[offset + i] == ch) {
205                         return i;
206                 }
207         }
208
209         return -1;
210 }
211
212
213 #if 0
214 /*
215  * Class:     java/lang/String
216  * Method:    equals
217  * Signature: (Ljava/lang/Object;)Z;
218  */
219 JNIEXPORT s4 JNICALL Java_java_lang_String_equals(JNIEnv *env, java_lang_String* this, java_lang_Object *o)
220 {
221         java_lang_String* s;
222
223         /* TODO: is this the correct implementation for short-circuiting on object identity? */
224         if ((java_lang_Object*)this == o)
225                 return 1;
226
227         if (o->header.vftbl->class != class_java_lang_String) 
228                 return 0;
229
230         s = (java_lang_String *) o;
231
232         if (this->count != s->count)
233                 return 0;
234
235         return ( 0 == memcmp((void*)(this->value->data + this->offset),
236                                                  (void*)(s->value->data + s->offset),
237                                                  this->count) );
238 }
239 #endif
240
241
242 /*
243  * Class:     java/lang/String
244  * Method:    intern
245  * Signature: ()Ljava/lang/String;
246  */
247 JNIEXPORT java_lang_String* JNICALL Java_java_lang_String_intern(JNIEnv *env, java_lang_String *this)
248 {
249         java_objectheader *o;
250
251         if (this == NULL)
252                 return NULL;
253
254         /* search table so identical strings will get identical pointers */
255
256         o = literalstring_u2(this->value, this->count, this->offset, true);
257
258         return (java_lang_String *) o;
259 }
260
261
262 /*
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  * ---------------------------------------------------------------------
267  * Local variables:
268  * mode: c
269  * indent-tabs-mode: t
270  * c-basic-offset: 4
271  * tab-width: 4
272  * End:
273  */