1 /*
2    +----------------------------------------------------------------------+
3    | This source file is subject to version 3.01 of the PHP license,      |
4    | that is bundled with this package in the file LICENSE, and is        |
5    | available through the world-wide-web at the following url:           |
6    | http://www.php.net/license/3_01.txt                                  |
7    | If you did not receive a copy of the PHP license and are unable to   |
8    | obtain it through the world-wide-web, please send a note to          |
9    | license@php.net so we can mail you a copy immediately.               |
10    +----------------------------------------------------------------------+
11    | Authors: Gustavo Lopes <cataphract@php.net>                          |
12    +----------------------------------------------------------------------+
13 */
14 
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
18 
19 #include <unicode/brkiter.h>
20 #include <unicode/rbbi.h>
21 #include "codepointiterator_internal.h"
22 
23 #include "breakiterator_iterators.h"
24 
25 #include <typeinfo>
26 
27 extern "C" {
28 #define USE_BREAKITERATOR_POINTER 1
29 #include "breakiterator_class.h"
30 #include "breakiterator_arginfo.h"
31 #include <zend_exceptions.h>
32 #include <zend_interfaces.h>
33 #include <assert.h>
34 }
35 
36 using PHP::CodePointBreakIterator;
37 using icu::RuleBasedBreakIterator;
38 
39 /* {{{ Global variables */
40 zend_class_entry *BreakIterator_ce_ptr;
41 zend_class_entry *RuleBasedBreakIterator_ce_ptr;
42 zend_class_entry *CodePointBreakIterator_ce_ptr;
43 zend_object_handlers BreakIterator_handlers;
44 /* }}} */
45 
breakiterator_object_create(zval * object,BreakIterator * biter,int brand_new)46 U_CFUNC	void breakiterator_object_create(zval *object,
47 										 BreakIterator *biter, int brand_new)
48 {
49 	UClassID classId = biter->getDynamicClassID();
50 	zend_class_entry *ce;
51 
52 	if (classId == RuleBasedBreakIterator::getStaticClassID()) {
53 		ce = RuleBasedBreakIterator_ce_ptr;
54 	} else if (classId == CodePointBreakIterator::getStaticClassID()) {
55 		ce = CodePointBreakIterator_ce_ptr;
56 	} else {
57 		ce = BreakIterator_ce_ptr;
58 	}
59 
60 	if (brand_new) {
61 		object_init_ex(object, ce);
62 	}
63 	breakiterator_object_construct(object, biter);
64 }
65 
breakiterator_object_construct(zval * object,BreakIterator * biter)66 U_CFUNC void breakiterator_object_construct(zval *object,
67 											BreakIterator *biter)
68 {
69 	BreakIterator_object *bio;
70 
71 	BREAKITER_METHOD_FETCH_OBJECT_NO_CHECK; //populate to from object
72 	ZEND_ASSERT(bio->biter == NULL);
73 	bio->biter = biter;
74 }
75 
76 /* {{{ compare handler for BreakIterator */
BreakIterator_compare_objects(zval * object1,zval * object2)77 static int BreakIterator_compare_objects(zval *object1,
78 										 zval *object2)
79 {
80 	BreakIterator_object	*bio1,
81 							*bio2;
82 
83 	ZEND_COMPARE_OBJECTS_FALLBACK(object1, object2);
84 
85 	bio1 = Z_INTL_BREAKITERATOR_P(object1);
86 	bio2 = Z_INTL_BREAKITERATOR_P(object2);
87 
88 	if (bio1->biter == NULL || bio2->biter == NULL) {
89 		return bio1->biter == bio2->biter ? 0 : 1;
90 	}
91 
92 	return *bio1->biter == *bio2->biter ? 0 : 1;
93 }
94 /* }}} */
95 
96 /* {{{ clone handler for BreakIterator */
BreakIterator_clone_obj(zend_object * object)97 static zend_object *BreakIterator_clone_obj(zend_object *object)
98 {
99 	BreakIterator_object	*bio_orig,
100 							*bio_new;
101 	zend_object				*ret_val;
102 
103 	bio_orig = php_intl_breakiterator_fetch_object(object);
104 	intl_errors_reset(INTL_DATA_ERROR_P(bio_orig));
105 
106 	ret_val = BreakIterator_ce_ptr->create_object(object->ce);
107 	bio_new  = php_intl_breakiterator_fetch_object(ret_val);
108 
109 	zend_objects_clone_members(&bio_new->zo, &bio_orig->zo);
110 
111 	if (bio_orig->biter != NULL) {
112 		BreakIterator *new_biter;
113 
114 		new_biter = bio_orig->biter->clone();
115 		if (!new_biter) {
116 			zend_string *err_msg;
117 			intl_errors_set_code(BREAKITER_ERROR_P(bio_orig),
118 				U_MEMORY_ALLOCATION_ERROR);
119 			intl_errors_set_custom_msg(BREAKITER_ERROR_P(bio_orig),
120 				"Could not clone BreakIterator", 0);
121 			err_msg = intl_error_get_message(BREAKITER_ERROR_P(bio_orig));
122 			zend_throw_exception(NULL, ZSTR_VAL(err_msg), 0);
123 			zend_string_free(err_msg);
124 		} else {
125 			bio_new->biter = new_biter;
126 			ZVAL_COPY(&bio_new->text, &bio_orig->text);
127 		}
128 	} else {
129 		zend_throw_exception(NULL, "Cannot clone unconstructed BreakIterator", 0);
130 	}
131 
132 	return ret_val;
133 }
134 /* }}} */
135 
136 /* {{{ get_debug_info handler for BreakIterator */
BreakIterator_get_debug_info(zend_object * object,int * is_temp)137 static HashTable *BreakIterator_get_debug_info(zend_object *object, int *is_temp)
138 {
139 	zval val;
140 	HashTable *debug_info;
141 	BreakIterator_object	*bio;
142 	const BreakIterator		*biter;
143 
144 	*is_temp = 1;
145 
146 	debug_info = zend_new_array(8);
147 
148 	bio  = php_intl_breakiterator_fetch_object(object);
149 	biter = bio->biter;
150 
151 	if (biter == NULL) {
152 		ZVAL_FALSE(&val);
153 		zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &val);
154 		return debug_info;
155 	}
156 	ZVAL_TRUE(&val);
157 	zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &val);
158 
159 	if (Z_ISUNDEF(bio->text)) {
160 		ZVAL_NULL(&val);
161 		zend_hash_str_update(debug_info, "text", sizeof("text") - 1, &val);
162 	} else {
163 		Z_TRY_ADDREF(bio->text);
164 		zend_hash_str_update(debug_info, "text", sizeof("text") - 1, &bio->text);
165 	}
166 
167 	ZVAL_STRING(&val, const_cast<char*>(typeid(*biter).name()));
168 	zend_hash_str_update(debug_info, "type", sizeof("type") - 1, &val);
169 
170 	return debug_info;
171 }
172 /* }}} */
173 
174 /* {{{ void breakiterator_object_init(BreakIterator_object* to)
175  * Initialize internals of BreakIterator_object not specific to zend standard objects.
176  */
breakiterator_object_init(BreakIterator_object * bio)177 static void breakiterator_object_init(BreakIterator_object *bio)
178 {
179 	intl_error_init(BREAKITER_ERROR_P(bio));
180 	bio->biter = NULL;
181 	ZVAL_UNDEF(&bio->text);
182 }
183 /* }}} */
184 
185 /* {{{ BreakIterator_objects_free */
BreakIterator_objects_free(zend_object * object)186 static void BreakIterator_objects_free(zend_object *object)
187 {
188 	BreakIterator_object* bio = php_intl_breakiterator_fetch_object(object);
189 
190 	zval_ptr_dtor(&bio->text);
191 	if (bio->biter) {
192 		delete bio->biter;
193 		bio->biter = NULL;
194 	}
195 	intl_error_reset(BREAKITER_ERROR_P(bio));
196 
197 	zend_object_std_dtor(&bio->zo);
198 }
199 /* }}} */
200 
201 /* {{{ BreakIterator_object_create */
BreakIterator_object_create(zend_class_entry * ce)202 static zend_object *BreakIterator_object_create(zend_class_entry *ce)
203 {
204 	BreakIterator_object*	intern;
205 
206 	intern = (BreakIterator_object*) zend_object_alloc(sizeof(BreakIterator_object), ce);
207 
208 	zend_object_std_init(&intern->zo, ce);
209 	object_properties_init(&intern->zo, ce);
210 	breakiterator_object_init(intern);
211 
212 	intern->zo.handlers = &BreakIterator_handlers;
213 
214 	return &intern->zo;
215 }
216 /* }}} */
217 
218 /* {{{ breakiterator_register_BreakIterator_class
219  * Initialize 'BreakIterator' class
220  */
breakiterator_register_BreakIterator_class(void)221 U_CFUNC void breakiterator_register_BreakIterator_class(void)
222 {
223 	zend_class_entry ce;
224 
225 	/* Create and register 'BreakIterator' class. */
226 	INIT_CLASS_ENTRY(ce, "IntlBreakIterator", class_IntlBreakIterator_methods);
227 	ce.create_object = BreakIterator_object_create;
228 	ce.get_iterator = _breakiterator_get_iterator;
229 	BreakIterator_ce_ptr = zend_register_internal_class(&ce);
230 
231 	memcpy(&BreakIterator_handlers, &std_object_handlers,
232 		sizeof BreakIterator_handlers);
233 	BreakIterator_handlers.offset = XtOffsetOf(BreakIterator_object, zo);
234 	BreakIterator_handlers.compare = BreakIterator_compare_objects;
235 	BreakIterator_handlers.clone_obj = BreakIterator_clone_obj;
236 	BreakIterator_handlers.get_debug_info = BreakIterator_get_debug_info;
237 	BreakIterator_handlers.free_obj = BreakIterator_objects_free;
238 
239 	zend_class_implements(BreakIterator_ce_ptr, 1, zend_ce_aggregate);
240 
241 	zend_declare_class_constant_long(BreakIterator_ce_ptr,
242 		"DONE", sizeof("DONE") - 1, BreakIterator::DONE );
243 
244 	/* Declare constants that are defined in the C header */
245 #define BREAKITER_DECL_LONG_CONST(name) \
246 	zend_declare_class_constant_long(BreakIterator_ce_ptr, #name, \
247 		sizeof(#name) - 1, UBRK_ ## name)
248 
249 	BREAKITER_DECL_LONG_CONST(WORD_NONE);
250 	BREAKITER_DECL_LONG_CONST(WORD_NONE_LIMIT);
251 	BREAKITER_DECL_LONG_CONST(WORD_NUMBER);
252 	BREAKITER_DECL_LONG_CONST(WORD_NUMBER_LIMIT);
253 	BREAKITER_DECL_LONG_CONST(WORD_LETTER);
254 	BREAKITER_DECL_LONG_CONST(WORD_LETTER_LIMIT);
255 	BREAKITER_DECL_LONG_CONST(WORD_KANA);
256 	BREAKITER_DECL_LONG_CONST(WORD_KANA_LIMIT);
257 	BREAKITER_DECL_LONG_CONST(WORD_IDEO);
258 	BREAKITER_DECL_LONG_CONST(WORD_IDEO_LIMIT);
259 
260 	BREAKITER_DECL_LONG_CONST(LINE_SOFT);
261 	BREAKITER_DECL_LONG_CONST(LINE_SOFT_LIMIT);
262 	BREAKITER_DECL_LONG_CONST(LINE_HARD);
263 	BREAKITER_DECL_LONG_CONST(LINE_HARD_LIMIT);
264 
265 	BREAKITER_DECL_LONG_CONST(SENTENCE_TERM);
266 	BREAKITER_DECL_LONG_CONST(SENTENCE_TERM_LIMIT);
267 	BREAKITER_DECL_LONG_CONST(SENTENCE_SEP);
268 	BREAKITER_DECL_LONG_CONST(SENTENCE_SEP_LIMIT);
269 
270 #undef BREAKITER_DECL_LONG_CONST
271 
272 
273 	/* Create and register 'RuleBasedBreakIterator' class. */
274 	INIT_CLASS_ENTRY(ce, "IntlRuleBasedBreakIterator",
275 			class_IntlRuleBasedBreakIterator_methods);
276 	RuleBasedBreakIterator_ce_ptr = zend_register_internal_class_ex(&ce,
277 			BreakIterator_ce_ptr);
278 
279 	/* Create and register 'CodePointBreakIterator' class. */
280 	INIT_CLASS_ENTRY(ce, "IntlCodePointBreakIterator",
281 			class_IntlCodePointBreakIterator_methods);
282 	CodePointBreakIterator_ce_ptr = zend_register_internal_class_ex(&ce,
283 			BreakIterator_ce_ptr);
284 }
285 /* }}} */
286