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