1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
/*
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Gustavo Lopes <cataphract@php.net> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <unicode/brkiter.h>
#include <unicode/rbbi.h>
#include "codepointiterator_internal.h"
#include "breakiterator_iterators.h"
#include <typeinfo>
extern "C" {
#define USE_BREAKITERATOR_POINTER 1
#include "breakiterator_class.h"
#include "breakiterator_arginfo.h"
#include <zend_exceptions.h>
#include <zend_interfaces.h>
#include <assert.h>
}
using PHP::CodePointBreakIterator;
using icu::RuleBasedBreakIterator;
/* {{{ Global variables */
zend_class_entry *BreakIterator_ce_ptr;
zend_class_entry *RuleBasedBreakIterator_ce_ptr;
zend_class_entry *CodePointBreakIterator_ce_ptr;
zend_object_handlers BreakIterator_handlers;
/* }}} */
U_CFUNC void breakiterator_object_create(zval *object,
BreakIterator *biter, int brand_new)
{
UClassID classId = biter->getDynamicClassID();
zend_class_entry *ce;
if (classId == RuleBasedBreakIterator::getStaticClassID()) {
ce = RuleBasedBreakIterator_ce_ptr;
} else if (classId == CodePointBreakIterator::getStaticClassID()) {
ce = CodePointBreakIterator_ce_ptr;
} else {
ce = BreakIterator_ce_ptr;
}
if (brand_new) {
object_init_ex(object, ce);
}
breakiterator_object_construct(object, biter);
}
U_CFUNC void breakiterator_object_construct(zval *object,
BreakIterator *biter)
{
BreakIterator_object *bio;
BREAKITER_METHOD_FETCH_OBJECT_NO_CHECK; //populate to from object
ZEND_ASSERT(bio->biter == NULL);
bio->biter = biter;
}
/* {{{ compare handler for BreakIterator */
static int BreakIterator_compare_objects(zval *object1,
zval *object2)
{
BreakIterator_object *bio1,
*bio2;
ZEND_COMPARE_OBJECTS_FALLBACK(object1, object2);
bio1 = Z_INTL_BREAKITERATOR_P(object1);
bio2 = Z_INTL_BREAKITERATOR_P(object2);
if (bio1->biter == NULL || bio2->biter == NULL) {
return bio1->biter == bio2->biter ? 0 : 1;
}
return *bio1->biter == *bio2->biter ? 0 : 1;
}
/* }}} */
/* {{{ clone handler for BreakIterator */
static zend_object *BreakIterator_clone_obj(zend_object *object)
{
BreakIterator_object *bio_orig = php_intl_breakiterator_fetch_object(object);
zend_object *ret_val = BreakIterator_ce_ptr->create_object(object->ce);
BreakIterator_object *bio_new = php_intl_breakiterator_fetch_object(ret_val);
zend_objects_clone_members(&bio_new->zo, &bio_orig->zo);
if (bio_orig->biter != NULL) {
BreakIterator *new_biter = bio_orig->biter->clone();
if (!new_biter) {
zend_throw_error(NULL, "Failed to clone BreakIterator");
} else {
bio_new->biter = new_biter;
ZVAL_COPY(&bio_new->text, &bio_orig->text);
}
} else {
zend_throw_error(NULL, "Cannot clone uninitialized BreakIterator");
}
return ret_val;
}
/* }}} */
/* {{{ get_debug_info handler for BreakIterator */
static HashTable *BreakIterator_get_debug_info(zend_object *object, int *is_temp)
{
zval val;
HashTable *debug_info;
BreakIterator_object *bio;
const BreakIterator *biter;
*is_temp = 1;
debug_info = zend_new_array(8);
bio = php_intl_breakiterator_fetch_object(object);
biter = bio->biter;
if (biter == NULL) {
ZVAL_FALSE(&val);
zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &val);
return debug_info;
}
ZVAL_TRUE(&val);
zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &val);
if (Z_ISUNDEF(bio->text)) {
ZVAL_NULL(&val);
zend_hash_str_update(debug_info, "text", sizeof("text") - 1, &val);
} else {
Z_TRY_ADDREF(bio->text);
zend_hash_str_update(debug_info, "text", sizeof("text") - 1, &bio->text);
}
ZVAL_STRING(&val, const_cast<char*>(typeid(*biter).name()));
zend_hash_str_update(debug_info, "type", sizeof("type") - 1, &val);
return debug_info;
}
/* }}} */
/* {{{ void breakiterator_object_init(BreakIterator_object* to)
* Initialize internals of BreakIterator_object not specific to zend standard objects.
*/
static void breakiterator_object_init(BreakIterator_object *bio)
{
intl_error_init(BREAKITER_ERROR_P(bio));
bio->biter = NULL;
ZVAL_UNDEF(&bio->text);
}
/* }}} */
/* {{{ BreakIterator_objects_free */
static void BreakIterator_objects_free(zend_object *object)
{
BreakIterator_object* bio = php_intl_breakiterator_fetch_object(object);
zval_ptr_dtor(&bio->text);
if (bio->biter) {
delete bio->biter;
bio->biter = NULL;
}
intl_error_reset(BREAKITER_ERROR_P(bio));
zend_object_std_dtor(&bio->zo);
}
/* }}} */
/* {{{ BreakIterator_object_create */
static zend_object *BreakIterator_object_create(zend_class_entry *ce)
{
BreakIterator_object* intern;
intern = (BreakIterator_object*) zend_object_alloc(sizeof(BreakIterator_object), ce);
zend_object_std_init(&intern->zo, ce);
object_properties_init(&intern->zo, ce);
breakiterator_object_init(intern);
return &intern->zo;
}
/* }}} */
/* {{{ breakiterator_register_BreakIterator_class
* Initialize 'BreakIterator' class
*/
U_CFUNC void breakiterator_register_BreakIterator_class(void)
{
/* Create and register 'BreakIterator' class. */
BreakIterator_ce_ptr = register_class_IntlBreakIterator(zend_ce_aggregate);
BreakIterator_ce_ptr->create_object = BreakIterator_object_create;
BreakIterator_ce_ptr->default_object_handlers = &BreakIterator_handlers;
BreakIterator_ce_ptr->get_iterator = _breakiterator_get_iterator;
memcpy(&BreakIterator_handlers, &std_object_handlers,
sizeof BreakIterator_handlers);
BreakIterator_handlers.offset = XtOffsetOf(BreakIterator_object, zo);
BreakIterator_handlers.compare = BreakIterator_compare_objects;
BreakIterator_handlers.clone_obj = BreakIterator_clone_obj;
BreakIterator_handlers.get_debug_info = BreakIterator_get_debug_info;
BreakIterator_handlers.free_obj = BreakIterator_objects_free;
/* Create and register 'RuleBasedBreakIterator' class. */
RuleBasedBreakIterator_ce_ptr = register_class_IntlRuleBasedBreakIterator(BreakIterator_ce_ptr);
/* Create and register 'CodePointBreakIterator' class. */
CodePointBreakIterator_ce_ptr = register_class_IntlCodePointBreakIterator(BreakIterator_ce_ptr);
}
/* }}} */
|