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: 32 38 84.2 %
Date: 2014-09-29 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       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             :  * @param target      Where to place the result.
      39             :  * @param target_len  Result length.
      40             :  * @param source      String to convert.
      41             :  * @param source_len  Length of the source string.
      42             :  * @param status      Conversion status.
      43             :  *
      44             :  * @return void       This function does not return anything.
      45             :  */
      46        1735 : void intl_convert_utf8_to_utf16(
      47             :         UChar**     target, int* target_len,
      48             :         const char* src,    int  src_len,
      49             :         UErrorCode* status )
      50             : {
      51        1735 :         UChar*      dst_buf = NULL;
      52        1735 :         int32_t     dst_len = 0;
      53             : 
      54             :         /* If *target is NULL determine required destination buffer size (pre-flighting).
      55             :          * Otherwise, attempt to convert source string; if *target buffer is not large enough
      56             :          * it will be resized appropriately.
      57             :          */
      58        1735 :         *status = U_ZERO_ERROR;
      59             : 
      60        1735 :         u_strFromUTF8( *target, *target_len, &dst_len, src, src_len, status );
      61             : 
      62        1735 :         if( *status == U_ZERO_ERROR )
      63             :         {
      64             :                 /* String is converted successfuly */
      65         108 :                 (*target)[dst_len] = 0;
      66         108 :                 *target_len = dst_len;
      67         108 :                 return;
      68             :         }
      69             : 
      70             :         /* Bail out if an unexpected error occurred.
      71             :          * (U_BUFFER_OVERFLOW_ERROR means that *target buffer is not large enough).
      72             :          * (U_STRING_NOT_TERMINATED_WARNING usually means that the input string is empty).
      73             :          */
      74        1627 :         if( *status != U_BUFFER_OVERFLOW_ERROR && *status != U_STRING_NOT_TERMINATED_WARNING )
      75          11 :                 return;
      76             : 
      77             :         /* Allocate memory for the destination buffer (it will be zero-terminated). */
      78        1616 :         dst_buf = eumalloc( dst_len + 1 );
      79             : 
      80             :         /* Convert source string from UTF-8 to UTF-16. */
      81        1616 :         *status = U_ZERO_ERROR;
      82        1616 :         u_strFromUTF8( dst_buf, dst_len+1, NULL, src, src_len, status );
      83        1616 :         if( U_FAILURE( *status ) )
      84             :         {
      85           0 :                 efree( dst_buf );
      86           0 :                 return;
      87             :         }
      88             : 
      89        1616 :         dst_buf[dst_len] = 0;
      90             : 
      91        1616 :         if( *target )
      92           0 :                 efree( *target );
      93             : 
      94        1616 :         *target     = dst_buf;
      95        1616 :         *target_len = dst_len;
      96             : }
      97             : /* }}} */
      98             : 
      99             : /* {{{ intl_convert_utf16_to_utf8
     100             :  * Convert given string from UTF-16 to UTF-8.
     101             :  *
     102             :  * @param target      Where to place the result.
     103             :  * @param target_len  Result length.
     104             :  * @param source      String to convert.
     105             :  * @param source_len  Length of the source string.
     106             :  * @param status      Conversion status.
     107             :  *
     108             :  * @return void       This function does not return anything.
     109             :  */
     110        2474 : void intl_convert_utf16_to_utf8(
     111             :         char**       target, int* target_len,
     112             :         const UChar* src,    int  src_len,
     113             :         UErrorCode*  status )
     114             : {
     115        2474 :         char*       dst_buf = NULL;
     116             :         int32_t     dst_len;
     117             : 
     118             :         /* Determine required destination buffer size (pre-flighting). */
     119        2474 :         *status = U_ZERO_ERROR;
     120        2474 :         u_strToUTF8( NULL, 0, &dst_len, src, src_len, status );
     121             : 
     122             :         /* Bail out if an unexpected error occurred.
     123             :          * (U_BUFFER_OVERFLOW_ERROR means that *target buffer is not large enough).
     124             :          * (U_STRING_NOT_TERMINATED_WARNING usually means that the input string is empty).
     125             :          */
     126        2474 :         if( *status != U_BUFFER_OVERFLOW_ERROR && *status != U_STRING_NOT_TERMINATED_WARNING )
     127           0 :                 return;
     128             : 
     129             :         /* Allocate memory for the destination buffer (it will be zero-terminated). */
     130        2474 :         dst_buf = emalloc( dst_len+1 );
     131             : 
     132             :         /* Convert source string from UTF-8 to UTF-16. */
     133        2474 :         *status = U_ZERO_ERROR;
     134        2474 :         u_strToUTF8( dst_buf, dst_len, NULL, src, src_len, status );
     135        2474 :         if( U_FAILURE( *status ) )
     136             :         {
     137           0 :                 efree( dst_buf );
     138           0 :                 return;
     139             :         }
     140             : 
     141             :         /* U_STRING_NOT_TERMINATED_WARNING is OK for us => reset 'status'. */
     142        2474 :         *status = U_ZERO_ERROR;
     143             : 
     144        2474 :         dst_buf[dst_len] = 0;
     145        2474 :         *target     = dst_buf;
     146        2474 :         *target_len = dst_len;
     147             : }
     148             : /* }}} */
     149             : 
     150             : /*
     151             :  * Local variables:
     152             :  * tab-width: 4
     153             :  * c-basic-offset: 4
     154             :  * End:
     155             :  * vim600: noet sw=4 ts=4 fdm=marker
     156             :  * vim<600: noet sw=4 ts=4
     157             :  */

Generated by: LCOV version 1.10

Generated at Mon, 29 Sep 2014 14:26:22 +0000 (23 hours ago)

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