breakiterator_class.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  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. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. #include <unicode/brkiter.h>
  20. #include <unicode/rbbi.h>
  21. #include "codepointiterator_internal.h"
  22. #include "breakiterator_iterators.h"
  23. #include <typeinfo>
  24. extern "C" {
  25. #define USE_BREAKITERATOR_POINTER 1
  26. #include "breakiterator_class.h"
  27. #include "breakiterator_methods.h"
  28. #include "rulebasedbreakiterator_methods.h"
  29. #include "codepointiterator_methods.h"
  30. #include <zend_exceptions.h>
  31. #include <zend_interfaces.h>
  32. #include <assert.h>
  33. }
  34. using PHP::CodePointBreakIterator;
  35. /* {{{ Global variables */
  36. zend_class_entry *BreakIterator_ce_ptr;
  37. zend_class_entry *RuleBasedBreakIterator_ce_ptr;
  38. zend_class_entry *CodePointBreakIterator_ce_ptr;
  39. zend_object_handlers BreakIterator_handlers;
  40. /* }}} */
  41. U_CFUNC void breakiterator_object_create(zval *object,
  42. BreakIterator *biter TSRMLS_DC)
  43. {
  44. UClassID classId = biter->getDynamicClassID();
  45. zend_class_entry *ce;
  46. if (classId == RuleBasedBreakIterator::getStaticClassID()) {
  47. ce = RuleBasedBreakIterator_ce_ptr;
  48. } else if (classId == CodePointBreakIterator::getStaticClassID()) {
  49. ce = CodePointBreakIterator_ce_ptr;
  50. } else {
  51. ce = BreakIterator_ce_ptr;
  52. }
  53. object_init_ex(object, ce);
  54. breakiterator_object_construct(object, biter TSRMLS_CC);
  55. }
  56. U_CFUNC void breakiterator_object_construct(zval *object,
  57. BreakIterator *biter TSRMLS_DC)
  58. {
  59. BreakIterator_object *bio;
  60. BREAKITER_METHOD_FETCH_OBJECT_NO_CHECK; //populate to from object
  61. assert(bio->biter == NULL);
  62. bio->biter = biter;
  63. }
  64. /* {{{ compare handler for BreakIterator */
  65. static int BreakIterator_compare_objects(zval *object1,
  66. zval *object2 TSRMLS_DC)
  67. {
  68. BreakIterator_object *bio1,
  69. *bio2;
  70. bio1 = (BreakIterator_object*)zend_object_store_get_object(object1 TSRMLS_CC);
  71. bio2 = (BreakIterator_object*)zend_object_store_get_object(object2 TSRMLS_CC);
  72. if (bio1->biter == NULL || bio2->biter == NULL) {
  73. return bio1->biter == bio2->biter ? 0 : 1;
  74. }
  75. return *bio1->biter == *bio2->biter ? 0 : 1;
  76. }
  77. /* }}} */
  78. /* {{{ clone handler for BreakIterator */
  79. static zend_object_value BreakIterator_clone_obj(zval *object TSRMLS_DC)
  80. {
  81. BreakIterator_object *bio_orig,
  82. *bio_new;
  83. zend_object_value ret_val;
  84. bio_orig = (BreakIterator_object*)zend_object_store_get_object(object TSRMLS_CC);
  85. intl_errors_reset(INTL_DATA_ERROR_P(bio_orig) TSRMLS_CC);
  86. ret_val = BreakIterator_ce_ptr->create_object(Z_OBJCE_P(object) TSRMLS_CC);
  87. bio_new = (BreakIterator_object*)zend_object_store_get_object_by_handle(
  88. ret_val.handle TSRMLS_CC);
  89. zend_objects_clone_members(&bio_new->zo, ret_val,
  90. &bio_orig->zo, Z_OBJ_HANDLE_P(object) TSRMLS_CC);
  91. if (bio_orig->biter != NULL) {
  92. BreakIterator *new_biter;
  93. new_biter = bio_orig->biter->clone();
  94. if (!new_biter) {
  95. char *err_msg;
  96. intl_errors_set_code(BREAKITER_ERROR_P(bio_orig),
  97. U_MEMORY_ALLOCATION_ERROR TSRMLS_CC);
  98. intl_errors_set_custom_msg(BREAKITER_ERROR_P(bio_orig),
  99. "Could not clone BreakIterator", 0 TSRMLS_CC);
  100. err_msg = intl_error_get_message(BREAKITER_ERROR_P(bio_orig) TSRMLS_CC);
  101. zend_throw_exception(NULL, err_msg, 0 TSRMLS_CC);
  102. efree(err_msg);
  103. } else {
  104. bio_new->biter = new_biter;
  105. bio_new->text = bio_orig->text;
  106. if (bio_new->text) {
  107. zval_add_ref(&bio_new->text);
  108. }
  109. }
  110. } else {
  111. zend_throw_exception(NULL, "Cannot clone unconstructed BreakIterator", 0 TSRMLS_CC);
  112. }
  113. return ret_val;
  114. }
  115. /* }}} */
  116. /* {{{ get_debug_info handler for BreakIterator */
  117. static HashTable *BreakIterator_get_debug_info(zval *object, int *is_temp TSRMLS_DC)
  118. {
  119. zval zv = zval_used_for_init;
  120. BreakIterator_object *bio;
  121. const BreakIterator *biter;
  122. *is_temp = 1;
  123. array_init_size(&zv, 8);
  124. bio = (BreakIterator_object*)zend_object_store_get_object(object TSRMLS_CC);
  125. biter = bio->biter;
  126. if (biter == NULL) {
  127. add_assoc_bool_ex(&zv, "valid", sizeof("valid"), 0);
  128. return Z_ARRVAL(zv);
  129. }
  130. add_assoc_bool_ex(&zv, "valid", sizeof("valid"), 1);
  131. if (bio->text == NULL) {
  132. add_assoc_null_ex(&zv, "text", sizeof("text"));
  133. } else {
  134. zval_add_ref(&bio->text);
  135. add_assoc_zval_ex(&zv, "text", sizeof("text"), bio->text);
  136. }
  137. add_assoc_string_ex(&zv, "type", sizeof("type"),
  138. const_cast<char*>(typeid(*biter).name()), 1);
  139. return Z_ARRVAL(zv);
  140. }
  141. /* }}} */
  142. /* {{{ void breakiterator_object_init(BreakIterator_object* to)
  143. * Initialize internals of BreakIterator_object not specific to zend standard objects.
  144. */
  145. static void breakiterator_object_init(BreakIterator_object *bio TSRMLS_DC)
  146. {
  147. intl_error_init(BREAKITER_ERROR_P(bio) TSRMLS_CC);
  148. bio->biter = NULL;
  149. bio->text = NULL;
  150. }
  151. /* }}} */
  152. /* {{{ BreakIterator_objects_dtor */
  153. static void BreakIterator_objects_dtor(void *object,
  154. zend_object_handle handle TSRMLS_DC)
  155. {
  156. zend_objects_destroy_object((zend_object*)object, handle TSRMLS_CC);
  157. }
  158. /* }}} */
  159. /* {{{ BreakIterator_objects_free */
  160. static void BreakIterator_objects_free(zend_object *object TSRMLS_DC)
  161. {
  162. BreakIterator_object* bio = (BreakIterator_object*) object;
  163. if (bio->text) {
  164. zval_ptr_dtor(&bio->text);
  165. }
  166. if (bio->biter) {
  167. delete bio->biter;
  168. bio->biter = NULL;
  169. }
  170. intl_error_reset(BREAKITER_ERROR_P(bio) TSRMLS_CC);
  171. zend_object_std_dtor(&bio->zo TSRMLS_CC);
  172. efree(bio);
  173. }
  174. /* }}} */
  175. /* {{{ BreakIterator_object_create */
  176. static zend_object_value BreakIterator_object_create(zend_class_entry *ce TSRMLS_DC)
  177. {
  178. zend_object_value retval;
  179. BreakIterator_object* intern;
  180. intern = (BreakIterator_object*)ecalloc(1, sizeof(BreakIterator_object));
  181. zend_object_std_init(&intern->zo, ce TSRMLS_CC);
  182. #if PHP_VERSION_ID < 50399
  183. zend_hash_copy(intern->zo.properties, &(ce->default_properties),
  184. (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval*));
  185. #else
  186. object_properties_init((zend_object*) intern, ce);
  187. #endif
  188. breakiterator_object_init(intern TSRMLS_CC);
  189. retval.handle = zend_objects_store_put(
  190. intern,
  191. BreakIterator_objects_dtor,
  192. (zend_objects_free_object_storage_t) BreakIterator_objects_free,
  193. NULL TSRMLS_CC);
  194. retval.handlers = &BreakIterator_handlers;
  195. return retval;
  196. }
  197. /* }}} */
  198. /* {{{ BreakIterator/RuleBasedBreakIterator methods arguments info */
  199. ZEND_BEGIN_ARG_INFO_EX(ainfo_biter_void, 0, 0, 0)
  200. ZEND_END_ARG_INFO()
  201. ZEND_BEGIN_ARG_INFO_EX(ainfo_biter_locale, 0, 0, 0)
  202. ZEND_ARG_INFO(0, locale)
  203. ZEND_END_ARG_INFO()
  204. ZEND_BEGIN_ARG_INFO_EX(ainfo_biter_setText, 0, 0, 1)
  205. ZEND_ARG_INFO(0, text)
  206. ZEND_END_ARG_INFO()
  207. ZEND_BEGIN_ARG_INFO_EX(ainfo_biter_next, 0, 0, 0)
  208. ZEND_ARG_INFO(0, offset)
  209. ZEND_END_ARG_INFO()
  210. ZEND_BEGIN_ARG_INFO_EX(ainfo_biter_offset, 0, 0, 1)
  211. ZEND_ARG_INFO(0, offset)
  212. ZEND_END_ARG_INFO()
  213. ZEND_BEGIN_ARG_INFO_EX(ainfo_biter_get_locale, 0, 0, 1)
  214. ZEND_ARG_INFO(0, locale_type)
  215. ZEND_END_ARG_INFO()
  216. ZEND_BEGIN_ARG_INFO_EX(ainfo_biter_getPartsIterator, 0, 0, 0)
  217. ZEND_ARG_INFO(0, key_type)
  218. ZEND_END_ARG_INFO()
  219. ZEND_BEGIN_ARG_INFO_EX(ainfo_rbbi___construct, 0, 0, 1)
  220. ZEND_ARG_INFO(0, rules)
  221. ZEND_ARG_INFO(0, areCompiled)
  222. ZEND_END_ARG_INFO()
  223. /* }}} */
  224. /* {{{ BreakIterator_class_functions
  225. * Every 'BreakIterator' class method has an entry in this table
  226. */
  227. static const zend_function_entry BreakIterator_class_functions[] = {
  228. PHP_ME(BreakIterator, __construct, ainfo_biter_void, ZEND_ACC_PRIVATE)
  229. PHP_ME_MAPPING(createWordInstance, breakiter_create_word_instance, ainfo_biter_locale, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
  230. PHP_ME_MAPPING(createLineInstance, breakiter_create_line_instance, ainfo_biter_locale, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
  231. PHP_ME_MAPPING(createCharacterInstance, breakiter_create_character_instance, ainfo_biter_locale, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
  232. PHP_ME_MAPPING(createSentenceInstance, breakiter_create_sentence_instance, ainfo_biter_locale, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
  233. PHP_ME_MAPPING(createTitleInstance, breakiter_create_title_instance, ainfo_biter_locale, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
  234. PHP_ME_MAPPING(createCodePointInstance, breakiter_create_code_point_instance, ainfo_biter_void, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
  235. PHP_ME_MAPPING(getText, breakiter_get_text, ainfo_biter_void, ZEND_ACC_PUBLIC)
  236. PHP_ME_MAPPING(setText, breakiter_set_text, ainfo_biter_setText, ZEND_ACC_PUBLIC)
  237. PHP_ME_MAPPING(first, breakiter_first, ainfo_biter_void, ZEND_ACC_PUBLIC)
  238. PHP_ME_MAPPING(last, breakiter_last, ainfo_biter_void, ZEND_ACC_PUBLIC)
  239. PHP_ME_MAPPING(previous, breakiter_previous, ainfo_biter_void, ZEND_ACC_PUBLIC)
  240. PHP_ME_MAPPING(next, breakiter_next, ainfo_biter_next, ZEND_ACC_PUBLIC)
  241. PHP_ME_MAPPING(current, breakiter_current, ainfo_biter_void, ZEND_ACC_PUBLIC)
  242. PHP_ME_MAPPING(following, breakiter_following, ainfo_biter_offset, ZEND_ACC_PUBLIC)
  243. PHP_ME_MAPPING(preceding, breakiter_preceding, ainfo_biter_offset, ZEND_ACC_PUBLIC)
  244. PHP_ME_MAPPING(isBoundary, breakiter_is_boundary, ainfo_biter_offset, ZEND_ACC_PUBLIC)
  245. PHP_ME_MAPPING(getLocale, breakiter_get_locale, ainfo_biter_get_locale, ZEND_ACC_PUBLIC)
  246. PHP_ME_MAPPING(getPartsIterator, breakiter_get_parts_iterator, ainfo_biter_getPartsIterator, ZEND_ACC_PUBLIC)
  247. PHP_ME_MAPPING(getErrorCode, breakiter_get_error_code, ainfo_biter_void, ZEND_ACC_PUBLIC)
  248. PHP_ME_MAPPING(getErrorMessage, breakiter_get_error_message, ainfo_biter_void, ZEND_ACC_PUBLIC)
  249. PHP_FE_END
  250. };
  251. /* }}} */
  252. /* {{{ RuleBasedBreakIterator_class_functions
  253. */
  254. static const zend_function_entry RuleBasedBreakIterator_class_functions[] = {
  255. PHP_ME(IntlRuleBasedBreakIterator, __construct, ainfo_rbbi___construct, ZEND_ACC_PUBLIC)
  256. PHP_ME_MAPPING(getRules, rbbi_get_rules, ainfo_biter_void, ZEND_ACC_PUBLIC)
  257. PHP_ME_MAPPING(getRuleStatus, rbbi_get_rule_status, ainfo_biter_void, ZEND_ACC_PUBLIC)
  258. PHP_ME_MAPPING(getRuleStatusVec, rbbi_get_rule_status_vec, ainfo_biter_void, ZEND_ACC_PUBLIC)
  259. #if U_ICU_VERSION_MAJOR_NUM * 10 + U_ICU_VERSION_MINOR_NUM >= 48
  260. PHP_ME_MAPPING(getBinaryRules, rbbi_get_binary_rules, ainfo_biter_void, ZEND_ACC_PUBLIC)
  261. #endif
  262. PHP_FE_END
  263. };
  264. /* }}} */
  265. /* {{{ CodePointBreakIterator_class_functions
  266. */
  267. static const zend_function_entry CodePointBreakIterator_class_functions[] = {
  268. PHP_ME_MAPPING(getLastCodePoint, cpbi_get_last_code_point, ainfo_biter_void, ZEND_ACC_PUBLIC)
  269. PHP_FE_END
  270. };
  271. /* }}} */
  272. /* {{{ breakiterator_register_BreakIterator_class
  273. * Initialize 'BreakIterator' class
  274. */
  275. U_CFUNC void breakiterator_register_BreakIterator_class(TSRMLS_D)
  276. {
  277. zend_class_entry ce;
  278. /* Create and register 'BreakIterator' class. */
  279. INIT_CLASS_ENTRY(ce, "IntlBreakIterator", BreakIterator_class_functions);
  280. ce.create_object = BreakIterator_object_create;
  281. ce.get_iterator = _breakiterator_get_iterator;
  282. BreakIterator_ce_ptr = zend_register_internal_class(&ce TSRMLS_CC);
  283. memcpy(&BreakIterator_handlers, zend_get_std_object_handlers(),
  284. sizeof BreakIterator_handlers);
  285. BreakIterator_handlers.compare_objects = BreakIterator_compare_objects;
  286. BreakIterator_handlers.clone_obj = BreakIterator_clone_obj;
  287. BreakIterator_handlers.get_debug_info = BreakIterator_get_debug_info;
  288. zend_class_implements(BreakIterator_ce_ptr TSRMLS_CC, 1,
  289. zend_ce_traversable);
  290. zend_declare_class_constant_long(BreakIterator_ce_ptr,
  291. "DONE", sizeof("DONE") - 1, BreakIterator::DONE TSRMLS_CC );
  292. /* Declare constants that are defined in the C header */
  293. #define BREAKITER_DECL_LONG_CONST(name) \
  294. zend_declare_class_constant_long(BreakIterator_ce_ptr, #name, \
  295. sizeof(#name) - 1, UBRK_ ## name TSRMLS_CC)
  296. BREAKITER_DECL_LONG_CONST(WORD_NONE);
  297. BREAKITER_DECL_LONG_CONST(WORD_NONE_LIMIT);
  298. BREAKITER_DECL_LONG_CONST(WORD_NUMBER);
  299. BREAKITER_DECL_LONG_CONST(WORD_NUMBER_LIMIT);
  300. BREAKITER_DECL_LONG_CONST(WORD_LETTER);
  301. BREAKITER_DECL_LONG_CONST(WORD_LETTER_LIMIT);
  302. BREAKITER_DECL_LONG_CONST(WORD_KANA);
  303. BREAKITER_DECL_LONG_CONST(WORD_KANA_LIMIT);
  304. BREAKITER_DECL_LONG_CONST(WORD_IDEO);
  305. BREAKITER_DECL_LONG_CONST(WORD_IDEO_LIMIT);
  306. BREAKITER_DECL_LONG_CONST(LINE_SOFT);
  307. BREAKITER_DECL_LONG_CONST(LINE_SOFT_LIMIT);
  308. BREAKITER_DECL_LONG_CONST(LINE_HARD);
  309. BREAKITER_DECL_LONG_CONST(LINE_HARD_LIMIT);
  310. BREAKITER_DECL_LONG_CONST(SENTENCE_TERM);
  311. BREAKITER_DECL_LONG_CONST(SENTENCE_TERM_LIMIT);
  312. BREAKITER_DECL_LONG_CONST(SENTENCE_SEP);
  313. BREAKITER_DECL_LONG_CONST(SENTENCE_SEP_LIMIT);
  314. #undef BREAKITER_DECL_LONG_CONST
  315. /* Create and register 'RuleBasedBreakIterator' class. */
  316. INIT_CLASS_ENTRY(ce, "IntlRuleBasedBreakIterator",
  317. RuleBasedBreakIterator_class_functions);
  318. RuleBasedBreakIterator_ce_ptr = zend_register_internal_class_ex(&ce,
  319. BreakIterator_ce_ptr, NULL TSRMLS_CC);
  320. /* Create and register 'CodePointBreakIterator' class. */
  321. INIT_CLASS_ENTRY(ce, "IntlCodePointBreakIterator",
  322. CodePointBreakIterator_class_functions);
  323. CodePointBreakIterator_ce_ptr = zend_register_internal_class_ex(&ce,
  324. BreakIterator_ce_ptr, NULL TSRMLS_CC);
  325. }
  326. /* }}} */