forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodepointiterator_internal.cpp
293 lines (234 loc) · 7.38 KB
/
codepointiterator_internal.cpp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Gustavo Lopes <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "codepointiterator_internal.h"
#include <unicode/uchriter.h>
#include <typeinfo>
#include "php.h"
//copied from cmemory.h, which is not public
typedef union {
zend_long t1;
double t2;
void *t3;
} UAlignedMemory;
#define U_POINTER_MASK_LSB(ptr, mask) (((ptrdiff_t)(char *)(ptr)) & (mask))
#define U_ALIGNMENT_OFFSET(ptr) U_POINTER_MASK_LSB(ptr, sizeof(UAlignedMemory) - 1)
#define U_ALIGNMENT_OFFSET_UP(ptr) (sizeof(UAlignedMemory) - U_ALIGNMENT_OFFSET(ptr))
using namespace PHP;
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CodePointBreakIterator);
CodePointBreakIterator::CodePointBreakIterator()
: BreakIterator(), fCharIter(NULL), lastCodePoint(U_SENTINEL)
{
UErrorCode uec = UErrorCode();
this->fText = utext_openUChars(NULL, NULL, 0, &uec);
}
CodePointBreakIterator::CodePointBreakIterator(const PHP::CodePointBreakIterator &other)
: BreakIterator(other), fText(NULL), fCharIter(NULL), lastCodePoint(U_SENTINEL)
{
*this = other;
}
CodePointBreakIterator& CodePointBreakIterator::operator=(const CodePointBreakIterator& that)
{
UErrorCode uec = UErrorCode();
UText *ut_clone = NULL;
if (this == &that) {
return *this;
}
this->fText = utext_clone(this->fText, that.fText, FALSE, TRUE, &uec);
//don't bother copying the character iterator, getText() is deprecated
clearCurrentCharIter();
this->lastCodePoint = that.lastCodePoint;
return *this;
}
CodePointBreakIterator::~CodePointBreakIterator()
{
if (this->fText) {
utext_close(this->fText);
}
clearCurrentCharIter();
}
UBool CodePointBreakIterator::operator==(const BreakIterator& that) const
{
if (typeid(*this) != typeid(that)) {
return FALSE;
}
const CodePointBreakIterator& that2 =
static_cast<const CodePointBreakIterator&>(that);
if (!utext_equals(this->fText, that2.fText)) {
return FALSE;
}
return TRUE;
}
CodePointBreakIterator* CodePointBreakIterator::clone(void) const
{
return new CodePointBreakIterator(*this);
}
CharacterIterator& CodePointBreakIterator::getText(void) const
{
if (this->fCharIter == NULL) {
//this method is deprecated anyway; setup bogus iterator
static const UChar c = 0;
this->fCharIter = new UCharCharacterIterator(&c, 0);
}
return *this->fCharIter;
}
UText *CodePointBreakIterator::getUText(UText *fillIn, UErrorCode &status) const
{
return utext_clone(fillIn, this->fText, FALSE, TRUE, &status);
}
void CodePointBreakIterator::setText(const UnicodeString &text)
{
UErrorCode uec = UErrorCode();
//this closes the previous utext, if any
this->fText = utext_openConstUnicodeString(this->fText, &text, &uec);
clearCurrentCharIter();
}
void CodePointBreakIterator::setText(UText *text, UErrorCode &status)
{
if (U_FAILURE(status)) {
return;
}
this->fText = utext_clone(this->fText, text, FALSE, TRUE, &status);
clearCurrentCharIter();
}
void CodePointBreakIterator::adoptText(CharacterIterator* it)
{
UErrorCode uec = UErrorCode();
clearCurrentCharIter();
this->fCharIter = it;
this->fText = utext_openCharacterIterator(this->fText, it, &uec);
}
int32_t CodePointBreakIterator::first(void)
{
UTEXT_SETNATIVEINDEX(this->fText, 0);
this->lastCodePoint = U_SENTINEL;
return 0;
}
int32_t CodePointBreakIterator::last(void)
{
int32_t pos = (int32_t)utext_nativeLength(this->fText);
UTEXT_SETNATIVEINDEX(this->fText, pos);
this->lastCodePoint = U_SENTINEL;
return pos;
}
int32_t CodePointBreakIterator::previous(void)
{
this->lastCodePoint = UTEXT_PREVIOUS32(this->fText);
if (this->lastCodePoint == U_SENTINEL) {
return BreakIterator::DONE;
}
return (int32_t)UTEXT_GETNATIVEINDEX(this->fText);
}
int32_t CodePointBreakIterator::next(void)
{
this->lastCodePoint = UTEXT_NEXT32(this->fText);
if (this->lastCodePoint == U_SENTINEL) {
return BreakIterator::DONE;
}
return (int32_t)UTEXT_GETNATIVEINDEX(this->fText);
}
int32_t CodePointBreakIterator::current(void) const
{
return (int32_t)UTEXT_GETNATIVEINDEX(this->fText);
}
int32_t CodePointBreakIterator::following(int32_t offset)
{
this->lastCodePoint = utext_next32From(this->fText, offset);
if (this->lastCodePoint == U_SENTINEL) {
return BreakIterator::DONE;
}
return (int32_t)UTEXT_GETNATIVEINDEX(this->fText);
}
int32_t CodePointBreakIterator::preceding(int32_t offset)
{
this->lastCodePoint = utext_previous32From(this->fText, offset);
if (this->lastCodePoint == U_SENTINEL) {
return BreakIterator::DONE;
}
return (int32_t)UTEXT_GETNATIVEINDEX(this->fText);
}
UBool CodePointBreakIterator::isBoundary(int32_t offset)
{
//this function has side effects, and it's supposed to
utext_setNativeIndex(this->fText, offset);
return (offset == utext_getNativeIndex(this->fText));
}
int32_t CodePointBreakIterator::next(int32_t n)
{
UBool res = utext_moveIndex32(this->fText, n);
#ifndef UTEXT_CURRENT32
#define UTEXT_CURRENT32 utext_current32
#endif
if (res) {
this->lastCodePoint = UTEXT_CURRENT32(this->fText);
return (int32_t)UTEXT_GETNATIVEINDEX(this->fText);
} else {
this->lastCodePoint = U_SENTINEL;
return BreakIterator::DONE;
}
}
CodePointBreakIterator *CodePointBreakIterator::createBufferClone(
void *stackBuffer, int32_t &bufferSize, UErrorCode &status)
{
//see implementation of RuleBasedBreakIterator::createBufferClone()
if (U_FAILURE(status)) {
return NULL;
}
if (bufferSize <= 0) {
bufferSize = sizeof(CodePointBreakIterator) + U_ALIGNMENT_OFFSET_UP(0);
return NULL;
}
char *buf = (char*)stackBuffer;
uint32_t s = bufferSize;
if (stackBuffer == NULL) {
s = 0;
}
if (U_ALIGNMENT_OFFSET(stackBuffer) != 0) {
uint32_t offsetUp = (uint32_t)U_ALIGNMENT_OFFSET_UP(buf);
s -= offsetUp;
buf += offsetUp;
}
if (s < sizeof(CodePointBreakIterator)) {
CodePointBreakIterator *clonedBI = new CodePointBreakIterator(*this);
if (clonedBI == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
} else {
status = U_SAFECLONE_ALLOCATED_WARNING;
}
return clonedBI;
}
return new(buf) CodePointBreakIterator(*this);
}
CodePointBreakIterator &CodePointBreakIterator::refreshInputText(UText *input, UErrorCode &status)
{
//see implementation of RuleBasedBreakIterator::createBufferClone()
if (U_FAILURE(status)) {
return *this;
}
if (input == NULL) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return *this;
}
int64_t pos = utext_getNativeIndex(this->fText);
this->fText = utext_clone(this->fText, input, FALSE, TRUE, &status);
if (U_FAILURE(status)) {
return *this;
}
utext_setNativeIndex(this->fText, pos);
if (utext_getNativeIndex(fText) != pos) {
status = U_ILLEGAL_ARGUMENT_ERROR;
}
return *this;
}