Skip to content

ext/intl: Various refactoring #14360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 6 additions & 21 deletions ext/intl/breakiterator/breakiterator_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,37 +96,22 @@ static int BreakIterator_compare_objects(zval *object1,
/* {{{ clone handler for BreakIterator */
static zend_object *BreakIterator_clone_obj(zend_object *object)
{
BreakIterator_object *bio_orig,
*bio_new;
zend_object *ret_val;

bio_orig = php_intl_breakiterator_fetch_object(object);
intl_errors_reset(INTL_DATA_ERROR_P(bio_orig));

ret_val = BreakIterator_ce_ptr->create_object(object->ce);
bio_new = php_intl_breakiterator_fetch_object(ret_val);
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;

new_biter = bio_orig->biter->clone();
BreakIterator *new_biter = bio_orig->biter->clone();
if (!new_biter) {
zend_string *err_msg;
intl_errors_set_code(BREAKITER_ERROR_P(bio_orig),
U_MEMORY_ALLOCATION_ERROR);
intl_errors_set_custom_msg(BREAKITER_ERROR_P(bio_orig),
"Could not clone BreakIterator", 0);
err_msg = intl_error_get_message(BREAKITER_ERROR_P(bio_orig));
zend_throw_exception(NULL, ZSTR_VAL(err_msg), 0);
zend_string_free(err_msg);
zend_throw_error(NULL, "Failed to clone BreakIterator");
Copy link
Member

@devnexen devnexen May 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are the best parts of the refactoring :)

} else {
bio_new->biter = new_biter;
ZVAL_COPY(&bio_new->text, &bio_orig->text);
}
} else {
zend_throw_exception(NULL, "Cannot clone unconstructed BreakIterator", 0);
zend_throw_error(NULL, "Cannot clone uninitialized BreakIterator");
}

return ret_val;
Expand Down
24 changes: 5 additions & 19 deletions ext/intl/calendar/calendar_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,9 @@ U_CFUNC void calendar_object_construct(zval *object,
/* {{{ clone handler for Calendar */
static zend_object *Calendar_clone_obj(zend_object *object)
{
Calendar_object *co_orig,
*co_new;
zend_object *ret_val;
intl_error_reset(NULL);

co_orig = php_intl_calendar_fetch_object(object);
intl_error_reset(INTL_DATA_ERROR_P(co_orig));

ret_val = Calendar_ce_ptr->create_object(object->ce);
co_new = php_intl_calendar_fetch_object(ret_val);
Calendar_object *co_orig = php_intl_calendar_fetch_object(object);
zend_object *ret_val = Calendar_ce_ptr->create_object(object->ce);
Calendar_object *co_new = php_intl_calendar_fetch_object(ret_val);

zend_objects_clone_members(&co_new->zo, &co_orig->zo);

Expand All @@ -95,19 +88,12 @@ static zend_object *Calendar_clone_obj(zend_object *object)

newCalendar = co_orig->ucal->clone();
if (UNEXPECTED(!newCalendar)) {
zend_string *err_msg;
intl_errors_set_code(CALENDAR_ERROR_P(co_orig),
U_MEMORY_ALLOCATION_ERROR);
intl_errors_set_custom_msg(CALENDAR_ERROR_P(co_orig),
"Could not clone IntlCalendar", 0);
err_msg = intl_error_get_message(CALENDAR_ERROR_P(co_orig));
zend_throw_exception(NULL, ZSTR_VAL(err_msg), 0);
zend_string_free(err_msg);
zend_throw_error(NULL, "Failed to clone IntlCalendar");
} else {
co_new->ucal = newCalendar;
}
} else {
zend_throw_exception(NULL, "Cannot clone unconstructed IntlCalendar", 0);
zend_throw_error(NULL, "Cannot clone uninitialized IntlCalendar");
}

return ret_val;
Expand Down
9 changes: 0 additions & 9 deletions ext/intl/collator/collator_class.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,6 @@ void collator_register_Collator_symbols(int module_number)
Collator_handlers.offset = XtOffsetOf(Collator_object, zo);
Collator_handlers.clone_obj = NULL;
Collator_handlers.free_obj = Collator_objects_free;

/* Declare 'Collator' class properties. */
if( !Collator_ce_ptr )
{
zend_error( E_ERROR,
"Collator: attempt to create properties "
"on a non-registered class." );
return;
}
}
/* }}} */

