Skip to content

Commit 550a662

Browse files
committed
strtr() optimization
1 parent 79a9f80 commit 550a662

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

ext/standard/string.c

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2708,16 +2708,16 @@ PHPAPI char *php_strtr(char *str, size_t len, const char *str_from, const char *
27082708
}
27092709
}
27102710
} else {
2711-
unsigned char xlat[256], j = 0;
2711+
unsigned char xlat[256];
27122712

2713-
do { xlat[j] = j; } while (++j != 0);
2713+
memset(xlat, 0, sizeof(xlat));
27142714

27152715
for (i = 0; i < trlen; i++) {
2716-
xlat[(size_t)(unsigned char) str_from[i]] = str_to[i];
2716+
xlat[(size_t)(unsigned char) str_from[i]] = str_to[i] - str_from[i];
27172717
}
27182718

27192719
for (i = 0; i < len; i++) {
2720-
str[i] = xlat[(size_t)(unsigned char) str[i]];
2720+
str[i] += xlat[(size_t)(unsigned char) str[i]];
27212721
}
27222722
}
27232723

@@ -2742,41 +2742,38 @@ static zend_string *php_strtr_ex(zend_string *str, const char *str_from, const c
27422742
new_str = zend_string_alloc(ZSTR_LEN(str), 0);
27432743
memcpy(ZSTR_VAL(new_str), ZSTR_VAL(str), i);
27442744
ZSTR_VAL(new_str)[i] = ch_to;
2745-
break;
2745+
i++;
2746+
for (; i < ZSTR_LEN(str); i++) {
2747+
ZSTR_VAL(new_str)[i] = (ZSTR_VAL(str)[i] != ch_from) ? ZSTR_VAL(str)[i] : ch_to;
2748+
}
2749+
ZSTR_VAL(new_str)[i] = 0;
2750+
return new_str;
27462751
}
27472752
}
2748-
for (; i < ZSTR_LEN(str); i++) {
2749-
ZSTR_VAL(new_str)[i] = (ZSTR_VAL(str)[i] != ch_from) ? ZSTR_VAL(str)[i] : ch_to;
2750-
}
27512753
} else {
2752-
unsigned char xlat[256], j = 0;
2754+
unsigned char xlat[256];
27532755

2754-
do { xlat[j] = j; } while (++j != 0);
2756+
memset(xlat, 0, sizeof(xlat));;
27552757

27562758
for (i = 0; i < trlen; i++) {
2757-
xlat[(size_t)(unsigned char) str_from[i]] = str_to[i];
2759+
xlat[(size_t)(unsigned char) str_from[i]] = str_to[i] - str_from[i];
27582760
}
27592761

27602762
for (i = 0; i < ZSTR_LEN(str); i++) {
2761-
if (ZSTR_VAL(str)[i] != xlat[(size_t)(unsigned char) ZSTR_VAL(str)[i]]) {
2763+
if (xlat[(size_t)(unsigned char) ZSTR_VAL(str)[i]]) {
27622764
new_str = zend_string_alloc(ZSTR_LEN(str), 0);
27632765
memcpy(ZSTR_VAL(new_str), ZSTR_VAL(str), i);
2764-
ZSTR_VAL(new_str)[i] = xlat[(size_t)(unsigned char) ZSTR_VAL(str)[i]];
2765-
break;
2766+
do {
2767+
ZSTR_VAL(new_str)[i] = ZSTR_VAL(str)[i] + xlat[(size_t)(unsigned char) ZSTR_VAL(str)[i]];
2768+
i++;
2769+
} while (i < ZSTR_LEN(str));
2770+
ZSTR_VAL(new_str)[i] = 0;
2771+
return new_str;
27662772
}
27672773
}
2768-
2769-
for (;i < ZSTR_LEN(str); i++) {
2770-
ZSTR_VAL(new_str)[i] = xlat[(size_t)(unsigned char) ZSTR_VAL(str)[i]];
2771-
}
27722774
}
27732775

2774-
if (!new_str) {
2775-
return zend_string_copy(str);
2776-
}
2777-
2778-
ZSTR_VAL(new_str)[ZSTR_LEN(new_str)] = 0;
2779-
return new_str;
2776+
return zend_string_copy(str);
27802777
}
27812778
/* }}} */
27822779

0 commit comments

Comments
 (0)