2020
2121#include "php.h"
2222
23- static int metaphone (unsigned char * word , size_t word_len , zend_long max_phonemes , zend_string * * phoned_word , int traditional );
23+ static void metaphone (unsigned char * word , size_t word_len , zend_long max_phonemes , zend_string * * phoned_word , int traditional );
2424
2525/* {{{ Break english phrases down into their phonemes */
2626PHP_FUNCTION (metaphone )
@@ -35,14 +35,13 @@ PHP_FUNCTION(metaphone)
3535 Z_PARAM_LONG (phones )
3636 ZEND_PARSE_PARAMETERS_END ();
3737
38- if (metaphone ((unsigned char * )ZSTR_VAL (str ), ZSTR_LEN (str ), phones , & result , 1 ) == 0 ) {
39- RETVAL_STR (result );
40- } else {
41- if (result ) {
42- zend_string_free (result );
43- }
44- RETURN_FALSE ;
38+ if (phones < 0 ) {
39+ zend_argument_value_error (2 , "must be greater than or equal to 0" );
40+ RETURN_THROWS ();
4541 }
42+
43+ metaphone ((unsigned char * )ZSTR_VAL (str ), ZSTR_LEN (str ), phones , & result , 1 );
44+ RETVAL_STR (result );
4645}
4746/* }}} */
4847
@@ -145,7 +144,7 @@ static char Lookahead(char *word, int how_far)
145144 ZSTR_LEN(*phoned_word) = p_idx; \
146145 }
147146/* Slap a null character on the end of the phoned word */
148- #define End_Phoned_Word { \
147+ #define End_Phoned_Word () { \
149148 if (p_idx == max_buffer_len) { \
150149 *phoned_word = zend_string_extend(*phoned_word, 1 * sizeof(char) + max_buffer_len, 0); \
151150 max_buffer_len += 1; \
@@ -160,24 +159,13 @@ static char Lookahead(char *word, int how_far)
160159#define Isbreak (c ) (!isalpha(c))
161160
162161/* {{{ metaphone */
163- static int metaphone (unsigned char * word , size_t word_len , zend_long max_phonemes , zend_string * * phoned_word , int traditional )
162+ static void metaphone (unsigned char * word , size_t word_len , zend_long max_phonemes , zend_string * * phoned_word , int traditional )
164163{
165164 int w_idx = 0 ; /* point in the phonization we're at. */
166165 size_t p_idx = 0 ; /* end of the phoned phrase */
167166 size_t max_buffer_len = 0 ; /* maximum length of the destination buffer */
168-
169- /*-- Parameter checks --*/
170- /* Negative phoneme length is meaningless */
171-
172- if (max_phonemes < 0 )
173- return -1 ;
174-
175- /* Empty/null string is meaningless */
176- /* Overly paranoid */
177- /* assert(word != NULL && word[0] != '\0'); */
178-
179- if (word == NULL )
180- return -1 ;
167+ ZEND_ASSERT (word != NULL );
168+ ZEND_ASSERT (max_phonemes >= 0 );
181169
182170/*-- Allocate memory for our phoned_phrase --*/
183171 if (max_phonemes == 0 ) { /* Assume largest possible */
@@ -194,8 +182,8 @@ static int metaphone(unsigned char *word, size_t word_len, zend_long max_phoneme
194182 for (; !isalpha (Curr_Letter ); w_idx ++ ) {
195183 /* On the off chance we were given nothing but crap... */
196184 if (Curr_Letter == '\0' ) {
197- End_Phoned_Word
198- return SUCCESS ; /* For testing */
185+ End_Phoned_Word ();
186+ return ;
199187 }
200188 }
201189
@@ -460,8 +448,6 @@ static int metaphone(unsigned char *word, size_t word_len, zend_long max_phoneme
460448 w_idx += skip_letter ;
461449 } /* END FOR */
462450
463- End_Phoned_Word ;
464-
465- return 0 ;
451+ End_Phoned_Word ();
466452} /* END metaphone */
467453/* }}} */
0 commit comments