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