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 - intl_convert.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 31 38 81.6 %
Date: 2015-09-02 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: Vadim Savchuk <vsavchuk@productengine.com>                  |
      14             :    |          Dmitry Lakhtyuk <dlakhtyuk@productengine.com>               |
      15             :    +----------------------------------------------------------------------+
      16             :  */
      17             : 
      18             : #ifdef HAVE_CONFIG_H
      19             : #include "config.h"
      20             : #endif
      21             : 
      22             : #include <php.h>
      23             : 
      24             : #include "intl_common.h"
      25             : #include "intl_convert.h"
      26             : 
      27             : /* {{{ intl_convert_utf8_to_utf16
      28             :  * Convert given string from UTF-8 to UTF-16 to *target buffer.
      29             :  *
      30             :  * It *target is NULL then we allocate a large enough buffer,
      31             :  * store the converted string into it, and make target point to it.
      32             :  *
      33             :  * Otherwise, if *target is non-NULL, we assume that it points to a
      34             :  * dynamically allocated buffer of *target_len bytes length.
      35             :  * In this case the buffer will be used to store the converted string to,
      36             :  * and may be resized (made larger) if needed.
      37             :  *
      38             :  * Note that ICU uses int32_t as string length and PHP uses size_t. While
      39             :  * it is not likely in practical situations to have strings longer than
      40             :  * INT32_MAX, these are different types and need to be handled carefully.
      41             :  *
      42             :  * @param target      Where to place the result.
      43             :  * @param target_len  Result length.
      44             :  * @param source      String to convert.
      45             :  * @param source_len  Length of the source string.
      46             :  * @param status      Conversion status.
      47             :  *
      48             :  * @return void       This function does not return anything.
      49             :  */
      50        1476 : void intl_convert_utf8_to_utf16(
      51             :         UChar**     target, int32_t* target_len,
      52             :         const char* src,    size_t  src_len,
      53             :         UErrorCode* status )
      54             : {
      55        1476 :         UChar*      dst_buf = NULL;
      56        1476 :         int32_t     dst_len = 0;
      57             : 
      58             :         /* If *target is NULL determine required destination buffer size (pre-flighting).
      59             :          * Otherwise, attempt to convert source string; if *target buffer is not large enough
      60             :          * it will be resized appropriately.
      61             :          */
      62        1476 :         *status = U_ZERO_ERROR;
      63             : 
      64        1476 :         if(src_len > INT32_MAX) {
      65             :                 /* we can not fit this string */
      66           0 :                 *status = U_BUFFER_OVERFLOW_ERROR;
      67           0 :                 return;
      68             :         }
      69             : 
      70        1476 :         u_strFromUTF8( *target, *target_len, &dst_len, src, (int32_t)src_len, status );
      71             : 
      72        1476 :         if( *status == U_ZERO_ERROR )
      73             :         {
      74             :                 /* String is converted successfully */
      75         108 :                 (*target)[dst_len] = 0;
      76         108 :                 *target_len = dst_len;
      77         108 :                 return;
      78             :         }
      79             : 
      80             :         /* Bail out if an unexpected error occurred.
      81             :          * (U_BUFFER_OVERFLOW_ERROR means that *target buffer is not large enough).
      82             :          * (U_STRING_NOT_TERMINATED_WARNING usually means that the input string is empty).
      83             :          */
      84        1368 :         if( *status != U_BUFFER_OVERFLOW_ERROR && *status != U_STRING_NOT_TERMINATED_WARNING )
      85          10 :                 return;
      86             : 
      87             :         /* Allocate memory for the destination buffer (it will be zero-terminated). */
      88        1358 :         dst_buf = eumalloc( dst_len + 1 );
      89             : 
      90             :         /* Convert source string from UTF-8 to UTF-16. */
      91        1358 :         *status = U_ZERO_ERROR;
      92        1358 :         u_strFromUTF8( dst_buf, dst_len+1, NULL, src, src_len, status );
      93        1358 :         if( U_FAILURE( *status ) )
      94             :         {
      95           0 :                 efree( dst_buf );
      96           0 :                 return;
      97             :         }
      98             : 
      99        1358 :         dst_buf[dst_len] = 0;
     100             : 
     101        1358 :         if( *target )
     102           0 :                 efree( *target );
     103             : 
     104        1358 :         *target     = dst_buf;
     105        1358 :         *target_len = dst_len;
     106             : }
     107             : /* }}} */
     108             : 
     109             : /* {{{ intl_convert_utf16_to_utf8
     110             :  * Convert given string from UTF-16 to UTF-8.
     111             :  *
     112             :  * @param source      String to convert.
     113             :  * @param source_len  Length of the source string.
     114             :  * @param status      Conversion status.
     115             :  *
     116             :  * @return zend_string
     117             :  */
     118        2540 : zend_string* intl_convert_utf16_to_utf8(
     119             :         const UChar* src,    int32_t  src_len,
     120             :         UErrorCode*  status )
     121             : {
     122             :         zend_string* dst;
     123             :         int32_t      dst_len;
     124             : 
     125             :         /* Determine required destination buffer size (pre-flighting). */
     126        2540 :         *status = U_ZERO_ERROR;
     127        2540 :         u_strToUTF8( NULL, 0, &dst_len, src, src_len, status );
     128             : 
     129             :         /* Bail out if an unexpected error occurred.
     130             :          * (U_BUFFER_OVERFLOW_ERROR means that *target buffer is not large enough).
     131             :          * (U_STRING_NOT_TERMINATED_WARNING usually means that the input string is empty).
     132             :          */
     133        2540 :         if( *status != U_BUFFER_OVERFLOW_ERROR && *status != U_STRING_NOT_TERMINATED_WARNING )
     134           0 :                 return NULL;
     135             : 
     136             :         /* Allocate memory for the destination buffer (it will be zero-terminated). */
     137        5080 :         dst = zend_string_alloc(dst_len, 0);
     138             : 
     139             :         /* Convert source string from UTF-8 to UTF-16. */
     140        2540 :         *status = U_ZERO_ERROR;
     141        2540 :         u_strToUTF8( ZSTR_VAL(dst), dst_len, NULL, src, src_len, status );
     142        2540 :         if( U_FAILURE( *status ) )
     143             :         {
     144             :                 zend_string_free(dst);
     145           0 :                 return NULL;
     146             :         }
     147             : 
     148             :         /* U_STRING_NOT_TERMINATED_WARNING is OK for us => reset 'status'. */
     149        2540 :         *status = U_ZERO_ERROR;
     150             : 
     151        2540 :         ZSTR_VAL(dst)[dst_len] = 0;
     152        2540 :         return dst;
     153             : }
     154             : /* }}} */
     155             : 
     156             : /*
     157             :  * Local variables:
     158             :  * tab-width: 4
     159             :  * c-basic-offset: 4
     160             :  * End:
     161             :  * vim600: noet sw=4 ts=4 fdm=marker
     162             :  * vim<600: noet sw=4 ts=4
     163             :  */

Generated by: LCOV version 1.10

Generated at Wed, 02 Sep 2015 17:19:10 +0000 (32 hours ago)

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