1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "NativeBreakIterator"
#include "JNIHelp.h"
#include "JniConstants.h"
#include "ErrorCode.h"
#include "ScopedJavaUnicodeString.h"
#include "ScopedUtfChars.h"
#include "unicode/ubrk.h"
#include "unicode/putil.h"
#include <stdlib.h>
static jint getIterator(JNIEnv* env, jstring locale, UBreakIteratorType type) {
UErrorCode status = U_ZERO_ERROR;
ScopedUtfChars localeChars(env, locale);
if (localeChars.c_str() == NULL) {
return 0;
}
UBreakIterator* it = ubrk_open(type, localeChars.c_str(), NULL, 0, &status);
icu4jni_error(env, status);
return reinterpret_cast<uintptr_t>(it);
}
static jint NativeBreakIterator_getCharacterInstanceImpl(JNIEnv* env, jclass, jstring locale) {
return getIterator(env, locale, UBRK_CHARACTER);
}
static jint NativeBreakIterator_getLineInstanceImpl(JNIEnv* env, jclass, jstring locale) {
return getIterator(env, locale, UBRK_LINE);
}
static jint NativeBreakIterator_getSentenceInstanceImpl(JNIEnv* env, jclass, jstring locale) {
return getIterator(env, locale, UBRK_SENTENCE);
}
static jint NativeBreakIterator_getWordInstanceImpl(JNIEnv* env, jclass, jstring locale) {
return getIterator(env, locale, UBRK_WORD);
}
static UBreakIterator* breakIterator(jint address) {
return reinterpret_cast<UBreakIterator*>(static_cast<uintptr_t>(address));
}
static void NativeBreakIterator_closeBreakIteratorImpl(JNIEnv*, jclass, jint address) {
ubrk_close(breakIterator(address));
}
static jint NativeBreakIterator_cloneImpl(JNIEnv* env, jclass, jint address) {
UErrorCode status = U_ZERO_ERROR;
jint bufferSize = U_BRK_SAFECLONE_BUFFERSIZE;
UBreakIterator* it = ubrk_safeClone(breakIterator(address), NULL, &bufferSize, &status);
icu4jni_error(env, status);
return reinterpret_cast<uintptr_t>(it);
}
static void NativeBreakIterator_setTextImpl(JNIEnv* env, jclass, jint address, jstring javaText) {
ScopedJavaUnicodeString text(env, javaText);
UnicodeString& s(text.unicodeString());
UErrorCode status = U_ZERO_ERROR;
ubrk_setText(breakIterator(address), s.getBuffer(), s.length(), &status);
icu4jni_error(env, status);
}
static jboolean NativeBreakIterator_isBoundaryImpl(JNIEnv*, jclass, jint address, jint offset) {
return ubrk_isBoundary(breakIterator(address), offset);
}
static jint NativeBreakIterator_nextImpl(JNIEnv*, jclass, jint address, jint n) {
UBreakIterator* bi = breakIterator(address);
if (n < 0) {
while (n++ < -1) {
ubrk_previous(bi);
}
return ubrk_previous(bi);
} else if (n == 0) {
return ubrk_current(bi);
} else {
while (n-- > 1) {
ubrk_next(bi);
}
return ubrk_next(bi);
}
return -1;
}
static jint NativeBreakIterator_precedingImpl(JNIEnv*, jclass, jint address, jint offset) {
return ubrk_preceding(breakIterator(address), offset);
}
static jint NativeBreakIterator_firstImpl(JNIEnv*, jclass, jint address) {
return ubrk_first(breakIterator(address));
}
static jint NativeBreakIterator_followingImpl(JNIEnv*, jclass, jint address, jint offset) {
return ubrk_following(breakIterator(address), offset);
}
static jint NativeBreakIterator_currentImpl(JNIEnv*, jclass, jint address) {
return ubrk_current(breakIterator(address));
}
static jint NativeBreakIterator_previousImpl(JNIEnv*, jclass, jint address) {
return ubrk_previous(breakIterator(address));
}
static jint NativeBreakIterator_lastImpl(JNIEnv*, jclass, jint address) {
return ubrk_last(breakIterator(address));
}
static JNINativeMethod gMethods[] = {
NATIVE_METHOD(NativeBreakIterator, cloneImpl, "(I)I"),
NATIVE_METHOD(NativeBreakIterator, closeBreakIteratorImpl, "(I)V"),
NATIVE_METHOD(NativeBreakIterator, currentImpl, "(I)I"),
NATIVE_METHOD(NativeBreakIterator, firstImpl, "(I)I"),
NATIVE_METHOD(NativeBreakIterator, followingImpl, "(II)I"),
NATIVE_METHOD(NativeBreakIterator, getCharacterInstanceImpl, "(Ljava/lang/String;)I"),
NATIVE_METHOD(NativeBreakIterator, getLineInstanceImpl, "(Ljava/lang/String;)I"),
NATIVE_METHOD(NativeBreakIterator, getSentenceInstanceImpl, "(Ljava/lang/String;)I"),
NATIVE_METHOD(NativeBreakIterator, getWordInstanceImpl, "(Ljava/lang/String;)I"),
NATIVE_METHOD(NativeBreakIterator, isBoundaryImpl, "(II)Z"),
NATIVE_METHOD(NativeBreakIterator, lastImpl, "(I)I"),
NATIVE_METHOD(NativeBreakIterator, nextImpl, "(II)I"),
NATIVE_METHOD(NativeBreakIterator, precedingImpl, "(II)I"),
NATIVE_METHOD(NativeBreakIterator, previousImpl, "(I)I"),
NATIVE_METHOD(NativeBreakIterator, setTextImpl, "(ILjava/lang/String;)V"),
};
int register_libcore_icu_NativeBreakIterator(JNIEnv* env) {
return jniRegisterNativeMethods(env, "libcore/icu/NativeBreakIterator", gMethods, NELEM(gMethods));
}
|