e0cb3517b9c26585f815a14ec6c70d53aadd998d
[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 #if defined(ENABLE_JNI_HEADERS)
37 # include "native/include/java_lang_String.h"
38 #endif
39
40 #include "vm/javaobjects.hpp"
41 #include "vm/string.hpp"
42
43
44 // Native functions are exported as C functions.
45 extern "C" {
46
47 /*
48  * Class:     java/lang/String
49  * Method:    hashCode
50  * Signature: ()I
51  */
52 JNIEXPORT jint JNICALL Java_java_lang_String_hashCode(JNIEnv *env, jstring _this)
53 {
54         java_lang_String jls(_this);
55
56         java_handle_chararray_t* value = jls.get_value();
57         int32_t offset = jls.get_offset();
58         int32_t count  = jls.get_count();
59
60         int32_t hash = 0;
61
62         for (int32_t i = 0; i < count; i++) {
63                 hash = (31 * hash) + LLNI_array_direct(value, offset + i);
64         }
65
66         return hash;
67 }
68
69
70 /*
71  * Class:     java/lang/String
72  * Method:    indexOf
73  * Signature: (I)I
74  */
75 JNIEXPORT jint JNICALL Java_java_lang_String_indexOf__I(JNIEnv *env, jstring _this, jint ch)
76 {
77         java_lang_String jls(_this);
78
79         java_handle_chararray_t* value = jls.get_value();
80         int32_t offset = jls.get_offset();
81         int32_t count  = jls.get_count();
82
83         for (int32_t i = 0; i < count; i++) {
84                 if (LLNI_array_direct(value, offset + i) == ch) {
85                         return i;
86                 }
87         }
88
89         return -1;
90 }
91
92
93 /*
94  * Class:     java/lang/String
95  * Method:    indexOf
96  * Signature: (II)I
97  */
98 JNIEXPORT jint JNICALL Java_java_lang_String_indexOf__II(JNIEnv *env, jstring _this, jint ch, jint fromIndex)
99 {
100         java_lang_String jls(_this);
101
102         java_handle_chararray_t* value = jls.get_value();
103         int32_t offset = jls.get_offset();
104         int32_t count  = jls.get_count();
105
106         if (fromIndex < 0) {
107                 fromIndex = 0;
108         }
109         else if (fromIndex >= count) {
110                 // Note: fromIndex might be near -1>>>1.
111                 return -1;
112         }
113
114         for (int32_t i = fromIndex ; i < count ; i++) {
115                 if (LLNI_array_direct(value, offset + i) == ch) {
116                         return i;
117                 }
118         }
119
120         return -1;
121 }
122
123
124 /*
125  * Class:     java/lang/String
126  * Method:    lastIndexOf
127  * Signature: (II)I
128  */
129 JNIEXPORT jint JNICALL Java_java_lang_String_lastIndexOf__II(JNIEnv *env, jstring _this, jint ch, jint fromIndex)
130 {
131         java_lang_String jls(_this);
132
133         java_handle_chararray_t* value = jls.get_value();
134         int32_t offset = jls.get_offset();
135         int32_t count  = jls.get_count();
136
137         int32_t start = ((fromIndex >= count) ? count - 1 : fromIndex);
138
139         for (int32_t i = start; i >= 0; i--) {
140                 if (LLNI_array_direct(value, offset + i) == ch) {
141                         return i;
142                 }
143         }
144
145         return -1;
146 }
147
148
149 /*
150  * Class:     java/lang/String
151  * Method:    lastIndexOf
152  * Signature: (I)I
153  */
154 JNIEXPORT jint JNICALL Java_java_lang_String_lastIndexOf__I(JNIEnv *env, jstring _this, jint ch)
155 {
156         java_lang_String jls(_this);
157         
158         return Java_java_lang_String_lastIndexOf__II(env, _this, ch, jls.get_count() - 1);
159 }
160
161
162 /*
163  * Class:     java/lang/String
164  * Method:    intern
165  * Signature: ()Ljava/lang/String;
166  */
167 JNIEXPORT jstring JNICALL Java_java_lang_String_intern(JNIEnv *env, jstring _this)
168 {
169         java_lang_String jls(_this);
170
171         if (jls.is_null())
172                 return NULL;
173
174         return (jstring) javastring_intern(jls.get_handle());
175 }
176
177 } // extern "C"
178
179
180 /* native methods implemented by this file ************************************/
181  
182 static JNINativeMethod methods[] = {
183         { (char*) "hashCode",    (char*) "()I",                    (void*) (uintptr_t) &Java_java_lang_String_hashCode        },
184         { (char*) "indexOf",     (char*) "(I)I",                   (void*) (uintptr_t) &Java_java_lang_String_indexOf__I      },
185         { (char*) "indexOf",     (char*) "(II)I",                  (void*) (uintptr_t) &Java_java_lang_String_indexOf__II     },
186         { (char*) "lastIndexOf", (char*) "(II)I",                  (void*) (uintptr_t) &Java_java_lang_String_lastIndexOf__II },
187         { (char*) "lastIndexOf", (char*) "(I)I",                   (void*) (uintptr_t) &Java_java_lang_String_lastIndexOf__I  },
188 #if 0
189         { (char*) "equals",      (char*) "(Ljava/lang/Object;)Z;", (void*) (uintptr_t) &Java_java_lang_String_equals          },
190 #endif
191         { (char*) "intern",      (char*) "()Ljava/lang/String;",   (void*) (uintptr_t) &Java_java_lang_String_intern          },
192 };
193
194
195 /* _Jv_java_lang_String_init ***************************************************
196  
197    Register native functions.
198  
199 *******************************************************************************/
200  
201 // FIXME
202 extern "C" {
203 void _Jv_java_lang_String_init(void)
204 {
205         utf *u;
206  
207         u = utf_new_char("java/lang/String");
208  
209         native_method_register(u, methods, NATIVE_METHODS_COUNT);
210 }
211 }
212
213
214 /*
215  * These are local overrides for various environment variables in Emacs.
216  * Please do not remove this and leave it at the end of the file, where
217  * Emacs will automagically detect them.
218  * ---------------------------------------------------------------------
219  * Local variables:
220  * mode: c++
221  * indent-tabs-mode: t
222  * c-basic-offset: 4
223  * tab-width: 4
224  * End:
225  */