php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
transliterator_class.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: Gustavo Lopes <cataphract@php.net> |
12 +----------------------------------------------------------------------+
13 */
14
16#include "php_intl.h"
18#include "intl_error.h"
19#include "intl_convert.h"
20#include "intl_data.h"
21
22#include <unicode/utrans.h>
23
25
27
28/* {{{ int transliterator_object_construct( zval *object, UTransliterator *utrans, UErrorCode *status )
29 * Initialize internals of Transliterator_object.
30 */
32 UTransliterator *utrans,
33 UErrorCode *status )
34{
35 const UChar *ustr_id;
36 int32_t ustr_id_len;
37 zend_string *u8str;
38 zval tmp;
40
42
43 assert( to->utrans == NULL );
44 /* this assignment must happen before any return with failure because the
45 * caller relies on it always being made (so it can just destroy the object
46 * to close the transliterator) */
47 to->utrans = utrans;
48
49 ustr_id = utrans_getUnicodeID( utrans, &ustr_id_len );
50 u8str = intl_convert_utf16_to_utf8(ustr_id, (int ) ustr_id_len, status );
51 if( !u8str )
52 {
53 return FAILURE;
54 }
55
56 ZVAL_NEW_STR(&tmp, u8str);
58 "id", sizeof( "id" ) - 1, &tmp );
59 GC_DELREF(u8str);
60 return SUCCESS;
61}
62/* }}} */
63
64/*
65 * Auxiliary functions needed by objects of 'Transliterator' class
66 */
67
68/* {{{ void transliterator_object_init( Transliterator_object* to )
69 * Initialize internals of Transliterator_object.
70 */
71static void transliterator_object_init( Transliterator_object* to )
72{
73 if( !to )
74 return;
75
77}
78/* }}} */
79
80/* {{{ void transliterator_object_destroy( Transliterator_object* to )
81 * Clean up mem allocted by internals of Transliterator_object
82 */
83static void transliterator_object_destroy( Transliterator_object* to )
84{
85 if( !to )
86 return;
87
88 if( to->utrans )
89 {
90 utrans_close( to->utrans );
91 to->utrans = NULL;
92 }
93
95}
96/* }}} */
97
98/* {{{ Transliterator_objects_free */
99static void Transliterator_objects_free( zend_object *object )
100{
101 Transliterator_object* to = php_intl_transliterator_fetch_object(object);
102
103 zend_object_std_dtor( &to->zo );
104
105 transliterator_object_destroy( to );
106}
107/* }}} */
108
109/* {{{ Transliterator_object_create */
110static zend_object *Transliterator_object_create( zend_class_entry *ce )
111{
112 Transliterator_object* intern;
113
114 intern = zend_object_alloc(sizeof(Transliterator_object), ce);
115
116 zend_object_std_init( &intern->zo, ce );
117 object_properties_init( &intern->zo, ce );
118 transliterator_object_init( intern );
119
120 return &intern->zo;
121}
122/* }}} */
123
124/*
125 * Object handlers for Transliterator class (and subclasses)
126 */
127
128/* {{{ clone handler for Transliterator */
129static zend_object *Transliterator_clone_obj( zend_object *object )
130{
131 Transliterator_object *to_orig = php_intl_transliterator_fetch_object(object);
132 zend_object *ret_val = Transliterator_ce_ptr->create_object(object->ce);
133 Transliterator_object *to_new = php_intl_transliterator_fetch_object(ret_val);
134
135 zend_objects_clone_members( &to_new->zo, &to_orig->zo );
136 if (to_orig->utrans != NULL) {
137 /* guaranteed to return NULL if it fails */
138 UErrorCode error = U_ZERO_ERROR;
139 UTransliterator *utrans = utrans_clone( to_orig->utrans, &error);
140
141 if (U_FAILURE(error)) {
142 if (utrans != NULL) {
143 transliterator_object_destroy(to_new);
144 }
145 zend_throw_error(NULL, "Failed to clone Transliterator");
146 } else {
147 to_new->utrans = utrans;
148 }
149 } else {
150 /* We shouldn't have unconstructed objects in the first place */
151 zend_throw_error(NULL, "Cannot clone uninitialized Transliterator");
152 }
153
154 return ret_val;
155}
156/* }}} */
157
158/* {{{ transliterator_register_Transliterator_class
159 * Initialize 'Transliterator' class
160 */
162{
163 /* Create and register 'Transliterator' class. */
164 Transliterator_ce_ptr = register_class_Transliterator();
165 Transliterator_ce_ptr->create_object = Transliterator_object_create;
166 Transliterator_ce_ptr->default_object_handlers = &Transliterator_handlers;
169 Transliterator_handlers.free_obj = Transliterator_objects_free;
170 Transliterator_handlers.clone_obj = Transliterator_clone_obj;
171}
172/* }}} */
assert(mixed $assertion, Throwable|string|null $description=null)
const U_ZERO_ERROR
DNS_STATUS status
Definition dns_win32.c:49
error($message)
Definition ext_skel.php:22
memcpy(ptr1, ptr2, size)
#define NULL
Definition gdcache.h:45
#define SUCCESS
Definition hash_sha3.c:261
zend_string * intl_convert_utf16_to_utf8(const UChar *src, int32_t src_len, UErrorCode *status)
void intl_error_init(intl_error *err)
Definition intl_error.c:66
void intl_error_reset(intl_error *err)
Definition intl_error.c:78
zend_object_handlers Transliterator_handlers
zend_class_entry * Transliterator_ce_ptr
int transliterator_object_construct(zval *object, UTransliterator *utrans, UErrorCode *status)
void transliterator_register_Transliterator_class(void)
#define TRANSLITERATOR_ERROR_P(co)
#define TRANSLITERATOR_METHOD_FETCH_OBJECT_NO_CHECK
ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format,...)
Definition zend.c:1772
ZEND_API void zend_update_property(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zval *value)
Definition zend_API.c:4991
ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type)
Definition zend_API.c:1688
struct _zval_struct zval
struct _zend_string zend_string
ZEND_API const zend_object_handlers std_object_handlers
ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object, zend_object *old_object)
ZEND_API void ZEND_FASTCALL zend_object_std_init(zend_object *object, zend_class_entry *ce)
ZEND_API void zend_object_std_dtor(zend_object *object)
#define XtOffsetOf(s_type, field)
struct _zend_class_entry zend_class_entry
struct _zend_object zend_object
#define Z_OBJ_P(zval_p)
Definition zend_types.h:990
#define GC_DELREF(p)
Definition zend_types.h:710
@ FAILURE
Definition zend_types.h:61
#define ZVAL_NEW_STR(z, s)
struct _zend_object_handlers zend_object_handlers
Definition zend_types.h:88
object