Skip to content

Commit a096a14

Browse files
Simplify the implementation
1 parent 8524fdc commit a096a14

File tree

9 files changed

+68
-196
lines changed

9 files changed

+68
-196
lines changed

ext/intl/config.m4

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ if test "$PHP_INTL" != "no"; then
4040
locale/locale_methods.c
4141
locale/locale.c
4242
listformatter/listformatter_class.c
43-
listformatter/listformatter_data.c
4443
msgformat/msgformat_attr.c
4544
msgformat/msgformat_class.c
4645
msgformat/msgformat_data.c

ext/intl/config.w32

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ if (PHP_INTL != "no") {
4141
", "intl");
4242
ADD_SOURCES(configure_module_dirname + "/listformatter", "\
4343
listformatter_class.c \
44-
listformatter_data.c \
4544
", "intl");
4645
ADD_SOURCES(configure_module_dirname + "/locale", "\
4746
locale.c \

ext/intl/listformatter/listformatter_class.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ static zend_object_handlers listformatter_handlers;
2525
static void listformatter_free_obj(zend_object *object)
2626
{
2727
ListFormatter_object *obj = php_intl_listformatter_fetch_object(object);
28-
listformatter_data_free(&obj->lf_data);
28+
29+
if( obj->lf_data.ulistfmt )
30+
ulistfmt_close( obj->lf_data.ulistfmt );
31+
32+
obj->lf_data.ulistfmt = NULL;
33+
intl_error_reset( &obj->lf_data.error );
34+
2935
zend_object_std_dtor(&obj->zo);
3036
}
3137
/* }}} */
@@ -35,7 +41,10 @@ static zend_object *listformatter_create_object(zend_class_entry *class_type)
3541
{
3642
ListFormatter_object *obj;
3743
obj = zend_object_alloc(sizeof(ListFormatter_object), class_type);
38-
listformatter_data_init(&obj->lf_data);
44+
45+
obj->lf_data.ulistfmt = NULL;
46+
intl_error_reset( &obj->lf_data.error );
47+
3948
zend_object_std_init(&obj->zo, class_type);
4049
object_properties_init(&obj->zo, class_type);
4150
obj->zo.handlers = &listformatter_handlers;
@@ -62,13 +71,13 @@ PHP_METHOD(IntlListFormatter, __construct)
6271
locale = (char *)intl_locale_get_default();
6372
}
6473

65-
if (strlen(uloc_getISO3Language(locale)) == 0) {
66-
zend_argument_value_error(1, "\"%s\" is invalid", locale);
74+
if (locale_len > INTL_MAX_LOCALE_LEN) {
75+
zend_argument_value_error(1, "Locale string too long, should be no longer than %d characters", INTL_MAX_LOCALE_LEN);
6776
RETURN_THROWS();
6877
}
6978

70-
if (locale_len > INTL_MAX_LOCALE_LEN) {
71-
zend_argument_value_error(1, "Locale string too long, should be no longer than %d characters", INTL_MAX_LOCALE_LEN);
79+
if (strlen(uloc_getISO3Language(locale)) == 0) {
80+
zend_argument_value_error(1, "\"%s\" is invalid", locale);
7281
RETURN_THROWS();
7382
}
7483

@@ -145,6 +154,9 @@ PHP_METHOD(IntlListFormatter, format)
145154

146155
intl_convert_utf8_to_utf16(&ustr, &ustr_len, ZSTR_VAL(str_val), ZSTR_LEN(str_val), &status);
147156
if (U_FAILURE(status)) {
157+
for (uint32_t j = 0; j < i; j++) {
158+
efree((void *)items[j]);
159+
}
148160
efree(items);
149161
efree(itemLengths);
150162
zend_string_release(str_val);
@@ -198,7 +210,7 @@ PHP_METHOD(IntlListFormatter, format)
198210
RETURN_FALSE;
199211
}
200212

201-
RETURN_STR(ret);
213+
RETURN_NEW_STR(ret);
202214
}
203215
/* }}} */
204216

ext/intl/listformatter/listformatter_class.h

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,16 @@
2020
#include "intl_common.h"
2121
#include "intl_error.h"
2222
#include "intl_data.h"
23-
#include "listformatter_data.h"
23+
24+
#include <unicode/ulistformatter.h>
25+
26+
typedef struct {
27+
// error handling
28+
intl_error error;
29+
30+
// formatter handling
31+
UListFormatter* ulistfmt;
32+
} listformatter_data;
2433

2534
typedef struct {
2635
listformatter_data lf_data;
@@ -32,22 +41,10 @@ static inline ListFormatter_object *php_intl_listformatter_fetch_object(zend_obj
3241
}
3342
#define Z_INTL_LISTFORMATTER_P(zv) php_intl_listformatter_fetch_object(Z_OBJ_P(zv))
3443

35-
#define LISTFORMATTER_ERROR(lfo) (lfo)->lf_data.error
36-
#define LISTFORMATTER_ERROR_P(lfo) &(LISTFORMATTER_ERROR(lfo))
37-
38-
#define LISTFORMATTER_ERROR_CODE(lfo) INTL_ERROR_CODE(LISTFORMATTER_ERROR(lfo))
39-
#define LISTFORMATTER_ERROR_CODE_P(lfo) &(INTL_ERROR_CODE(LISTFORMATTER_ERROR(lfo)))
40-
41-
#define LISTFORMATTER_METHOD_INIT_VARS INTL_METHOD_INIT_VARS(ListFormatter, lfo)
42-
#define LISTFORMATTER_OBJECT(lfo) (lfo)->lf_data.ulistfmt
43-
#define LISTFORMATTER_METHOD_FETCH_OBJECT_NO_CHECK INTL_METHOD_FETCH_OBJECT(INTL_LISTFORMATTER, lfo)
44-
#define LISTFORMATTER_METHOD_FETCH_OBJECT \
45-
LISTFORMATTER_METHOD_FETCH_OBJECT_NO_CHECK; \
46-
if (LISTFORMATTER_OBJECT(lfo) == NULL) \
47-
{ \
48-
zend_throw_error(NULL, "Found unconstructed ListFormatter"); \
49-
RETURN_THROWS(); \
50-
}
44+
#define LISTFORMATTER_ERROR(lfo) (lfo)->lf_data.error
45+
#define LISTFORMATTER_ERROR_P(lfo) &(LISTFORMATTER_ERROR(lfo))
46+
47+
#define LISTFORMATTER_OBJECT(lfo) (lfo)->lf_data.ulistfmt
5148

5249
void listformatter_register_class( void );
5350
extern zend_class_entry *ListFormatter_ce_ptr;

ext/intl/listformatter/listformatter_data.c

Lines changed: 0 additions & 48 deletions
This file was deleted.

ext/intl/listformatter/listformatter_data.h

Lines changed: 0 additions & 35 deletions
This file was deleted.

ext/intl/tests/listformatter/listformatter_basic.phpt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,17 @@ intl
88
echo 'EN_US' .PHP_EOL;
99

1010
$formatter = new IntlListFormatter('EN_US', IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_WIDE);
11-
echo $formatter->format([1,2,3]);
12-
echo PHP_EOL;
11+
echo $formatter->format([1,2,3]) . PHP_EOL;
1312
$formatter = new IntlListFormatter('EN_US');
14-
echo $formatter->format([1,2,3]);
13+
echo $formatter->format([1,2,3]) . PHP_EOL;
1514

16-
echo PHP_EOL . 'FR' . PHP_EOL;
15+
echo 'FR' . PHP_EOL;
1716

1817
$formatter = new IntlListFormatter('FR', IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_WIDE);
19-
echo $formatter->format([1,2,3]);
20-
echo PHP_EOL;
18+
echo $formatter->format([1,2,3]) . PHP_EOL;
2119

2220
$formatter = new IntlListFormatter('FR');
23-
echo $formatter->format([1,2,3]);
24-
echo PHP_EOL;
21+
echo $formatter->format([1,2,3]) . PHP_EOL;
2522

2623
--EXPECT--
2724
EN_US

ext/intl/tests/listformatter/listformatter_error.phpt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,15 @@ intl
88
try {
99
$formatter = new IntlListFormatter('f', IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_WIDE);
1010
} catch(ValueError $exception) {
11-
echo $exception->getMessage();
11+
echo $exception->getMessage() . PHP_EOL;
1212
}
1313

14-
echo PHP_EOL;
15-
1614
try {
1715
$formatter = new IntlListFormatter('ro_thisiswaytooooooooooooooooooooooooooooooooooooooooooooolongtobevaliditneedstobeatleast157characterstofailthevalidationinthelistformattercodeimplementation', IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_WIDE);
1816
} catch(ValueError $exception) {
19-
echo $exception->getMessage();
17+
echo $exception->getMessage() . PHP_EOL;
2018
}
2119

22-
echo PHP_EOL;
23-
2420
$formatter = new IntlListFormatter('ro', IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_WIDE);
2521

2622
try {

0 commit comments

Comments
 (0)