Skip to content

Commit df837e6

Browse files
author
Sterling Hughes
committed
have implode use the smart_str_*() functions, this should speed things up
quite a bit...
1 parent 1fe42e1 commit df837e6

File tree

1 file changed

+15
-29
lines changed

1 file changed

+15
-29
lines changed

ext/standard/string.c

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -818,42 +818,28 @@ PHP_FUNCTION(explode)
818818
*/
819819
PHPAPI void php_implode(zval *delim, zval *arr, zval *return_value)
820820
{
821-
zval **tmp;
822-
char *tmp_str;
823-
int len = 0, count = 0, target = 0;
824-
HashPosition pos;
821+
zval **tmp;
822+
HashPosition pos;
823+
smart_str implstr = {0};
824+
int numelems, i = 0;
825+
826+
numelems = zend_hash_num_elements(Z_ARRVAL_P(arr));
825827

826-
/* convert everything to strings, and calculate length */
827828
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(arr), &pos);
828-
while (zend_hash_get_current_data_ex(Z_ARRVAL_P(arr), (void **) &tmp, &pos) == SUCCESS) {
829+
while (zend_hash_get_current_data_ex(Z_ARRVAL_P(arr),
830+
(void **) &tmp,
831+
&pos) == SUCCESS) {
829832
convert_to_string_ex(tmp);
830-
len += Z_STRLEN_PP(tmp);
831-
if (count > 0) {
832-
len += Z_STRLEN_P(delim);
833-
}
834-
835-
count++;
836-
zend_hash_move_forward_ex(Z_ARRVAL_P(arr), &pos);
837-
}
838833

839-
/* do it */
840-
tmp_str = (char *) emalloc(len + 1);
841-
tmp_str[0] = 0;
842-
843-
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(arr), &pos);
844-
while (zend_hash_get_current_data_ex(Z_ARRVAL_P(arr), (void **) &tmp, &pos) == SUCCESS) {
845-
count--;
846-
memcpy(tmp_str + target, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
847-
target += Z_STRLEN_PP(tmp);
848-
if (count > 0) {
849-
memcpy(tmp_str + target, Z_STRVAL_P(delim), Z_STRLEN_P(delim));
850-
target += Z_STRLEN_P(delim);
834+
smart_str_appendl(&implstr, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
835+
if (++i != numelems) {
836+
smart_str_appendl(&implstr, Z_STRVAL_P(delim), Z_STRLEN_P(delim));
851837
}
852838
zend_hash_move_forward_ex(Z_ARRVAL_P(arr), &pos);
853839
}
854-
tmp_str[len] = 0;
855-
856-
RETURN_STRINGL(tmp_str, len, 0);
840+
smart_str_0(&implstr);
841+
842+
RETURN_STRINGL(implstr.c, implstr.len, 0);
857843
}
858844
/* }}} */
859845

0 commit comments

Comments
 (0)