CACAO
java_lang_String.cpp
Go to the documentation of this file.
1 /* src/native/vm/cldc1.1/java_lang_String.cpp
2 
3  Copyright (C) 2006-2013
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/array.hpp"
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  CharArray value(jls.get_value());
57 
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) + value.get_element(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  CharArray value(jls.get_value());
81 
82  int32_t offset = jls.get_offset();
83  int32_t count = jls.get_count();
84 
85  for (int32_t i = 0; i < count; i++) {
86  if (value.get_element(offset + i) == ch) {
87  return i;
88  }
89  }
90 
91  return -1;
92 }
93 
94 
95 /*
96  * Class: java/lang/String
97  * Method: indexOf
98  * Signature: (II)I
99  */
100 JNIEXPORT jint JNICALL Java_java_lang_String_indexOf__II(JNIEnv *env, jstring _this, jint ch, jint fromIndex)
101 {
102  java_lang_String jls(_this);
103 
104  CharArray value(jls.get_value());
105 
106  int32_t offset = jls.get_offset();
107  int32_t count = jls.get_count();
108 
109  if (fromIndex < 0) {
110  fromIndex = 0;
111  }
112  else if (fromIndex >= count) {
113  // Note: fromIndex might be near -1>>>1.
114  return -1;
115  }
116 
117  for (int32_t i = fromIndex ; i < count ; i++) {
118  if (value.get_element(offset + i) == ch) {
119  return i;
120  }
121  }
122 
123  return -1;
124 }
125 
126 
127 /*
128  * Class: java/lang/String
129  * Method: lastIndexOf
130  * Signature: (II)I
131  */
132 JNIEXPORT jint JNICALL Java_java_lang_String_lastIndexOf__II(JNIEnv *env, jstring _this, jint ch, jint fromIndex)
133 {
134  java_lang_String jls(_this);
135 
136  CharArray value(jls.get_value());
137 
138  int32_t offset = jls.get_offset();
139  int32_t count = jls.get_count();
140 
141  int32_t start = ((fromIndex >= count) ? count - 1 : fromIndex);
142 
143  for (int32_t i = start; i >= 0; i--) {
144  if (value.get_element(offset + i) == ch) {
145  return i;
146  }
147  }
148 
149  return -1;
150 }
151 
152 
153 /*
154  * Class: java/lang/String
155  * Method: lastIndexOf
156  * Signature: (I)I
157  */
158 JNIEXPORT jint JNICALL Java_java_lang_String_lastIndexOf__I(JNIEnv *env, jstring _this, jint ch)
159 {
160  java_lang_String jls(_this);
161 
162  return Java_java_lang_String_lastIndexOf__II(env, _this, ch, jls.get_count() - 1);
163 }
164 
165 
166 /*
167  * Class: java/lang/String
168  * Method: intern
169  * Signature: ()Ljava/lang/String;
170  */
171 JNIEXPORT jstring JNICALL Java_java_lang_String_intern(JNIEnv *env, jstring _this)
172 {
173  java_lang_String jls(_this);
174 
175  if (jls.is_null())
176  return NULL;
177 
178  return (jstring) JavaString(jls.get_handle()).intern();
179 }
180 
181 } // extern "C"
182 
183 
184 /* native methods implemented by this file ************************************/
185 
186 static JNINativeMethod methods[] = {
187  { (char*) "hashCode", (char*) "()I", (void*) (uintptr_t) &Java_java_lang_String_hashCode },
188  { (char*) "indexOf", (char*) "(I)I", (void*) (uintptr_t) &Java_java_lang_String_indexOf__I },
189  { (char*) "indexOf", (char*) "(II)I", (void*) (uintptr_t) &Java_java_lang_String_indexOf__II },
190  { (char*) "lastIndexOf", (char*) "(II)I", (void*) (uintptr_t) &Java_java_lang_String_lastIndexOf__II },
191  { (char*) "lastIndexOf", (char*) "(I)I", (void*) (uintptr_t) &Java_java_lang_String_lastIndexOf__I },
192 #if 0
193  { (char*) "equals", (char*) "(Ljava/lang/Object;)Z;", (void*) (uintptr_t) &Java_java_lang_String_equals },
194 #endif
195  { (char*) "intern", (char*) "()Ljava/lang/String;", (void*) (uintptr_t) &Java_java_lang_String_intern },
196 };
197 
198 
199 /* _Jv_java_lang_String_init ***************************************************
200 
201  Register native functions.
202 
203 *******************************************************************************/
204 
206 {
207  Utf8String u = Utf8String::from_utf8("java/lang/String");
208 
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  */
#define hash(_i1, _i2)
Definition: peephole.c:55
virtual java_handle_t * get_handle() const
Table containing all native methods registered with the VM.
Definition: native.hpp:132
NativeMethods & get_nativemethods()
Definition: vm.hpp:128
bool is_null() const
void register_methods(Utf8String classname, const JNINativeMethod *methods, size_t count)
Register native methods with the VM.
Definition: native.cpp:242
JNIEXPORT jint JNICALL Java_java_lang_String_hashCode(JNIEnv *env, jstring _this)
_Jv_JNIEnv JNIEnv
Definition: jni.hpp:112
JNIEXPORT jint JNICALL Java_java_lang_String_indexOf__II(JNIEnv *env, jstring _this, jint ch, jint fromIndex)
#define NATIVE_METHODS_COUNT
Definition: native.hpp:45
java_handle_chararray_t * get_value() const
JNIEXPORT jint JNICALL Java_java_lang_String_lastIndexOf__I(JNIEnv *env, jstring _this, jint ch)
JNIEXPORT jstring JNICALL Java_java_lang_String_intern(JNIEnv *env, jstring _this)
static Utf8String from_utf8(const char *, size_t)
Definition: utf8.cpp:335
MIIterator i
JNIEXPORT jint JNICALL Java_java_lang_String_lastIndexOf__II(JNIEnv *env, jstring _this, jint ch, jint fromIndex)
void _Jv_java_lang_String_init(void)
GNU Classpath java/lang/String.
static JNINativeMethod methods[]
int32_t get_offset() const
JNIEXPORT jint JNICALL Java_java_lang_String_indexOf__I(JNIEnv *env, jstring _this, jint ch)
int32_t get_count() const
static VM * get_current()
Definition: vm.hpp:99