Skip to content

Commit 1ce1fe9

Browse files
committed
ext/intl: migrate C code to C++ step 3.
1 parent fe88711 commit 1ce1fe9

19 files changed

+183
-104
lines changed

ext/intl/common/common_date.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ U_CDECL_END
2424

2525
#ifdef __cplusplus
2626

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

2932
using icu::TimeZone;

ext/intl/config.m4

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,8 @@ if test "$PHP_INTL" != "no"; then
1818
collator/collator_locale.c
1919
collator/collator_sort.c
2020
common/common_error.c
21-
converter/converter.c
22-
dateformat/dateformat_attr.c
2321
dateformat/dateformat_class.c
24-
dateformat/dateformat_data.c
25-
dateformat/dateformat_format.c
26-
dateformat/dateformat_parse.c
27-
dateformat/dateformat.c
28-
formatter/formatter_attr.c
2922
formatter/formatter_class.c
30-
formatter/formatter_data.c
31-
formatter/formatter_format.c
32-
formatter/formatter_main.c
33-
formatter/formatter_parse.c
3423
grapheme/grapheme_string.c
3524
grapheme/grapheme_util.c
3625
intl_convert.c
@@ -53,10 +42,16 @@ if test "$PHP_INTL" != "no"; then
5342
PHP_INTL_CXX_SOURCES="intl_convertcpp.cpp \
5443
common/common_enum.cpp \
5544
common/common_date.cpp \
56-
dateformat/dateformat_format_object.cpp \
57-
dateformat/dateformat_create.cpp \
45+
converter/converter.cpp \
46+
dateformat/dateformat.cpp \
47+
dateformat/dateformat_attr.cpp \
5848
dateformat/dateformat_attrcpp.cpp \
49+
dateformat/dateformat_create.cpp \
50+
dateformat/dateformat_data.cpp \
51+
dateformat/dateformat_format.cpp \
52+
dateformat/dateformat_format_object.cpp \
5953
dateformat/dateformat_helpers.cpp \
54+
dateformat/dateformat_parse.cpp \
6055
dateformat/datepatterngenerator_class.cpp \
6156
dateformat/datepatterngenerator_methods.cpp \
6257
msgformat/msgformat_helpers.cpp \
@@ -65,6 +60,11 @@ if test "$PHP_INTL" != "no"; then
6560
calendar/calendar_class.cpp \
6661
calendar/calendar_methods.cpp \
6762
calendar/gregoriancalendar_methods.cpp \
63+
formatter/formatter_attr.cpp \
64+
formatter/formatter_data.cpp \
65+
formatter/formatter_format.cpp \
66+
formatter/formatter_main.cpp \
67+
formatter/formatter_parse.cpp \
6868
msgformat/msgformat_attr.cpp \
6969
msgformat/msgformat_class.cpp \
7070
msgformat/msgformat_data.cpp \

