1 /*
2 +----------------------------------------------------------------------+
3 | This source file is subject to version 3.01 of the PHP license, |
4 | that is bundled with this package in the file LICENSE, and is |
5 | available through the world-wide-web at the following url: |
6 | http://www.php.net/license/3_01.txt |
7 | If you did not receive a copy of the PHP license and are unable to |
8 | obtain it through the world-wide-web, please send a note to |
9 | license@php.net so we can mail you a copy immediately. |
10 +----------------------------------------------------------------------+
11 | Authors: Stanislav Malyshev <stas@zend.com> |
12 +----------------------------------------------------------------------+
13 */
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
18
19 #include "php_intl.h"
20
21 #include <unicode/ustring.h>
22
23 #include "formatter_class.h"
24 #include "formatter_format.h"
25 #include "intl_convert.h"
26
27 /* {{{ Format a number. */
PHP_FUNCTION(numfmt_format)28 PHP_FUNCTION( numfmt_format )
29 {
30 zval *number;
31 zend_long type = FORMAT_TYPE_DEFAULT;
32 UChar format_buf[32];
33 UChar* formatted = format_buf;
34 int32_t formatted_len = USIZE(format_buf);
35 FORMATTER_METHOD_INIT_VARS;
36
37 /* Parse parameters. */
38 if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "On|l",
39 &object, NumberFormatter_ce_ptr, &number, &type ) == FAILURE )
40 {
41 RETURN_THROWS();
42 }
43
44 /* Fetch the object. */
45 FORMATTER_METHOD_FETCH_OBJECT;
46
47 if(type == FORMAT_TYPE_DEFAULT) {
48 switch(Z_TYPE_P(number)) {
49 case IS_LONG:
50 /* take INT32 on 32-bit, int64 on 64-bit */
51 type = (sizeof(zend_long) == 8)?FORMAT_TYPE_INT64:FORMAT_TYPE_INT32;
52 break;
53 case IS_DOUBLE:
54 type = FORMAT_TYPE_DOUBLE;
55 break;
56 EMPTY_SWITCH_DEFAULT_CASE();
57 }
58 }
59
60 switch(type) {
61 case FORMAT_TYPE_INT32:
62 convert_to_long_ex(number);
63 formatted_len = unum_format(FORMATTER_OBJECT(nfo), (int32_t)Z_LVAL_P(number),
64 formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
65 if (INTL_DATA_ERROR_CODE(nfo) == U_BUFFER_OVERFLOW_ERROR) {
66 intl_error_reset(INTL_DATA_ERROR_P(nfo));
67 formatted = eumalloc(formatted_len);
68 formatted_len = unum_format(FORMATTER_OBJECT(nfo), (int32_t)Z_LVAL_P(number),
69 formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
70 if (U_FAILURE( INTL_DATA_ERROR_CODE(nfo) ) ) {
71 efree(formatted);
72 }
73 }
74 INTL_METHOD_CHECK_STATUS( nfo, "Number formatting failed" );
75 break;
76
77 case FORMAT_TYPE_INT64:
78 {
79 int64_t value = (Z_TYPE_P(number) == IS_DOUBLE)?(int64_t)Z_DVAL_P(number):Z_LVAL_P(number);
80 formatted_len = unum_formatInt64(FORMATTER_OBJECT(nfo), value, formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
81 if (INTL_DATA_ERROR_CODE(nfo) == U_BUFFER_OVERFLOW_ERROR) {
82 intl_error_reset(INTL_DATA_ERROR_P(nfo));
83 formatted = eumalloc(formatted_len);
84 formatted_len = unum_formatInt64(FORMATTER_OBJECT(nfo), value, formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
85 if (U_FAILURE( INTL_DATA_ERROR_CODE(nfo) ) ) {
86 efree(formatted);
87 }
88 }
89 INTL_METHOD_CHECK_STATUS( nfo, "Number formatting failed" );
90 }
91 break;
92
93 case FORMAT_TYPE_DOUBLE:
94 convert_to_double_ex(number);
95 formatted_len = unum_formatDouble(FORMATTER_OBJECT(nfo), Z_DVAL_P(number), formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
96 if (INTL_DATA_ERROR_CODE(nfo) == U_BUFFER_OVERFLOW_ERROR) {
97 intl_error_reset(INTL_DATA_ERROR_P(nfo));
98 formatted = eumalloc(formatted_len);
99 unum_formatDouble(FORMATTER_OBJECT(nfo), Z_DVAL_P(number), formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
100 if (U_FAILURE( INTL_DATA_ERROR_CODE(nfo) ) ) {
101 efree(formatted);
102 }
103 }
104 INTL_METHOD_CHECK_STATUS( nfo, "Number formatting failed" );
105 break;
106 case FORMAT_TYPE_CURRENCY:
107 if (getThis()) {
108 const char *space;
109 const char *class_name = get_active_class_name(&space);
110 zend_argument_value_error(2, "cannot be NumberFormatter::TYPE_CURRENCY constant, "
111 "use %s%sformatCurrency() method instead", class_name, space);
112 } else {
113 zend_argument_value_error(3, "cannot be NumberFormatter::TYPE_CURRENCY constant, use numfmt_format_currency() function instead");
114 }
115 RETURN_THROWS();
116 default:
117 zend_argument_value_error(getThis() ? 2 : 3, "must be a NumberFormatter::TYPE_* constant");
118 RETURN_THROWS();
119 }
120
121 INTL_METHOD_RETVAL_UTF8( nfo, formatted, formatted_len, ( formatted != format_buf ) );
122 }
123 /* }}} */
124
125 /* {{{ Format a number as currency. */
PHP_FUNCTION(numfmt_format_currency)126 PHP_FUNCTION( numfmt_format_currency )
127 {
128 double number;
129 UChar format_buf[32];
130 UChar* formatted = format_buf;
131 int32_t formatted_len = USIZE(format_buf);
132 char* currency = NULL;
133 size_t currency_len = 0;
134 UChar* scurrency = NULL;
135 int32_t scurrency_len = 0;
136 FORMATTER_METHOD_INIT_VARS;
137
138 /* Parse parameters. */
139 if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Ods",
140 &object, NumberFormatter_ce_ptr, &number, ¤cy, ¤cy_len ) == FAILURE )
141 {
142 RETURN_THROWS();
143 }
144
145 /* Fetch the object. */
146 FORMATTER_METHOD_FETCH_OBJECT;
147
148 /* Convert currency to UTF-16. */
149 intl_convert_utf8_to_utf16(&scurrency, &scurrency_len, currency, currency_len, &INTL_DATA_ERROR_CODE(nfo));
150 INTL_METHOD_CHECK_STATUS( nfo, "Currency conversion to UTF-16 failed" );
151
152 /* Format the number using a fixed-length buffer. */
153 formatted_len = unum_formatDoubleCurrency(FORMATTER_OBJECT(nfo), number, scurrency, formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
154
155 /* If the buffer turned out to be too small
156 * then allocate another buffer dynamically
157 * and use it to format the number.
158 */
159 if (INTL_DATA_ERROR_CODE(nfo) == U_BUFFER_OVERFLOW_ERROR) {
160 intl_error_reset(INTL_DATA_ERROR_P(nfo));
161 formatted = eumalloc(formatted_len);
162 unum_formatDoubleCurrency(FORMATTER_OBJECT(nfo), number, scurrency, formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
163 }
164
165 if( U_FAILURE( INTL_DATA_ERROR_CODE((nfo)) ) ) {
166 intl_error_set_code( NULL, INTL_DATA_ERROR_CODE((nfo)) );
167 intl_errors_set_custom_msg( INTL_DATA_ERROR_P(nfo), "Number formatting failed", 0 );
168 RETVAL_FALSE;
169 if (formatted != format_buf) {
170 efree(formatted);
171 }
172 } else {
173 INTL_METHOD_RETVAL_UTF8( nfo, formatted, formatted_len, ( formatted != format_buf ) );
174 }
175
176 if(scurrency) {
177 efree(scurrency);
178 }
179 }
180
181 /* }}} */
182