1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 6 |
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: Andrei Zmievski <andrei@php.net> |
14 : +----------------------------------------------------------------------+
15 : */
16 :
17 : /* $Id: transform.c 226099 2007-01-01 09:29:37Z sebastian $ */
18 :
19 : #include "php_unicode.h"
20 :
21 : /* {{{ proto string str_transliterate(string str, string from_script, string to_script[, string variant]) U
22 : Transliterate a string from the source script to the target script */
23 : PHP_FUNCTION(str_transliterate)
24 0 : {
25 0 : UChar *str, *from, *to, *variant = NULL;
26 0 : int str_len, from_len, to_len, variant_len = 0;
27 : UChar id[256];
28 : int id_len;
29 0 : UChar *result = NULL;
30 0 : int result_len = 0;
31 0 : UTransliterator *trans = NULL;
32 0 : UErrorCode status = U_ZERO_ERROR;
33 : int32_t capacity, limit;
34 :
35 0 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "uuu|u", &str,
36 : &str_len, &from, &from_len, &to, &to_len,
37 : &variant, &variant_len) == FAILURE) {
38 0 : return;
39 : }
40 :
41 0 : if (variant) {
42 0 : id_len = u_snprintf(id, sizeof(id)-1, "%S-%S/%S", from, to, variant);
43 : } else {
44 0 : id_len = u_snprintf(id, sizeof(id)-1, "%S-%S", from, to);
45 : }
46 :
47 0 : if (id_len < 0) {
48 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Transliterator ID too long");
49 0 : return;
50 : }
51 :
52 0 : id[id_len] = 0;
53 :
54 0 : trans = utrans_openU(id, id_len, UTRANS_FORWARD, NULL, 0, NULL, &status);
55 0 : if (U_FAILURE(status)) {
56 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to create transliterator");
57 0 : return;
58 : }
59 :
60 0 : result = eustrndup(str, str_len);
61 0 : result_len = limit = str_len;
62 0 : capacity = str_len + 1;
63 :
64 : while (1) {
65 0 : utrans_transUChars(trans, result, &result_len, capacity, 0, &limit, &status);
66 0 : if (status == U_BUFFER_OVERFLOW_ERROR) {
67 0 : result = eurealloc(result, result_len + 1);
68 0 : memcpy(result, str, UBYTES(str_len));
69 0 : capacity = result_len + 1;
70 0 : result_len = limit = str_len;
71 0 : status = U_ZERO_ERROR;
72 0 : utrans_transUChars(trans, result, &result_len, capacity, 0, &limit, &status);
73 : } else {
74 0 : if (status == U_STRING_NOT_TERMINATED_WARNING) {
75 0 : result = eurealloc(result, result_len + 1);
76 : }
77 : break;
78 : }
79 0 : }
80 :
81 0 : result[result_len] = 0;
82 0 : utrans_close(trans);
83 :
84 0 : RETURN_UNICODEL(result, result_len, 0);
85 : }
86 : /* }}} */
87 :
88 : /*
89 : * Local variables:
90 : * tab-width: 4
91 : * c-basic-offset: 4
92 : * End:
93 : * vim600: noet sw=4 ts=4 fdm=marker
94 : * vim<600: noet sw=4 ts=4
95 : */
|