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 		convert_to_long(zposition);
73 		position = (int32_t)Z_LVAL_P( zposition );
74 		position_p = &position;
75 	}
76 
77 #if ICU_LOCALE_BUG && defined(LC_NUMERIC)
78 	/* need to copy here since setlocale may change it later */
79 	oldlocale = estrdup(setlocale(LC_NUMERIC, NULL));
80 	setlocale(LC_NUMERIC, "C");
81 #endif
82 
83 	switch(type) {
84 		case FORMAT_TYPE_INT32:
85 			val32 = unum_parse(FORMATTER_OBJECT(nfo), sstr, sstr_len, position_p, &INTL_DATA_ERROR_CODE(nfo));
86 			RETVAL_LONG(val32);
87 			break;
88 		case FORMAT_TYPE_INT64:
89 			val64 = unum_parseInt64(FORMATTER_OBJECT(nfo), sstr, sstr_len, position_p, &INTL_DATA_ERROR_CODE(nfo));
90 			if(val64 > ZEND_LONG_MAX || val64 < ZEND_LONG_MIN) {
91 				RETVAL_DOUBLE(val64);
92 			} else {
93 				RETVAL_LONG((zend_long)val64);
94 			}
95 			break;
96 		case FORMAT_TYPE_DOUBLE:
97 			val_double = unum_parseDouble(FORMATTER_OBJECT(nfo), sstr, sstr_len, position_p, &INTL_DATA_ERROR_CODE(nfo));
98 			RETVAL_DOUBLE(val_double);
99 			break;
100 		default:
101 			php_error_docref(NULL, E_WARNING, "Unsupported format type " ZEND_LONG_FMT, type);
102 			RETVAL_FALSE;
103 			break;
104 	}
105 #if ICU_LOCALE_BUG && defined(LC_NUMERIC)
106 	setlocale(LC_NUMERIC, oldlocale);
107 	efree(oldlocale);
108 #endif
109 	if(zposition) {
110 		zval_dtor(zposition);
111 		ZVAL_LONG(zposition, position);
112 	}
113 
114 	if (sstr) {
115 		efree(sstr);
116 	}
117 
118 	INTL_METHOD_CHECK_STATUS( nfo, "Number parsing failed" );
119 }
120 /* }}} */
121 
122 /* {{{ proto double NumberFormatter::parseCurrency( string $str, string $&currency[, int $&position] )
123  * Parse a number as currency. }}} */
124 /* {{{ proto double numfmt_parse_currency( NumberFormatter $nf, string $str, string $&currency[, int $&position] )
125  * Parse a number as currency.
126  */
PHP_FUNCTION(numfmt_parse_currency)127 PHP_FUNCTION( numfmt_parse_currency )
128 {
129 	double number;
130 	UChar currency[5] = {0};
131 	UChar* sstr = NULL;
132 	int32_t sstr_len = 0;
133 	zend_string *u8str;
134 	char *str;
135 	size_t str_len;
136 	int32_t* position_p = NULL;
137 	int32_t position = 0;
138 	zval *zcurrency, *zposition = NULL;
139 	FORMATTER_METHOD_INIT_VARS;
140 
141 	/* Parse parameters. */
142 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Osz/|z/!",
143 		&object, NumberFormatter_ce_ptr,  &str, &str_len, &zcurrency, &zposition ) == FAILURE )
144 	{
145 		intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
146 			"number_parse_currency: unable to parse input params", 0 );
147 
148 		RETURN_FALSE;
149 	}
150 
151 	/* Fetch the object. */
152 	FORMATTER_METHOD_FETCH_OBJECT;
153 
154 	/* Convert given string to UTF-16. */
155 	intl_convert_utf8_to_utf16(&sstr, &sstr_len, str, str_len, &INTL_DATA_ERROR_CODE(nfo));
156 	INTL_METHOD_CHECK_STATUS( nfo, "String conversion to UTF-16 failed" );
157 
158 	if(zposition) {
159 		ZVAL_DEREF(zposition);
160 		convert_to_long(zposition);
161 		position = (int32_t)Z_LVAL_P( zposition );
162 		position_p = &position;
163 	}
164 
165 	number = unum_parseDoubleCurrency(FORMATTER_OBJECT(nfo), sstr, sstr_len, position_p, currency, &INTL_DATA_ERROR_CODE(nfo));
166 	if(zposition) {
167 		zval_dtor(zposition);
168 		ZVAL_LONG(zposition, position);
169 	}
170 	if (sstr) {
171 		efree(sstr);
172 	}
173 	INTL_METHOD_CHECK_STATUS( nfo, "Number parsing failed" );
174 
175 	/* Convert parsed currency to UTF-8 and pass it back to caller. */
176 	u8str = intl_convert_utf16_to_utf8(currency, u_strlen(currency), &INTL_DATA_ERROR_CODE(nfo));
177 	INTL_METHOD_CHECK_STATUS( nfo, "Currency conversion to UTF-8 failed" );
178 	zval_dtor( zcurrency );
179 	ZVAL_NEW_STR(zcurrency, u8str);
180 
181 	RETVAL_DOUBLE( number );
182 }
183 /* }}} */
184 
185 /*
186  * Local variables:
187  * tab-width: 4
188  * c-basic-offset: 4
189  * End:
190  * vim600: noet sw=4 ts=4 fdm=marker
191  * vim<600: noet sw=4 ts=4
192  */
193