@@ -1712,41 +1712,10 @@ PHP_FUNCTION(mb_output_handler)
1712
1712
/* {{{ Convert a multibyte string to an array. If split_length is specified,
1713
1713
break the string down into chunks each split_length characters long. */
1714
1714
1715
- /* structure to pass split params to the callback */
1716
- struct mbfl_split_params {
1717
- zval * return_value ; /* php function return value structure pointer */
1718
- mbfl_string * result_string ; /* string to store result chunk */
1719
- size_t mb_chunk_length ; /* actual chunk length in chars */
1720
- size_t split_length ; /* split length in chars */
1721
- mbfl_convert_filter * next_filter ; /* widechar to encoding converter */
1722
- };
1723
-
1724
- /* callback function to fill split array */
1725
- static int mbfl_split_output (int c , void * data )
1726
- {
1727
- struct mbfl_split_params * params = (struct mbfl_split_params * )data ; /* cast passed data */
1728
-
1729
- (* params -> next_filter -> filter_function )(c , params -> next_filter ); /* decoder filter */
1730
-
1731
- if (params -> split_length == ++ params -> mb_chunk_length ) { /* if current chunk size reached defined chunk size or last char reached */
1732
- mbfl_convert_filter_flush (params -> next_filter );/* concatenate separate decoded chars to the solid string */
1733
- mbfl_memory_device * device = (mbfl_memory_device * )params -> next_filter -> data ; /* chars container */
1734
- mbfl_string * chunk = params -> result_string ;
1735
- mbfl_memory_device_result (device , chunk ); /* make chunk */
1736
- add_next_index_stringl (params -> return_value , (const char * )chunk -> val , chunk -> len ); /* add chunk to the array */
1737
- efree (chunk -> val );
1738
- params -> mb_chunk_length = 0 ; /* reset mb_chunk size */
1739
- }
1740
-
1741
- return 0 ;
1742
- }
1743
-
1744
1715
/* TODO Document this function on php.net */
1745
1716
PHP_FUNCTION (mb_str_split )
1746
1717
{
1747
1718
zend_string * str , * encoding = NULL ;
1748
- size_t mb_len , chunks , chunk_len ;
1749
- mbfl_string string , result_string ;
1750
1719
zend_long split_length = 1 ;
1751
1720
1752
1721
ZEND_PARSE_PARAMETERS_START (1 , 3 )
@@ -1761,91 +1730,17 @@ PHP_FUNCTION(mb_str_split)
1761
1730
RETURN_THROWS ();
1762
1731
}
1763
1732
1764
- string .val = (unsigned char * ) ZSTR_VAL (str );
1765
- string .len = ZSTR_LEN (str );
1766
- const mbfl_encoding * mbfl_encoding = string .encoding = php_mb_get_encoding (encoding , 3 );
1767
- if (!string .encoding ) {
1733
+ const mbfl_encoding * mbfl_encoding = php_mb_get_encoding (encoding , 3 );
1734
+ if (!mbfl_encoding ) {
1768
1735
RETURN_THROWS ();
1769
1736
}
1770
1737
1771
- const char * p = ZSTR_VAL (str );
1772
- const char * last = ZSTR_VAL (str ) + ZSTR_LEN (str );
1773
-
1774
- /* first scenario: 1/2/4-byte fixed width encoding */
1775
- if (mbfl_encoding -> flag & MBFL_ENCTYPE_SBCS ) { /* 1 byte */
1776
- mb_len = string .len ;
1777
- chunk_len = (size_t )split_length ; /* chunk length in bytes */
1778
- } else if (mbfl_encoding -> flag & (MBFL_ENCTYPE_WCS2BE | MBFL_ENCTYPE_WCS2LE )) { /* 2 bytes */
1779
- mb_len = string .len / 2 ;
1780
- chunk_len = split_length * 2 ;
1781
- } else if (mbfl_encoding -> flag & (MBFL_ENCTYPE_WCS4BE | MBFL_ENCTYPE_WCS4LE )) { /* 4 bytes */
1782
- mb_len = string .len / 4 ;
1783
- chunk_len = split_length * 4 ;
1784
- } else if (mbfl_encoding -> mblen_table ) {
1785
- /* second scenario: variable width encoding with length table */
1786
- const unsigned char * mbtab = mbfl_encoding -> mblen_table ;
1787
-
1788
- /* assume that we have 1-byte characters */
1789
- array_init_size (return_value , (string .len + split_length ) / split_length ); /* round up */
1790
-
1791
- while (p < last ) {
1792
- char * chunk_p = p ; /* pointer to first byte in chunk */
1793
-
1794
- for (int char_count = 0 ; char_count < split_length && p < last ; char_count ++ ) {
1795
- p += mbtab [* (unsigned char * )p ]; /* character byte length table */
1796
- }
1797
- if (p > last ) { /* check if chunk is in bounds */
1798
- p = last ;
1799
- }
1800
- add_next_index_stringl (return_value , chunk_p , p - chunk_p );
1801
- }
1802
- return ;
1803
- } else {
1804
- /* third scenario: other multibyte encodings */
1805
- /* assume that we have 1-byte characters */
1806
- array_init_size (return_value , (string .len + split_length ) / split_length ); /* round up */
1807
-
1808
- /* decoder filter to decode wchar to encoding */
1809
- mbfl_memory_device device ;
1810
- mbfl_memory_device_init (& device , split_length + 1 , 0 );
1811
-
1812
- mbfl_convert_filter * decoder = mbfl_convert_filter_new (& mbfl_encoding_wchar , string .encoding ,
1813
- mbfl_memory_device_output , NULL , & device );
1814
- ZEND_ASSERT (decoder );
1815
-
1816
- /* wchar filter */
1817
- mbfl_string_init (& result_string ); /* mbfl_string to store chunk in the callback */
1818
- struct mbfl_split_params params = { /* init callback function params structure */
1819
- .return_value = return_value ,
1820
- .result_string = & result_string ,
1821
- .mb_chunk_length = 0 ,
1822
- .split_length = (size_t )split_length ,
1823
- .next_filter = decoder ,
1824
- };
1825
-
1826
- mbfl_convert_filter * filter = mbfl_convert_filter_new (string .encoding , & mbfl_encoding_wchar ,
1827
- mbfl_split_output , NULL , & params );
1828
- ZEND_ASSERT (filter );
1829
-
1830
- while (p < last - 1 ) { /* cycle each byte except last with callback function */
1831
- (* filter -> filter_function )(* p ++ , filter );
1832
- }
1833
- params .mb_chunk_length = split_length - 1 ; /* force to finish current chunk */
1834
- (* filter -> filter_function )(* p ++ , filter ); /* process last char */
1835
-
1836
- mbfl_convert_filter_delete (decoder );
1837
- mbfl_convert_filter_delete (filter );
1838
- mbfl_memory_device_clear (& device );
1839
- return ;
1840
- }
1738
+ mbfl_string string ;
1739
+ string .val = (unsigned char * )ZSTR_VAL (str );
1740
+ string .len = ZSTR_LEN (str );
1741
+ string .encoding = mbfl_encoding ;
1841
1742
1842
- /* first scenario: 1/2/4-byte fixed width encoding */
1843
- chunks = (mb_len + split_length - 1 ) / split_length ; /* round up */
1844
- array_init_size (return_value , chunks );
1845
- while (chunks -- ) {
1846
- add_next_index_stringl (return_value , p , chunk_len );
1847
- p += chunk_len ;
1848
- }
1743
+ RETVAL_ARR (mbfl_str_split (& string , split_length ));
1849
1744
}
1850
1745
/* }}} */
1851
1746
0 commit comments