Skip to content

Commit e90152b

Browse files
committed
Refactor std string extension
Mainly using more appropriate types, early returns, and moving the happy path to the primary scope
1 parent 27be6c3 commit e90152b

File tree

3 files changed

+63
-70
lines changed

3 files changed

+63
-70
lines changed

ext/standard/php_string.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ PHP_MINIT_FUNCTION(string_intrin);
3333
strnatcmp_ex(a, strlen(a), b, strlen(b), 0)
3434
#define strnatcasecmp(a, b) \
3535
strnatcmp_ex(a, strlen(a), b, strlen(b), 1)
36-
PHPAPI int strnatcmp_ex(char const *a, size_t a_len, char const *b, size_t b_len, int fold_case);
36+
PHPAPI int strnatcmp_ex(char const *a, size_t a_len, char const *b, size_t b_len, bool is_case_insensitive);
3737
PHPAPI struct lconv *localeconv_r(struct lconv *out);
3838
PHPAPI char *php_strtoupper(char *s, size_t len);
3939
PHPAPI char *php_strtolower(char *s, size_t len);

ext/standard/string.c

Lines changed: 60 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ void register_string_constants(INIT_FUNC_ARGS)
8989
}
9090
/* }}} */
9191

92-
int php_tag_find(char *tag, size_t len, const char *set);
93-
9492
/* this is read-only, so it's ok */
9593
ZEND_SET_ALIGNED(16, static const char hexconvtab[]) = "0123456789abcdef";
9694

@@ -695,11 +693,11 @@ PHP_FUNCTION(strcoll)
695693
* it needs to be incrementing.
696694
* Returns: FAILURE/SUCCESS whether the input was correct (i.e. no range errors)
697695
*/
698-
static inline int php_charmask(const unsigned char *input, size_t len, char *mask)
696+
static inline zend_result php_charmask(const unsigned char *input, size_t len, char *mask)
699697
{
700698
const unsigned char *end;
701699
unsigned char c;
702-
int result = SUCCESS;
700+
zend_result result = SUCCESS;
703701

704702
memset(mask, 0, 256);
705703
for (end = input+len; input < end; input++) {
@@ -1144,7 +1142,7 @@ PHP_FUNCTION(explode)
11441142
PHPAPI void php_implode(const zend_string *glue, HashTable *pieces, zval *return_value)
11451143
{
11461144
zval *tmp;
1147-
int numelems;
1145+
uint32_t numelems;
11481146
zend_string *str;
11491147
char *cptr;
11501148
size_t len = 0;
@@ -1606,7 +1604,7 @@ PHP_FUNCTION(pathinfo)
16061604
zval tmp;
16071605
char *path, *dirname;
16081606
size_t path_len;
1609-
int have_basename;
1607+
bool have_basename;
16101608
zend_long opt = PHP_PATHINFO_ALL;
16111609
zend_string *ret = NULL;
16121610

@@ -1744,16 +1742,14 @@ PHP_FUNCTION(stristr)
17441742

17451743
found = php_stristr(ZSTR_VAL(haystack), ZSTR_VAL(needle), ZSTR_LEN(haystack), ZSTR_LEN(needle));
17461744

1747-
if (found) {
1748-
found_offset = found - ZSTR_VAL(haystack);
1749-
if (part) {
1750-
RETVAL_STRINGL(ZSTR_VAL(haystack), found_offset);
1751-
} else {
1752-
RETVAL_STRINGL(ZSTR_VAL(haystack) + found_offset, ZSTR_LEN(haystack) - found_offset);
1753-
}
1754-
} else {
1755-
RETVAL_FALSE;
1745+
if (!found) {
1746+
RETURN_FALSE;
1747+
}
1748+
found_offset = found - ZSTR_VAL(haystack);
1749+
if (part) {
1750+
RETURN_STRINGL(ZSTR_VAL(haystack), found_offset);
17561751
}
1752+
RETURN_STRINGL(ZSTR_VAL(haystack) + found_offset, ZSTR_LEN(haystack) - found_offset);
17571753
}
17581754
/* }}} */
17591755

@@ -1774,15 +1770,14 @@ PHP_FUNCTION(strstr)
17741770

17751771
found = php_memnstr(ZSTR_VAL(haystack), ZSTR_VAL(needle), ZSTR_LEN(needle), ZSTR_VAL(haystack) + ZSTR_LEN(haystack));
17761772

1777-
if (found) {
1778-
found_offset = found - ZSTR_VAL(haystack);
1779-
if (part) {
1780-
RETURN_STRINGL(ZSTR_VAL(haystack), found_offset);
1781-
} else {
1782-
RETURN_STRINGL(found, ZSTR_LEN(haystack) - found_offset);
1783-
}
1773+
if (!found) {
1774+
RETURN_FALSE;
17841775
}
1785-
RETURN_FALSE;
1776+
found_offset = found - ZSTR_VAL(haystack);
1777+
if (part) {
1778+
RETURN_STRINGL(ZSTR_VAL(haystack), found_offset);
1779+
}
1780+
RETURN_STRINGL(ZSTR_VAL(haystack) + found_offset, ZSTR_LEN(haystack) - found_offset);
17861781
}
17871782
/* }}} */
17881783

@@ -1867,11 +1862,10 @@ PHP_FUNCTION(strpos)
18671862
ZSTR_VAL(needle), ZSTR_LEN(needle),
18681863
ZSTR_VAL(haystack) + ZSTR_LEN(haystack));
18691864

1870-
if (found) {
1871-
RETURN_LONG(found - ZSTR_VAL(haystack));
1872-
} else {
1865+
if (!found) {
18731866
RETURN_FALSE;
18741867
}
1868+
RETURN_LONG(found - ZSTR_VAL(haystack));
18751869
}
18761870
/* }}} */
18771871

@@ -1900,11 +1894,10 @@ PHP_FUNCTION(stripos)
19001894
found = (char*)php_memnistr(ZSTR_VAL(haystack) + offset,
19011895
ZSTR_VAL(needle), ZSTR_LEN(needle), ZSTR_VAL(haystack) + ZSTR_LEN(haystack));
19021896

1903-
if (found) {
1904-
RETVAL_LONG(found - ZSTR_VAL(haystack));
1905-
} else {
1906-
RETVAL_FALSE;
1897+
if (!found) {
1898+
RETURN_FALSE;
19071899
}
1900+
RETURN_LONG(found - ZSTR_VAL(haystack));
19081901
}
19091902
/* }}} */
19101903

@@ -1944,11 +1937,12 @@ PHP_FUNCTION(strrpos)
19441937
}
19451938
}
19461939