ext/intl/config.w32

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ if (PHP_INTL != "no") {
2929
common_date.cpp \
3030
", "intl");
3131
ADD_SOURCES(configure_module_dirname + "/converter", "\
32-
converter.c \
32+
converter.cpp \
3333
", "intl");
3434
ADD_SOURCES(configure_module_dirname + "/formatter", "\
35-
formatter_attr.c \
35+
formatter_attr.cpp \
3636
formatter_class.c \
37-
formatter_data.c \
38-
formatter_format.c \
39-
formatter_main.c \
40-
formatter_parse.c \
37+
formatter_data.cpp \
38+
formatter_format.cpp \
39+
formatter_main.cpp \
40+
formatter_parse.cpp \
4141
", "intl");
4242
ADD_SOURCES(configure_module_dirname + "/listformatter", "\
4343
listformatter_class.c \
@@ -64,13 +64,13 @@ if (PHP_INTL != "no") {
6464
normalizer_normalize.cpp \
6565
", "intl");
6666
ADD_SOURCES(configure_module_dirname + "/dateformat", "\
67-
dateformat.c \
67+
dateformat.cpp \
6868
dateformat_class.c \
69-
dateformat_attr.c \
70-
dateformat_format.c \
69+
dateformat_attr.cpp \
70+
dateformat_format.cpp \
7171
dateformat_format_object.cpp \
72-
dateformat_parse.c \
73-
dateformat_data.c \
72+
dateformat_parse.cpp \
73+
dateformat_data.cpp \
7474
dateformat_attrcpp.cpp \
7575
dateformat_helpers.cpp \
7676
dateformat_create.cpp \

ext/intl/converter/converter.c renamed to ext/intl/converter/converter.cpp

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
#include <unicode/ucnv.h>
2222
#include <unicode/ustring.h>
2323

24+
extern "C" {
25+
#include "php_intl.h"
2426
#include "../intl_error.h"
2527
#include "../intl_common.h"
28+
}
2629
#include "converter_arginfo.h"
27-
#include "php_intl.h"
2830

2931
typedef struct _php_converter_object {
3032
UConverter *src, *dest;
@@ -238,9 +240,9 @@ static void php_converter_to_u_callback(const void *context,
238240
}
239241

240242
if (Z_TYPE(zargs[3]) == IS_LONG) {
241-
*pErrorCode = Z_LVAL(zargs[3]);
243+
*pErrorCode = static_cast<UErrorCode>(Z_LVAL(zargs[3]));
242244
} else if (Z_ISREF(zargs[3]) && Z_TYPE_P(Z_REFVAL(zargs[3])) == IS_LONG) {
243-
*pErrorCode = Z_LVAL_P(Z_REFVAL(zargs[3]));
245+
*pErrorCode = static_cast<UErrorCode>(Z_LVAL_P(Z_REFVAL(zargs[3])));
244246
}
245247

246248
zval_ptr_dtor(&zargs[0]);
@@ -265,7 +267,7 @@ static void php_converter_append_fromUnicode_target(zval *val, UConverterFromUni
265267
{
266268
size_t vallen = Z_STRLEN_P(val);
267269
if (TARGET_CHECK(args, vallen)) {
268-
args->target = zend_mempcpy(args->target, Z_STRVAL_P(val), vallen);
270+
args->target = reinterpret_cast<char *>(zend_mempcpy(args->target, Z_STRVAL_P(val), vallen));
269271
}
270272
return;
271273
}
@@ -315,9 +317,9 @@ static void php_converter_from_u_callback(const void *context,
315317
}
316318

317319
if (Z_TYPE(zargs[3]) == IS_LONG) {
318-
*pErrorCode = Z_LVAL(zargs[3]);
320+
*pErrorCode = static_cast<UErrorCode>(Z_LVAL(zargs[3]));
319321
} else if (Z_ISREF(zargs[3]) && Z_TYPE_P(Z_REFVAL(zargs[3])) == IS_LONG) {
320-
*pErrorCode = Z_LVAL_P(Z_REFVAL(zargs[3]));
322+
*pErrorCode = static_cast<UErrorCode>(Z_LVAL_P(Z_REFVAL(zargs[3])));
321323
}
322324

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

342344
ucnv_setToUCallBack(cnv, (UConverterToUCallback)php_converter_to_u_callback, (const void*)objval,
343-
NULL, NULL, &error);
345+
nullptr, nullptr, &error);
344346
if (U_FAILURE(error)) {
345347
THROW_UFAILURE(objval, error);
346348
ret = 0;
347349
}
348350

349351
error = U_ZERO_ERROR;
350352
ucnv_setFromUCallBack(cnv, (UConverterFromUCallback)php_converter_from_u_callback, (const void*)objval,
351-
NULL, NULL, &error);
353+
nullptr, nullptr, &error);
352354
if (U_FAILURE(error)) {
353355
THROW_UFAILURE(objval, error);
354356
ret = 0;
@@ -507,14 +509,14 @@ static void php_converter_resolve_callback(
507509
const char *callback_name,
508510
size_t callback_name_len
509511
) {
510-
zend_function *fn = zend_hash_str_find_ptr_lc(&obj->ce->function_table, callback_name, callback_name_len);
511-
ZEND_ASSERT(fn != NULL);
512+
zend_function *fn = reinterpret_cast<zend_function *>(zend_hash_str_find_ptr_lc(&obj->ce->function_table, callback_name, callback_name_len));
513+
ZEND_ASSERT(fn != nullptr);
512514

513515
fcc->function_handler = fn;
514516
fcc->object = obj;
515517
fcc->called_scope = obj->ce;
516-
fcc->calling_scope = NULL;
517-
fcc->closure = NULL;
518+
fcc->calling_scope = nullptr;
519+
fcc->closure = nullptr;
518520
}
519521
/* }}} */
520522

@@ -635,24 +637,24 @@ static zend_string* php_converter_do_convert(UConverter *dest_cnv,
635637
if (!src_cnv || !dest_cnv) {
636638
php_converter_throw_failure(objval, U_INVALID_STATE_ERROR,
637639
"Internal converters not initialized");
638-
return NULL;
640+
return nullptr;
639641
}
640642

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

649651
/* Convert to intermediate UChar* array */
650652
error = U_ZERO_ERROR;
651653
temp_len = ucnv_toUChars(src_cnv, temp, temp_len, src, src_len, &error);
652654
if (U_FAILURE(error)) {
653655
THROW_UFAILURE(objval, error);
654656
efree(temp);
655-
return NULL;
657+
return nullptr;
656658
}
657659
temp[temp_len] = 0;
658660

@@ -661,7 +663,7 @@ static zend_string* php_converter_do_convert(UConverter *dest_cnv,
661663
if (U_FAILURE(error) && error != U_BUFFER_OVERFLOW_ERROR) {
662664
THROW_UFAILURE(objval, error);
663665
efree(temp);
664-
return NULL;
666+
return nullptr;
665667
}
666668

667669
ret = zend_string_alloc(ret_len, 0);
@@ -673,7 +675,7 @@ static zend_string* php_converter_do_convert(UConverter *dest_cnv,
673675
if (U_FAILURE(error)) {
674676
THROW_UFAILURE(objval, error);
675677
zend_string_efree(ret);
676-
return NULL;
678+
return nullptr;
677679
}
678680

679681
return ret;
@@ -735,8 +737,8 @@ PHP_METHOD(UConverter, convert) {
735737
PHP_METHOD(UConverter, transcode) {
736738
char *str, *src, *dest;
737739
size_t str_len, src_len, dest_len;
738-
zval *options = NULL;
739-
UConverter *src_cnv = NULL, *dest_cnv = NULL;
740+
zval *options = nullptr;
741+
UConverter *src_cnv = nullptr, *dest_cnv = nullptr;
740742

741743
ZEND_PARSE_PARAMETERS_START(3, 4)
742744
Z_PARAM_STRING(str, str_len)
@@ -911,7 +913,7 @@ static void php_converter_free_object(zend_object *obj) {
911913
static zend_object *php_converter_object_ctor(zend_class_entry *ce, php_converter_object **pobjval) {
912914
php_converter_object *objval;
913915

914-
objval = zend_object_alloc(sizeof(php_converter_object), ce);
916+
objval = reinterpret_cast<php_converter_object *>(zend_object_alloc(sizeof(php_converter_object), ce));
915917

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

925927
static zend_object *php_converter_create_object(zend_class_entry *ce) {
926-
php_converter_object *objval = NULL;
928+
php_converter_object *objval = nullptr;
927929
zend_object *retval = php_converter_object_ctor(ce, &objval);
928930

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

970972
/* {{{ php_converter_minit */
971-
int php_converter_minit(INIT_FUNC_ARGS) {
973+
U_CFUNC int php_converter_minit(INIT_FUNC_ARGS) {
972974
php_converter_ce = register_class_UConverter();
973975
php_converter_ce->create_object = php_converter_create_object;
974976
php_converter_ce->default_object_handlers = &php_converter_object_handlers;

ext/intl/converter/converter.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121

2222
#include "php.h"
2323

24+
#ifdef __cplusplus
25+
extern "C" {
26+
#endif
2427
int php_converter_minit(INIT_FUNC_ARGS);
28+
#ifdef __cplusplus
29+
}
30+
#endif
2531

2632
#endif /* PHP_INTL_CONVERTER_H */

ext/intl/dateformat/dateformat.c renamed to ext/intl/dateformat/dateformat.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717

1818
#include <unicode/udat.h>
1919

20+
extern "C" {
2021
#include "php_intl.h"
22+
}
2123
#include "dateformat_class.h"
2224
#include "dateformat.h"
2325

2426
/* {{{ Get formatter's last error code. */
25-
PHP_FUNCTION( datefmt_get_error_code )
27+
U_CFUNC PHP_FUNCTION( datefmt_get_error_code )
2628
{
2729
DATE_FORMAT_METHOD_INIT_VARS;
2830

@@ -41,7 +43,7 @@ PHP_FUNCTION( datefmt_get_error_code )
4143
/* }}} */
4244

4345
/* {{{ Get text description for formatter's last error code. */
44-
PHP_FUNCTION( datefmt_get_error_message )
46+
U_CFUNC PHP_FUNCTION( datefmt_get_error_message )
4547
{
4648
zend_string *message = NULL;
4749
DATE_FORMAT_METHOD_INIT_VARS;

ext/intl/dateformat/dateformat_attr.c renamed to ext/intl/dateformat/dateformat_attr.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@
1515
#include <config.h>
1616
#endif
1717

18+
extern "C" {
1819
#include "../php_intl.h"
19-
#include "dateformat_class.h"
2020
#include "../intl_convert.h"
21+
}
22+
#include "dateformat_class.h"
2123
#include "dateformat_class.h"
2224

2325
#include <unicode/ustring.h>
2426
#include <unicode/udat.h>
2527

2628
/* {{{ Get formatter datetype. */
27-
PHP_FUNCTION( datefmt_get_datetype )
29+
U_CFUNC PHP_FUNCTION( datefmt_get_datetype )
2830
{
2931
DATE_FORMAT_METHOD_INIT_VARS;
3032

@@ -44,7 +46,7 @@ PHP_FUNCTION( datefmt_get_datetype )
4446
/* }}} */
4547

4648
/* {{{ Get formatter timetype. */
47-
PHP_FUNCTION( datefmt_get_timetype )
49+
U_CFUNC PHP_FUNCTION( datefmt_get_timetype )
4850
{
4951
DATE_FORMAT_METHOD_INIT_VARS;
5052

@@ -64,7 +66,7 @@ PHP_FUNCTION( datefmt_get_timetype )
6466
/* }}} */
6567

6668
/* {{{ Get formatter pattern. */
67-
PHP_FUNCTION( datefmt_get_pattern )
69+
U_CFUNC PHP_FUNCTION( datefmt_get_pattern )
6870
{
6971
UChar value_buf[64];
7072
uint32_t length = USIZE( value_buf );
@@ -100,12 +102,12 @@ PHP_FUNCTION( datefmt_get_pattern )
100102
/* }}} */
101103

102104
/* {{{ Set formatter pattern. */
103-
PHP_FUNCTION( datefmt_set_pattern )
105+
U_CFUNC PHP_FUNCTION( datefmt_set_pattern )
104106
{
105-
char* value = NULL;
107+
char* value = nullptr;
106108
size_t value_len = 0;
107109
int32_t slength = 0;
108-
UChar* svalue = NULL;
110+
UChar* svalue = nullptr;
109111
bool is_pattern_localized = false;
110112

111113

@@ -136,7 +138,7 @@ PHP_FUNCTION( datefmt_set_pattern )
136138
/* }}} */
137139

138140
/* {{{ Get formatter locale. */
139-
PHP_FUNCTION( datefmt_get_locale )
141+
U_CFUNC PHP_FUNCTION( datefmt_get_locale )
140142
{
141143
char *loc;
142144
zend_long loc_type =ULOC_ACTUAL_LOCALE;
@@ -154,14 +156,14 @@ PHP_FUNCTION( datefmt_get_locale )
154156
/* Fetch the object. */
155157
DATE_FORMAT_METHOD_FETCH_OBJECT;
156158

157-
loc = (char *)udat_getLocaleByType(DATE_FORMAT_OBJECT(dfo), loc_type,&INTL_DATA_ERROR_CODE(dfo));
159+
loc = (char *)udat_getLocaleByType(DATE_FORMAT_OBJECT(dfo), static_cast<ULocDataLocaleType>(loc_type),&INTL_DATA_ERROR_CODE(dfo));
158160
INTL_METHOD_CHECK_STATUS(dfo, "Error getting locale");
159161
RETURN_STRING(loc);
160162
}
161163
/* }}} */
162164

163165
/* {{{ Get formatter isLenient. */
164-
PHP_FUNCTION( datefmt_is_lenient )
166+
U_CFUNC PHP_FUNCTION( datefmt_is_lenient )
165167
{
166168

167169
DATE_FORMAT_METHOD_INIT_VARS;
@@ -182,7 +184,7 @@ PHP_FUNCTION( datefmt_is_lenient )
182184
/* }}} */
183185

184186
/* {{{ Set formatter lenient. */
185-
PHP_FUNCTION( datefmt_set_lenient )
187+
U_CFUNC PHP_FUNCTION( datefmt_set_lenient )
186188
{
187189
bool isLenient = false;
188190

0 commit comments

Comments
 (0)