@@ -818,42 +818,28 @@ PHP_FUNCTION(explode)
818
818
*/
819
819
PHPAPI void php_implode (zval * delim , zval * arr , zval * return_value )
820
820
{
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 ));
825
827
826
- /* convert everything to strings, and calculate length */
827
828
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 ) {
829
832
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
- }
838
833
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 ));
851
837
}
852
838
zend_hash_move_forward_ex (Z_ARRVAL_P (arr ), & pos );
853
839
}
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 );
857
843
}
858
844
/* }}} */
859
845
0 commit comments