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