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