Expand Down
11 changes: 2 additions & 9 deletions ext/intl/converter/converter.c
Original file line number Diff line number Diff line change
Expand Up @@ -929,8 +929,6 @@ static zend_object *php_converter_clone_object(zend_object *object) {
zend_object *retval = php_converter_object_ctor(object->ce, &objval);
UErrorCode error = U_ZERO_ERROR;

intl_errors_reset(&oldobj->error);

#if U_ICU_VERSION_MAJOR_NUM > 70
objval->src = ucnv_clone(oldobj->src, &error);
#else
Expand All @@ -944,14 +942,9 @@ static zend_object *php_converter_clone_object(zend_object *object) {
objval->dest = ucnv_safeClone(oldobj->dest, NULL, NULL, &error);
#endif
}
if (U_FAILURE(error)) {
zend_string *err_msg;
THROW_UFAILURE(oldobj, "ucnv_safeClone", error);

err_msg = intl_error_get_message(&oldobj->error);
zend_throw_exception(NULL, ZSTR_VAL(err_msg), 0);
zend_string_release_ex(err_msg, 0);

if (U_FAILURE(error)) {
zend_throw_error(NULL, "Failed to clone UConverter");
return retval;
}

Expand Down
26 changes: 11 additions & 15 deletions ext/intl/dateformat/dateformat_class.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,23 @@ zend_object *IntlDateFormatter_object_create(zend_class_entry *ce)
/* {{{ IntlDateFormatter_object_clone */
zend_object *IntlDateFormatter_object_clone(zend_object *object)
{
IntlDateFormatter_object *dfo, *new_dfo;
zend_object *new_obj;
IntlDateFormatter_object *dfo = php_intl_dateformatter_fetch_object(object);
zend_object *new_obj = IntlDateFormatter_ce_ptr->create_object(object->ce);
IntlDateFormatter_object *new_dfo = php_intl_dateformatter_fetch_object(new_obj);

dfo = php_intl_dateformatter_fetch_object(object);
intl_error_reset(INTL_DATA_ERROR_P(dfo));

new_obj = IntlDateFormatter_ce_ptr->create_object(object->ce);
new_dfo = php_intl_dateformatter_fetch_object(new_obj);
/* clone standard parts */
zend_objects_clone_members(&new_dfo->zo, &dfo->zo);

/* clone formatter object */
if (dfo->datef_data.udatf != NULL) {
DATE_FORMAT_OBJECT(new_dfo) = udat_clone(DATE_FORMAT_OBJECT(dfo), &INTL_DATA_ERROR_CODE(dfo));
if (U_FAILURE(INTL_DATA_ERROR_CODE(dfo))) {
/* set up error in case error handler is interested */
intl_errors_set(INTL_DATA_ERROR_P(dfo), INTL_DATA_ERROR_CODE(dfo),
"Failed to clone IntlDateFormatter object", 0 );
zend_throw_exception(NULL, "Failed to clone IntlDateFormatter object", 0);
if (DATE_FORMAT_OBJECT(dfo) != NULL) {
UErrorCode error = U_ZERO_ERROR;
DATE_FORMAT_OBJECT(new_dfo) = udat_clone(DATE_FORMAT_OBJECT(dfo), &error);

if (U_FAILURE(error)) {
zend_throw_error(NULL, "Failed to clone IntlDateFormatter");
}
} else {
zend_throw_exception(NULL, "Cannot clone unconstructed IntlDateFormatter", 0);
zend_throw_error(NULL, "Cannot clone uninitialized IntlDateFormatter");
}
return new_obj;
}
Expand Down
19 changes: 4 additions & 15 deletions ext/intl/dateformat/datepatterngenerator_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,21 @@ zend_object_handlers IntlDatePatternGenerator_handlers;

static zend_object *IntlDatePatternGenerator_object_clone(zend_object *object)
{
intl_error_reset(NULL);

IntlDatePatternGenerator_object *dtpgo_orig = php_intl_datepatterngenerator_fetch_object(object);
intl_error_reset(DTPATTERNGEN_ERROR_P(dtpgo_orig));

zend_object *ret_val = IntlDatePatternGenerator_ce_ptr->create_object(object->ce);
IntlDatePatternGenerator_object *dtpgo_new = php_intl_datepatterngenerator_fetch_object(ret_val);
zend_object *ret_val = IntlDatePatternGenerator_ce_ptr->create_object(object->ce);
IntlDatePatternGenerator_object *dtpgo_new = php_intl_datepatterngenerator_fetch_object(ret_val);

zend_objects_clone_members(&dtpgo_new->zo, &dtpgo_orig->zo);

if (dtpgo_orig->dtpg != NULL) {
DateTimePatternGenerator *newDtpg = dtpgo_orig->dtpg->clone();
if (!newDtpg) {
zend_string *err_msg;
intl_errors_set_code(DTPATTERNGEN_ERROR_P(dtpgo_orig),
U_MEMORY_ALLOCATION_ERROR);
intl_errors_set_custom_msg(DTPATTERNGEN_ERROR_P(dtpgo_orig),
"Could not clone IntlDatePatternGenerator", 0);
err_msg = intl_error_get_message(DTPATTERNGEN_ERROR_P(dtpgo_orig));
zend_throw_exception(NULL, ZSTR_VAL(err_msg), 0);
zend_string_free(err_msg);
zend_throw_error(NULL, "Failed to clone IntlDatePatternGenerator");
} else {
dtpgo_new->dtpg = newDtpg;
}
} else {
zend_throw_exception(NULL, "Cannot clone unconstructed IntlDatePatternGenerator", 0);
zend_throw_error(NULL, "Cannot clone uninitialized IntlDatePatternGenerator");
}

return ret_val;
Expand Down
24 changes: 9 additions & 15 deletions ext/intl/formatter/formatter_class.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,22 @@ zend_object *NumberFormatter_object_create(zend_class_entry *ce)
/* {{{ NumberFormatter_object_clone */
zend_object *NumberFormatter_object_clone(zend_object *object)
{
NumberFormatter_object *nfo, *new_nfo;
zend_object *new_obj;
NumberFormatter_object *nfo = php_intl_number_format_fetch_object(object);
zend_object *new_obj = NumberFormatter_ce_ptr->create_object(object->ce);
NumberFormatter_object *new_nfo = php_intl_number_format_fetch_object(new_obj);

nfo = php_intl_number_format_fetch_object(object);
intl_error_reset(INTL_DATA_ERROR_P(nfo));

new_obj = NumberFormatter_ce_ptr->create_object(object->ce);
new_nfo = php_intl_number_format_fetch_object(new_obj);
/* clone standard parts */
zend_objects_clone_members(&new_nfo->zo, &nfo->zo);

/* clone formatter object. It may fail, the destruction code must handle this case */
if (FORMATTER_OBJECT(nfo) != NULL) {
FORMATTER_OBJECT(new_nfo) = unum_clone(FORMATTER_OBJECT(nfo),
&INTL_DATA_ERROR_CODE(nfo));
if (U_FAILURE(INTL_DATA_ERROR_CODE(nfo))) {
/* set up error in case error handler is interested */
intl_errors_set(INTL_DATA_ERROR_P(nfo), INTL_DATA_ERROR_CODE(nfo),
"Failed to clone NumberFormatter object", 0);
zend_throw_exception(NULL, "Failed to clone NumberFormatter object", 0);
UErrorCode error = U_ZERO_ERROR;
FORMATTER_OBJECT(new_nfo) = unum_clone(FORMATTER_OBJECT(nfo), &error);
if (U_FAILURE(error)) {
zend_throw_error(NULL, "Failed to clone NumberFormatter");
}
} else {
zend_throw_exception(NULL, "Cannot clone unconstructed NumberFormatter", 0);
zend_throw_error(NULL, "Cannot clone uninitialized NumberFormatter");
}
return new_obj;
}
Expand Down
2 changes: 0 additions & 2 deletions ext/intl/grapheme/grapheme_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
#include <unicode/ubrk.h>
#include <unicode/usearch.h>

#include "ext/standard/php_string.h"

ZEND_EXTERN_MODULE_GLOBALS( intl )

/* }}} */
Expand Down
26 changes: 10 additions & 16 deletions ext/intl/idn/idn.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ enum {
};

/* like INTL_CHECK_STATUS, but as a function and varying the name of the func */
static int php_intl_idn_check_status(UErrorCode err, const char *msg)
static zend_result php_intl_idn_check_status(UErrorCode err, const char *msg)
{
intl_error_set_code(NULL, err);
if (U_FAILURE(err)) {
Expand All @@ -53,11 +53,6 @@ static int php_intl_idn_check_status(UErrorCode err, const char *msg)
return SUCCESS;
}

static inline void php_intl_bad_args(const char *msg)
{
php_intl_idn_check_status(U_ILLEGAL_ARGUMENT_ERROR, msg);
}

static void php_intl_idn_to_46(INTERNAL_FUNCTION_PARAMETERS,
const zend_string *domain, uint32_t option, int mode, zval *idna_info)
{
Expand Down Expand Up @@ -128,18 +123,17 @@ static void php_intl_idn_handoff(INTERNAL_FUNCTION_PARAMETERS, int mode)
RETURN_THROWS();
}

if (variant != INTL_IDN_VARIANT_UTS46) {
php_intl_bad_args("invalid variant, must be INTL_IDNA_VARIANT_UTS46");
RETURN_FALSE;
}

if (ZSTR_LEN(domain) < 1) {
php_intl_bad_args("empty domain name");
RETURN_FALSE;
if (ZSTR_LEN(domain) == 0) {
zend_argument_value_error(1, "cannot be empty");
RETURN_THROWS();
}
if (ZSTR_LEN(domain) > INT32_MAX - 1) {
php_intl_bad_args("domain name too large");
RETURN_FALSE;
zend_argument_value_error(1, "must be less than " PRId32 " bytes", INT32_MAX);
RETURN_THROWS();
}
if (variant != INTL_IDN_VARIANT_UTS46) {
zend_argument_value_error(2, "must be INTL_IDNA_VARIANT_UTS46");
RETURN_THROWS();
}
/* don't check options; it wasn't checked before */

Expand Down
22 changes: 8 additions & 14 deletions ext/intl/msgformat/msgformat_class.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,23 @@ zend_object *MessageFormatter_object_create(zend_class_entry *ce)
/* {{{ MessageFormatter_object_clone */
zend_object *MessageFormatter_object_clone(zend_object *object)
{
MessageFormatter_object *mfo, *new_mfo;
zend_object *new_obj;
MessageFormatter_object *mfo = php_intl_messageformatter_fetch_object(object);
zend_object *new_obj = MessageFormatter_ce_ptr->create_object(object->ce);
MessageFormatter_object *new_mfo = php_intl_messageformatter_fetch_object(new_obj);

mfo = php_intl_messageformatter_fetch_object(object);
intl_error_reset(INTL_DATA_ERROR_P(mfo));

new_obj = MessageFormatter_ce_ptr->create_object(object->ce);
new_mfo = php_intl_messageformatter_fetch_object(new_obj);
/* clone standard parts */
zend_objects_clone_members(&new_mfo->zo, &mfo->zo);

/* clone formatter object */
if (MSG_FORMAT_OBJECT(mfo) != NULL) {
MSG_FORMAT_OBJECT(new_mfo) = umsg_clone(MSG_FORMAT_OBJECT(mfo),
&INTL_DATA_ERROR_CODE(mfo));
UErrorCode error = U_ZERO_ERROR;
MSG_FORMAT_OBJECT(new_mfo) = umsg_clone(MSG_FORMAT_OBJECT(mfo), &error);

if (U_FAILURE(INTL_DATA_ERROR_CODE(mfo))) {
intl_errors_set(INTL_DATA_ERROR_P(mfo), INTL_DATA_ERROR_CODE(mfo),
"Failed to clone MessageFormatter object", 0);
zend_throw_exception_ex(NULL, 0, "Failed to clone MessageFormatter object");
if (U_FAILURE(error)) {
zend_throw_error(NULL, "Failed to clone MessageFormatter");
}
} else {
zend_throw_exception_ex(NULL, 0, "Cannot clone unconstructed MessageFormatter");
zend_throw_error(NULL, "Cannot clone uninitialized MessageFormatter");
}
return new_obj;
}
Expand Down
9 changes: 5 additions & 4 deletions ext/intl/resourcebundle/resourcebundle_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,14 @@ static const zend_object_iterator_funcs resourcebundle_iterator_funcs = {
/* {{{ resourcebundle_get_iterator */
zend_object_iterator *resourcebundle_get_iterator( zend_class_entry *ce, zval *object, int byref )
{
ResourceBundle_object *rb = Z_INTL_RESOURCEBUNDLE_P(object );
ResourceBundle_iterator *iterator = emalloc( sizeof( ResourceBundle_iterator ) );

if (byref) {
php_error( E_ERROR, "ResourceBundle does not support writable iterators" );
zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
return NULL;
}

ResourceBundle_object *rb = Z_INTL_RESOURCEBUNDLE_P(object );
ResourceBundle_iterator *iterator = emalloc( sizeof( ResourceBundle_iterator ) );

zend_iterator_init(&iterator->intern);
Z_ADDREF_P(object);
ZVAL_OBJ(&iterator->intern.data, Z_OBJ_P(object));
Expand Down
35 changes: 18 additions & 17 deletions ext/intl/spoofchecker/spoofchecker_class.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,25 @@ zend_object *Spoofchecker_object_create(zend_class_entry *ce)

static zend_object *spoofchecker_clone_obj(zend_object *object) /* {{{ */
{
zend_object *new_obj_val;
Spoofchecker_object *sfo, *new_sfo;

sfo = php_intl_spoofchecker_fetch_object(object);
intl_error_reset(SPOOFCHECKER_ERROR_P(sfo));

new_obj_val = Spoofchecker_ce_ptr->create_object(object->ce);
new_sfo = php_intl_spoofchecker_fetch_object(new_obj_val);
/* clone standard parts */
zend_objects_clone_members(&new_sfo->zo, &sfo->zo);
/* clone internal object */
new_sfo->uspoof = uspoof_clone(sfo->uspoof, SPOOFCHECKER_ERROR_CODE_P(new_sfo));
if(U_FAILURE(SPOOFCHECKER_ERROR_CODE(new_sfo))) {
/* set up error in case error handler is interested */
intl_error_set( NULL, SPOOFCHECKER_ERROR_CODE(new_sfo), "Failed to clone SpoofChecker object", 0 );
Spoofchecker_objects_free(&new_sfo->zo); /* free new object */
zend_error_noreturn(E_ERROR, "Failed to clone SpoofChecker object");
Spoofchecker_object *spoofchecker_orig = php_intl_spoofchecker_fetch_object(object);
zend_object *new_obj_val = Spoofchecker_ce_ptr->create_object(object->ce);
Spoofchecker_object *spoofchecker_new = php_intl_spoofchecker_fetch_object(new_obj_val);

zend_objects_clone_members(&spoofchecker_new->zo, &spoofchecker_orig->zo);

if (spoofchecker_orig->uspoof != NULL) {
/* guaranteed to return NULL if it fails */
UErrorCode error = U_ZERO_ERROR;
spoofchecker_new->uspoof = uspoof_clone(spoofchecker_orig->uspoof, &error);
if (U_FAILURE(error)) {
/* free new object */
Spoofchecker_objects_free(&spoofchecker_new->zo);
zend_throw_error(NULL, "Failed to clone SpoofChecker");
}
} else {
zend_throw_error(NULL, "Cannot clone uninitialized SpoofChecker");
}

return new_obj_val;
}
/* }}} */
Expand Down
Loading
Loading