PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/intl/formatter - formatter_format.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 61 83 73.5 %
Date: 2016-09-18 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sun, 18 Sep 2016 08:20:07 +0000 (7 days ago)

Copyright © 2005-2016 The PHP Group
All rights reserved.