Skip to content

Commit 464dd1f

Browse files
committed
simplify locale_filter_matches removing manual memory management when applicable.
1 parent 9b5420f commit 464dd1f

1 file changed

Lines changed: 25 additions & 51 deletions

File tree

ext/intl/locale/locale_methods.cpp

Lines changed: 25 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,8 +1354,8 @@ PHP_INTL_FUNCTION_WITH_ERROR_RESET(locale_filter_matches)
13541354
zend_string* can_lang_tag = nullptr;
13551355
zend_string* can_loc_range = nullptr;
13561356

1357-
char* cur_lang_tag = nullptr;
1358-
char* cur_loc_range = nullptr;
1357+
std::unique_ptr<char, char_deleter> cur_lang_tag;
1358+
std::unique_ptr<char, char_deleter> cur_loc_range;
13591359

13601360
bool boolCanonical = 0;
13611361
UErrorCode status = U_ZERO_ERROR;
@@ -1396,98 +1396,72 @@ PHP_INTL_FUNCTION_WITH_ERROR_RESET(locale_filter_matches)
13961396
}
13971397

13981398
/* Convert to lower case for case-insensitive comparison */
1399-
cur_lang_tag = reinterpret_cast<char *>(ecalloc( 1, can_lang_tag->len + 1));
1399+
cur_lang_tag = std::unique_ptr<char, char_deleter>(reinterpret_cast<char *>(ecalloc( 1, can_lang_tag->len + 1)));
1400+
char *p_cur_lang_tag = cur_lang_tag.get();
14001401

14011402
/* Convert to lower case for case-insensitive comparison */
1402-
result = strToMatch( can_lang_tag->val , cur_lang_tag);
1403+
result = strToMatch( can_lang_tag->val , p_cur_lang_tag);
14031404
if( result == 0) {
1404-
efree( cur_lang_tag );
1405-
zend_string_release_ex( can_lang_tag, 0 );
1405+
zend_string_release_ex( can_lang_tag, false );
14061406
RETURN_FALSE;
14071407
}
14081408

1409-
cur_loc_range = reinterpret_cast<char *>(ecalloc( 1, can_loc_range->len + 1));
1410-
result = strToMatch( can_loc_range->val , cur_loc_range );
1409+
cur_loc_range = std::unique_ptr<char, char_deleter>(reinterpret_cast<char *>(ecalloc( 1, can_loc_range->len + 1)));
1410+
char *p_cur_loc_range = cur_loc_range.get();
1411+
result = strToMatch( can_loc_range->val , p_cur_loc_range );
14111412
if( result == 0) {
1412-
efree( cur_lang_tag );
1413-
zend_string_release_ex( can_lang_tag, 0 );
1414-
efree( cur_loc_range );
1415-
zend_string_release_ex( can_loc_range, 0 );
1413+
zend_string_release_ex( can_lang_tag, false );
1414+
zend_string_release_ex( can_loc_range, false );
14161415
RETURN_FALSE;
14171416
}
14181417

14191418
/* check if prefix */
1420-
token = strstr( cur_lang_tag , cur_loc_range );
1419+
token = strstr( p_cur_lang_tag , p_cur_loc_range );
14211420

1422-
if( token && (token==cur_lang_tag) ){
1421+
if( token && (token==p_cur_lang_tag) ){
14231422
/* check if the char. after match is SEPARATOR */
14241423
chrcheck = token + can_loc_range->len;
14251424
if( isIDSeparator(*chrcheck) || isKeywordSeparator(*chrcheck) || isEndOfTag(*chrcheck) ){
1426-
efree( cur_lang_tag );
1427-
efree( cur_loc_range );
1428-
if( can_lang_tag){
1429-
zend_string_release_ex( can_lang_tag, 0 );
1430-
}
1431-
if( can_loc_range){
1432-
zend_string_release_ex( can_loc_range, 0 );
1433-
}
1425+
zend_string_release_ex( can_lang_tag, false );
1426+
zend_string_release_ex( can_loc_range, false );
14341427
RETURN_TRUE;
14351428
}
14361429
}
14371430

14381431
/* No prefix as loc_range */
1439-
if( cur_lang_tag){
1440-
efree( cur_lang_tag );
1441-
}
1442-
if( cur_loc_range){
1443-
efree( cur_loc_range );
1444-
}
1445-
if( can_lang_tag){
1446-
zend_string_release_ex( can_lang_tag, 0 );
1447-
}
1448-
if( can_loc_range){
1449-
zend_string_release_ex( can_loc_range, 0 );
1450-
}
1432+
zend_string_release_ex( can_lang_tag, false );
1433+
zend_string_release_ex( can_loc_range, false );
14511434
RETURN_FALSE;
14521435

14531436
} /* end of if isCanonical */
14541437
else{
14551438
/* Convert to lower case for case-insensitive comparison */
1456-
cur_lang_tag = reinterpret_cast<char *>(ecalloc(1, lang_tag_len + 1));
1439+
cur_lang_tag = std::unique_ptr<char, char_deleter>(reinterpret_cast<char *>(ecalloc(1, lang_tag_len + 1)));
1440+
char *p_cur_lang_tag = cur_lang_tag.get();
14571441

1458-
result = strToMatch( lang_tag , cur_lang_tag);
1442+
result = strToMatch( lang_tag , p_cur_lang_tag);
14591443
if( result == 0) {
1460-
efree( cur_lang_tag );
14611444
RETURN_FALSE;
14621445
}
1463-
cur_loc_range = reinterpret_cast<char *>(ecalloc(1, loc_range_len + 1));
1464-
result = strToMatch( loc_range , cur_loc_range );
1446+
cur_loc_range = std::unique_ptr<char, char_deleter>(reinterpret_cast<char *>(ecalloc(1, loc_range_len + 1)));
1447+
char *p_cur_loc_range = cur_loc_range.get();
1448+
result = strToMatch( loc_range , p_cur_loc_range );
14651449
if( result == 0) {
1466-
efree( cur_lang_tag );
1467-
efree( cur_loc_range );
14681450
RETURN_FALSE;
14691451
}
14701452

14711453
/* check if prefix */
1472-
token = strstr( cur_lang_tag , cur_loc_range );
1454+
token = strstr( p_cur_lang_tag , p_cur_loc_range );
14731455

1474-
if( token && (token==cur_lang_tag) ){
1456+
if( token && (token==p_cur_lang_tag) ){
14751457
/* check if the char. after match is SEPARATOR */
14761458
chrcheck = token + loc_range_len;
14771459
if( isIDSeparator(*chrcheck) || isEndOfTag(*chrcheck) ){
1478-
efree( cur_lang_tag );
1479-
efree( cur_loc_range );
14801460
RETURN_TRUE;
14811461
}
14821462
}
14831463

14841464
/* No prefix as loc_range */
1485-
if( cur_lang_tag){
1486-
efree( cur_lang_tag );
1487-
}
1488-
if( cur_loc_range){
1489-
efree( cur_loc_range );
1490-
}
14911465
RETURN_FALSE;
14921466

14931467
}

0 commit comments

Comments
 (0)