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