breakiterator_methods.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 "codepointiterator_internal.h"
  21. #include "breakiterator_iterators.h"
  22. extern "C" {
  23. #include "../php_intl.h"
  24. #define USE_BREAKITERATOR_POINTER 1
  25. #include "breakiterator_class.h"
  26. #include "../locale/locale.h"
  27. #include <zend_exceptions.h>
  28. }
  29. using PHP::CodePointBreakIterator;
  30. U_CFUNC PHP_METHOD(BreakIterator, __construct)
  31. {
  32. zend_throw_exception( NULL,
  33. "An object of this type cannot be created with the new operator",
  34. 0 TSRMLS_CC );
  35. }
  36. static void _breakiter_factory(const char *func_name,
  37. BreakIterator *(*func)(const Locale&, UErrorCode&),
  38. INTERNAL_FUNCTION_PARAMETERS)
  39. {
  40. BreakIterator *biter;
  41. const char *locale_str = NULL;
  42. int dummy;
  43. char *msg;
  44. UErrorCode status = UErrorCode();
  45. intl_error_reset(NULL TSRMLS_CC);
  46. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!",
  47. &locale_str, &dummy) == FAILURE) {
  48. spprintf(&msg, 0, "%s: bad arguments", func_name);
  49. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, msg, 1 TSRMLS_CC);
  50. efree(msg);
  51. RETURN_NULL();
  52. }
  53. if (locale_str == NULL) {
  54. locale_str = intl_locale_get_default(TSRMLS_C);
  55. }
  56. biter = func(Locale::createFromName(locale_str), status);
  57. intl_error_set_code(NULL, status TSRMLS_CC);
  58. if (U_FAILURE(status)) {
  59. spprintf(&msg, 0, "%s: error creating BreakIterator",
  60. func_name);
  61. intl_error_set_custom_msg(NULL, msg, 1 TSRMLS_CC);
  62. efree(msg);
  63. RETURN_NULL();
  64. }
  65. breakiterator_object_create(return_value, biter TSRMLS_CC);
  66. }
  67. U_CFUNC PHP_FUNCTION(breakiter_create_word_instance)
  68. {
  69. _breakiter_factory("breakiter_create_word_instance",
  70. &BreakIterator::createWordInstance,
  71. INTERNAL_FUNCTION_PARAM_PASSTHRU);
  72. }
  73. U_CFUNC PHP_FUNCTION(breakiter_create_line_instance)
  74. {
  75. _breakiter_factory("breakiter_create_line_instance",
  76. &BreakIterator::createLineInstance,
  77. INTERNAL_FUNCTION_PARAM_PASSTHRU);
  78. }
  79. U_CFUNC PHP_FUNCTION(breakiter_create_character_instance)
  80. {
  81. _breakiter_factory("breakiter_create_character_instance",
  82. &BreakIterator::createCharacterInstance,
  83. INTERNAL_FUNCTION_PARAM_PASSTHRU);
  84. }
  85. U_CFUNC PHP_FUNCTION(breakiter_create_sentence_instance)
  86. {
  87. _breakiter_factory("breakiter_create_sentence_instance",
  88. &BreakIterator::createSentenceInstance,
  89. INTERNAL_FUNCTION_PARAM_PASSTHRU);
  90. }
  91. U_CFUNC PHP_FUNCTION(breakiter_create_title_instance)
  92. {
  93. _breakiter_factory("breakiter_create_title_instance",
  94. &BreakIterator::createTitleInstance,
  95. INTERNAL_FUNCTION_PARAM_PASSTHRU);
  96. }
  97. U_CFUNC PHP_FUNCTION(breakiter_create_code_point_instance)
  98. {
  99. UErrorCode status = UErrorCode();
  100. intl_error_reset(NULL TSRMLS_CC);
  101. if (zend_parse_parameters_none() == FAILURE) {
  102. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  103. "breakiter_create_code_point_instance: bad arguments", 0 TSRMLS_CC);
  104. RETURN_NULL();
  105. }
  106. CodePointBreakIterator *cpbi = new CodePointBreakIterator();
  107. breakiterator_object_create(return_value, cpbi TSRMLS_CC);
  108. }
  109. U_CFUNC PHP_FUNCTION(breakiter_get_text)
  110. {
  111. BREAKITER_METHOD_INIT_VARS;
  112. object = getThis();
  113. if (zend_parse_parameters_none() == FAILURE) {
  114. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  115. "breakiter_get_text: bad arguments", 0 TSRMLS_CC);
  116. RETURN_FALSE;
  117. }
  118. BREAKITER_METHOD_FETCH_OBJECT;
  119. if (bio->text == NULL) {
  120. RETURN_NULL();
  121. } else {
  122. RETURN_ZVAL(bio->text, 1, 0);
  123. }
  124. }
  125. U_CFUNC PHP_FUNCTION(breakiter_set_text)
  126. {
  127. char *text;
  128. int text_len;
  129. UText *ut = NULL;
  130. zval **textzv;
  131. BREAKITER_METHOD_INIT_VARS;
  132. object = getThis();
  133. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
  134. &text, &text_len) == FAILURE) {
  135. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  136. "breakiter_set_text: bad arguments", 0 TSRMLS_CC);
  137. RETURN_FALSE;
  138. }
  139. int res = zend_get_parameters_ex(1, &textzv);
  140. assert(res == SUCCESS);
  141. BREAKITER_METHOD_FETCH_OBJECT;
  142. /* assert it's safe to use text and text_len because zpp changes the
  143. * arguments in the stack */
  144. assert(text == Z_STRVAL_PP(textzv));
  145. ut = utext_openUTF8(ut, text, text_len, BREAKITER_ERROR_CODE_P(bio));
  146. INTL_CTOR_CHECK_STATUS(bio, "breakiter_set_text: error opening UText");
  147. bio->biter->setText(ut, BREAKITER_ERROR_CODE(bio));
  148. utext_close(ut); /* ICU shallow clones the UText */
  149. INTL_CTOR_CHECK_STATUS(bio, "breakiter_set_text: error calling "
  150. "BreakIterator::setText()");
  151. /* When ICU clones the UText, it does not copy the buffer, so we have to
  152. * keep the string buffer around by holding a reference to its zval. This
  153. * also allows a faste implementation of getText() */
  154. if (bio->text != NULL) {
  155. zval_ptr_dtor(&bio->text);
  156. }
  157. bio->text = *textzv;
  158. zval_add_ref(&bio->text);
  159. RETURN_TRUE;
  160. }
  161. static void _breakiter_no_args_ret_int32(
  162. const char *func_name,
  163. int32_t (BreakIterator::*func)(),
  164. INTERNAL_FUNCTION_PARAMETERS)
  165. {
  166. char *msg;
  167. BREAKITER_METHOD_INIT_VARS;
  168. object = getThis();
  169. if (zend_parse_parameters_none() == FAILURE) {
  170. spprintf(&msg, 0, "%s: bad arguments", func_name);
  171. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, msg, 1 TSRMLS_CC);
  172. efree(msg);
  173. RETURN_FALSE;
  174. }
  175. BREAKITER_METHOD_FETCH_OBJECT;
  176. int32_t res = (bio->biter->*func)();
  177. RETURN_LONG((long)res);
  178. }
  179. static void _breakiter_int32_ret_int32(
  180. const char *func_name,
  181. int32_t (BreakIterator::*func)(int32_t),
  182. INTERNAL_FUNCTION_PARAMETERS)
  183. {
  184. char *msg;
  185. long arg;
  186. BREAKITER_METHOD_INIT_VARS;
  187. object = getThis();
  188. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &arg) == FAILURE) {
  189. spprintf(&msg, 0, "%s: bad arguments", func_name);
  190. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, msg, 1 TSRMLS_CC);
  191. efree(msg);
  192. RETURN_FALSE;
  193. }
  194. BREAKITER_METHOD_FETCH_OBJECT;
  195. if (arg < INT32_MIN || arg > INT32_MAX) {
  196. spprintf(&msg, 0, "%s: offset argument is outside bounds of "
  197. "a 32-bit wide integer", func_name);
  198. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, msg, 1 TSRMLS_CC);
  199. efree(msg);
  200. RETURN_FALSE;
  201. }
  202. int32_t res = (bio->biter->*func)((int32_t)arg);
  203. RETURN_LONG((long)res);
  204. }
  205. U_CFUNC PHP_FUNCTION(breakiter_first)
  206. {
  207. _breakiter_no_args_ret_int32("breakiter_first",
  208. &BreakIterator::first,
  209. INTERNAL_FUNCTION_PARAM_PASSTHRU);
  210. }
  211. U_CFUNC PHP_FUNCTION(breakiter_last)
  212. {
  213. _breakiter_no_args_ret_int32("breakiter_last",
  214. &BreakIterator::last,
  215. INTERNAL_FUNCTION_PARAM_PASSTHRU);
  216. }
  217. U_CFUNC PHP_FUNCTION(breakiter_previous)
  218. {
  219. _breakiter_no_args_ret_int32("breakiter_previous",
  220. &BreakIterator::previous,
  221. INTERNAL_FUNCTION_PARAM_PASSTHRU);
  222. }
  223. U_CFUNC PHP_FUNCTION(breakiter_next)
  224. {
  225. bool no_arg_version = false;
  226. if (ZEND_NUM_ARGS() == 0) {
  227. no_arg_version = true;
  228. } else if (ZEND_NUM_ARGS() == 1) {
  229. zval **arg;
  230. int res = zend_get_parameters_ex(1, &arg);
  231. assert(res == SUCCESS);
  232. if (Z_TYPE_PP(arg) == IS_NULL) {
  233. no_arg_version = true;
  234. ht = 0; /* pretend we don't have any argument */
  235. } else {
  236. no_arg_version = false;
  237. }
  238. }
  239. if (no_arg_version) {
  240. _breakiter_no_args_ret_int32("breakiter_next",
  241. &BreakIterator::next,
  242. INTERNAL_FUNCTION_PARAM_PASSTHRU);
  243. } else {
  244. _breakiter_int32_ret_int32("breakiter_next",
  245. &BreakIterator::next,
  246. INTERNAL_FUNCTION_PARAM_PASSTHRU);
  247. }
  248. }
  249. U_CFUNC PHP_FUNCTION(breakiter_current)
  250. {
  251. BREAKITER_METHOD_INIT_VARS;
  252. object = getThis();
  253. if (zend_parse_parameters_none() == FAILURE) {
  254. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  255. "breakiter_current: bad arguments", 0 TSRMLS_CC);
  256. RETURN_FALSE;
  257. }
  258. BREAKITER_METHOD_FETCH_OBJECT;
  259. int32_t res = bio->biter->current();
  260. RETURN_LONG((long)res);
  261. }
  262. U_CFUNC PHP_FUNCTION(breakiter_following)
  263. {
  264. _breakiter_int32_ret_int32("breakiter_following",
  265. &BreakIterator::following,
  266. INTERNAL_FUNCTION_PARAM_PASSTHRU);
  267. }
  268. U_CFUNC PHP_FUNCTION(breakiter_preceding)
  269. {
  270. _breakiter_int32_ret_int32("breakiter_preceding",
  271. &BreakIterator::preceding,
  272. INTERNAL_FUNCTION_PARAM_PASSTHRU);
  273. }
  274. U_CFUNC PHP_FUNCTION(breakiter_is_boundary)
  275. {
  276. long offset;
  277. BREAKITER_METHOD_INIT_VARS;
  278. object = getThis();
  279. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l",
  280. &offset) == FAILURE) {
  281. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  282. "breakiter_is_boundary: bad arguments", 0 TSRMLS_CC);
  283. RETURN_FALSE;
  284. }
  285. if (offset < INT32_MIN || offset > INT32_MAX) {
  286. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  287. "breakiter_is_boundary: offset argument is outside bounds of "
  288. "a 32-bit wide integer", 0 TSRMLS_CC);
  289. RETURN_FALSE;
  290. }
  291. BREAKITER_METHOD_FETCH_OBJECT;
  292. UBool res = bio->biter->isBoundary((int32_t)offset);
  293. RETURN_BOOL((long)res);
  294. }
  295. U_CFUNC PHP_FUNCTION(breakiter_get_locale)
  296. {
  297. long locale_type;
  298. BREAKITER_METHOD_INIT_VARS;
  299. object = getThis();
  300. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &locale_type) == FAILURE) {
  301. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  302. "breakiter_get_locale: bad arguments", 0 TSRMLS_CC);
  303. RETURN_FALSE;
  304. }
  305. if (locale_type != ULOC_ACTUAL_LOCALE && locale_type != ULOC_VALID_LOCALE) {
  306. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  307. "breakiter_get_locale: invalid locale type", 0 TSRMLS_CC);
  308. RETURN_FALSE;
  309. }
  310. BREAKITER_METHOD_FETCH_OBJECT;
  311. Locale locale = bio->biter->getLocale((ULocDataLocaleType)locale_type,
  312. BREAKITER_ERROR_CODE(bio));
  313. INTL_METHOD_CHECK_STATUS(bio,
  314. "breakiter_get_locale: Call to ICU method has failed");
  315. RETURN_STRING(locale.getName(), 1);
  316. }
  317. U_CFUNC PHP_FUNCTION(breakiter_get_parts_iterator)
  318. {
  319. long key_type = 0;
  320. BREAKITER_METHOD_INIT_VARS;
  321. object = getThis();
  322. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &key_type) == FAILURE) {
  323. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  324. "breakiter_get_parts_iterator: bad arguments", 0 TSRMLS_CC);
  325. RETURN_FALSE;
  326. }
  327. if (key_type != PARTS_ITERATOR_KEY_SEQUENTIAL
  328. && key_type != PARTS_ITERATOR_KEY_LEFT
  329. && key_type != PARTS_ITERATOR_KEY_RIGHT) {
  330. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  331. "breakiter_get_parts_iterator: bad key type", 0 TSRMLS_CC);
  332. RETURN_FALSE;
  333. }
  334. BREAKITER_METHOD_FETCH_OBJECT;
  335. IntlIterator_from_BreakIterator_parts(
  336. object, return_value, (parts_iter_key_type)key_type TSRMLS_CC);
  337. }
  338. U_CFUNC PHP_FUNCTION(breakiter_get_error_code)
  339. {
  340. BREAKITER_METHOD_INIT_VARS;
  341. object = getThis();
  342. if (zend_parse_parameters_none() == FAILURE) {
  343. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  344. "breakiter_get_error_code: bad arguments", 0 TSRMLS_CC);
  345. RETURN_FALSE;
  346. }
  347. /* Fetch the object (without resetting its last error code ). */
  348. bio = (BreakIterator_object*)zend_object_store_get_object(object TSRMLS_CC);
  349. if (bio == NULL)
  350. RETURN_FALSE;
  351. RETURN_LONG((long)BREAKITER_ERROR_CODE(bio));
  352. }
  353. U_CFUNC PHP_FUNCTION(breakiter_get_error_message)
  354. {
  355. const char* message = NULL;
  356. BREAKITER_METHOD_INIT_VARS;
  357. object = getThis();
  358. if (zend_parse_parameters_none() == FAILURE) {
  359. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  360. "breakiter_get_error_message: bad arguments", 0 TSRMLS_CC );
  361. RETURN_FALSE;
  362. }
  363. /* Fetch the object (without resetting its last error code ). */
  364. bio = (BreakIterator_object*)zend_object_store_get_object(object TSRMLS_CC);
  365. if (bio == NULL)
  366. RETURN_FALSE;
  367. /* Return last error message. */
  368. message = intl_error_get_message(BREAKITER_ERROR_P(bio) TSRMLS_CC);
  369. RETURN_STRING(message, 0);
  370. }