1818
1919#include "php_intl.h"
2020
21+ #include <unicode/unumberformatter.h>
2122#include <unicode/ustring.h>
2223
2324#include "formatter_class.h"
2829PHP_FUNCTION ( numfmt_format )
2930{
3031 zval * number ;
32+ UFormattedNumber * result = NULL ;
3133 zend_long type = FORMAT_TYPE_DEFAULT ;
3234 UChar format_buf [32 ];
3335 UChar * formatted = format_buf ;
@@ -42,7 +44,12 @@ PHP_FUNCTION( numfmt_format )
4244 }
4345
4446 /* Fetch the object. */
45- FORMATTER_METHOD_FETCH_OBJECT ;
47+ FORMATTER_METHOD_FETCH_OBJECT2 ;
48+
49+ if (FORMATTER_OBJECT2 (nfo )) {
50+ result = unumf_openResult (& INTL_DATA_ERROR_CODE (nfo ));
51+ INTL_METHOD_CHECK_STATUS (nfo , "Error opening formatter result" );
52+ }
4653
4754 if (type == FORMAT_TYPE_DEFAULT ) {
4855 switch (Z_TYPE_P (number )) {
@@ -60,48 +67,77 @@ PHP_FUNCTION( numfmt_format )
6067 switch (type ) {
6168 case FORMAT_TYPE_INT32 :
6269 convert_to_long (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 ));
70+ if (FORMATTER_OBJECT (nfo )) {
71+ formatted_len = unum_format (FORMATTER_OBJECT (nfo ), (int32_t )Z_LVAL_P (number ),
72+ formatted , formatted_len , NULL , & INTL_DATA_ERROR_CODE (nfo ));
73+ } else {
74+ unumf_formatInt (FORMATTER_OBJECT2 (nfo ), (int32_t )Z_LVAL_P (number ), result , & INTL_DATA_ERROR_CODE (nfo ));
75+ formatted_len = unumf_resultToString (result , formatted , formatted_len , & INTL_DATA_ERROR_CODE (nfo ));
76+ }
77+ INTL_METHOD_CHECK_STATUS ( nfo , "Number formatting failed" );
6578 if (INTL_DATA_ERROR_CODE (nfo ) == U_BUFFER_OVERFLOW_ERROR ) {
6679 intl_error_reset (INTL_DATA_ERROR_P (nfo ));
6780 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 ));
81+ if (FORMATTER_OBJECT (nfo )) {
82+ formatted_len = unum_format (FORMATTER_OBJECT (nfo ), (int32_t )Z_LVAL_P (number ),
83+ formatted , formatted_len , NULL , & INTL_DATA_ERROR_CODE (nfo ));
84+ } else {
85+ formatted_len = unumf_resultToString (result , formatted , formatted_len , & INTL_DATA_ERROR_CODE (nfo ));
86+ }
7087 if (U_FAILURE ( INTL_DATA_ERROR_CODE (nfo ) ) ) {
7188 efree (formatted );
7289 }
7390 }
74- INTL_METHOD_CHECK_STATUS ( nfo , "Number formatting failed" );
7591 break ;
7692
7793 case FORMAT_TYPE_INT64 :
7894 {
7995 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 ));
96+ if (FORMATTER_OBJECT (nfo )) {
97+ formatted_len = unum_formatInt64 (FORMATTER_OBJECT (nfo ), value , formatted , formatted_len , NULL , & INTL_DATA_ERROR_CODE (nfo ));
98+ } else {
99+ unumf_formatInt (FORMATTER_OBJECT2 (nfo ), value , result , & INTL_DATA_ERROR_CODE (nfo ));
100+ INTL_METHOD_CHECK_STATUS ( nfo , "Number formatting failed" );
101+ formatted_len = unumf_resultToString (result , formatted , formatted_len , & INTL_DATA_ERROR_CODE (nfo ));
102+ }
81103 if (INTL_DATA_ERROR_CODE (nfo ) == U_BUFFER_OVERFLOW_ERROR ) {
82104 intl_error_reset (INTL_DATA_ERROR_P (nfo ));
83105 formatted = eumalloc (formatted_len );
84- formatted_len = unum_formatInt64 (FORMATTER_OBJECT (nfo ), value , formatted , formatted_len , NULL , & INTL_DATA_ERROR_CODE (nfo ));
106+ if (FORMATTER_OBJECT (nfo )) {
107+ formatted_len = unum_formatInt64 (FORMATTER_OBJECT (nfo ), value , formatted , formatted_len , NULL , & INTL_DATA_ERROR_CODE (nfo ));
108+ } else {
109+ formatted_len = unumf_resultToString (result , formatted , formatted_len , & INTL_DATA_ERROR_CODE (nfo ));
110+ }
85111 if (U_FAILURE ( INTL_DATA_ERROR_CODE (nfo ) ) ) {
86112 efree (formatted );
87113 }
88114 }
89- INTL_METHOD_CHECK_STATUS ( nfo , "Number formatting failed" );
115+ INTL_METHOD_CHECK_STATUS ( nfo , "Number formatting to string failed" );
90116 }
91117 break ;
92118
93119 case FORMAT_TYPE_DOUBLE :
94120 convert_to_double (number );
95- formatted_len = unum_formatDouble (FORMATTER_OBJECT (nfo ), Z_DVAL_P (number ), formatted , formatted_len , NULL , & INTL_DATA_ERROR_CODE (nfo ));
121+ if (FORMATTER_OBJECT (nfo )) {
122+ formatted_len = unum_formatDouble (FORMATTER_OBJECT (nfo ), Z_DVAL_P (number ), formatted , formatted_len , NULL , & INTL_DATA_ERROR_CODE (nfo ));
123+ } else {
124+ unumf_formatDouble (FORMATTER_OBJECT2 (nfo ), Z_DVAL_P (number ), result , & INTL_DATA_ERROR_CODE (nfo ));
125+ INTL_METHOD_CHECK_STATUS ( nfo , "Number formatting failed" );
126+ formatted_len = unumf_resultToString (result , formatted , formatted_len , & INTL_DATA_ERROR_CODE (nfo ));
127+ }
96128 if (INTL_DATA_ERROR_CODE (nfo ) == U_BUFFER_OVERFLOW_ERROR ) {
97129 intl_error_reset (INTL_DATA_ERROR_P (nfo ));
98130 formatted = eumalloc (formatted_len );
99- unum_formatDouble (FORMATTER_OBJECT (nfo ), Z_DVAL_P (number ), formatted , formatted_len , NULL , & INTL_DATA_ERROR_CODE (nfo ));
131+ if (FORMATTER_OBJECT (nfo )) {
132+ unum_formatDouble (FORMATTER_OBJECT (nfo ), Z_DVAL_P (number ), formatted , formatted_len , NULL , & INTL_DATA_ERROR_CODE (nfo ));
133+ } else {
134+ formatted_len = unumf_resultToString (result , formatted , formatted_len , & INTL_DATA_ERROR_CODE (nfo ));
135+ }
100136 if (U_FAILURE ( INTL_DATA_ERROR_CODE (nfo ) ) ) {
101137 efree (formatted );
102138 }
103139 }
104- INTL_METHOD_CHECK_STATUS ( nfo , "Number formatting failed" );
140+ INTL_METHOD_CHECK_STATUS ( nfo , "Number formatting to string failed" );
105141 break ;
106142 case FORMAT_TYPE_CURRENCY :
107143 if (getThis ()) {
@@ -119,6 +155,8 @@ PHP_FUNCTION( numfmt_format )
119155 }
120156
121157 INTL_METHOD_RETVAL_UTF8 ( nfo , formatted , formatted_len , ( formatted != format_buf ) );
158+
159+ unumf_closeResult (result );
122160}
123161/* }}} */
124162
0 commit comments