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_parse.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 72 74 97.3 %
Date: 2016-08-28 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             : #include <locale.h>
      25             : 
      26             : #include "formatter_class.h"
      27             : #include "formatter_format.h"
      28             : #include "formatter_parse.h"
      29             : #include "intl_convert.h"
      30             : 
      31             : #define ICU_LOCALE_BUG 1
      32             : 
      33             : /* {{{ proto mixed NumberFormatter::parse( string $str[, int $type, int &$position ])
      34             :  * Parse a number. }}} */
      35             : /* {{{ proto mixed numfmt_parse( NumberFormatter $nf, string $str[, int $type, int &$position ])
      36             :  * Parse a number.
      37             :  */
      38          15 : PHP_FUNCTION( numfmt_parse )
      39             : {
      40          15 :         zend_long type = FORMAT_TYPE_DOUBLE;
      41          15 :         UChar* sstr = NULL;
      42          15 :         int32_t sstr_len = 0;
      43          15 :         char* str = NULL;
      44             :         size_t str_len;
      45          15 :         int32_t val32, position = 0;
      46             :         int64_t val64;
      47             :         double val_double;
      48          15 :         int32_t* position_p = NULL;
      49          15 :         zval *zposition = NULL;
      50             :         char *oldlocale;
      51          15 :         FORMATTER_METHOD_INIT_VARS;
      52             : 
      53             :         /* Parse parameters. */
      54          30 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Os|lz/!",
      55             :                 &object, NumberFormatter_ce_ptr,  &str, &str_len, &type, &zposition ) == FAILURE )
      56             :         {
      57           1 :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
      58             :                         "number_parse: unable to parse input params", 0 );
      59             : 
      60           1 :                 RETURN_FALSE;
      61             :         }
      62             : 
      63             :         /* Fetch the object. */
      64          14 :         FORMATTER_METHOD_FETCH_OBJECT;
      65             : 
      66             :         /* Convert given string to UTF-16. */
      67          14 :         intl_convert_utf8_to_utf16(&sstr, &sstr_len, str, str_len, &INTL_DATA_ERROR_CODE(nfo));
      68          14 :         INTL_METHOD_CHECK_STATUS( nfo, "String conversion to UTF-16 failed" );
      69             : 
      70          14 :         if(zposition) {
      71           4 :                 ZVAL_DEREF(zposition);
      72           2 :                 convert_to_long(zposition);
      73           2 :                 position = (int32_t)Z_LVAL_P( zposition );
      74           2 :                 position_p = &position;
      75             :         }
      76             : 
      77             : #if ICU_LOCALE_BUG && defined(LC_NUMERIC)
      78             :         /* need to copy here since setlocale may change it later */
      79          14 :         oldlocale = estrdup(setlocale(LC_NUMERIC, NULL));
      80          14 :         setlocale(LC_NUMERIC, "C");
      81             : #endif
      82             : 
      83          14 :         switch(type) {
      84             :                 case FORMAT_TYPE_INT32:
      85           3 :                         val32 = unum_parse(FORMATTER_OBJECT(nfo), sstr, sstr_len, position_p, &INTL_DATA_ERROR_CODE(nfo));
      86           3 :                         RETVAL_LONG(val32);
      87           3 :                         break;
      88             :                 case FORMAT_TYPE_INT64:
      89           1 :                         val64 = unum_parseInt64(FORMATTER_OBJECT(nfo), sstr, sstr_len, position_p, &INTL_DATA_ERROR_CODE(nfo));
      90           1 :                         if(val64 > ZEND_LONG_MAX || val64 < ZEND_LONG_MIN) {
      91             :                                 RETVAL_DOUBLE(val64);
      92             :                         } else {
      93           1 :                                 RETVAL_LONG((zend_long)val64);
      94             :                         }
      95           1 :                         break;
      96             :                 case FORMAT_TYPE_DOUBLE:
      97          10 :                         val_double = unum_parseDouble(FORMATTER_OBJECT(nfo), sstr, sstr_len, position_p, &INTL_DATA_ERROR_CODE(nfo));
      98          10 :                         RETVAL_DOUBLE(val_double);
      99          10 :                         break;
     100             :                 default:
     101           0 :                         php_error_docref(NULL, E_WARNING, "Unsupported format type " ZEND_LONG_FMT, type);
     102           0 :                         RETVAL_FALSE;
     103             :                         break;
     104             :         }
     105             : #if ICU_LOCALE_BUG && defined(LC_NUMERIC)
     106          14 :         setlocale(LC_NUMERIC, oldlocale);
     107          14 :         efree(oldlocale);
     108             : #endif
     109          14 :         if(zposition) {
     110           2 :                 zval_dtor(zposition);
     111           2 :                 ZVAL_LONG(zposition, position);
     112             :         }
     113             : 
     114          14 :         if (sstr) {
     115          14 :                 efree(sstr);
     116             :         }
     117             : 
     118          14 :         INTL_METHOD_CHECK_STATUS( nfo, "Number parsing failed" );
     119             : }
     120             : /* }}} */
     121             : 
     122             : /* {{{ proto double NumberFormatter::parseCurrency( string $str, string $&currency[, int $&position] )
     123             :  * Parse a number as currency. }}} */
     124             : /* {{{ proto double numfmt_parse_currency( NumberFormatter $nf, string $str, string $&currency[, int $&position] )
     125             :  * Parse a number as currency.
     126             :  */
     127           7 : PHP_FUNCTION( numfmt_parse_currency )
     128             : {
     129             :         double number;
     130           7 :         UChar currency[5] = {0};
     131           7 :         UChar* sstr = NULL;
     132           7 :         int32_t sstr_len = 0;
     133             :         zend_string *u8str;
     134             :         char *str;
     135             :         size_t str_len;
     136           7 :         int32_t* position_p = NULL;
     137           7 :         int32_t position = 0;
     138           7 :         zval *zcurrency, *zposition = NULL;
     139           7 :         FORMATTER_METHOD_INIT_VARS;
     140             : 
     141             :         /* Parse parameters. */
     142          14 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Osz/|z/!",
     143             :                 &object, NumberFormatter_ce_ptr,  &str, &str_len, &zcurrency, &zposition ) == FAILURE )
     144             :         {
     145           1 :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
     146             :                         "number_parse_currency: unable to parse input params", 0 );
     147             : 
     148           1 :                 RETURN_FALSE;
     149             :         }
     150             : 
     151             :         /* Fetch the object. */
     152           6 :         FORMATTER_METHOD_FETCH_OBJECT;
     153             : 
     154             :         /* Convert given string to UTF-16. */
     155           6 :         intl_convert_utf8_to_utf16(&sstr, &sstr_len, str, str_len, &INTL_DATA_ERROR_CODE(nfo));
     156           6 :         INTL_METHOD_CHECK_STATUS( nfo, "String conversion to UTF-16 failed" );
     157             : 
     158           6 :         if(zposition) {
     159          12 :                 ZVAL_DEREF(zposition);
     160           6 :                 convert_to_long(zposition);
     161           6 :                 position = (int32_t)Z_LVAL_P( zposition );
     162           6 :                 position_p = &position;
     163             :         }
     164             : 
     165           6 :         number = unum_parseDoubleCurrency(FORMATTER_OBJECT(nfo), sstr, sstr_len, position_p, currency, &INTL_DATA_ERROR_CODE(nfo));
     166           6 :         if(zposition) {
     167           6 :                 zval_dtor(zposition);
     168           6 :                 ZVAL_LONG(zposition, position);
     169             :         }
     170           6 :         if (sstr) {
     171           6 :                 efree(sstr);
     172             :         }
     173           6 :         INTL_METHOD_CHECK_STATUS( nfo, "Number parsing failed" );
     174             : 
     175             :         /* Convert parsed currency to UTF-8 and pass it back to caller. */
     176           4 :         u8str = intl_convert_utf16_to_utf8(currency, u_strlen(currency), &INTL_DATA_ERROR_CODE(nfo));
     177           4 :         INTL_METHOD_CHECK_STATUS( nfo, "Currency conversion to UTF-8 failed" );
     178           4 :         zval_dtor( zcurrency );
     179           4 :         ZVAL_NEW_STR(zcurrency, u8str);
     180             : 
     181           4 :         RETVAL_DOUBLE( number );
     182             : }
     183             : /* }}} */
     184             : 
     185             : /*
     186             :  * Local variables:
     187             :  * tab-width: 4
     188             :  * c-basic-offset: 4
     189             :  * End:
     190             :  * vim600: noet sw=4 ts=4 fdm=marker
     191             :  * vim<600: noet sw=4 ts=4
     192             :  */

Generated by: LCOV version 1.10

Generated at Sun, 28 Aug 2016 17:10:02 +0000 (3 hours ago)

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