PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/intl/grapheme/grapheme_util.c

http://github.com/infusion/PHP
C | 619 lines | 392 code | 148 blank | 79 comment | 98 complexity | dfcba022b659847c59a35e8e7ebd47b9 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause
  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. | Author: Ed Batutis <ed@batutis.com> |
  14. +----------------------------------------------------------------------+
  15. */
  16. /* {{{ includes */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include <php.h>
  21. #include "grapheme.h"
  22. #include "grapheme_util.h"
  23. #include "intl_common.h"
  24. #include <unicode/utypes.h>
  25. #include <unicode/ucol.h>
  26. #include <unicode/ustring.h>
  27. #include <unicode/ubrk.h>
  28. #include "ext/standard/php_string.h"
  29. ZEND_EXTERN_MODULE_GLOBALS( intl )
  30. /* }}} */
  31. /* {{{ grapheme_close_global_iterator - clean up */
  32. void
  33. grapheme_close_global_iterator( TSRMLS_D )
  34. {
  35. UBreakIterator *global_break_iterator = INTL_G( grapheme_iterator );
  36. if ( NULL != global_break_iterator ) {
  37. ubrk_close(global_break_iterator);
  38. }
  39. }
  40. /* }}} */
  41. /* {{{ grapheme_intl_case_fold: convert string to lowercase */
  42. void
  43. grapheme_intl_case_fold(UChar** ptr_to_free, UChar **str, int32_t *str_len, UErrorCode *pstatus )
  44. {
  45. UChar *dest;
  46. int32_t dest_len, size_required;
  47. /* allocate a destination string that is a bit larger than the src, hoping that is enough */
  48. dest_len = (*str_len) + ( *str_len / 10 );
  49. dest = (UChar*) eumalloc(dest_len);
  50. *pstatus = U_ZERO_ERROR;
  51. size_required = u_strFoldCase(dest, dest_len, *str, *str_len, U_FOLD_CASE_DEFAULT, pstatus);
  52. dest_len = size_required;
  53. if ( U_BUFFER_OVERFLOW_ERROR == *pstatus ) {
  54. dest = (UChar*) eurealloc(dest, dest_len);
  55. *pstatus = U_ZERO_ERROR;
  56. size_required = u_strFoldCase(dest, dest_len, *str, *str_len, U_FOLD_CASE_DEFAULT, pstatus);
  57. }
  58. if ( U_FAILURE(*pstatus) ) {
  59. return;
  60. }
  61. if ( NULL != ptr_to_free) {
  62. efree(*ptr_to_free);
  63. *ptr_to_free = dest;
  64. }
  65. *str = dest;
  66. *str_len = dest_len;
  67. return;
  68. }
  69. /* }}} */
  70. /* {{{ grapheme_substr_ascii f='from' - starting point, l='length' */
  71. void
  72. grapheme_substr_ascii(char *str, int str_len, int f, int l, int argc, char **sub_str, int *sub_str_len)
  73. {
  74. *sub_str = NULL;
  75. if (argc > 2) {
  76. if ((l < 0 && -l > str_len)) {
  77. return;
  78. } else if (l > str_len) {
  79. l = str_len;
  80. }
  81. } else {
  82. l = str_len;
  83. }
  84. if (f > str_len || (f < 0 && -f > str_len)) {
  85. return;
  86. }
  87. if (l < 0 && (l + str_len - f) < 0) {
  88. return;
  89. }
  90. /* if "from" position is negative, count start position from the end
  91. * of the string
  92. */
  93. if (f < 0) {
  94. f = str_len + f;
  95. if (f < 0) {
  96. f = 0;
  97. }
  98. }
  99. /* if "length" position is negative, set it to the length
  100. * needed to stop that many chars from the end of the string
  101. */
  102. if (l < 0) {
  103. l = (str_len - f) + l;
  104. if (l < 0) {
  105. l = 0;
  106. }
  107. }
  108. if (f >= str_len) {
  109. return;
  110. }
  111. if ((f + l) > str_len) {
  112. l = str_len - f;
  113. }
  114. *sub_str = str + f;
  115. *sub_str_len = l;
  116. return;
  117. }
  118. /* }}} */
  119. /* {{{ grapheme_strrpos_utf16 - strrpos using utf16 */
  120. int
  121. grapheme_strrpos_utf16(unsigned char *haystack, int32_t haystack_len, unsigned char*needle, int32_t needle_len, int32_t offset, int f_ignore_case TSRMLS_DC)
  122. {
  123. UChar *uhaystack, *puhaystack, *uhaystack_end, *uneedle;
  124. int32_t uhaystack_len, uneedle_len;
  125. UErrorCode status;
  126. unsigned char u_break_iterator_buffer[U_BRK_SAFECLONE_BUFFERSIZE];
  127. UBreakIterator* bi = NULL;
  128. int ret_pos, pos;
  129. /* convert the strings to UTF-16. */
  130. uhaystack = NULL;
  131. uhaystack_len = 0;
  132. status = U_ZERO_ERROR;
  133. intl_convert_utf8_to_utf16(&uhaystack, &uhaystack_len, (char *) haystack, haystack_len, &status );
  134. if ( U_FAILURE( status ) ) {
  135. /* Set global error code. */
  136. intl_error_set_code( NULL, status TSRMLS_CC );
  137. /* Set error messages. */
  138. intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 TSRMLS_CC );
  139. efree( uhaystack );
  140. return -1;
  141. }
  142. if ( f_ignore_case ) {
  143. grapheme_intl_case_fold(&uhaystack, &uhaystack, &uhaystack_len, &status );
  144. }
  145. /* get a pointer to the haystack taking into account the offset */
  146. bi = NULL;
  147. status = U_ZERO_ERROR;
  148. bi = grapheme_get_break_iterator(u_break_iterator_buffer, &status TSRMLS_CC );
  149. puhaystack = grapheme_get_haystack_offset(bi, uhaystack, uhaystack_len, offset);
  150. if ( NULL == puhaystack ) {
  151. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Offset not contained in string", 1 TSRMLS_CC );
  152. efree( uhaystack );
  153. ubrk_close (bi);
  154. return -1;
  155. }
  156. uneedle = NULL;
  157. uneedle_len = 0;
  158. status = U_ZERO_ERROR;
  159. intl_convert_utf8_to_utf16(&uneedle, &uneedle_len, (char *) needle, needle_len, &status );
  160. if ( U_FAILURE( status ) ) {
  161. /* Set global error code. */
  162. intl_error_set_code( NULL, status TSRMLS_CC );
  163. /* Set error messages. */
  164. intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 TSRMLS_CC );
  165. efree( uhaystack );
  166. efree( uneedle );
  167. ubrk_close (bi);
  168. return -1;
  169. }
  170. if ( f_ignore_case ) {
  171. grapheme_intl_case_fold(&uneedle, &uneedle, &uneedle_len, &status );
  172. }
  173. ret_pos = -1; /* -1 represents 'not found' */
  174. /* back up until there's needle_len characters to compare */
  175. uhaystack_end = uhaystack + uhaystack_len;
  176. pos = ubrk_last(bi);
  177. puhaystack = uhaystack + pos;
  178. while ( uhaystack_end - puhaystack < uneedle_len ) {
  179. pos = ubrk_previous(bi);
  180. if ( UBRK_DONE == pos ) {
  181. break;
  182. }
  183. puhaystack = uhaystack + pos;
  184. }
  185. /* is there enough haystack left to hold the needle? */
  186. if ( ( uhaystack_end - puhaystack ) < uneedle_len ) {
  187. /* not enough, not found */
  188. goto exit;
  189. }
  190. while ( UBRK_DONE != pos ) {
  191. if (!u_memcmp(uneedle, puhaystack, uneedle_len)) { /* needle_len - 1 in zend memnstr? */
  192. /* does the grapheme in the haystack end at the same place as the last grapheme in the needle? */
  193. if ( ubrk_isBoundary(bi, pos + uneedle_len) ) {
  194. /* found it, get grapheme count offset */
  195. ret_pos = grapheme_count_graphemes(bi, uhaystack, pos);
  196. break;
  197. }
  198. /* set position back */
  199. ubrk_isBoundary(bi, pos);
  200. }
  201. pos = ubrk_previous(bi);
  202. puhaystack = uhaystack + pos;
  203. }
  204. exit:
  205. efree( uhaystack );
  206. efree( uneedle );
  207. ubrk_close (bi);
  208. return ret_pos;
  209. }
  210. /* }}} */
  211. /* {{{ grapheme_strpos_utf16 - strrpos using utf16*/
  212. int
  213. grapheme_strpos_utf16(unsigned char *haystack, int32_t haystack_len, unsigned char*needle, int32_t needle_len, int32_t offset, int32_t *puchar_pos, int f_ignore_case TSRMLS_DC)
  214. {
  215. UChar *uhaystack, *puhaystack, *uneedle;
  216. int32_t uhaystack_len, uneedle_len;
  217. int ret_pos;
  218. unsigned char u_break_iterator_buffer[U_BRK_SAFECLONE_BUFFERSIZE];
  219. UBreakIterator* bi;
  220. UErrorCode status;
  221. *puchar_pos = -1;
  222. /* convert the strings to UTF-16. */
  223. uhaystack = NULL;
  224. uhaystack_len = 0;
  225. status = U_ZERO_ERROR;
  226. intl_convert_utf8_to_utf16(&uhaystack, &uhaystack_len, (char *) haystack, haystack_len, &status );
  227. if ( U_FAILURE( status ) ) {
  228. /* Set global error code. */
  229. intl_error_set_code( NULL, status TSRMLS_CC );
  230. /* Set error messages. */
  231. intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 TSRMLS_CC );
  232. efree( uhaystack );
  233. return -1;
  234. }
  235. /* get a pointer to the haystack taking into account the offset */
  236. bi = NULL;
  237. status = U_ZERO_ERROR;
  238. bi = grapheme_get_break_iterator(u_break_iterator_buffer, &status TSRMLS_CC );
  239. puhaystack = grapheme_get_haystack_offset(bi, uhaystack, uhaystack_len, offset);
  240. uhaystack_len = (uhaystack_len - ( puhaystack - uhaystack));
  241. if ( NULL == puhaystack ) {
  242. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Offset not contained in string", 1 TSRMLS_CC );
  243. efree( uhaystack );
  244. ubrk_close (bi);
  245. return -1;
  246. }
  247. if ( f_ignore_case ) {
  248. grapheme_intl_case_fold(&uhaystack, &puhaystack, &uhaystack_len, &status );
  249. }
  250. uneedle = NULL;
  251. uneedle_len = 0;
  252. status = U_ZERO_ERROR;
  253. intl_convert_utf8_to_utf16(&uneedle, &uneedle_len, (char *) needle, needle_len, &status );
  254. if ( U_FAILURE( status ) ) {
  255. /* Set global error code. */
  256. intl_error_set_code( NULL, status TSRMLS_CC );
  257. /* Set error messages. */
  258. intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 TSRMLS_CC );
  259. efree( uhaystack );
  260. efree( uneedle );
  261. ubrk_close (bi);
  262. return -1;
  263. }
  264. if ( f_ignore_case ) {
  265. grapheme_intl_case_fold(&uneedle, &uneedle, &uneedle_len, &status );
  266. }
  267. ret_pos = grapheme_memnstr_grapheme(bi, puhaystack, uneedle, uneedle_len, puhaystack + uhaystack_len );
  268. *puchar_pos = ubrk_current(bi);
  269. efree( uhaystack );
  270. efree( uneedle );
  271. ubrk_close (bi);
  272. return ret_pos;
  273. }
  274. /* }}} */
  275. /* {{{ grapheme_ascii_check: ASCII check */
  276. int grapheme_ascii_check(const unsigned char *day, int32_t len)
  277. {
  278. int ret_len = len;
  279. while ( len-- ) {
  280. if ( *day++ > 0x7f )
  281. return -1;
  282. }
  283. return ret_len;
  284. }
  285. /* }}} */
  286. /* {{{ grapheme_split_string: find and optionally return grapheme boundaries */
  287. int grapheme_split_string(const UChar *text, int32_t text_length, int boundary_array[], int boundary_array_len TSRMLS_DC )
  288. {
  289. unsigned char u_break_iterator_buffer[U_BRK_SAFECLONE_BUFFERSIZE];
  290. UErrorCode status = U_ZERO_ERROR;
  291. int ret_len, pos;
  292. UBreakIterator* bi;
  293. bi = grapheme_get_break_iterator((void*)u_break_iterator_buffer, &status TSRMLS_CC );
  294. if( U_FAILURE(status) ) {
  295. return -1;
  296. }
  297. ubrk_setText(bi, text, text_length, &status);
  298. pos = 0;
  299. for ( ret_len = 0; pos != UBRK_DONE; ) {
  300. pos = ubrk_next(bi);
  301. if ( pos != UBRK_DONE ) {
  302. if ( NULL != boundary_array && ret_len < boundary_array_len ) {
  303. boundary_array[ret_len] = pos;
  304. }
  305. ret_len++;
  306. }
  307. }
  308. ubrk_close(bi);
  309. return ret_len;
  310. }
  311. /* }}} */
  312. /* {{{ grapheme_count_graphemes */
  313. inline int32_t
  314. grapheme_count_graphemes(UBreakIterator *bi, UChar *string, int32_t string_len)
  315. {
  316. int ret_len = 0;
  317. int pos = 0;
  318. UErrorCode status = U_ZERO_ERROR;
  319. ubrk_setText(bi, string, string_len, &status);
  320. do {
  321. pos = ubrk_next(bi);
  322. if ( UBRK_DONE != pos ) {
  323. ret_len++;
  324. }
  325. } while ( UBRK_DONE != pos );
  326. return ret_len;
  327. }
  328. /* }}} */
  329. /* {{{ grapheme_memnstr_grapheme: find needle in haystack using grapheme boundaries */
  330. inline int32_t
  331. grapheme_memnstr_grapheme(UBreakIterator *bi, UChar *haystack, UChar *needle, int32_t needle_len, UChar *end)
  332. {
  333. UChar *p = haystack;
  334. UChar ne = needle[needle_len-1];
  335. UErrorCode status;
  336. int32_t grapheme_offset;
  337. end -= needle_len;
  338. while (p <= end) {
  339. if ((p = u_memchr(p, *needle, (end-p+1))) && ne == p[needle_len-1]) {
  340. if (!u_memcmp(needle, p, needle_len - 1)) { /* needle_len - 1 works because if needle_len is 1, we've already tested the char */
  341. /* does the grapheme end here? */
  342. status = U_ZERO_ERROR;
  343. ubrk_setText (bi, haystack, (end - haystack) + needle_len, &status);
  344. if ( ubrk_isBoundary (bi, (p - haystack) + needle_len) ) {
  345. /* found it, get grapheme count offset */
  346. grapheme_offset = grapheme_count_graphemes(bi, haystack, (p - haystack));
  347. return grapheme_offset;
  348. }
  349. }
  350. }
  351. if (p == NULL) {
  352. return -1;
  353. }
  354. p++;
  355. }
  356. return -1;
  357. }
  358. /* }}} */
  359. /* {{{ grapheme_memrstr_grapheme: reverse find needle in haystack using grapheme boundaries */
  360. inline void *grapheme_memrchr_grapheme(const void *s, int c, int32_t n)
  361. {
  362. register unsigned char *e;
  363. if (n <= 0) {
  364. return NULL;
  365. }
  366. for (e = (unsigned char *)s + n - 1; e >= (unsigned char *)s; e--) {
  367. if (*e == (unsigned char)c) {
  368. return (void *)e;
  369. }
  370. }
  371. return NULL;
  372. }
  373. /* }}} */
  374. /* {{{ grapheme_get_haystack_offset - bump the haystack pointer based on the grapheme count offset */
  375. UChar *
  376. grapheme_get_haystack_offset(UBreakIterator* bi, UChar *uhaystack, int32_t uhaystack_len, int32_t offset)
  377. {
  378. UErrorCode status;
  379. int32_t pos;
  380. int32_t (*iter_op)(UBreakIterator* bi);
  381. int iter_incr;
  382. if ( NULL != bi ) {
  383. status = U_ZERO_ERROR;
  384. ubrk_setText (bi, uhaystack, uhaystack_len, &status);
  385. }
  386. if ( 0 == offset ) {
  387. return uhaystack;
  388. }
  389. if ( offset < 0 ) {
  390. iter_op = ubrk_previous;
  391. ubrk_last(bi); /* one past the end */
  392. iter_incr = 1;
  393. }
  394. else {
  395. iter_op = ubrk_next;
  396. iter_incr = -1;
  397. }
  398. pos = 0;
  399. while ( pos != UBRK_DONE && offset != 0 ) {
  400. pos = iter_op(bi);
  401. if ( UBRK_DONE != pos ) {
  402. offset += iter_incr;
  403. }
  404. }
  405. if ( offset != 0 ) {
  406. return NULL;
  407. }
  408. return uhaystack + pos;
  409. }
  410. /* }}} */
  411. /* {{{ grapheme_strrpos_ascii: borrowed from the php ext/standard/string.c */
  412. int32_t
  413. grapheme_strrpos_ascii(unsigned char *haystack, int32_t haystack_len, unsigned char *needle, int32_t needle_len, int32_t offset)
  414. {
  415. unsigned char *p, *e;
  416. if (offset >= 0) {
  417. p = haystack + offset;
  418. e = haystack + haystack_len - needle_len;
  419. } else {
  420. p = haystack;
  421. if (needle_len > -offset) {
  422. e = haystack + haystack_len - needle_len;
  423. } else {
  424. e = haystack + haystack_len + offset;
  425. }
  426. }
  427. if (needle_len == 1) {
  428. /* Single character search can shortcut memcmps */
  429. while (e >= p) {
  430. if (*e == *needle) {
  431. return (e - p + (offset > 0 ? offset : 0));
  432. }
  433. e--;
  434. }
  435. return -1;
  436. }
  437. while (e >= p) {
  438. if (memcmp(e, needle, needle_len) == 0) {
  439. return (e - p + (offset > 0 ? offset : 0));
  440. }
  441. e--;
  442. }
  443. return -1;
  444. }
  445. /* }}} */
  446. /* {{{ grapheme_get_break_iterator: get a clone of the global character break iterator */
  447. UBreakIterator*
  448. grapheme_get_break_iterator(void *stack_buffer, UErrorCode *status TSRMLS_DC )
  449. {
  450. int32_t buffer_size;
  451. UBreakIterator *global_break_iterator = INTL_G( grapheme_iterator );
  452. if ( NULL == global_break_iterator ) {
  453. global_break_iterator = ubrk_open(UBRK_CHARACTER,
  454. NULL, /* icu default locale - locale has no effect on this iterator */
  455. NULL, /* text not set in global iterator */
  456. 0, /* text length = 0 */
  457. status);
  458. INTL_G(grapheme_iterator) = global_break_iterator;
  459. }
  460. buffer_size = U_BRK_SAFECLONE_BUFFERSIZE;
  461. return ubrk_safeClone(global_break_iterator, stack_buffer, &buffer_size, status);
  462. }
  463. /* }}} */
  464. /*
  465. * Local variables:
  466. * tab-width: 4
  467. * c-basic-offset: 4
  468. * End:
  469. * vim600: fdm=marker
  470. * vim: noet sw=4 ts=4
  471. */