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: Stanislav Malyshev <stas@zend.com> |
14 +----------------------------------------------------------------------+
15 */
16
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include "php_intl.h"
22
23 #include <unicode/ustring.h>
24 #include <locale.h>
25
26 #include "formatter_class.h"
27 #include "formatter_format.h"
28 #include "formatter_parse.h"
29 #include "intl_convert.h"
30
31 #define ICU_LOCALE_BUG 1
32
33 /* {{{ proto mixed NumberFormatter::parse( string $str[, int $type, int &$position ])
34 * Parse a number. }}} */
35 /* {{{ proto mixed numfmt_parse( NumberFormatter $nf, string $str[, int $type, int &$position ])
36 * Parse a number.
37 */
PHP_FUNCTION(numfmt_parse)38 PHP_FUNCTION( numfmt_parse )
39 {
40 zend_long type = FORMAT_TYPE_DOUBLE;
41 UChar* sstr = NULL;
42 int32_t sstr_len = 0;
43 char* str = NULL;
44 size_t str_len;
45 int32_t val32, position = 0;
46 int64_t val64;
47 double val_double;
48 int32_t* position_p = NULL;
49 zval *zposition = NULL;
50 char *oldlocale;
51 FORMATTER_METHOD_INIT_VARS;
52
53 /* Parse parameters. */
54 if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Os|lz/!",
55 &object, NumberFormatter_ce_ptr, &str, &str_len, &type, &zposition ) == FAILURE )
56 {
57 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
58 "number_parse: unable to parse input params", 0 );
59
60 RETURN_FALSE;
61 }
62
63 /* Fetch the object. */
64 FORMATTER_METHOD_FETCH_OBJECT;
65
66 /* Convert given string to UTF-16. */
67 intl_convert_utf8_to_utf16(&sstr, &sstr_len, str, str_len, &INTL_DATA_ERROR_CODE(nfo));
68 INTL_METHOD_CHECK_STATUS( nfo, "String conversion to UTF-16 failed" );
69
70 if(zposition) {
71 ZVAL_DEREF(zposition);
72 position = (int32_t)zval_get_long( zposition );
73 position_p = &position;
74 }
75
76 #if ICU_LOCALE_BUG && defined(LC_NUMERIC)
77 /* need to copy here since setlocale may change it later */
78 oldlocale = estrdup(setlocale(LC_NUMERIC, NULL));
79 setlocale(LC_NUMERIC, "C");
80 #endif
81
82 switch(type) {
83 case FORMAT_TYPE_INT32:
84 val32 = unum_parse(FORMATTER_OBJECT(nfo), sstr, sstr_len, position_p, &INTL_DATA_ERROR_CODE(nfo));
85 RETVAL_LONG(val32);
86 break;
87 case FORMAT_TYPE_INT64:
88 val64 = unum_parseInt64(FORMATTER_OBJECT(nfo), sstr, sstr_len, position_p, &INTL_DATA_ERROR_CODE(nfo));
89 if(val64 > ZEND_LONG_MAX || val64 < ZEND_LONG_MIN) {
90 RETVAL_DOUBLE(val64);
91 } else {
92 RETVAL_LONG((zend_long)val64);
93 }
94 break;
95 case FORMAT_TYPE_DOUBLE:
96 val_double = unum_parseDouble(FORMATTER_OBJECT(nfo), sstr, sstr_len, position_p, &INTL_DATA_ERROR_CODE(nfo));
97 RETVAL_DOUBLE(val_double);
98 break;
99 default:
100 php_error_docref(NULL, E_WARNING, "Unsupported format type " ZEND_LONG_FMT, type);
101 RETVAL_FALSE;
102 break;
103 }
104 #if ICU_LOCALE_BUG && defined(LC_NUMERIC)
105 setlocale(LC_NUMERIC, oldlocale);
106 efree(oldlocale);
107 #endif
108 if(zposition) {
109 zval_ptr_dtor(zposition);
110 ZVAL_LONG(zposition, position);
111 }
112
113 if (sstr) {
114 efree(sstr);
115 }
116
117 INTL_METHOD_CHECK_STATUS( nfo, "Number parsing failed" );
118 }
119 /* }}} */
120
121 /* {{{ proto float NumberFormatter::parseCurrency( string $str, string &$currency[, int &$position] )
122 * Parse a number as currency. }}} */
123 /* {{{ proto float numfmt_parse_currency( NumberFormatter $nf, string $str, string &$currency[, int &$position] )
124 * Parse a number as currency.
125 */
PHP_FUNCTION(numfmt_parse_currency)126 PHP_FUNCTION( numfmt_parse_currency )
127 {
128 double number;
129 UChar currency[5] = {0};
130 UChar* sstr = NULL;
131 int32_t sstr_len = 0;
132 zend_string *u8str;
133 char *str;
134 size_t str_len;
135 int32_t* position_p = NULL;
136 int32_t position = 0;
137 zval *zcurrency, *zposition = NULL;
138 FORMATTER_METHOD_INIT_VARS;
139
140 /* Parse parameters. */
141 if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Osz/|z/!",
142 &object, NumberFormatter_ce_ptr, &str, &str_len, &zcurrency, &zposition ) == FAILURE )
143 {
144 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
145 "number_parse_currency: unable to parse input params", 0 );
146
147 RETURN_FALSE;
148 }
149
150 /* Fetch the object. */
151 FORMATTER_METHOD_FETCH_OBJECT;
152
153 /* Convert given string to UTF-16. */
154 intl_convert_utf8_to_utf16(&sstr, &sstr_len, str, str_len, &INTL_DATA_ERROR_CODE(nfo));
155 INTL_METHOD_CHECK_STATUS( nfo, "String conversion to UTF-16 failed" );
156
157 if(zposition) {
158 ZVAL_DEREF(zposition);
159 position = (int32_t)zval_get_long( zposition );
160 position_p = &position;
161 }
162
163 number = unum_parseDoubleCurrency(FORMATTER_OBJECT(nfo), sstr, sstr_len, position_p, currency, &INTL_DATA_ERROR_CODE(nfo));
164 if(zposition) {
165 zval_ptr_dtor(zposition);
166 ZVAL_LONG(zposition, position);
167 }
168 if (sstr) {
169 efree(sstr);
170 }
171 INTL_METHOD_CHECK_STATUS( nfo, "Number parsing failed" );
172
173 /* Convert parsed currency to UTF-8 and pass it back to caller. */
174 u8str = intl_convert_utf16_to_utf8(currency, u_strlen(currency), &INTL_DATA_ERROR_CODE(nfo));
175 INTL_METHOD_CHECK_STATUS( nfo, "Currency conversion to UTF-8 failed" );
176 zval_ptr_dtor( zcurrency );
177 ZVAL_NEW_STR(zcurrency, u8str);
178
179 RETVAL_DOUBLE( number );
180 }
181 /* }}} */
182
183 /*
184 * Local variables:
185 * tab-width: 4
186 * c-basic-offset: 4
187 * End:
188 * vim600: noet sw=4 ts=4 fdm=marker
189 * vim<600: noet sw=4 ts=4
190 */
191