Skip to content

ext/intl: migrate C code to C++ step 3. #19411

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions ext/intl/common/common_date.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ U_CDECL_END

#ifdef __cplusplus

// TODO once C++ migration done we can drop this workaround
#undef U_SHOW_CPLUSPLUS_API
#define U_SHOW_CPLUSPLUS_API 1
#include <unicode/timezone.h>

using icu::TimeZone;
Expand Down
26 changes: 13 additions & 13 deletions ext/intl/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,8 @@ if test "$PHP_INTL" != "no"; then
collator/collator_locale.c
collator/collator_sort.c
common/common_error.c
converter/converter.c
dateformat/dateformat_attr.c
dateformat/dateformat_class.c
dateformat/dateformat_data.c
dateformat/dateformat_format.c
dateformat/dateformat_parse.c
dateformat/dateformat.c
formatter/formatter_attr.c
formatter/formatter_class.c
formatter/formatter_data.c
formatter/formatter_format.c
formatter/formatter_main.c
formatter/formatter_parse.c
grapheme/grapheme_string.c
grapheme/grapheme_util.c
intl_convert.c
Expand All @@ -53,10 +42,16 @@ if test "$PHP_INTL" != "no"; then
PHP_INTL_CXX_SOURCES="intl_convertcpp.cpp \
common/common_enum.cpp \
common/common_date.cpp \
dateformat/dateformat_format_object.cpp \
dateformat/dateformat_create.cpp \
converter/converter.cpp \
dateformat/dateformat.cpp \
dateformat/dateformat_attr.cpp \
dateformat/dateformat_attrcpp.cpp \
dateformat/dateformat_create.cpp \
dateformat/dateformat_data.cpp \
dateformat/dateformat_format.cpp \
dateformat/dateformat_format_object.cpp \
dateformat/dateformat_helpers.cpp \
dateformat/dateformat_parse.cpp \
dateformat/datepatterngenerator_class.cpp \
dateformat/datepatterngenerator_methods.cpp \
msgformat/msgformat_helpers.cpp \
Expand All @@ -65,6 +60,11 @@ if test "$PHP_INTL" != "no"; then
calendar/calendar_class.cpp \
calendar/calendar_methods.cpp \
calendar/gregoriancalendar_methods.cpp \
formatter/formatter_attr.cpp \
formatter/formatter_data.cpp \
formatter/formatter_format.cpp \
formatter/formatter_main.cpp \
formatter/formatter_parse.cpp \
msgformat/msgformat_attr.cpp \
msgformat/msgformat_class.cpp \
msgformat/msgformat_data.cpp \
Expand Down
22 changes: 11 additions & 11 deletions ext/intl/config.w32
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ if (PHP_INTL != "no") {
common_date.cpp \
", "intl");
ADD_SOURCES(configure_module_dirname + "/converter", "\
converter.c \
converter.cpp \
", "intl");
ADD_SOURCES(configure_module_dirname + "/formatter", "\
formatter_attr.c \
formatter_attr.cpp \
formatter_class.c \
formatter_data.c \
formatter_format.c \
formatter_main.c \
formatter_parse.c \
formatter_data.cpp \
formatter_format.cpp \
formatter_main.cpp \
formatter_parse.cpp \
", "intl");
ADD_SOURCES(configure_module_dirname + "/listformatter", "\
listformatter_class.c \
Expand All @@ -64,13 +64,13 @@ if (PHP_INTL != "no") {
normalizer_normalize.cpp \
", "intl");
ADD_SOURCES(configure_module_dirname + "/dateformat", "\
dateformat.c \
dateformat.cpp \
dateformat_class.c \
dateformat_attr.c \
dateformat_format.c \
dateformat_attr.cpp \
dateformat_format.cpp \
dateformat_format_object.cpp \
dateformat_parse.c \
dateformat_data.c \
dateformat_parse.cpp \
dateformat_data.cpp \
dateformat_attrcpp.cpp \
dateformat_helpers.cpp \
dateformat_create.cpp \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
+----------------------------------------------------------------------+
*/

#include "converter.h"
#include "zend_exceptions.h"

#include <unicode/utypes.h>
Expand All @@ -21,10 +20,13 @@
#include <unicode/ucnv.h>
#include <unicode/ustring.h>

extern "C" {
#include "converter.h"
#include "php_intl.h"
#include "../intl_error.h"
#include "../intl_common.h"
}
#include "converter_arginfo.h"
#include "php_intl.h"

typedef struct _php_converter_object {
UConverter *src, *dest;
Expand Down Expand Up @@ -238,9 +240,9 @@ static void php_converter_to_u_callback(const void *context,
}

if (Z_TYPE(zargs[3]) == IS_LONG) {
*pErrorCode = Z_LVAL(zargs[3]);
*pErrorCode = static_cast<UErrorCode>(Z_LVAL(zargs[3]));
} else if (Z_ISREF(zargs[3]) && Z_TYPE_P(Z_REFVAL(zargs[3])) == IS_LONG) {
*pErrorCode = Z_LVAL_P(Z_REFVAL(zargs[3]));
*pErrorCode = static_cast<UErrorCode>(Z_LVAL_P(Z_REFVAL(zargs[3])));
}

zval_ptr_dtor(&zargs[0]);
Expand All @@ -265,7 +267,7 @@ static void php_converter_append_fromUnicode_target(zval *val, UConverterFromUni
{
size_t vallen = Z_STRLEN_P(val);
if (TARGET_CHECK(args, vallen)) {
args->target = zend_mempcpy(args->target, Z_STRVAL_P(val), vallen);
args->target = reinterpret_cast<char *>(zend_mempcpy(args->target, Z_STRVAL_P(val), vallen));
}
return;
}
Expand Down Expand Up @@ -315,9 +317,9 @@ static void php_converter_from_u_callback(const void *context,
}

if (Z_TYPE(zargs[3]) == IS_LONG) {
*pErrorCode = Z_LVAL(zargs[3]);
*pErrorCode = static_cast<UErrorCode>(Z_LVAL(zargs[3]));
} else if (Z_ISREF(zargs[3]) && Z_TYPE_P(Z_REFVAL(zargs[3])) == IS_LONG) {
*pErrorCode = Z_LVAL_P(Z_REFVAL(zargs[3]));
*pErrorCode = static_cast<UErrorCode>(Z_LVAL_P(Z_REFVAL(zargs[3])));
}

zval_ptr_dtor(&zargs[0]);
Expand All @@ -340,15 +342,15 @@ static inline bool php_converter_set_callbacks(php_converter_object *objval, UCo
}

ucnv_setToUCallBack(cnv, (UConverterToUCallback)php_converter_to_u_callback, (const void*)objval,
NULL, NULL, &error);
nullptr, nullptr, &error);
if (U_FAILURE(error)) {
THROW_UFAILURE(objval, error);
ret = 0;
}

error = U_ZERO_ERROR;
ucnv_setFromUCallBack(cnv, (UConverterFromUCallback)php_converter_from_u_callback, (const void*)objval,
NULL, NULL, &error);
nullptr, nullptr, &error);
if (U_FAILURE(error)) {
THROW_UFAILURE(objval, error);
ret = 0;
Expand Down Expand Up @@ -507,14 +509,14 @@ static void php_converter_resolve_callback(
const char *callback_name,
size_t callback_name_len
) {
zend_function *fn = zend_hash_str_find_ptr_lc(&obj->ce->function_table, callback_name, callback_name_len);
ZEND_ASSERT(fn != NULL);
zend_function *fn = reinterpret_cast<zend_function *>(zend_hash_str_find_ptr_lc(&obj->ce->function_table, callback_name, callback_name_len));
ZEND_ASSERT(fn != nullptr);

fcc->function_handler = fn;
fcc->object = obj;
fcc->called_scope = obj->ce;
fcc->calling_scope = NULL;
fcc->closure = NULL;
fcc->calling_scope = nullptr;
fcc->closure = nullptr;
}
/* }}} */

Expand Down Expand Up @@ -635,24 +637,24 @@ static zend_string* php_converter_do_convert(UConverter *dest_cnv,
if (!src_cnv || !dest_cnv) {
php_converter_throw_failure(objval, U_INVALID_STATE_ERROR,
"Internal converters not initialized");
return NULL;
return nullptr;
}

/* Get necessary buffer size first */
temp_len = 1 + ucnv_toUChars(src_cnv, NULL, 0, src, src_len, &error);
if (U_FAILURE(error) && error != U_BUFFER_OVERFLOW_ERROR) {
THROW_UFAILURE(objval, error);
return NULL;
return nullptr;
}
temp = safe_emalloc(sizeof(UChar), temp_len, sizeof(UChar));
temp = reinterpret_cast<UChar *>(safe_emalloc(sizeof(UChar), temp_len, sizeof(UChar)));

/* Convert to intermediate UChar* array */
error = U_ZERO_ERROR;
temp_len = ucnv_toUChars(src_cnv, temp, temp_len, src, src_len, &error);
if (U_FAILURE(error)) {
THROW_UFAILURE(objval, error);
efree(temp);
return NULL;
return nullptr;
}
temp[temp_len] = 0;

Expand All @@ -661,7 +663,7 @@ static zend_string* php_converter_do_convert(UConverter *dest_cnv,
if (U_FAILURE(error) && error != U_BUFFER_OVERFLOW_ERROR) {
THROW_UFAILURE(objval, error);
efree(temp);
return NULL;
return nullptr;
}

ret = zend_string_alloc(ret_len, 0);
Expand All @@ -673,7 +675,7 @@ static zend_string* php_converter_do_convert(UConverter *dest_cnv,
if (U_FAILURE(error)) {
THROW_UFAILURE(objval, error);
zend_string_efree(ret);
return NULL;
return nullptr;
}

return ret;
Expand Down Expand Up @@ -735,8 +737,8 @@ PHP_METHOD(UConverter, convert) {
PHP_METHOD(UConverter, transcode) {
char *str, *src, *dest;
size_t str_len, src_len, dest_len;
zval *options = NULL;
UConverter *src_cnv = NULL, *dest_cnv = NULL;
zval *options = nullptr;
UConverter *src_cnv = nullptr, *dest_cnv = nullptr;

ZEND_PARSE_PARAMETERS_START(3, 4)
Z_PARAM_STRING(str, str_len)
Expand Down Expand Up @@ -911,7 +913,7 @@ static void php_converter_free_object(zend_object *obj) {
static zend_object *php_converter_object_ctor(zend_class_entry *ce, php_converter_object **pobjval) {
php_converter_object *objval;

objval = zend_object_alloc(sizeof(php_converter_object), ce);
objval = reinterpret_cast<php_converter_object *>(zend_object_alloc(sizeof(php_converter_object), ce));

zend_object_std_init(&objval->obj, ce);
object_properties_init(&objval->obj, ce);
Expand All @@ -923,7 +925,7 @@ static zend_object *php_converter_object_ctor(zend_class_entry *ce, php_converte
}

static zend_object *php_converter_create_object(zend_class_entry *ce) {
php_converter_object *objval = NULL;
php_converter_object *objval = nullptr;
zend_object *retval = php_converter_object_ctor(ce, &objval);

object_properties_init(&(objval->obj), ce);
Expand Down Expand Up @@ -968,7 +970,7 @@ static zend_object *php_converter_clone_object(zend_object *object) {
/* }}} */

/* {{{ php_converter_minit */
int php_converter_minit(INIT_FUNC_ARGS) {
U_CFUNC int php_converter_minit(INIT_FUNC_ARGS) {
php_converter_ce = register_class_UConverter();
php_converter_ce->create_object = php_converter_create_object;
php_converter_ce->default_object_handlers = &php_converter_object_handlers;
Expand Down
6 changes: 6 additions & 0 deletions ext/intl/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@

#include "php.h"

#ifdef __cplusplus
extern "C" {
#endif
int php_converter_minit(INIT_FUNC_ARGS);
#ifdef __cplusplus
}
#endif

#endif /* PHP_INTL_CONVERTER_H */
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

#include <unicode/udat.h>

extern "C" {
#include "php_intl.h"
}
#include "dateformat_class.h"
#include "dateformat.h"

/* {{{ Get formatter's last error code. */
PHP_FUNCTION( datefmt_get_error_code )
U_CFUNC PHP_FUNCTION( datefmt_get_error_code )
{
DATE_FORMAT_METHOD_INIT_VARS;

Expand All @@ -41,7 +43,7 @@ PHP_FUNCTION( datefmt_get_error_code )
/* }}} */

/* {{{ Get text description for formatter's last error code. */
PHP_FUNCTION( datefmt_get_error_message )
U_CFUNC PHP_FUNCTION( datefmt_get_error_message )
{
zend_string *message = NULL;
DATE_FORMAT_METHOD_INIT_VARS;
Expand Down
Loading