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: 33 41 80.5 %
Date: 2015-04-14 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 target      Where to place the result.
     113             :  * @param target_len  Result length.
     114             :  * @param source      String to convert.
     115             :  * @param source_len  Length of the source string.
     116             :  * @param status      Conversion status.
     117             :  *
     118             :  * @return void       This function does not return anything.
     119             :  */
     120        2536 : void intl_convert_utf16_to_utf8(
     121             :         char**       target, size_t* target_len,
     122             :         const UChar* src,    int32_t  src_len,
     123             :         UErrorCode*  status )
     124             : {
     125        2536 :         char*       dst_buf = NULL;
     126             :         int32_t     dst_len;
     127             : 
     128             :         /* Determine required destination buffer size (pre-flighting). */
     129        2536 :         *status = U_ZERO_ERROR;
     130        2536 :         u_strToUTF8( NULL, 0, &dst_len, src, src_len, status );
     131             : 
     132             :         /* Bail out if an unexpected error occurred.
     133             :          * (U_BUFFER_OVERFLOW_ERROR means that *target buffer is not large enough).
     134             :          * (U_STRING_NOT_TERMINATED_WARNING usually means that the input string is empty).
     135             :          */
     136        2536 :         if( *status != U_BUFFER_OVERFLOW_ERROR && *status != U_STRING_NOT_TERMINATED_WARNING )
     137           0 :                 return;
     138             : 
     139             :         /* Allocate memory for the destination buffer (it will be zero-terminated). */
     140        2536 :         dst_buf = emalloc( dst_len+1 );
     141             : 
     142             :         /* Convert source string from UTF-8 to UTF-16. */
     143        2536 :         *status = U_ZERO_ERROR;
     144        2536 :         u_strToUTF8( dst_buf, dst_len, NULL, src, src_len, status );
     145        2536 :         if( U_FAILURE( *status ) )
     146             :         {
     147           0 :                 efree( dst_buf );
     148           0 :                 return;
     149             :         }
     150             : 
     151             :         /* U_STRING_NOT_TERMINATED_WARNING is OK for us => reset 'status'. */
     152        2536 :         *status = U_ZERO_ERROR;
     153             : 
     154        2536 :         dst_buf[dst_len] = 0;
     155        2536 :         *target     = dst_buf;
     156        2536 :         *target_len = (size_t)dst_len;
     157             : }
     158             : /* }}} */
     159             : 
     160             : /*
     161             :  * Local variables:
     162             :  * tab-width: 4
     163             :  * c-basic-offset: 4
     164             :  * End:
     165             :  * vim600: noet sw=4 ts=4 fdm=marker
     166             :  * vim<600: noet sw=4 ts=4
     167             :  */

Generated by: LCOV version 1.10

Generated at Tue, 14 Apr 2015 11:48:43 +0000 (14 days ago)

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