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/msgformat - msgformat_format.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 44 54 81.5 %
Date: 2016-09-18 Functions: 3 3 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             : 
      23             : #include "php_intl.h"
      24             : #include "msgformat_class.h"
      25             : #include "msgformat_format.h"
      26             : #include "msgformat_data.h"
      27             : #include "msgformat_helpers.h"
      28             : #include "intl_convert.h"
      29             : 
      30             : #ifndef Z_ADDREF_P
      31             : #define Z_ADDREF_P(z) ((z)->refcount++)
      32             : #endif
      33             : 
      34             : /* {{{ */
      35          37 : static void msgfmt_do_format(MessageFormatter_object *mfo, zval *args, zval *return_value)
      36             : {
      37             :         int count;
      38          37 :         UChar* formatted = NULL;
      39          37 :         int32_t formatted_len = 0;
      40             :         HashTable *args_copy;
      41             : 
      42          37 :         count = zend_hash_num_elements(Z_ARRVAL_P(args));
      43             : 
      44          37 :         ALLOC_HASHTABLE(args_copy);
      45          37 :         zend_hash_init(args_copy, count, NULL, ZVAL_PTR_DTOR, 0);
      46          37 :         zend_hash_copy(args_copy, Z_ARRVAL_P(args), (copy_ctor_func_t)zval_add_ref);
      47             : 
      48          37 :         umsg_format_helper(mfo, args_copy, &formatted, &formatted_len);
      49             : 
      50          37 :         zend_hash_destroy(args_copy);
      51          37 :         efree(args_copy);
      52             : 
      53          37 :         if (U_FAILURE(INTL_DATA_ERROR_CODE(mfo))) {
      54           0 :                 if (formatted) {
      55           0 :                         efree(formatted);
      56             :                 }
      57           0 :                 RETURN_FALSE;
      58             :         } else {
      59          37 :                 INTL_METHOD_RETVAL_UTF8(mfo, formatted, formatted_len, 1);
      60             :         }
      61             : }
      62             : /* }}} */
      63             : 
      64             : /* {{{ proto mixed MessageFormatter::format( array $args )
      65             :  * Format a message. }}} */
      66             : /* {{{ proto mixed msgfmt_format( MessageFormatter $nf, array $args )
      67             :  * Format a message.
      68             :  */
      69          28 : PHP_FUNCTION( msgfmt_format )
      70             : {
      71             :         zval *args;
      72          28 :         MSG_FORMAT_METHOD_INIT_VARS;
      73             : 
      74             : 
      75             :         /* Parse parameters. */
      76          56 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Oa",
      77             :                 &object, MessageFormatter_ce_ptr,  &args ) == FAILURE )
      78             :         {
      79           1 :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
      80             :                         "msgfmt_format: unable to parse input params", 0 );
      81             : 
      82           1 :                 RETURN_FALSE;
      83             :         }
      84             : 
      85             :         /* Fetch the object. */
      86          27 :         MSG_FORMAT_METHOD_FETCH_OBJECT;
      87             : 
      88          27 :         msgfmt_do_format(mfo, args, return_value);
      89             : }
      90             : /* }}} */
      91             : 
      92             : /* {{{ proto mixed MessageFormatter::formatMessage( string $locale, string $pattern, array $args )
      93             :  * Format a message. }}} */
      94             : /* {{{ proto mixed msgfmt_format_message( string $locale, string $pattern, array $args )
      95             :  * Format a message.
      96             :  */
      97          11 : PHP_FUNCTION( msgfmt_format_message )
      98             : {
      99             :         zval       *args;
     100          11 :         UChar      *spattern = NULL;
     101          11 :         int         spattern_len = 0;
     102          11 :         char       *pattern = NULL;
     103          11 :         size_t      pattern_len = 0;
     104          11 :         const char *slocale = NULL;
     105          11 :         size_t      slocale_len = 0;
     106             :         MessageFormatter_object mf;
     107          11 :         MessageFormatter_object *mfo = &mf;
     108             : 
     109             :         /* Parse parameters. */
     110          22 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "ssa",
     111             :                   &slocale, &slocale_len, &pattern, &pattern_len, &args ) == FAILURE )
     112             :         {
     113           1 :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
     114             :                         "msgfmt_format_message: unable to parse input params", 0 );
     115             : 
     116           1 :                 RETURN_FALSE;
     117             :         }
     118             : 
     119          10 :         INTL_CHECK_LOCALE_LEN(slocale_len);
     120             : 
     121          10 :         memset(mfo, 0, sizeof(*mfo));
     122          10 :         msgformat_data_init(&mfo->mf_data);
     123             : 
     124          20 :         if(pattern && pattern_len) {
     125          10 :                 intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo));
     126          10 :                 if( U_FAILURE(INTL_DATA_ERROR_CODE((mfo))) )
     127             :                 {
     128           0 :                         intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
     129             :                                 "msgfmt_format_message: error converting pattern to UTF-16", 0 );
     130           0 :                         RETURN_FALSE;
     131             :                 }
     132             :         } else {
     133           0 :                 spattern_len = 0;
     134           0 :                 spattern = NULL;
     135             :         }
     136             : 
     137          10 :         if(slocale_len == 0) {
     138           0 :                 slocale = intl_locale_get_default();
     139             :         }
     140             : 
     141             : #ifdef MSG_FORMAT_QUOTE_APOS
     142          10 :         if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
     143           0 :                 intl_error_set( NULL, U_INVALID_FORMAT_ERROR,
     144             :                         "msgfmt_format_message: error converting pattern to quote-friendly format", 0 );
     145           0 :                 RETURN_FALSE;
     146             :         }
     147             : #endif
     148             : 
     149             :         /* Create an ICU message formatter. */
     150          10 :         MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, slocale, NULL, &INTL_DATA_ERROR_CODE(mfo));
     151          10 :         if(spattern && spattern_len) {
     152          10 :                 efree(spattern);
     153             :         }
     154          10 :         INTL_METHOD_CHECK_STATUS(mfo, "Creating message formatter failed");
     155             : 
     156          10 :         msgfmt_do_format(mfo, args, return_value);
     157             : 
     158             :         /* drop the temporary formatter */
     159          10 :         msgformat_data_free(&mfo->mf_data);
     160             : }
     161             : /* }}} */
     162             : 
     163             : /*
     164             :  * Local variables:
     165             :  * tab-width: 4
     166             :  * c-basic-offset: 4
     167             :  * End:
     168             :  * vim600: noet sw=4 ts=4 fdm=marker
     169             :  * vim<600: noet sw=4 ts=4
     170             :  */

Generated by: LCOV version 1.10

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

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