1947-
if ((found = zend_memnrstr(p, ZSTR_VAL(needle), ZSTR_LEN(needle), e))) {
1948-
RETURN_LONG(found - ZSTR_VAL(haystack));
1949-
}
1940+
found = zend_memnrstr(p, ZSTR_VAL(needle), ZSTR_LEN(needle), e);
19501941

1951-
RETURN_FALSE;
1942+
if (!found) {
1943+
RETURN_FALSE;
1944+
}
1945+
RETURN_LONG(found - ZSTR_VAL(haystack));
19521946
}
19531947
/* }}} */
19541948

@@ -2047,12 +2041,11 @@ PHP_FUNCTION(strrchr)
20472041
ZEND_PARSE_PARAMETERS_END();
20482042

20492043
found = zend_memrchr(ZSTR_VAL(haystack), *ZSTR_VAL(needle), ZSTR_LEN(haystack));
2050-
if (found) {
2051-
found_offset = found - ZSTR_VAL(haystack);
2052-
RETURN_STRINGL(found, ZSTR_LEN(haystack) - found_offset);
2053-
} else {
2044+
if (!found) {
20542045
RETURN_FALSE;
20552046
}
2047+
found_offset = found - ZSTR_VAL(haystack);
2048+
RETURN_STRINGL(found, ZSTR_LEN(haystack) - found_offset);
20562049
}
20572050
/* }}} */
20582051

