You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
7.0 KiB

  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  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. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. #include <unicode/ustring.h>
  20. #include "php_intl.h"
  21. #include "formatter_class.h"
  22. #include "formatter_format.h"
  23. #include "intl_convert.h"
  24. /* {{{ proto mixed NumberFormatter::format( mixed $num[, int $type] )
  25. * Format a number. }}} */
  26. /* {{{ proto mixed numfmt_format( NumberFormatter $nf, mixed $num[, int type] )
  27. * Format a number.
  28. */
  29. PHP_FUNCTION( numfmt_format )
  30. {
  31. zval **number;
  32. long type = FORMAT_TYPE_DEFAULT;
  33. UChar format_buf[32];
  34. UChar* formatted = format_buf;
  35. int formatted_len = USIZE(format_buf);
  36. FORMATTER_METHOD_INIT_VARS;
  37. /* Parse parameters. */
  38. if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "OZ|l",
  39. &object, NumberFormatter_ce_ptr, &number, &type ) == FAILURE )
  40. {
  41. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  42. "numfmt_format: unable to parse input params", 0 TSRMLS_CC );
  43. RETURN_FALSE;
  44. }
  45. /* Fetch the object. */
  46. FORMATTER_METHOD_FETCH_OBJECT;
  47. if(type == FORMAT_TYPE_DEFAULT) {
  48. if(Z_TYPE_PP(number) == IS_STRING) {
  49. convert_scalar_to_number_ex(number);
  50. }
  51. if(Z_TYPE_PP(number) == IS_LONG) {
  52. /* take INT32 on 32-bit, int64 on 64-bit */
  53. type = (sizeof(long) == 8)?FORMAT_TYPE_INT64:FORMAT_TYPE_INT32;
  54. } else if(Z_TYPE_PP(number) == IS_DOUBLE) {
  55. type = FORMAT_TYPE_DOUBLE;
  56. } else {
  57. type = FORMAT_TYPE_INT32;
  58. }
  59. }
  60. if(Z_TYPE_PP(number) != IS_DOUBLE && Z_TYPE_PP(number) != IS_LONG) {
  61. SEPARATE_ZVAL_IF_NOT_REF(number);
  62. convert_scalar_to_number( *number TSRMLS_CC );
  63. }
  64. switch(type) {
  65. case FORMAT_TYPE_INT32:
  66. convert_to_long_ex(number);
  67. formatted_len = unum_format(FORMATTER_OBJECT(nfo), (int32_t)Z_LVAL_PP(number),
  68. formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
  69. if (INTL_DATA_ERROR_CODE(nfo) == U_BUFFER_OVERFLOW_ERROR) {
  70. intl_error_reset(INTL_DATA_ERROR_P(nfo) TSRMLS_CC);
  71. formatted = eumalloc(formatted_len);
  72. formatted_len = unum_format(FORMATTER_OBJECT(nfo), (int32_t)Z_LVAL_PP(number),
  73. formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
  74. if (U_FAILURE( INTL_DATA_ERROR_CODE(nfo) ) ) {
  75. efree(formatted);
  76. }
  77. }
  78. INTL_METHOD_CHECK_STATUS( nfo, "Number formatting failed" );
  79. break;
  80. case FORMAT_TYPE_INT64:
  81. {
  82. int64_t value = (Z_TYPE_PP(number) == IS_DOUBLE)?(int64_t)Z_DVAL_PP(number):Z_LVAL_PP(number);
  83. formatted_len = unum_formatInt64(FORMATTER_OBJECT(nfo), value, formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
  84. if (INTL_DATA_ERROR_CODE(nfo) == U_BUFFER_OVERFLOW_ERROR) {
  85. intl_error_reset(INTL_DATA_ERROR_P(nfo) TSRMLS_CC);
  86. formatted = eumalloc(formatted_len);
  87. formatted_len = unum_formatInt64(FORMATTER_OBJECT(nfo), value, formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
  88. if (U_FAILURE( INTL_DATA_ERROR_CODE(nfo) ) ) {
  89. efree(formatted);
  90. }
  91. }
  92. INTL_METHOD_CHECK_STATUS( nfo, "Number formatting failed" );
  93. }
  94. break;
  95. case FORMAT_TYPE_DOUBLE:
  96. convert_to_double_ex(number);
  97. formatted_len = unum_formatDouble(FORMATTER_OBJECT(nfo), Z_DVAL_PP(number), formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
  98. if (INTL_DATA_ERROR_CODE(nfo) == U_BUFFER_OVERFLOW_ERROR) {
  99. intl_error_reset(INTL_DATA_ERROR_P(nfo) TSRMLS_CC);
  100. formatted = eumalloc(formatted_len);
  101. unum_formatDouble(FORMATTER_OBJECT(nfo), Z_DVAL_PP(number), formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
  102. if (U_FAILURE( INTL_DATA_ERROR_CODE(nfo) ) ) {
  103. efree(formatted);
  104. }
  105. }
  106. INTL_METHOD_CHECK_STATUS( nfo, "Number formatting failed" );
  107. break;
  108. default:
  109. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported format type %ld", type);
  110. RETURN_FALSE;
  111. break;
  112. }
  113. INTL_METHOD_RETVAL_UTF8( nfo, formatted, formatted_len, ( formatted != format_buf ) );
  114. }
  115. /* }}} */
  116. /* {{{ proto mixed NumberFormatter::formatCurrency( double $num, string $currency )
  117. * Format a number as currency. }}} */
  118. /* {{{ proto mixed numfmt_format_currency( NumberFormatter $nf, double $num, string $currency )
  119. * Format a number as currency.
  120. */
  121. PHP_FUNCTION( numfmt_format_currency )
  122. {
  123. double number;
  124. UChar format_buf[32];
  125. UChar* formatted = format_buf;
  126. int formatted_len = USIZE(format_buf);
  127. char* currency = NULL;
  128. int currency_len = 0;
  129. UChar* scurrency = NULL;
  130. int scurrency_len = 0;
  131. FORMATTER_METHOD_INIT_VARS;
  132. /* Parse parameters. */
  133. if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ods",
  134. &object, NumberFormatter_ce_ptr, &number, &currency, &currency_len ) == FAILURE )
  135. {
  136. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  137. "numfmt_format_currency: unable to parse input params", 0 TSRMLS_CC );
  138. RETURN_FALSE;
  139. }
  140. /* Fetch the object. */
  141. FORMATTER_METHOD_FETCH_OBJECT;
  142. /* Convert currency to UTF-16. */
  143. intl_convert_utf8_to_utf16(&scurrency, &scurrency_len, currency, currency_len, &INTL_DATA_ERROR_CODE(nfo));
  144. INTL_METHOD_CHECK_STATUS( nfo, "Currency conversion to UTF-16 failed" );
  145. /* Format the number using a fixed-length buffer. */
  146. formatted_len = unum_formatDoubleCurrency(FORMATTER_OBJECT(nfo), number, scurrency, formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
  147. /* If the buffer turned out to be too small
  148. * then allocate another buffer dynamically
  149. * and use it to format the number.
  150. */
  151. if (INTL_DATA_ERROR_CODE(nfo) == U_BUFFER_OVERFLOW_ERROR) {
  152. intl_error_reset(INTL_DATA_ERROR_P(nfo) TSRMLS_CC);
  153. formatted = eumalloc(formatted_len);
  154. unum_formatDoubleCurrency(FORMATTER_OBJECT(nfo), number, scurrency, formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
  155. }
  156. if( U_FAILURE( INTL_DATA_ERROR_CODE((nfo)) ) ) {
  157. intl_error_set_code( NULL, INTL_DATA_ERROR_CODE((nfo)) TSRMLS_CC );
  158. intl_errors_set_custom_msg( INTL_DATA_ERROR_P(nfo), "Number formatting failed", 0 TSRMLS_CC );
  159. RETVAL_FALSE;
  160. if (formatted != format_buf) {
  161. efree(formatted);
  162. }
  163. } else {
  164. INTL_METHOD_RETVAL_UTF8( nfo, formatted, formatted_len, ( formatted != format_buf ) );
  165. }
  166. if(scurrency) {
  167. efree(scurrency);
  168. }
  169. }
  170. /* }}} */
  171. /*
  172. * Local variables:
  173. * tab-width: 4
  174. * c-basic-offset: 4
  175. * End:
  176. * vim600: noet sw=4 ts=4 fdm=marker
  177. * vim<600: noet sw=4 ts=4
  178. */