1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | http://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: Gustavo Lopes <cataphract@php.net> |
14 +----------------------------------------------------------------------+
15 */
16
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <unicode/brkiter.h>
22 #include <unicode/rbbi.h>
23 #include "codepointiterator_internal.h"
24
25 #include "breakiterator_iterators.h"
26
27 #include <typeinfo>
28
29 extern "C" {
30 #define USE_BREAKITERATOR_POINTER 1
31 #include "breakiterator_class.h"
32 #include "breakiterator_methods.h"
33 #include "rulebasedbreakiterator_methods.h"
34 #include "codepointiterator_methods.h"
35 #include <zend_exceptions.h>
36 #include <zend_interfaces.h>
37 #include <assert.h>
38 }
39
40 using PHP::CodePointBreakIterator;
41
42 /* {{{ Global variables */
43 zend_class_entry *BreakIterator_ce_ptr;
44 zend_class_entry *RuleBasedBreakIterator_ce_ptr;
45 zend_class_entry *CodePointBreakIterator_ce_ptr;
46 zend_object_handlers BreakIterator_handlers;
47 /* }}} */
48
breakiterator_object_create(zval * object,BreakIterator * biter,int brand_new)49 U_CFUNC void breakiterator_object_create(zval *object,
50 BreakIterator *biter, int brand_new)
51 {
52 UClassID classId = biter->getDynamicClassID();
53 zend_class_entry *ce;
54
55 if (classId == RuleBasedBreakIterator::getStaticClassID()) {
56 ce = RuleBasedBreakIterator_ce_ptr;
57 } else if (classId == CodePointBreakIterator::getStaticClassID()) {
58 ce = CodePointBreakIterator_ce_ptr;
59 } else {
60 ce = BreakIterator_ce_ptr;
61 }
62
63 if (brand_new) {
64 object_init_ex(object, ce);
65 }
66 breakiterator_object_construct(object, biter);
67 }
68
breakiterator_object_construct(zval * object,BreakIterator * biter)69 U_CFUNC void breakiterator_object_construct(zval *object,
70 BreakIterator *biter)
71 {
72 BreakIterator_object *bio;
73
74 BREAKITER_METHOD_FETCH_OBJECT_NO_CHECK; //populate to from object
75 assert(bio->biter == NULL);
76 bio->biter = biter;
77 }
78
79 /* {{{ compare handler for BreakIterator */
BreakIterator_compare_objects(zval * object1,zval * object2)80 static int BreakIterator_compare_objects(zval *object1,
81 zval *object2)
82 {
83 BreakIterator_object *bio1,
84 *bio2;
85
86 bio1 = Z_INTL_BREAKITERATOR_P(object1);
87 bio2 = Z_INTL_BREAKITERATOR_P(object2);
88
89 if (bio1->biter == NULL || bio2->biter == NULL) {
90 return bio1->biter == bio2->biter ? 0 : 1;
91 }
92
93 return *bio1->biter == *bio2->biter ? 0 : 1;
94 }
95 /* }}} */
96
97 /* {{{ clone handler for BreakIterator */
BreakIterator_clone_obj(zval * object)98 static zend_object *BreakIterator_clone_obj(zval *object)
99 {
100 BreakIterator_object *bio_orig,
101 *bio_new;
102 zend_object *ret_val;
103
104 bio_orig = Z_INTL_BREAKITERATOR_P(object);
105 intl_errors_reset(INTL_DATA_ERROR_P(bio_orig));
106
107 ret_val = BreakIterator_ce_ptr->create_object(Z_OBJCE_P(object));
108 bio_new = php_intl_breakiterator_fetch_object(ret_val);
109
110 zend_objects_clone_members(&bio_new->zo, &bio_orig->zo);
111
112 if (bio_orig->biter != NULL) {
113 BreakIterator *new_biter;
114
115 new_biter = bio_orig->biter->clone();
116 if (!new_biter) {
117 zend_string *err_msg;
118 intl_errors_set_code(BREAKITER_ERROR_P(bio_orig),
119 U_MEMORY_ALLOCATION_ERROR);
120 intl_errors_set_custom_msg(BREAKITER_ERROR_P(bio_orig),
121 "Could not clone BreakIterator", 0);
122 err_msg = intl_error_get_message(BREAKITER_ERROR_P(bio_orig));
123 zend_throw_exception(NULL, ZSTR_VAL(err_msg), 0);
124 zend_string_free(err_msg);
125 } else {
126 bio_new->biter = new_biter;
127 ZVAL_COPY(&bio_new->text, &bio_orig->text);
128 }
129 } else {
130 zend_throw_exception(NULL, "Cannot clone unconstructed BreakIterator", 0);
131 }
132
133 return ret_val;
134 }
135 /* }}} */
136
137 /* {{{ get_debug_info handler for BreakIterator */
BreakIterator_get_debug_info(zval * object,int * is_temp)138 static HashTable *BreakIterator_get_debug_info(zval *object, int *is_temp)
139 {
140 zval val;
141 HashTable *debug_info;
142 BreakIterator_object *bio;
143 const BreakIterator *biter;
144
145 *is_temp = 1;
146
147 ALLOC_HASHTABLE(debug_info);
148 zend_hash_init(debug_info, 8, NULL, ZVAL_PTR_DTOR, 0);
149
150 bio = Z_INTL_BREAKITERATOR_P(object);
151 biter = bio->biter;
152
153 if (biter == NULL) {
154 ZVAL_FALSE(&val);
155 zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &val);
156 return debug_info;
157 }
158 ZVAL_TRUE(&val);
159 zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &val);
160
161 if (Z_ISUNDEF(bio->text)) {
162 ZVAL_NULL(&val);
163 zend_hash_str_update(debug_info, "text", sizeof("text") - 1, &val);
164 } else {
165 Z_TRY_ADDREF(bio->text);
166 zend_hash_str_update(debug_info, "text", sizeof("text") - 1, &bio->text);
167 }
168
169 ZVAL_STRING(&val, const_cast<char*>(typeid(*biter).name()));
170 zend_hash_str_update(debug_info, "type", sizeof("type") - 1, &bio->text);
171
172 return debug_info;
173 }
174 /* }}} */
175
176 /* {{{ void breakiterator_object_init(BreakIterator_object* to)
177 * Initialize internals of BreakIterator_object not specific to zend standard objects.
178 */
breakiterator_object_init(BreakIterator_object * bio)179 static void breakiterator_object_init(BreakIterator_object *bio)
180 {
181 intl_error_init(BREAKITER_ERROR_P(bio));
182 bio->biter = NULL;
183 ZVAL_UNDEF(&bio->text);
184 }
185 /* }}} */
186
187 /* {{{ BreakIterator_objects_dtor */
BreakIterator_objects_dtor(zend_object * object)188 static void BreakIterator_objects_dtor(zend_object *object)
189 {
190 zend_objects_destroy_object(object);
191 }
192 /* }}} */
193
194 /* {{{ BreakIterator_objects_free */
BreakIterator_objects_free(zend_object * object)195 static void BreakIterator_objects_free(zend_object *object)
196 {
197 BreakIterator_object* bio = php_intl_breakiterator_fetch_object(object);
198
199 zval_ptr_dtor(&bio->text);
200 if (bio->biter) {
201 delete bio->biter;
202 bio->biter = NULL;
203 }
204 intl_error_reset(BREAKITER_ERROR_P(bio));
205
206 zend_object_std_dtor(&bio->zo);
207 }
208 /* }}} */
209
210 /* {{{ BreakIterator_object_create */
BreakIterator_object_create(zend_class_entry * ce)211 static zend_object *BreakIterator_object_create(zend_class_entry *ce)
212 {
213 BreakIterator_object* intern;
214
215 intern = (BreakIterator_object*)ecalloc(1, sizeof(BreakIterator_object) + sizeof(zval) * (ce->default_properties_count - 1));
216
217 zend_object_std_init(&intern->zo, ce);
218 object_properties_init((zend_object*) intern, ce);
219 breakiterator_object_init(intern);
220
221 intern->zo.handlers = &BreakIterator_handlers;
222
223 return &intern->zo;
224 }
225 /* }}} */
226
227 /* {{{ BreakIterator/RuleBasedBreakIterator methods arguments info */
228
229 ZEND_BEGIN_ARG_INFO_EX(ainfo_biter_void, 0, 0, 0)
230 ZEND_END_ARG_INFO()
231
232 ZEND_BEGIN_ARG_INFO_EX(ainfo_biter_locale, 0, 0, 0)
233 ZEND_ARG_INFO(0, locale)
234 ZEND_END_ARG_INFO()
235
236 ZEND_BEGIN_ARG_INFO_EX(ainfo_biter_setText, 0, 0, 1)
237 ZEND_ARG_INFO(0, text)
238 ZEND_END_ARG_INFO()
239
240 ZEND_BEGIN_ARG_INFO_EX(ainfo_biter_next, 0, 0, 0)
241 ZEND_ARG_INFO(0, offset)
242 ZEND_END_ARG_INFO()
243
244 ZEND_BEGIN_ARG_INFO_EX(ainfo_biter_offset, 0, 0, 1)
245 ZEND_ARG_INFO(0, offset)
246 ZEND_END_ARG_INFO()
247
248 ZEND_BEGIN_ARG_INFO_EX(ainfo_biter_get_locale, 0, 0, 1)
249 ZEND_ARG_INFO(0, locale_type)
250 ZEND_END_ARG_INFO()
251
252 ZEND_BEGIN_ARG_INFO_EX(ainfo_biter_getPartsIterator, 0, 0, 0)
253 ZEND_ARG_INFO(0, key_type)
254 ZEND_END_ARG_INFO()
255
256 ZEND_BEGIN_ARG_INFO_EX(ainfo_rbbi___construct, 0, 0, 1)
257 ZEND_ARG_INFO(0, rules)
258 ZEND_ARG_INFO(0, areCompiled)
259 ZEND_END_ARG_INFO()
260
261 /* }}} */
262
263 /* {{{ BreakIterator_class_functions
264 * Every 'BreakIterator' class method has an entry in this table
265 */
266 static const zend_function_entry BreakIterator_class_functions[] = {
267 PHP_ME(BreakIterator, __construct, ainfo_biter_void, ZEND_ACC_PRIVATE)
268 PHP_ME_MAPPING(createWordInstance, breakiter_create_word_instance, ainfo_biter_locale, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
269 PHP_ME_MAPPING(createLineInstance, breakiter_create_line_instance, ainfo_biter_locale, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
270 PHP_ME_MAPPING(createCharacterInstance, breakiter_create_character_instance, ainfo_biter_locale, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
271 PHP_ME_MAPPING(createSentenceInstance, breakiter_create_sentence_instance, ainfo_biter_locale, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
272 PHP_ME_MAPPING(createTitleInstance, breakiter_create_title_instance, ainfo_biter_locale, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
273 PHP_ME_MAPPING(createCodePointInstance, breakiter_create_code_point_instance, ainfo_biter_void, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
274 PHP_ME_MAPPING(getText, breakiter_get_text, ainfo_biter_void, ZEND_ACC_PUBLIC)
275 PHP_ME_MAPPING(setText, breakiter_set_text, ainfo_biter_setText, ZEND_ACC_PUBLIC)
276 PHP_ME_MAPPING(first, breakiter_first, ainfo_biter_void, ZEND_ACC_PUBLIC)
277 PHP_ME_MAPPING(last, breakiter_last, ainfo_biter_void, ZEND_ACC_PUBLIC)
278 PHP_ME_MAPPING(previous, breakiter_previous, ainfo_biter_void, ZEND_ACC_PUBLIC)
279 PHP_ME_MAPPING(next, breakiter_next, ainfo_biter_next, ZEND_ACC_PUBLIC)
280 PHP_ME_MAPPING(current, breakiter_current, ainfo_biter_void, ZEND_ACC_PUBLIC)
281 PHP_ME_MAPPING(following, breakiter_following, ainfo_biter_offset, ZEND_ACC_PUBLIC)
282 PHP_ME_MAPPING(preceding, breakiter_preceding, ainfo_biter_offset, ZEND_ACC_PUBLIC)
283 PHP_ME_MAPPING(isBoundary, breakiter_is_boundary, ainfo_biter_offset, ZEND_ACC_PUBLIC)
284 PHP_ME_MAPPING(getLocale, breakiter_get_locale, ainfo_biter_get_locale, ZEND_ACC_PUBLIC)
285 PHP_ME_MAPPING(getPartsIterator, breakiter_get_parts_iterator, ainfo_biter_getPartsIterator, ZEND_ACC_PUBLIC)
286
287 PHP_ME_MAPPING(getErrorCode, breakiter_get_error_code, ainfo_biter_void, ZEND_ACC_PUBLIC)
288 PHP_ME_MAPPING(getErrorMessage, breakiter_get_error_message, ainfo_biter_void, ZEND_ACC_PUBLIC)
289 PHP_FE_END
290 };
291 /* }}} */
292
293 /* {{{ RuleBasedBreakIterator_class_functions
294 */
295 static const zend_function_entry RuleBasedBreakIterator_class_functions[] = {
296 PHP_ME(IntlRuleBasedBreakIterator, __construct, ainfo_rbbi___construct, ZEND_ACC_PUBLIC)
297 PHP_ME_MAPPING(getRules, rbbi_get_rules, ainfo_biter_void, ZEND_ACC_PUBLIC)
298 PHP_ME_MAPPING(getRuleStatus, rbbi_get_rule_status, ainfo_biter_void, ZEND_ACC_PUBLIC)
299 PHP_ME_MAPPING(getRuleStatusVec, rbbi_get_rule_status_vec, ainfo_biter_void, ZEND_ACC_PUBLIC)
300 #if U_ICU_VERSION_MAJOR_NUM * 10 + U_ICU_VERSION_MINOR_NUM >= 48
301 PHP_ME_MAPPING(getBinaryRules, rbbi_get_binary_rules, ainfo_biter_void, ZEND_ACC_PUBLIC)
302 #endif
303 PHP_FE_END
304 };
305 /* }}} */
306
307 /* {{{ CodePointBreakIterator_class_functions
308 */
309 static const zend_function_entry CodePointBreakIterator_class_functions[] = {
310 PHP_ME_MAPPING(getLastCodePoint, cpbi_get_last_code_point, ainfo_biter_void, ZEND_ACC_PUBLIC)
311 PHP_FE_END
312 };
313 /* }}} */
314
315
316 /* {{{ breakiterator_register_BreakIterator_class
317 * Initialize 'BreakIterator' class
318 */
breakiterator_register_BreakIterator_class(void)319 U_CFUNC void breakiterator_register_BreakIterator_class(void)
320 {
321 zend_class_entry ce;
322
323 /* Create and register 'BreakIterator' class. */
324 INIT_CLASS_ENTRY(ce, "IntlBreakIterator", BreakIterator_class_functions);
325 ce.create_object = BreakIterator_object_create;
326 ce.get_iterator = _breakiterator_get_iterator;
327 BreakIterator_ce_ptr = zend_register_internal_class(&ce);
328
329 memcpy(&BreakIterator_handlers, zend_get_std_object_handlers(),
330 sizeof BreakIterator_handlers);
331 BreakIterator_handlers.offset = XtOffsetOf(BreakIterator_object, zo);
332 BreakIterator_handlers.compare_objects = BreakIterator_compare_objects;
333 BreakIterator_handlers.clone_obj = BreakIterator_clone_obj;
334 BreakIterator_handlers.get_debug_info = BreakIterator_get_debug_info;
335 BreakIterator_handlers.dtor_obj = BreakIterator_objects_dtor;
336 BreakIterator_handlers.free_obj = BreakIterator_objects_free;
337
338 zend_class_implements(BreakIterator_ce_ptr, 1,
339 zend_ce_traversable);
340
341 zend_declare_class_constant_long(BreakIterator_ce_ptr,
342 "DONE", sizeof("DONE") - 1, BreakIterator::DONE );
343
344 /* Declare constants that are defined in the C header */
345 #define BREAKITER_DECL_LONG_CONST(name) \
346 zend_declare_class_constant_long(BreakIterator_ce_ptr, #name, \
347 sizeof(#name) - 1, UBRK_ ## name)
348
349 BREAKITER_DECL_LONG_CONST(WORD_NONE);
350 BREAKITER_DECL_LONG_CONST(WORD_NONE_LIMIT);
351 BREAKITER_DECL_LONG_CONST(WORD_NUMBER);
352 BREAKITER_DECL_LONG_CONST(WORD_NUMBER_LIMIT);
353 BREAKITER_DECL_LONG_CONST(WORD_LETTER);
354 BREAKITER_DECL_LONG_CONST(WORD_LETTER_LIMIT);
355 BREAKITER_DECL_LONG_CONST(WORD_KANA);
356 BREAKITER_DECL_LONG_CONST(WORD_KANA_LIMIT);
357 BREAKITER_DECL_LONG_CONST(WORD_IDEO);
358 BREAKITER_DECL_LONG_CONST(WORD_IDEO_LIMIT);
359
360 BREAKITER_DECL_LONG_CONST(LINE_SOFT);
361 BREAKITER_DECL_LONG_CONST(LINE_SOFT_LIMIT);
362 BREAKITER_DECL_LONG_CONST(LINE_HARD);
363 BREAKITER_DECL_LONG_CONST(LINE_HARD_LIMIT);
364
365 BREAKITER_DECL_LONG_CONST(SENTENCE_TERM);
366 BREAKITER_DECL_LONG_CONST(SENTENCE_TERM_LIMIT);
367 BREAKITER_DECL_LONG_CONST(SENTENCE_SEP);
368 BREAKITER_DECL_LONG_CONST(SENTENCE_SEP_LIMIT);
369
370 #undef BREAKITER_DECL_LONG_CONST
371
372
373 /* Create and register 'RuleBasedBreakIterator' class. */
374 INIT_CLASS_ENTRY(ce, "IntlRuleBasedBreakIterator",
375 RuleBasedBreakIterator_class_functions);
376 RuleBasedBreakIterator_ce_ptr = zend_register_internal_class_ex(&ce,
377 BreakIterator_ce_ptr);
378
379 /* Create and register 'CodePointBreakIterator' class. */
380 INIT_CLASS_ENTRY(ce, "IntlCodePointBreakIterator",
381 CodePointBreakIterator_class_functions);
382 CodePointBreakIterator_ce_ptr = zend_register_internal_class_ex(&ce,
383 BreakIterator_ce_ptr);
384 }
385 /* }}} */
386