php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
collator_convert.c
Go to the documentation of this file.
1/*
2 +----------------------------------------------------------------------+
3 | This source file is subject to version 3.01 of the PHP license, |
4 | that is bundled with this package in the file LICENSE, and is |
5 | available through the world-wide-web at the following url: |
6 | https://www.php.net/license/3_01.txt |
7 | If you did not receive a copy of the PHP license and are unable to |
8 | obtain it through the world-wide-web, please send a note to |
9 | license@php.net so we can mail you a copy immediately. |
10 +----------------------------------------------------------------------+
11 | Authors: Vadim Savchuk <vsavchuk@productengine.com> |
12 | Dmitry Lakhtyuk <dlakhtyuk@productengine.com> |
13 +----------------------------------------------------------------------+
14 */
15
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include "php_intl.h"
21#include "collator_class.h"
22#include "collator_is_numeric.h"
23#include "collator_convert.h"
24#include "intl_convert.h"
25
26#include <unicode/ustring.h>
27#include <php.h>
28
29#define COLLATOR_CONVERT_RETURN_FAILED(retval) { \
30 Z_TRY_ADDREF_P(retval); \
31 return retval; \
32 }
33
34/* {{{ collator_convert_hash_item_from_utf8_to_utf16 */
35static void collator_convert_hash_item_from_utf8_to_utf16(
36 HashTable* hash, zval *hashData, zend_string *hashKey, zend_ulong hashIndex,
37 UErrorCode* status )
38{
39 const char* old_val;
40 size_t old_val_len;
41 UChar* new_val = NULL;
42 int32_t new_val_len = 0;
43 zval znew_val;
44
45 /* Process string values only. */
46 if( Z_TYPE_P( hashData ) != IS_STRING )
47 return;
48
49 old_val = Z_STRVAL_P( hashData );
50 old_val_len = Z_STRLEN_P( hashData );
51
52 /* Convert it from UTF-8 to UTF-16LE and save the result to new_val[_len]. */
53 intl_convert_utf8_to_utf16( &new_val, &new_val_len, old_val, old_val_len, status );
54 if( U_FAILURE( *status ) )
55 return;
56
57 /* Update current hash item with the converted value. */
58 ZVAL_STRINGL( &znew_val, (char*)new_val, UBYTES(new_val_len + 1) );
59 //???
60 efree(new_val);
61 /* hack to fix use of initialized value */
62 Z_STRLEN(znew_val) = Z_STRLEN(znew_val) - UBYTES(1);
63
64 if( hashKey)
65 {
66 zend_hash_update( hash, hashKey, &znew_val);
67 }
68 else /* hashKeyType == HASH_KEY_IS_LONG */
69 {
70 zend_hash_index_update( hash, hashIndex, &znew_val);
71 }
72}
73/* }}} */
74
75/* {{{ collator_convert_hash_item_from_utf16_to_utf8 */
76static void collator_convert_hash_item_from_utf16_to_utf8(
77 HashTable* hash, zval * hashData, zend_string* hashKey, zend_ulong hashIndex,
78 UErrorCode* status )
79{
80 const char* old_val;
81 size_t old_val_len;
82 zend_string* u8str;
83 zval znew_val;
84
85 /* Process string values only. */
86 if( Z_TYPE_P( hashData ) != IS_STRING )
87 return;
88
89 old_val = Z_STRVAL_P( hashData );
90 old_val_len = Z_STRLEN_P( hashData );
91
92 /* Convert it from UTF-16LE to UTF-8 and save the result to new_val[_len]. */
94 (UChar*)old_val, UCHARS(old_val_len), status );
95 if( !u8str )
96 return;
97
98 /* Update current hash item with the converted value. */
99 ZVAL_NEW_STR( &znew_val, u8str);
100
101 if( hashKey )
102 {
103 zend_hash_update( hash, hashKey, &znew_val);
104 }
105 else /* hashKeyType == HASH_KEY_IS_LONG */
106 {
107 zend_hash_index_update( hash, hashIndex, &znew_val);
108 }
109}
110/* }}} */
111
112/* {{{ collator_convert_hash_from_utf8_to_utf16
113 * Convert values of the given hash from UTF-8 encoding to UTF-16LE.
114 */
116{
117 zend_ulong hashIndex;
118 zval *hashData;
119 zend_string *hashKey;
120
121 ZEND_HASH_FOREACH_KEY_VAL(hash, hashIndex, hashKey, hashData) {
122 /* Convert current hash item from UTF-8 to UTF-16LE. */
123 collator_convert_hash_item_from_utf8_to_utf16(
124 hash, hashData, hashKey, hashIndex, status );
125 if( U_FAILURE( *status ) )
126 return;
128}
129/* }}} */
130
131/* {{{ collator_convert_hash_from_utf16_to_utf8
132 * Convert values of the given hash from UTF-16LE encoding to UTF-8.
133 */
135{
136 zend_ulong hashIndex;
137 zend_string *hashKey;
138 zval *hashData;
139
140 ZEND_HASH_FOREACH_KEY_VAL(hash, hashIndex, hashKey, hashData) {
141 /* Convert current hash item from UTF-16LE to UTF-8. */
142 collator_convert_hash_item_from_utf16_to_utf8(
143 hash, hashData, hashKey, hashIndex, status );
144 if( U_FAILURE( *status ) ) {
145 return;
146 }
148}
149/* }}} */
150
151/* {{{ collator_convert_zstr_utf16_to_utf8
152 *
153 * Convert string from utf16 to utf8.
154 *
155 * @param zval* utf16_zval String to convert.
156 *
157 * @return zval* Converted string.
158 */
160{
161 zend_string* u8str;
162 UErrorCode status = U_ZERO_ERROR;
163
164 /* Convert to utf8 then. */
166 (UChar*) Z_STRVAL_P(utf16_zval), UCHARS( Z_STRLEN_P(utf16_zval) ), &status );
167 if( !u8str ) {
168 php_error( E_WARNING, "Error converting utf16 to utf8 in collator_convert_zval_utf16_to_utf8()" );
170 } else {
171 ZVAL_NEW_STR( rv, u8str );
172 }
173 return rv;
174}
175/* }}} */
176
178{
179 UChar *ustr = NULL;
180 int32_t ustr_len = 0;
181 UErrorCode status = U_ZERO_ERROR;
182
183 /* Convert the string to UTF-16. */
185 &ustr, &ustr_len,
186 ZSTR_VAL(utf8_str), ZSTR_LEN(utf8_str),
187 &status);
188 // FIXME Or throw error or use intl internal error handler
189 if (U_FAILURE(status)) {
191 "Error casting object to string in collator_convert_zstr_utf8_to_utf16()");
192 }
193
194 zend_string *zstr = zend_string_init((char *) ustr, UBYTES(ustr_len), 0);
195 efree((char *)ustr);
196 return zstr;
197}
198
199/* {{{ collator_convert_object_to_string
200 * Convert object to UTF16-encoded string.
201 */
203{
204 zval* zstr = NULL;
205 UErrorCode status = U_ZERO_ERROR;
206 UChar* ustr = NULL;
207 int32_t ustr_len = 0;
208
209 /* Bail out if it's not an object. */
210 if( Z_TYPE_P( obj ) != IS_OBJECT )
211 {
213 }
214
215 /* Try object's handlers. */
216 zstr = rv;
217
218 if( Z_OBJ_HT_P(obj)->cast_object( Z_OBJ_P(obj), zstr, IS_STRING ) == FAILURE )
219 {
220 /* cast_object failed => bail out. */
221 zval_ptr_dtor( zstr );
223 }
224
225 /* Object wasn't successfully converted => bail out. */
226 if( zstr == NULL )
227 {
229 }
230
231 /* Convert the string to UTF-16. */
233 &ustr, &ustr_len,
234 Z_STRVAL_P( zstr ), Z_STRLEN_P( zstr ),
235 &status );
236 // FIXME Or throw error or use intl internal error handler
237 if( U_FAILURE( status ) )
238 php_error( E_WARNING, "Error casting object to string in collator_convert_object_to_string()" );
239
240 /* Cleanup zstr to hold utf16 string. */
241 zval_ptr_dtor_str( zstr );
242
243 /* Set string. */
244 ZVAL_STRINGL( zstr, (char*)ustr, UBYTES(ustr_len));
245 //???
246 efree((char *)ustr);
247
248 /* Don't free ustr cause it's set in zstr without copy.
249 * efree( ustr );
250 */
251
252 return zstr;
253}
254/* }}} */
255
256/* {{{ collator_convert_string_to_number
257 *
258 * Convert string to number.
259 *
260 * @param zval* str String to convert.
261 *
262 * @return zval* Number. If str is not numeric string return number zero.
263 */
265{
267 if( num == str )
268 {
269 /* String wasn't converted => return zero. */
270 zval_ptr_dtor( num );
271
272 num = rv;
273 ZVAL_LONG( num, 0 );
274 }
275
276 return num;
277}
278/* }}} */
279
280/* {{{ collator_convert_string_to_double
281 *
282 * Convert string to double.
283 *
284 * @param zval* str String to convert.
285 *
286 * @return zval* Number. If str is not numeric string return number zero.
287 */
289{
291 if( Z_TYPE_P(num) == IS_LONG )
292 {
293 ZVAL_DOUBLE( num, Z_LVAL_P( num ) );
294 }
295
296 return num;
297}
298/* }}} */
299
300/* {{{ collator_convert_string_to_number_if_possible
301 *
302 * Convert string to numer.
303 *
304 * @param zval* str String to convert.
305 *
306 * @return zval* Number if str is numeric string. Otherwise
307 * original str param.
308 */
310{
311 uint8_t is_numeric = 0;
312 zend_long lval = 0;
313 double dval = 0;
314
315 if( Z_TYPE_P( str ) != IS_STRING )
316 {
318 }
319
320 if ( ( is_numeric = collator_is_numeric( (UChar*) Z_STRVAL_P(str), UCHARS( Z_STRLEN_P(str) ), &lval, &dval, /* allow_errors */ 1 ) ) )
321 {
322 if( is_numeric == IS_LONG ) {
323 ZVAL_LONG(rv, lval);
324 }
325 if( is_numeric == IS_DOUBLE )
327 }
328 else
329 {
331 }
332
333 return rv;
334}
335/* }}} */
336
337/* Returns string from input zval.
338 *
339 * @param zval* arg zval to get string from
340 *
341 * @return zend_string* UTF16 string.
342 */
344{
345 // TODO: This is extremely weird in that it leaves pre-existing strings alone and does not
346 // perform a UTF-8 to UTF-16 conversion for them. The assumption is that values that are
347 // already strings have already been converted beforehand. It would be good to clean this up.
348 if (Z_TYPE_P(arg) == IS_STRING) {
349 return zend_string_copy(Z_STR_P(arg));
350 }
351
352 zend_string *utf8_str = zval_get_string(arg);
354 zend_string_release(utf8_str);
355 return utf16_str;
356}
357
358/* {{{ collator_normalize_sort_argument
359 *
360 * Normalize argument to use in sort's compare function.
361 *
362 * @param zval* arg Sort's argument to normalize.
363 *
364 * @return zval* Normalized copy of arg or unmodified arg
365 * if normalization is not needed.
366 */
368{
369 zval* n_arg = NULL;
370
371 if( Z_TYPE_P( arg ) != IS_STRING )
372 {
373 /* If it's not a string then nothing to do.
374 * Return original arg.
375 */
377 }
378
379 /* Try convert to number. */
381
382 if( n_arg == arg )
383 {
384 /* Conversion to number failed. */
385 zval_ptr_dtor( n_arg );
386
387 /* Convert string to utf8. */
389 }
390
391 return n_arg;
392}
393/* }}} */
is_numeric(mixed $value)
zval * collator_normalize_sort_argument(zval *arg, zval *rv)
zend_string * collator_zval_to_string(zval *arg)
void collator_convert_hash_from_utf16_to_utf8(HashTable *hash, UErrorCode *status)
zval * collator_convert_string_to_number(zval *str, zval *rv)
zval * collator_convert_string_to_number_if_possible(zval *str, zval *rv)
zval * collator_convert_string_to_double(zval *str, zval *rv)
zend_string * collator_convert_zstr_utf8_to_utf16(zend_string *utf8_str)
void collator_convert_hash_from_utf8_to_utf16(HashTable *hash, UErrorCode *status)
#define COLLATOR_CONVERT_RETURN_FAILED(retval)
zval * collator_convert_zstr_utf16_to_utf8(zval *utf16_zval, zval *rv)
zval * collator_convert_object_to_string(zval *obj, zval *rv)
uint8_t collator_is_numeric(UChar *str, int32_t length, zend_long *lval, double *dval, bool allow_errors)
const U_ZERO_ERROR
DNS_STATUS status
Definition dns_win32.c:49
zval * arg
Definition ffi.c:3975
#define NULL
Definition gdcache.h:45
hash(string $algo, string $data, bool $binary=false, array $options=[])
Definition hash.stub.php:12
#define UBYTES(len)
Definition intl_common.h:27
#define UCHARS(len)
Definition intl_common.h:39
void intl_convert_utf8_to_utf16(UChar **target, int32_t *target_len, const char *src, size_t src_len, UErrorCode *status)
zend_string * intl_convert_utf16_to_utf8(const UChar *src, int32_t src_len, UErrorCode *status)
#define php_error
Definition php.h:310
zval rv
Definition session.c:1024
#define ZVAL_STRINGL(z, s, l)
Definition zend_API.h:952
#define ZVAL_EMPTY_STRING(z)
Definition zend_API.h:961
#define efree(ptr)
Definition zend_alloc.h:155
struct _zval_struct zval
#define E_WARNING
Definition zend_errors.h:24
ZEND_API zval *ZEND_FASTCALL zend_hash_index_update(HashTable *ht, zend_ulong h, zval *pData)
Definition zend_hash.c:1219
ZEND_API zval *ZEND_FASTCALL zend_hash_update(HashTable *ht, zend_string *key, zval *pData)
Definition zend_hash.c:997
#define ZEND_HASH_FOREACH_KEY_VAL(ht, _h, _key, _val)
Definition zend_hash.h:1181
#define ZEND_HASH_FOREACH_END()
Definition zend_hash.h:1086
int32_t zend_long
Definition zend_long.h:42
uint32_t zend_ulong
Definition zend_long.h:43
struct _zend_string zend_string
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
#define ZSTR_LEN(zstr)
Definition zend_string.h:69
#define dval(x)
#define Z_TYPE_P(zval_p)
Definition zend_types.h:660
#define Z_STRVAL_P(zval_p)
Definition zend_types.h:975
#define ZVAL_LONG(z, l)
#define IS_STRING
Definition zend_types.h:606
struct _zend_array HashTable
Definition zend_types.h:386
#define Z_OBJ_P(zval_p)
Definition zend_types.h:990
#define Z_OBJ_HT_P(zval_p)
Definition zend_types.h:993
#define IS_DOUBLE
Definition zend_types.h:605
#define Z_STR_P(zval_p)
Definition zend_types.h:972
#define Z_STRLEN_P(zval_p)
Definition zend_types.h:978
@ FAILURE
Definition zend_types.h:61
#define Z_STRLEN(zval)
Definition zend_types.h:977
#define IS_OBJECT
Definition zend_types.h:608
#define IS_LONG
Definition zend_types.h:604
#define ZVAL_NEW_STR(z, s)
#define ZVAL_DOUBLE(z, d)
#define Z_LVAL_P(zval_p)
Definition zend_types.h:966
ZEND_API void zval_ptr_dtor(zval *zval_ptr)