breakiterator_methods.cpp 11 KB

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