-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Expand file tree
/
Copy pathformatter_format.c
More file actions
248 lines (224 loc) · 9.2 KB
/
Copy pathformatter_format.c
File metadata and controls
248 lines (224 loc) · 9.2 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
+----------------------------------------------------------------------+
| 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: |
| https://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 "php_intl.h"
#include <unicode/unumberformatter.h>
#include <unicode/ustring.h>
#include "formatter_class.h"
#include "formatter_format.h"
#include "intl_convert.h"
/* {{{ Format a number. */
PHP_FUNCTION( numfmt_format )
{
zval *number;
UFormattedNumber *result = NULL;
zend_long type = FORMAT_TYPE_DEFAULT;
UChar format_buf[32];
UChar* formatted = format_buf;
int32_t formatted_len = USIZE(format_buf);
FORMATTER_METHOD_INIT_VARS;
/* Parse parameters. */
if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "On|l",
&object, NumberFormatter_ce_ptr, &number, &type ) == FAILURE )
{
RETURN_THROWS();
}
/* Fetch the object. */
FORMATTER_METHOD_FETCH_OBJECT2;
if (FORMATTER_OBJECT2(nfo)) {
result = unumf_openResult(&INTL_DATA_ERROR_CODE(nfo));
INTL_METHOD_CHECK_STATUS(nfo, "Error opening formatter result");
}
if(type == FORMAT_TYPE_DEFAULT) {
switch(Z_TYPE_P(number)) {
case IS_LONG:
/* take INT32 on 32-bit, int64 on 64-bit */
type = (sizeof(zend_long) == 8)?FORMAT_TYPE_INT64:FORMAT_TYPE_INT32;
break;
case IS_DOUBLE:
type = FORMAT_TYPE_DOUBLE;
break;
EMPTY_SWITCH_DEFAULT_CASE();
}
}
switch(type) {
case FORMAT_TYPE_INT32:
convert_to_long(number);
if (FORMATTER_OBJECT(nfo)) {
formatted_len = unum_format(FORMATTER_OBJECT(nfo), (int32_t)Z_LVAL_P(number),
formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
} else {
unumf_formatInt(FORMATTER_OBJECT2(nfo), (int32_t)Z_LVAL_P(number), result, &INTL_DATA_ERROR_CODE(nfo));
formatted_len = unumf_resultToString(result, formatted, formatted_len, &INTL_DATA_ERROR_CODE(nfo));
}
INTL_METHOD_CHECK_STATUS( nfo, "Number formatting failed" );
if (INTL_DATA_ERROR_CODE(nfo) == U_BUFFER_OVERFLOW_ERROR) {
intl_error_reset(INTL_DATA_ERROR_P(nfo));
formatted = eumalloc(formatted_len);
if (FORMATTER_OBJECT(nfo)) {
formatted_len = unum_format(FORMATTER_OBJECT(nfo), (int32_t)Z_LVAL_P(number),
formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
} else {
formatted_len = unumf_resultToString(result, formatted, formatted_len, &INTL_DATA_ERROR_CODE(nfo));
}
if (U_FAILURE( INTL_DATA_ERROR_CODE(nfo) ) ) {
efree(formatted);
}
}
break;
case FORMAT_TYPE_INT64:
{
int64_t value = (Z_TYPE_P(number) == IS_DOUBLE)?(int64_t)Z_DVAL_P(number):Z_LVAL_P(number);
if (FORMATTER_OBJECT(nfo)) {
formatted_len = unum_formatInt64(FORMATTER_OBJECT(nfo), value, formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
} else {
unumf_formatInt(FORMATTER_OBJECT2(nfo), value, result, &INTL_DATA_ERROR_CODE(nfo));
INTL_METHOD_CHECK_STATUS( nfo, "Number formatting failed" );
formatted_len = unumf_resultToString(result, formatted, formatted_len, &INTL_DATA_ERROR_CODE(nfo));
}
if (INTL_DATA_ERROR_CODE(nfo) == U_BUFFER_OVERFLOW_ERROR) {
intl_error_reset(INTL_DATA_ERROR_P(nfo));
formatted = eumalloc(formatted_len);
if (FORMATTER_OBJECT(nfo)) {
formatted_len = unum_formatInt64(FORMATTER_OBJECT(nfo), value, formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
} else {
formatted_len = unumf_resultToString(result, formatted, formatted_len, &INTL_DATA_ERROR_CODE(nfo));
}
if (U_FAILURE( INTL_DATA_ERROR_CODE(nfo) ) ) {
efree(formatted);
}
}
INTL_METHOD_CHECK_STATUS( nfo, "Number formatting to string failed" );
}
break;
case FORMAT_TYPE_DOUBLE:
convert_to_double(number);
if (FORMATTER_OBJECT(nfo)) {
formatted_len = unum_formatDouble(FORMATTER_OBJECT(nfo), Z_DVAL_P(number), formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
} else {
unumf_formatDouble(FORMATTER_OBJECT2(nfo), Z_DVAL_P(number), result, &INTL_DATA_ERROR_CODE(nfo));
INTL_METHOD_CHECK_STATUS( nfo, "Number formatting failed" );
formatted_len = unumf_resultToString(result, formatted, formatted_len, &INTL_DATA_ERROR_CODE(nfo));
}
if (INTL_DATA_ERROR_CODE(nfo) == U_BUFFER_OVERFLOW_ERROR) {
intl_error_reset(INTL_DATA_ERROR_P(nfo));
formatted = eumalloc(formatted_len);
if (FORMATTER_OBJECT(nfo)) {
unum_formatDouble(FORMATTER_OBJECT(nfo), Z_DVAL_P(number), formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
} else {
formatted_len = unumf_resultToString(result, formatted, formatted_len, &INTL_DATA_ERROR_CODE(nfo));
}
if (U_FAILURE( INTL_DATA_ERROR_CODE(nfo) ) ) {
efree(formatted);
}
}
INTL_METHOD_CHECK_STATUS( nfo, "Number formatting to string failed" );
break;
case FORMAT_TYPE_CURRENCY:
if (getThis()) {
const char *space;
const char *class_name = get_active_class_name(&space);
zend_argument_value_error(2, "cannot be NumberFormatter::TYPE_CURRENCY constant, "
"use %s%sformatCurrency() method instead", class_name, space);
} else {
zend_argument_value_error(3, "cannot be NumberFormatter::TYPE_CURRENCY constant, use numfmt_format_currency() function instead");
}
RETURN_THROWS();
default:
zend_argument_value_error(hasThis() ? 2 : 3, "must be a NumberFormatter::TYPE_* constant");
RETURN_THROWS();
}
INTL_METHOD_RETVAL_UTF8( nfo, formatted, formatted_len, ( formatted != format_buf ) );
unumf_closeResult(result);
}
/* }}} */
/* {{{ Format a number as currency. */
PHP_FUNCTION( numfmt_format_currency )
{
double number;
UChar format_buf[32];
UChar* formatted = format_buf;
int32_t formatted_len = USIZE(format_buf);
char* currency = NULL;
size_t currency_len = 0;
UChar* scurrency = NULL;
int32_t scurrency_len = 0;
UFormattedNumber *result = NULL;
FORMATTER_METHOD_INIT_VARS;
/* Parse parameters. */
if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Od|s!",
&object, NumberFormatter_ce_ptr, &number, ¤cy, ¤cy_len ) == FAILURE )
{
RETURN_THROWS();
}
/* Fetch the object. */
FORMATTER_METHOD_FETCH_OBJECT2;
if (FORMATTER_OBJECT(nfo)) {
if (!currency) {
zend_argument_value_error(3, "currency cannot be null when instantiating NumberFormatter"
"with a style constant");
RETURN_THROWS();
}
/* Convert currency to UTF-16. */
intl_convert_utf8_to_utf16(&scurrency, &scurrency_len, currency, currency_len, &INTL_DATA_ERROR_CODE(nfo));
INTL_METHOD_CHECK_STATUS( nfo, "Currency conversion to UTF-16 failed" );
/* Format the number using a fixed-length buffer. */
formatted_len = unum_formatDoubleCurrency(FORMATTER_OBJECT(nfo), number, scurrency, formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
/* If the buffer turned out to be too small
* then allocate another buffer dynamically
* and use it to format the number.
*/
if (INTL_DATA_ERROR_CODE(nfo) == U_BUFFER_OVERFLOW_ERROR) {
intl_error_reset(INTL_DATA_ERROR_P(nfo));
formatted = eumalloc(formatted_len);
unum_formatDoubleCurrency(FORMATTER_OBJECT(nfo), number, scurrency, formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
}
} else {
/* The new NumberFormatter takes the currency format (aka skeleton)
* from NumberFormatter::__construct 2nd argument.
*/
zend_error(E_DEPRECATED, "Calling NumberFormat::formatCurrency when the object has been instantiated with the currency/* token is deprecated.");
result = unumf_openResult(&INTL_DATA_ERROR_CODE(nfo));
INTL_METHOD_CHECK_STATUS(nfo, "Error opening formatter result");
unumf_formatDouble(FORMATTER_OBJECT2(nfo), number, result, &INTL_DATA_ERROR_CODE(nfo));
INTL_METHOD_CHECK_STATUS( nfo, "Number formatting failed" );
formatted_len = unumf_resultToString(result, formatted, formatted_len, &INTL_DATA_ERROR_CODE(nfo));
/* If the buffer turned out to be too small
* then allocate another buffer dynamically
* and use it to format the number.
*/
if (INTL_DATA_ERROR_CODE(nfo) == U_BUFFER_OVERFLOW_ERROR) {
intl_error_reset(INTL_DATA_ERROR_P(nfo));
formatted = eumalloc(formatted_len);
formatted_len = unumf_resultToString(result, formatted, formatted_len, &INTL_DATA_ERROR_CODE(nfo));
}
}
if( U_FAILURE( INTL_DATA_ERROR_CODE((nfo)) ) ) {
intl_error_set_code( NULL, INTL_DATA_ERROR_CODE((nfo)) );
intl_errors_set_custom_msg( INTL_DATA_ERROR_P(nfo), "Number formatting failed", 0 );
RETVAL_FALSE;
if (formatted != format_buf) {
efree(formatted);
}
} else {
INTL_METHOD_RETVAL_UTF8( nfo, formatted, formatted_len, ( formatted != format_buf ) );
}
if(scurrency) {
efree(scurrency);
}
unumf_closeResult(result);
}
/* }}} */