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: 75 77 97.4 %
Date: 2014-10-22 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 <unicode/ustring.h>
      22             : #include <locale.h>
      23             : 
      24             : #include "php_intl.h"
      25             : #include "formatter_class.h"
      26             : #include "formatter_format.h"
      27             : #include "formatter_parse.h"
      28             : #include "intl_convert.h"
      29             : 
      30             : #define ICU_LOCALE_BUG 1
      31             : 
      32             : /* {{{ proto mixed NumberFormatter::parse( string $str[, int $type, int &$position ])
      33             :  * Parse a number. }}} */
      34             : /* {{{ proto mixed numfmt_parse( NumberFormatter $nf, string $str[, int $type, int &$position ])
      35             :  * Parse a number.
      36             :  */
      37          15 : PHP_FUNCTION( numfmt_parse )
      38             : {
      39          15 :         zend_long type = FORMAT_TYPE_DOUBLE;
      40          15 :         UChar* sstr = NULL;
      41          15 :         int sstr_len = 0;
      42          15 :         char* str = NULL;
      43             :         size_t str_len;
      44          15 :         int32_t val32, position = 0;
      45             :         int64_t val64;
      46             :         double val_double;
      47          15 :         int32_t* position_p = NULL;
      48          15 :         zval *zposition = NULL;
      49             :         char *oldlocale;
      50          15 :         FORMATTER_METHOD_INIT_VARS;
      51             : 
      52             :         /* Parse parameters. */
      53          15 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|lz/!",
      54             :                 &object, NumberFormatter_ce_ptr,  &str, &str_len, &type, &zposition ) == FAILURE )
      55             :         {
      56           1 :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
      57             :                         "number_parse: unable to parse input params", 0 TSRMLS_CC );
      58             : 
      59           1 :                 RETURN_FALSE;
      60             :         }
      61             : 
      62             :         /* Fetch the object. */
      63          14 :         FORMATTER_METHOD_FETCH_OBJECT;
      64             : 
      65             :         /* Convert given string to UTF-16. */
      66          14 :         intl_convert_utf8_to_utf16(&sstr, &sstr_len, str, str_len, &INTL_DATA_ERROR_CODE(nfo));
      67          14 :         INTL_METHOD_CHECK_STATUS( nfo, "String conversion to UTF-16 failed" );
      68             : 
      69          14 :         if(zposition) {
      70           4 :                 ZVAL_DEREF(zposition);
      71           2 :                 convert_to_long(zposition);
      72           2 :                 position = (int32_t)Z_LVAL_P( zposition );
      73           2 :                 position_p = &position;
      74             :         }
      75             : 
      76             : #if ICU_LOCALE_BUG && defined(LC_NUMERIC)
      77             :         /* need to copy here since setlocale may change it later */
      78          14 :         oldlocale = estrdup(setlocale(LC_NUMERIC, NULL));
      79          14 :         setlocale(LC_NUMERIC, "C");
      80             : #endif
      81             : 
      82          14 :         switch(type) {
      83             :                 case FORMAT_TYPE_INT32:
      84           3 :                         val32 = unum_parse(FORMATTER_OBJECT(nfo), sstr, sstr_len, position_p, &INTL_DATA_ERROR_CODE(nfo));
      85           3 :                         RETVAL_LONG(val32);
      86           3 :                         break;
      87             :                 case FORMAT_TYPE_INT64:
      88           1 :                         val64 = unum_parseInt64(FORMATTER_OBJECT(nfo), sstr, sstr_len, position_p, &INTL_DATA_ERROR_CODE(nfo));
      89           1 :                         if(val64 > ZEND_LONG_MAX || val64 < ZEND_LONG_MIN) {
      90             :                                 RETVAL_DOUBLE(val64);
      91             :                         } else {
      92           1 :                                 RETVAL_LONG((zend_long)val64);
      93             :                         }
      94           1 :                         break;
      95             :                 case FORMAT_TYPE_DOUBLE:
      96          10 :                         val_double = unum_parseDouble(FORMATTER_OBJECT(nfo), sstr, sstr_len, position_p, &INTL_DATA_ERROR_CODE(nfo));
      97          10 :                         RETVAL_DOUBLE(val_double);
      98          10 :                         break;
      99             :                 default:
     100           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported format type %pd", type);
     101           0 :                         RETVAL_FALSE;
     102             :                         break;
     103             :         }
     104             : #if ICU_LOCALE_BUG && defined(LC_NUMERIC)
     105          14 :         setlocale(LC_NUMERIC, oldlocale);
     106          14 :         efree(oldlocale);
     107             : #endif
     108          14 :         if(zposition) {
     109           2 :                 zval_dtor(zposition);
     110           2 :                 ZVAL_LONG(zposition, position);
     111             :         }
     112             : 
     113          14 :         if (sstr) {
     114          14 :                 efree(sstr);
     115             :         }
     116             : 
     117          14 :         INTL_METHOD_CHECK_STATUS( nfo, "Number parsing failed" );
     118             : }
     119             : /* }}} */
     120             : 
     121             : /* {{{ proto double NumberFormatter::parseCurrency( string $str, string $&currency[, int $&position] )
     122             :  * Parse a number as currency. }}} */
     123             : /* {{{ proto double numfmt_parse_currency( NumberFormatter $nf, string $str, string $&currency[, int $&position] )
     124             :  * Parse a number as currency.
     125             :  */
     126           7 : PHP_FUNCTION( numfmt_parse_currency )
     127             : {
     128             :         double number;
     129           7 :         UChar currency[5] = {0};
     130           7 :         UChar* sstr = NULL;
     131           7 :         int sstr_len = 0;
     132           7 :         char *currency_str = NULL;
     133           7 :         int currency_len = 0;
     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           7 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, 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 TSRMLS_CC );
     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 :         intl_convert_utf16_to_utf8(&currency_str, &currency_len, 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           8 :         ZVAL_STRINGL(zcurrency, currency_str, currency_len);
     180             :         //????
     181           4 :         efree(currency_str);
     182             : 
     183           4 :         RETVAL_DOUBLE( number );
     184             : }
     185             : /* }}} */
     186             : 
     187             : /*
     188             :  * Local variables:
     189             :  * tab-width: 4
     190             :  * c-basic-offset: 4
     191             :  * End:
     192             :  * vim600: noet sw=4 ts=4 fdm=marker
     193             :  * vim<600: noet sw=4 ts=4
     194             :  */

Generated by: LCOV version 1.10

Generated at Wed, 22 Oct 2014 07:24:50 +0000 (2 days ago)

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