collator_convert.c [plain text]
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php_intl.h"
#include "collator_class.h"
#include "collator_is_numeric.h"
#include "collator_convert.h"
#include "intl_convert.h"
#include <unicode/ustring.h>
#include <php.h>
#define COLLATOR_CONVERT_RETURN_FAILED(retval) { \
Z_TRY_ADDREF_P(retval); \
return retval; \
}
static void collator_convert_hash_item_from_utf8_to_utf16(
HashTable* hash, zval *hashData, zend_string *hashKey, zend_ulong hashIndex,
UErrorCode* status )
{
const char* old_val;
size_t old_val_len;
UChar* new_val = NULL;
int32_t new_val_len = 0;
zval znew_val;
if( Z_TYPE_P( hashData ) != IS_STRING )
return;
old_val = Z_STRVAL_P( hashData );
old_val_len = Z_STRLEN_P( hashData );
intl_convert_utf8_to_utf16( &new_val, &new_val_len, old_val, old_val_len, status );
if( U_FAILURE( *status ) )
return;
ZVAL_STRINGL( &znew_val, (char*)new_val, UBYTES(new_val_len + 1) );
efree(new_val);
Z_STRLEN(znew_val) = Z_STRLEN(znew_val) - UBYTES(1);
if( hashKey)
{
zend_hash_update( hash, hashKey, &znew_val);
}
else
{
zend_hash_index_update( hash, hashIndex, &znew_val);
}
}
static void collator_convert_hash_item_from_utf16_to_utf8(
HashTable* hash, zval * hashData, zend_string* hashKey, zend_ulong hashIndex,
UErrorCode* status )
{
const char* old_val;
size_t old_val_len;
zend_string* u8str;
zval znew_val;
if( Z_TYPE_P( hashData ) != IS_STRING )
return;
old_val = Z_STRVAL_P( hashData );
old_val_len = Z_STRLEN_P( hashData );
u8str = intl_convert_utf16_to_utf8(
(UChar*)old_val, UCHARS(old_val_len), status );
if( !u8str )
return;
ZVAL_NEW_STR( &znew_val, u8str);
if( hashKey )
{
zend_hash_update( hash, hashKey, &znew_val);
}
else
{
zend_hash_index_update( hash, hashIndex, &znew_val);
}
}
void collator_convert_hash_from_utf8_to_utf16( HashTable* hash, UErrorCode* status )
{
zend_ulong hashIndex;
zval *hashData;
zend_string *hashKey;
ZEND_HASH_FOREACH_KEY_VAL(hash, hashIndex, hashKey, hashData) {
collator_convert_hash_item_from_utf8_to_utf16(
hash, hashData, hashKey, hashIndex, status );
if( U_FAILURE( *status ) )
return;
} ZEND_HASH_FOREACH_END();
}
void collator_convert_hash_from_utf16_to_utf8( HashTable* hash, UErrorCode* status )
{
zend_ulong hashIndex;
zend_string *hashKey;
zval *hashData;
ZEND_HASH_FOREACH_KEY_VAL(hash, hashIndex, hashKey, hashData) {
collator_convert_hash_item_from_utf16_to_utf8(
hash, hashData, hashKey, hashIndex, status );
if( U_FAILURE( *status ) ) {
return;
}
} ZEND_HASH_FOREACH_END();
}
zval* collator_convert_zstr_utf16_to_utf8( zval* utf16_zval, zval *rv )
{
zend_string* u8str;
UErrorCode status = U_ZERO_ERROR;
u8str = intl_convert_utf16_to_utf8(
(UChar*) Z_STRVAL_P(utf16_zval), UCHARS( Z_STRLEN_P(utf16_zval) ), &status );
if( !u8str ) {
php_error( E_WARNING, "Error converting utf16 to utf8 in collator_convert_zval_utf16_to_utf8()" );
ZVAL_EMPTY_STRING( rv );
} else {
ZVAL_NEW_STR( rv, u8str );
}
return rv;
}
zval* collator_convert_zstr_utf8_to_utf16( zval* utf8_zval, zval *rv )
{
zval* zstr = NULL;
UChar* ustr = NULL;
int32_t ustr_len = 0;
UErrorCode status = U_ZERO_ERROR;
intl_convert_utf8_to_utf16(
&ustr, &ustr_len,
Z_STRVAL_P( utf8_zval ), Z_STRLEN_P( utf8_zval ),
&status );
if( U_FAILURE( status ) )
php_error( E_WARNING, "Error casting object to string in collator_convert_zstr_utf8_to_utf16()" );
zstr = rv;
ZVAL_STRINGL( zstr, (char*)ustr, UBYTES(ustr_len));
efree((char *)ustr);
return zstr;
}
zval* collator_convert_object_to_string( zval* obj, zval *rv )
{
zval* zstr = NULL;
UErrorCode status = U_ZERO_ERROR;
UChar* ustr = NULL;
int32_t ustr_len = 0;
if( Z_TYPE_P( obj ) != IS_OBJECT )
{
COLLATOR_CONVERT_RETURN_FAILED( obj );
}
if( Z_OBJ_HT_P(obj)->get )
{
zstr = Z_OBJ_HT_P(obj)->get( obj, rv );
switch( Z_TYPE_P( zstr ) )
{
case IS_OBJECT:
{
zval_ptr_dtor( zstr );
COLLATOR_CONVERT_RETURN_FAILED( obj );
} break;
case IS_STRING:
break;
default:
{
convert_to_string( zstr );
} break;
}
}
else if( Z_OBJ_HT_P(obj)->cast_object )
{
zstr = rv;
if( Z_OBJ_HT_P(obj)->cast_object( obj, zstr, IS_STRING ) == FAILURE )
{
zval_ptr_dtor( zstr );
COLLATOR_CONVERT_RETURN_FAILED( obj );
}
}
if( zstr == NULL )
{
COLLATOR_CONVERT_RETURN_FAILED( obj );
}
intl_convert_utf8_to_utf16(
&ustr, &ustr_len,
Z_STRVAL_P( zstr ), Z_STRLEN_P( zstr ),
&status );
if( U_FAILURE( status ) )
php_error( E_WARNING, "Error casting object to string in collator_convert_object_to_string()" );
zval_dtor( zstr );
ZVAL_STRINGL( zstr, (char*)ustr, UBYTES(ustr_len));
efree((char *)ustr);
return zstr;
}
zval* collator_convert_string_to_number( zval* str, zval *rv )
{
zval* num = collator_convert_string_to_number_if_possible( str, rv );
if( num == str )
{
zval_ptr_dtor( num );
num = rv;
ZVAL_LONG( num, 0 );
}
return num;
}
zval* collator_convert_string_to_double( zval* str, zval *rv )
{
zval* num = collator_convert_string_to_number( str, rv );
if( Z_TYPE_P(num) == IS_LONG )
{
ZVAL_DOUBLE( num, Z_LVAL_P( num ) );
}
return num;
}
zval* collator_convert_string_to_number_if_possible( zval* str, zval *rv )
{
int is_numeric = 0;
zend_long lval = 0;
double dval = 0;
if( Z_TYPE_P( str ) != IS_STRING )
{
COLLATOR_CONVERT_RETURN_FAILED( str );
}
if( ( is_numeric = collator_is_numeric( (UChar*) Z_STRVAL_P(str), UCHARS( Z_STRLEN_P(str) ), &lval, &dval, 1 ) ) )
{
if( is_numeric == IS_LONG ) {
ZVAL_LONG(rv, lval);
}
if( is_numeric == IS_DOUBLE )
ZVAL_DOUBLE(rv, dval);
}
else
{
COLLATOR_CONVERT_RETURN_FAILED( str );
}
return rv;
}
zval* collator_make_printable_zval( zval* arg, zval *rv)
{
zval arg_copy;
int use_copy = 0;
zval* str = NULL;
if( Z_TYPE_P(arg) != IS_STRING )
{
use_copy = zend_make_printable_zval(arg, &arg_copy);
if( use_copy )
{
str = collator_convert_zstr_utf8_to_utf16( &arg_copy, rv );
zval_dtor( &arg_copy );
}
else
{
str = collator_convert_zstr_utf8_to_utf16( arg, rv );
}
}
else
{
COLLATOR_CONVERT_RETURN_FAILED( arg );
}
return str;
}
zval* collator_normalize_sort_argument( zval* arg, zval *rv )
{
zval* n_arg = NULL;
if( Z_TYPE_P( arg ) != IS_STRING )
{
COLLATOR_CONVERT_RETURN_FAILED( arg );
}
n_arg = collator_convert_string_to_number_if_possible( arg, rv );
if( n_arg == arg )
{
zval_ptr_dtor( n_arg );
n_arg = collator_convert_zstr_utf16_to_utf8( arg, rv );
}
return n_arg;
}