Skip to content

Commit 063d795

Browse files
authored
ext/intl: using a bit more modern c++ memory management features. (#19163)
not always possible (e.g. PHP objects) but when scoped we can manage here to simplify memory managament starting with IntlDateFormat.
1 parent 419f675 commit 063d795

File tree

1 file changed

+20
-31
lines changed

1 file changed

+20
-31
lines changed

ext/intl/dateformat/dateformat_format_object.cpp

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ U_CFUNC PHP_FUNCTION(datefmt_format_object)
7070
size_t locale_len;
7171
bool pattern = false;
7272
UDate date;
73-
TimeZone *timeZone = NULL;
73+
std::unique_ptr<TimeZone> timeZone;
7474
UErrorCode status = U_ZERO_ERROR;
75-
DateFormat *df = NULL;
76-
Calendar *cal = NULL;
75+
std::unique_ptr<DateFormat> df;
76+
std::unique_ptr<Calendar> cal;
7777
DateFormat::EStyle dateStyle = DateFormat::kDefault,
7878
timeStyle = DateFormat::kDefault;
7979

@@ -158,28 +158,28 @@ U_CFUNC PHP_FUNCTION(datefmt_format_object)
158158
"not initialized properly", 0);
159159
RETURN_FALSE;
160160
}
161-
timeZone = obj_cal->getTimeZone().clone();
161+
timeZone = std::unique_ptr<TimeZone>(obj_cal->getTimeZone().clone());
162162
date = obj_cal->getTime(status);
163163
if (U_FAILURE(status)) {
164164
intl_error_set(NULL, status,
165165
"datefmt_format_object: error obtaining instant from "
166166
"IntlCalendar", 0);
167-
RETVAL_FALSE;
168-
goto cleanup;
167+
RETURN_FALSE;
169168
}
170-
cal = obj_cal->clone();
169+
cal = std::unique_ptr<Calendar>(obj_cal->clone());
171170
} else if (instanceof_function(instance_ce, php_date_get_interface_ce())) {
172-
if (intl_datetime_decompose(object, &date, &timeZone, NULL,
171+
TimeZone *tz;
172+
if (intl_datetime_decompose(object, &date, &tz, NULL,
173173
"datefmt_format_object") == FAILURE) {
174174
RETURN_FALSE;
175175
}
176-
cal = new GregorianCalendar(Locale::createFromName(locale_str), status);
176+
timeZone = std::unique_ptr<TimeZone>(tz);
177+
cal = std::unique_ptr<Calendar>(new GregorianCalendar(Locale::createFromName(locale_str), status));
177178
if (U_FAILURE(status)) {
178179
intl_error_set(NULL, status,
179180
"datefmt_format_object: could not create GregorianCalendar",
180181
0);
181-
RETVAL_FALSE;
182-
goto cleanup;
182+
RETURN_FALSE;
183183
}
184184
} else {
185185
intl_error_set(NULL, status, "datefmt_format_object: the passed object "
@@ -190,36 +190,32 @@ U_CFUNC PHP_FUNCTION(datefmt_format_object)
190190

191191
if (pattern) {
192192
StringPiece sp(Z_STRVAL_P(format));
193-
df = new SimpleDateFormat(
193+
df = std::unique_ptr<DateFormat>(new SimpleDateFormat(
194194
UnicodeString::fromUTF8(sp),
195195
Locale::createFromName(locale_str),
196-
status);
196+
status));
197197

198198
if (U_FAILURE(status)) {
199199
intl_error_set(NULL, status,
200200
"datefmt_format_object: could not create SimpleDateFormat",
201201
0);
202-
RETVAL_FALSE;
203-
goto cleanup;
202+
RETURN_FALSE;
204203
}
205204
} else {
206-
df = DateFormat::createDateTimeInstance(dateStyle, timeStyle,
207-
Locale::createFromName(locale_str));
205+
df = std::unique_ptr<DateFormat>(DateFormat::createDateTimeInstance(dateStyle, timeStyle,
206+
Locale::createFromName(locale_str)));
208207

209208
if (df == NULL) { /* according to ICU sources, this should never happen */
210209
intl_error_set(NULL, status,
211210
"datefmt_format_object: could not create DateFormat",
212211
0);
213-
RETVAL_FALSE;
214-
goto cleanup;
212+
RETURN_FALSE;
215213
}
216214
}
217215

218216
//must be in this order (or have the cal adopt the tz)
219-
df->adoptCalendar(cal);
220-
cal = NULL;
221-
df->adoptTimeZone(timeZone);
222-
timeZone = NULL;
217+
df->adoptCalendar(cal.release());
218+
df->adoptTimeZone(timeZone.release());
223219

224220
{
225221
zend_string *u8str;
@@ -231,15 +227,8 @@ U_CFUNC PHP_FUNCTION(datefmt_format_object)
231227
intl_error_set(NULL, status,
232228
"datefmt_format_object: error converting result to UTF-8",
233229
0);
234-
RETVAL_FALSE;
235-
goto cleanup;
230+
RETURN_FALSE;
236231
}
237232
RETVAL_STR(u8str);
238233
}
239-
240-
241-
cleanup:
242-
delete df;
243-
delete timeZone;
244-
delete cal;
245234
}

0 commit comments

Comments
 (0)