blob: 0c8c3c925b2b45bc88ef5791c4c7efcb2eb2303b [file] [log] [blame]
Elliott Hughes1698d142010-01-22 18:22:53 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes757a7942010-04-16 14:14:28 -070017#define LOG_TAG "NativeBreakIterator"
18
Elliott Hughes3aac4dd2013-02-04 15:37:52 -080019#include "IcuUtilities.h"
Elliott Hughes1698d142010-01-22 18:22:53 -080020#include "JNIHelp.h"
Elliott Hughese22935d2010-08-12 17:27:27 -070021#include "JniConstants.h"
Elliott Hughesbef9ec32011-04-19 10:26:58 -070022#include "JniException.h"
Elliott Hughesbcf7c662010-03-26 11:30:00 -070023#include "ScopedUtfChars.h"
Elliott Hughes01584362012-10-08 14:22:51 -070024#include "unicode/brkiter.h"
Elliott Hughes1698d142010-01-22 18:22:53 -080025#include "unicode/putil.h"
26#include <stdlib.h>
27
Elliott Hughes01584362012-10-08 14:22:51 -070028// ICU documentation: http://icu-project.org/apiref/icu4c/classBreakIterator.html
29
Elliott Hughes1e5d7302013-04-22 17:01:52 -070030static BreakIterator* toBreakIterator(jlong address) {
Elliott Hughes01584362012-10-08 14:22:51 -070031 return reinterpret_cast<BreakIterator*>(static_cast<uintptr_t>(address));
32}
33
Elliott Hughesaa96cbd2011-04-22 16:03:49 -070034/**
Elliott Hughes01584362012-10-08 14:22:51 -070035 * We use ICU4C's BreakIterator class, but our input is on the Java heap and potentially moving
36 * around between calls. This wrapper class ensures that our RegexMatcher is always pointing at
37 * the current location of the char[]. Earlier versions of Android simply copied the data to the
38 * native heap, but that's wasteful and hides allocations from the garbage collector.
Elliott Hughesaa96cbd2011-04-22 16:03:49 -070039 */
Elliott Hughes01584362012-10-08 14:22:51 -070040class BreakIteratorAccessor {
41 public:
Elliott Hughes1e5d7302013-04-22 17:01:52 -070042 BreakIteratorAccessor(JNIEnv* env, jlong address, jstring javaInput, bool reset) {
Elliott Hughes01584362012-10-08 14:22:51 -070043 init(env, address);
44 mJavaInput = javaInput;
45
46 if (mJavaInput == NULL) {
47 return;
Elliott Hughesaa96cbd2011-04-22 16:03:49 -070048 }
49
Elliott Hughes01584362012-10-08 14:22:51 -070050 mChars = env->GetStringChars(mJavaInput, NULL);
51 if (mChars == NULL) {
52 return;
Elliott Hughesaa96cbd2011-04-22 16:03:49 -070053 }
54
Elliott Hughes01584362012-10-08 14:22:51 -070055 mUText = utext_openUChars(NULL, mChars, env->GetStringLength(mJavaInput), &mStatus);
56 if (mUText == NULL) {
57 return;
Elliott Hughesaa96cbd2011-04-22 16:03:49 -070058 }
59
Elliott Hughes01584362012-10-08 14:22:51 -070060 if (reset) {
61 mBreakIterator->setText(mUText, mStatus);
62 } else {
63 mBreakIterator->refreshInputText(mUText, mStatus);
Elliott Hughesaa96cbd2011-04-22 16:03:49 -070064 }
Elliott Hughes01584362012-10-08 14:22:51 -070065 }
Elliott Hughesaa96cbd2011-04-22 16:03:49 -070066
Elliott Hughes1e5d7302013-04-22 17:01:52 -070067 BreakIteratorAccessor(JNIEnv* env, jlong address) {
Elliott Hughes01584362012-10-08 14:22:51 -070068 init(env, address);
69 }
70
71 ~BreakIteratorAccessor() {
72 utext_close(mUText);
73 if (mJavaInput) {
74 mEnv->ReleaseStringChars(mJavaInput, mChars);
Elliott Hughesaa96cbd2011-04-22 16:03:49 -070075 }
Elliott Hughes01584362012-10-08 14:22:51 -070076 maybeThrowIcuException(mEnv, "utext_close", mStatus);
77 }
Elliott Hughesaa96cbd2011-04-22 16:03:49 -070078
Elliott Hughes01584362012-10-08 14:22:51 -070079 BreakIterator* operator->() {
80 return mBreakIterator;
81 }
Elliott Hughesaa96cbd2011-04-22 16:03:49 -070082
Elliott Hughes01584362012-10-08 14:22:51 -070083 UErrorCode& status() {
84 return mStatus;
85 }
Elliott Hughesaa96cbd2011-04-22 16:03:49 -070086
Elliott Hughes01584362012-10-08 14:22:51 -070087 private:
Elliott Hughes1e5d7302013-04-22 17:01:52 -070088 void init(JNIEnv* env, jlong address) {
Elliott Hughes01584362012-10-08 14:22:51 -070089 mEnv = env;
90 mJavaInput = NULL;
91 mBreakIterator = toBreakIterator(address);
92 mChars = NULL;
93 mStatus = U_ZERO_ERROR;
94 mUText = NULL;
95 }
Elliott Hughesaa96cbd2011-04-22 16:03:49 -070096
Elliott Hughes01584362012-10-08 14:22:51 -070097 JNIEnv* mEnv;
98 jstring mJavaInput;
99 BreakIterator* mBreakIterator;
100 const jchar* mChars;
101 UErrorCode mStatus;
102 UText* mUText;
Elliott Hughesaa96cbd2011-04-22 16:03:49 -0700103
Elliott Hughes01584362012-10-08 14:22:51 -0700104 // Disallow copy and assignment.
105 BreakIteratorAccessor(const BreakIteratorAccessor&);
106 void operator=(const BreakIteratorAccessor&);
Elliott Hughesaa96cbd2011-04-22 16:03:49 -0700107};
108
Elliott Hughes01584362012-10-08 14:22:51 -0700109#define MAKE_BREAK_ITERATOR_INSTANCE(F) \
110 UErrorCode status = U_ZERO_ERROR; \
111 const ScopedUtfChars localeChars(env, javaLocale); \
112 if (localeChars.c_str() == NULL) { \
113 return 0; \
114 } \
115 Locale locale(Locale::createFromName(localeChars.c_str())); \
116 BreakIterator* it = F(locale, status); \
117 if (maybeThrowIcuException(env, "ubrk_open", status)) { \
118 return 0; \
119 } \
120 return reinterpret_cast<uintptr_t>(it)
Elliott Hughes1698d142010-01-22 18:22:53 -0800121
Elliott Hughesb7176442014-02-21 09:58:17 -0800122static jlong NativeBreakIterator_cloneImpl(JNIEnv* env, jclass, jlong address) {
Elliott Hughes01584362012-10-08 14:22:51 -0700123 BreakIteratorAccessor it(env, address);
124 return reinterpret_cast<uintptr_t>(it->clone());
Elliott Hughes1698d142010-01-22 18:22:53 -0800125}
126
Elliott Hughes1e5d7302013-04-22 17:01:52 -0700127static void NativeBreakIterator_closeImpl(JNIEnv*, jclass, jlong address) {
Elliott Hughes01584362012-10-08 14:22:51 -0700128 delete toBreakIterator(address);
Elliott Hughes1698d142010-01-22 18:22:53 -0800129}
130
Elliott Hughes1e5d7302013-04-22 17:01:52 -0700131static jint NativeBreakIterator_currentImpl(JNIEnv* env, jclass, jlong address, jstring javaInput) {
Elliott Hughes01584362012-10-08 14:22:51 -0700132 BreakIteratorAccessor it(env, address, javaInput, false);
133 return it->current();
Elliott Hughes1698d142010-01-22 18:22:53 -0800134}
135
Elliott Hughes1e5d7302013-04-22 17:01:52 -0700136static jint NativeBreakIterator_firstImpl(JNIEnv* env, jclass, jlong address, jstring javaInput) {
Elliott Hughes01584362012-10-08 14:22:51 -0700137 BreakIteratorAccessor it(env, address, javaInput, false);
138 return it->first();
139}
140
Elliott Hughes1e5d7302013-04-22 17:01:52 -0700141static jint NativeBreakIterator_followingImpl(JNIEnv* env, jclass, jlong address, jstring javaInput, jint offset) {
Elliott Hughes01584362012-10-08 14:22:51 -0700142 BreakIteratorAccessor it(env, address, javaInput, false);
143 return it->following(offset);
144}
145
Elliott Hughes9adba442014-02-03 17:08:19 -0800146static jlong NativeBreakIterator_getCharacterInstanceImpl(JNIEnv* env, jclass, jstring javaLocale) {
Elliott Hughes01584362012-10-08 14:22:51 -0700147 MAKE_BREAK_ITERATOR_INSTANCE(BreakIterator::createCharacterInstance);
148}
149
Elliott Hughes9adba442014-02-03 17:08:19 -0800150static jlong NativeBreakIterator_getLineInstanceImpl(JNIEnv* env, jclass, jstring javaLocale) {
Elliott Hughes01584362012-10-08 14:22:51 -0700151 MAKE_BREAK_ITERATOR_INSTANCE(BreakIterator::createLineInstance);
152}
153
Elliott Hughes9adba442014-02-03 17:08:19 -0800154static jlong NativeBreakIterator_getSentenceInstanceImpl(JNIEnv* env, jclass, jstring javaLocale) {
Elliott Hughes01584362012-10-08 14:22:51 -0700155 MAKE_BREAK_ITERATOR_INSTANCE(BreakIterator::createSentenceInstance);
156}
157
Elliott Hughes9adba442014-02-03 17:08:19 -0800158static jlong NativeBreakIterator_getWordInstanceImpl(JNIEnv* env, jclass, jstring javaLocale) {
Elliott Hughes01584362012-10-08 14:22:51 -0700159 MAKE_BREAK_ITERATOR_INSTANCE(BreakIterator::createWordInstance);
160}
161
Elliott Hughes1e5d7302013-04-22 17:01:52 -0700162static jboolean NativeBreakIterator_isBoundaryImpl(JNIEnv* env, jclass, jlong address, jstring javaInput, jint offset) {
Elliott Hughes01584362012-10-08 14:22:51 -0700163 BreakIteratorAccessor it(env, address, javaInput, false);
164 return it->isBoundary(offset);
165}
166
Elliott Hughes1e5d7302013-04-22 17:01:52 -0700167static jint NativeBreakIterator_lastImpl(JNIEnv* env, jclass, jlong address, jstring javaInput) {
Elliott Hughes01584362012-10-08 14:22:51 -0700168 BreakIteratorAccessor it(env, address, javaInput, false);
169 return it->last();
170}
171
Elliott Hughes1e5d7302013-04-22 17:01:52 -0700172static jint NativeBreakIterator_nextImpl(JNIEnv* env, jclass, jlong address, jstring javaInput, jint n) {
Elliott Hughes01584362012-10-08 14:22:51 -0700173 BreakIteratorAccessor it(env, address, javaInput, false);
174 if (n < 0) {
175 while (n++ < -1) {
176 it->previous();
Elliott Hughes1698d142010-01-22 18:22:53 -0800177 }
Elliott Hughes01584362012-10-08 14:22:51 -0700178 return it->previous();
179 } else if (n == 0) {
180 return it->current();
181 } else {
182 while (n-- > 1) {
183 it->next();
184 }
185 return it->next();
186 }
187 return -1;
Elliott Hughes1698d142010-01-22 18:22:53 -0800188}
189
Elliott Hughes1e5d7302013-04-22 17:01:52 -0700190static jint NativeBreakIterator_precedingImpl(JNIEnv* env, jclass, jlong address, jstring javaInput, jint offset) {
Elliott Hughes01584362012-10-08 14:22:51 -0700191 BreakIteratorAccessor it(env, address, javaInput, false);
192 return it->preceding(offset);
Elliott Hughes1698d142010-01-22 18:22:53 -0800193}
194
Elliott Hughes1e5d7302013-04-22 17:01:52 -0700195static jint NativeBreakIterator_previousImpl(JNIEnv* env, jclass, jlong address, jstring javaInput) {
Elliott Hughes01584362012-10-08 14:22:51 -0700196 BreakIteratorAccessor it(env, address, javaInput, false);
197 return it->previous();
Elliott Hughes1698d142010-01-22 18:22:53 -0800198}
199
Elliott Hughes1e5d7302013-04-22 17:01:52 -0700200static void NativeBreakIterator_setTextImpl(JNIEnv* env, jclass, jlong address, jstring javaInput) {
Elliott Hughes01584362012-10-08 14:22:51 -0700201 BreakIteratorAccessor it(env, address, javaInput, true);
Elliott Hughes1698d142010-01-22 18:22:53 -0800202}
203
204static JNINativeMethod gMethods[] = {
Elliott Hughes1e5d7302013-04-22 17:01:52 -0700205 NATIVE_METHOD(NativeBreakIterator, cloneImpl, "(J)J"),
206 NATIVE_METHOD(NativeBreakIterator, closeImpl, "(J)V"),
207 NATIVE_METHOD(NativeBreakIterator, currentImpl, "(JLjava/lang/String;)I"),
208 NATIVE_METHOD(NativeBreakIterator, firstImpl, "(JLjava/lang/String;)I"),
209 NATIVE_METHOD(NativeBreakIterator, followingImpl, "(JLjava/lang/String;I)I"),
210 NATIVE_METHOD(NativeBreakIterator, getCharacterInstanceImpl, "(Ljava/lang/String;)J"),
211 NATIVE_METHOD(NativeBreakIterator, getLineInstanceImpl, "(Ljava/lang/String;)J"),
212 NATIVE_METHOD(NativeBreakIterator, getSentenceInstanceImpl, "(Ljava/lang/String;)J"),
213 NATIVE_METHOD(NativeBreakIterator, getWordInstanceImpl, "(Ljava/lang/String;)J"),
214 NATIVE_METHOD(NativeBreakIterator, isBoundaryImpl, "(JLjava/lang/String;I)Z"),
215 NATIVE_METHOD(NativeBreakIterator, lastImpl, "(JLjava/lang/String;)I"),
216 NATIVE_METHOD(NativeBreakIterator, nextImpl, "(JLjava/lang/String;I)I"),
217 NATIVE_METHOD(NativeBreakIterator, precedingImpl, "(JLjava/lang/String;I)I"),
218 NATIVE_METHOD(NativeBreakIterator, previousImpl, "(JLjava/lang/String;)I"),
219 NATIVE_METHOD(NativeBreakIterator, setTextImpl, "(JLjava/lang/String;)V"),
Elliott Hughes1698d142010-01-22 18:22:53 -0800220};
Elliott Hughes7cd67602012-05-03 17:21:04 -0700221void register_libcore_icu_NativeBreakIterator(JNIEnv* env) {
Elliott Hughes01584362012-10-08 14:22:51 -0700222 jniRegisterNativeMethods(env, "libcore/icu/NativeBreakIterator", gMethods, NELEM(gMethods));
Elliott Hughes1698d142010-01-22 18:22:53 -0800223}