@@ -2786,7 +2779,7 @@ static void php_strtr_array(zval *return_value, zend_string *input, HashTable *p
27862779
zend_ulong num_key;
27872780
zend_string *str_key;
27882781
size_t len, pos, old_pos;
2789-
int num_keys = 0;
2782+
bool has_num_keys = false;
27902783
size_t minlen = 128*1024;
27912784
size_t maxlen = 0;
27922785
HashTable str_hash;
@@ -2803,10 +2796,10 @@ static void php_strtr_array(zval *return_value, zend_string *input, HashTable *p
28032796
/* check if original array has numeric keys */
28042797
ZEND_HASH_FOREACH_STR_KEY(pats, str_key) {
28052798
if (UNEXPECTED(!str_key)) {
2806-
num_keys = 1;
2799+
has_num_keys = true;
28072800
} else {
28082801
len = ZSTR_LEN(str_key);
2809-
if (UNEXPECTED(len < 1)) {
2802+
if (UNEXPECTED(len == 0)) {
28102803
php_error_docref(NULL, E_WARNING, "Ignoring replacement of empty string");
28112804
continue;
28122805
} else if (UNEXPECTED(len > slen)) {
@@ -2825,7 +2818,7 @@ static void php_strtr_array(zval *return_value, zend_string *input, HashTable *p
28252818
}
28262819
} ZEND_HASH_FOREACH_END();
28272820

2828-
if (UNEXPECTED(num_keys)) {
2821+
if (UNEXPECTED(has_num_keys)) {
28292822
zend_string *key_used;
28302823
/* we have to rebuild HashTable with numeric keys */
28312824
zend_hash_init(&str_hash, zend_hash_num_elements(pats), NULL, NULL, 0);
@@ -2965,7 +2958,7 @@ static zend_always_inline zend_long count_chars(const char *p, zend_long length,
29652958
/* }}} */
29662959

29672960
/* {{{ php_char_to_str_ex */
2968-
static zend_string* php_char_to_str_ex(zend_string *str, char from, char *to, size_t to_len, int case_sensitivity, zend_long *replace_count)
2961+
static zend_string* php_char_to_str_ex(zend_string *str, char from, char *to, size_t to_len, bool case_sensitivity, zend_long *replace_count)
29692962
{
29702963
zend_string *result;
29712964
size_t char_count;
@@ -3323,7 +3316,7 @@ PHP_FUNCTION(strtr)
33233316
ZSTR_VAL(str_key)[0],
33243317
ZSTR_VAL(replace),
33253318
ZSTR_LEN(replace),
3326-
1,
3319+
/* case_sensitive */ true,
33273320
NULL));
33283321
} else {
33293322
zend_long dummy;
@@ -3459,7 +3452,7 @@ PHP_FUNCTION(similar_text)
34593452
{
34603453
zend_string *t1, *t2;
34613454
zval *percent = NULL;
3462-
int ac = ZEND_NUM_ARGS();
3455+
bool compute_percentage = ZEND_NUM_ARGS() == 3;
34633456
size_t sim;
34643457

34653458
ZEND_PARSE_PARAMETERS_START(2, 3)
@@ -3470,7 +3463,7 @@ PHP_FUNCTION(similar_text)
34703463
ZEND_PARSE_PARAMETERS_END();
34713464

34723465
if (ZSTR_LEN(t1) + ZSTR_LEN(t2) == 0) {
3473-
if (ac > 2) {
3466+
if (compute_percentage) {
34743467
ZEND_TRY_ASSIGN_REF_DOUBLE(percent, 0);
34753468
}
34763469

@@ -3479,7 +3472,7 @@ PHP_FUNCTION(similar_text)
34793472

34803473
sim = php_similar_char(ZSTR_VAL(t1), ZSTR_LEN(t1), ZSTR_VAL(t2), ZSTR_LEN(t2));
34813474

3482-
if (ac > 2) {
3475+
if (compute_percentage) {
34833476
ZEND_TRY_ASSIGN_REF_DOUBLE(percent, sim * 200.0 / (ZSTR_LEN(t1) + ZSTR_LEN(t2)));
34843477
}
34853478

@@ -3897,8 +3890,7 @@ static zend_always_inline quad_word aarch64_contains_slash_chars(uint8x16_t x) {
38973890

38983891
static zend_always_inline char *aarch64_add_slashes(quad_word res, const char *source, char *target)
38993892
{
3900-
int i = 0;
3901-
for (; i < 16; i++) {
3893+
for (int i = 0; i < 16; i++) {
39023894
char s = source[i];
39033895
if (res.mem[i] == 0)
39043896
*target++ = s;
@@ -4160,7 +4152,7 @@ PHPAPI void php_stripslashes(zend_string *str)
41604152
/* {{{ php_str_replace_in_subject */
41614153
static zend_long php_str_replace_in_subject(
41624154
zend_string *search_str, HashTable *search_ht, zend_string *replace_str, HashTable *replace_ht,
4163-
zend_string *subject_str, zval *result, int case_sensitivity
4155+
zend_string *subject_str, zval *result, bool case_sensitivity
41644156
) {
41654157
zval *search_entry;
41664158
zend_string *tmp_result;
@@ -4321,7 +4313,7 @@ static zend_long php_str_replace_in_subject(
43214313
/* }}} */
43224314

43234315
/* {{{ php_str_replace_common */
4324-
static void php_str_replace_common(INTERNAL_FUNCTION_PARAMETERS, int case_sensitivity)
4316+
static void php_str_replace_common(INTERNAL_FUNCTION_PARAMETERS, bool case_sensitivity)
43254317
{
43264318
zend_string *search_str;
43274319
HashTable *search_ht;
@@ -4836,10 +4828,11 @@ PHP_FUNCTION(parse_str)
48364828
* 0 start tag
48374829
* 1 first non-whitespace char seen
48384830
*/
4839-
int php_tag_find(char *tag, size_t len, const char *set) {
4831+
static bool php_tag_find(char *tag, size_t len, const char *set) {
48404832
char c, *n;
48414833
const char *t;
4842-
int state=0, done=0;
4834+
int state=0;
4835+
bool done=0;
48434836
char *norm;
48444837

48454838
if (len == 0) {
@@ -5391,7 +5384,7 @@ PHP_FUNCTION(count_chars)
53915384
/* }}} */
53925385

53935386
/* {{{ php_strnatcmp */
5394-
static void php_strnatcmp(INTERNAL_FUNCTION_PARAMETERS, int fold_case)
5387+
static void php_strnatcmp(INTERNAL_FUNCTION_PARAMETERS, bool is_case_insensitive)
53955388
{
53965389
zend_string *s1, *s2;
53975390

@@ -5402,7 +5395,7 @@ static void php_strnatcmp(INTERNAL_FUNCTION_PARAMETERS, int fold_case)
54025395

54035396
RETURN_LONG(strnatcmp_ex(ZSTR_VAL(s1), ZSTR_LEN(s1),
54045397
ZSTR_VAL(s2), ZSTR_LEN(s2),
5405-
fold_case));
5398+
is_case_insensitive));
54065399
}
54075400
/* }}} */
54085401

@@ -5439,11 +5432,18 @@ PHP_FUNCTION(strnatcmp)
54395432
}
54405433
/* }}} */
54415434

5435+
/* {{{ Returns the result of case-insensitive string comparison using 'natural' algorithm */
5436+
PHP_FUNCTION(strnatcasecmp)
5437+
{
5438+
php_strnatcmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
5439+
}
5440+
/* }}} */
5441+
54425442
/* {{{ Returns numeric formatting information based on the current locale */
54435443
PHP_FUNCTION(localeconv)
54445444
{
54455445
zval grouping, mon_grouping;
5446-
int len, i;
5446+
size_t len, i;
54475447

54485448
ZEND_PARSE_PARAMETERS_NONE();
54495449

@@ -5457,14 +5457,14 @@ PHP_FUNCTION(localeconv)
54575457
localeconv_r( &currlocdata );
54585458

54595459
/* Grab the grouping data out of the array */
5460-
len = (int)strlen(currlocdata.grouping);
5460+
len = strlen(currlocdata.grouping);
54615461

54625462
for (i = 0; i < len; i++) {
54635463
add_index_long(&grouping, i, currlocdata.grouping[i]);
54645464
}
54655465

54665466
/* Grab the monetary grouping data out of the array */
5467-
len = (int)strlen(currlocdata.mon_grouping);
5467+
len = strlen(currlocdata.mon_grouping);
54685468

54695469
for (i = 0; i < len; i++) {
54705470
add_index_long(&mon_grouping, i, currlocdata.mon_grouping[i]);
@@ -5493,13 +5493,6 @@ PHP_FUNCTION(localeconv)
54935493
}
54945494
/* }}} */
54955495

5496-
/* {{{ Returns the result of case-insensitive string comparison using 'natural' algorithm */
5497-
PHP_FUNCTION(strnatcasecmp)
5498-
{
5499-
php_strnatcmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
5500-
}
5501-
/* }}} */
5502-
55035496
/* {{{ Returns the number of times a substring occurs in the string */
55045497
PHP_FUNCTION(substr_count)
55055498
{
@@ -5771,7 +5764,7 @@ PHP_FUNCTION(str_rot13)
57715764
}
57725765
/* }}} */
57735766

5774-
static void php_string_shuffle(char *str, zend_long len) /* {{{ */
5767+
static void php_string_shuffle(char *str, size_t len) /* {{{ */
57755768
{
57765769
zend_long n_elems, rnd_idx, n_left;
57775770
char temp;
@@ -5807,7 +5800,7 @@ PHP_FUNCTION(str_shuffle)
58075800

58085801
RETVAL_STRINGL(ZSTR_VAL(arg), ZSTR_LEN(arg));
58095802
if (Z_STRLEN_P(return_value) > 1) {
5810-
php_string_shuffle(Z_STRVAL_P(return_value), (zend_long) Z_STRLEN_P(return_value));
5803+
php_string_shuffle(Z_STRVAL_P(return_value), Z_STRLEN_P(return_value));
58115804
}
58125805
}
58135806
/* }}} */

ext/standard/strnatcmp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ compare_left(char const **a, char const *aend, char const **b, char const *bend)
8585
/* }}} */
8686

8787
/* {{{ strnatcmp_ex */
88-
PHPAPI int strnatcmp_ex(char const *a, size_t a_len, char const *b, size_t b_len, int fold_case)
88+
PHPAPI int strnatcmp_ex(char const *a, size_t a_len, char const *b, size_t b_len, bool is_case_insensitive)
8989
{
9090
unsigned char ca, cb;
9191
char const *ap, *bp;
@@ -146,7 +146,7 @@ PHPAPI int strnatcmp_ex(char const *a, size_t a_len, char const *b, size_t b_len
146146
}
147147
}
148148

149-
if (fold_case) {
149+
if (is_case_insensitive) {
150150
ca = toupper((int)(unsigned char)ca);
151151
cb = toupper((int)(unsigned char)cb);
152152
}

0 commit comments

Comments
 (0)