Skip to content

Commit 51fb8dc

Browse files
committed
Add specialized pair construction API
Closes GH-3990.
1 parent 67ecc8a commit 51fb8dc

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

Zend/zend_hash.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,26 @@ ZEND_API HashTable* ZEND_FASTCALL _zend_new_array(uint32_t nSize)
258258
return ht;
259259
}
260260

261+
ZEND_API HashTable* ZEND_FASTCALL zend_new_pair(zval *val1, zval *val2)
262+
{
263+
Bucket *p;
264+
HashTable *ht = emalloc(sizeof(HashTable));
265+
_zend_hash_init_int(ht, HT_MIN_SIZE, ZVAL_PTR_DTOR, 0);
266+
ht->nNumUsed = ht->nNumOfElements = ht->nNextFreeElement = 2;
267+
zend_hash_real_init_packed_ex(ht);
268+
269+
p = ht->arData;
270+
ZVAL_COPY_VALUE(&p->val, val1);
271+
p->h = 0;
272+
p->key = NULL;
273+
274+
p++;
275+
ZVAL_COPY_VALUE(&p->val, val2);
276+
p->h = 1;
277+
p->key = NULL;
278+
return ht;
279+
}
280+
261281
static void ZEND_FASTCALL zend_hash_packed_grow(HashTable *ht)
262282
{
263283
HT_ASSERT_RC1(ht);

Zend/zend_hash.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ ZEND_API int ZEND_FASTCALL zend_hash_rehash(HashTable *ht);
297297

298298
ZEND_API HashTable* ZEND_FASTCALL _zend_new_array_0(void);
299299
ZEND_API HashTable* ZEND_FASTCALL _zend_new_array(uint32_t size);
300+
ZEND_API HashTable* ZEND_FASTCALL zend_new_pair(zval *val1, zval *val2);
300301
ZEND_API uint32_t zend_array_count(HashTable *ht);
301302
ZEND_API HashTable* ZEND_FASTCALL zend_array_dup(HashTable *source);
302303
ZEND_API void ZEND_FASTCALL zend_array_destroy(HashTable *ht);

ext/pcre/php_pcre.c

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -931,23 +931,17 @@ PHPAPI void php_pcre_free_match_data(pcre2_match_data *match_data)
931931
}/*}}}*/
932932

933933
static void init_unmatched_null_pair() {
934-
zval tmp;
935-
zval *pair = &PCRE_G(unmatched_null_pair);
936-
array_init_size(pair, 2);
937-
ZVAL_NULL(&tmp);
938-
zend_hash_next_index_insert_new(Z_ARRVAL_P(pair), &tmp);
939-
ZVAL_LONG(&tmp, -1);
940-
zend_hash_next_index_insert_new(Z_ARRVAL_P(pair), &tmp);
934+
zval val1, val2;
935+
ZVAL_NULL(&val1);
936+
ZVAL_LONG(&val2, -1);
937+
ZVAL_ARR(&PCRE_G(unmatched_null_pair), zend_new_pair(&val1, &val2));
941938
}
942939

943940
static void init_unmatched_empty_pair() {
944-
zval tmp;
945-
zval *pair = &PCRE_G(unmatched_empty_pair);
946-
array_init_size(pair, 2);
947-
ZVAL_EMPTY_STRING(&tmp);
948-
zend_hash_next_index_insert_new(Z_ARRVAL_P(pair), &tmp);
949-
ZVAL_LONG(&tmp, -1);
950-
zend_hash_next_index_insert_new(Z_ARRVAL_P(pair), &tmp);
941+
zval val1, val2;
942+
ZVAL_EMPTY_STRING(&val1);
943+
ZVAL_LONG(&val2, -1);
944+
ZVAL_ARR(&PCRE_G(unmatched_empty_pair), zend_new_pair(&val1, &val2));
951945
}
952946

953947
static zend_always_inline void populate_match_value_str(
@@ -980,7 +974,7 @@ static inline void add_offset_pair(
980974
zval *result, const char *subject, PCRE2_SIZE start_offset, PCRE2_SIZE end_offset,
981975
zend_string *name, uint32_t unmatched_as_null)
982976
{
983-
zval match_pair, tmp;
977+
zval match_pair;
984978

985979
/* Add (match, offset) to the return value */
986980
if (PCRE2_UNSET == start_offset) {
@@ -996,11 +990,10 @@ static inline void add_offset_pair(
996990
ZVAL_COPY(&match_pair, &PCRE_G(unmatched_empty_pair));
997991
}
998992
} else {
999-
array_init_size(&match_pair, 2);
1000-
populate_match_value_str(&tmp, subject, start_offset, end_offset);
1001-
zend_hash_next_index_insert_new(Z_ARRVAL(match_pair), &tmp);
1002-
ZVAL_LONG(&tmp, start_offset);
1003-
zend_hash_next_index_insert_new(Z_ARRVAL(match_pair), &tmp);
993+
zval val1, val2;
994+
populate_match_value_str(&val1, subject, start_offset, end_offset);
995+
ZVAL_LONG(&val2, start_offset);
996+
ZVAL_ARR(&match_pair, zend_new_pair(&val1, &val2));
1004997
}
1005998

1006999
if (name) {

0 commit comments

Comments
 (0)