cvs: php4 /ext/standard basic_functions.c php_string.h string.c

From: Date: Sat, 22 Jul 2000 01:12:24 +0000
Subject: cvs: php4 /ext/standard basic_functions.c php_string.h string.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-854@lists.php.net to get a copy of this message
david Fri Jul 21 18:12:24 2000 EDT Modified files: /php4/ext/standard basic_functions.c php_string.h string.c Log: wordwrap function from Chris Russel <russel@yorku.ca> differences from his patch: - wordwrap width and wrap-string now optional parameters (default to 75 and "\n" respectively) - wordwrap_byte is now just an automatic special case of wordwrap - Zend API compliant @- Added new function "wordwrap" to wordwrap long strings from Chris @ Russel <russel@yorku.ca> (David Croft) Index: php4/ext/standard/basic_functions.c diff -u php4/ext/standard/basic_functions.c:1.228 php4/ext/standard/basic_functions.c:1.229 --- php4/ext/standard/basic_functions.c:1.228 Sat Jul 8 13:38:23 2000 +++ php4/ext/standard/basic_functions.c Fri Jul 21 18:12:24 2000 @@ -106,6 +106,7 @@ PHP_FE(getimagesize, NULL) + PHP_FE(wordwrap, NULL) PHP_FE(htmlspecialchars, NULL) PHP_FE(htmlentities, NULL) PHP_FE(get_html_translation_table, NULL) Index: php4/ext/standard/php_string.h diff -u php4/ext/standard/php_string.h:1.23 php4/ext/standard/php_string.h:1.24 --- php4/ext/standard/php_string.h:1.23 Sat Jul 8 13:38:23 2000 +++ php4/ext/standard/php_string.h Fri Jul 21 18:12:24 2000 @@ -29,7 +29,7 @@ */ -/* $Id: php_string.h,v 1.23 2000/07/08 20:38:23 andrei Exp $ */ +/* $Id: php_string.h,v 1.24 2000/07/22 01:12:24 david Exp $ */ /* Synced with php 3.0 revision 1.43 1999-06-16 [ssb] */ @@ -46,6 +46,7 @@ PHP_FUNCTION(levenshtein); PHP_FUNCTION(count_chars); +PHP_FUNCTION(wordwrap); PHP_FUNCTION(explode); PHP_FUNCTION(implode); PHP_FUNCTION(strtok); Index: php4/ext/standard/string.c diff -u php4/ext/standard/string.c:1.139 php4/ext/standard/string.c:1.140 --- php4/ext/standard/string.c:1.139 Fri Jul 21 08:29:59 2000 +++ php4/ext/standard/string.c Fri Jul 21 18:12:24 2000 @@ -18,7 +18,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: string.c,v 1.139 2000/07/21 15:29:59 andrei Exp $ */ +/* $Id: string.c,v 1.140 2000/07/22 01:12:24 david Exp $ */ /* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */ @@ -225,6 +225,144 @@ RETURN_FALSE; } /* }}} */ + + +/* {{{ proto string wordwrap(string str[, int width[, string break]]) + * wrap buffer to selected number of characters using string break char */ +PHP_FUNCTION(wordwrap) +{ + pval **ptext, **plinelength, **pbreakchar; + long i=0, l=0, pgr=0, linelength=0, last=0, breakcharlen; + char *text, *breakchar, *newtext; + + if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 3 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &ptext, &plinelength, &pbreakchar) == FAILURE) { + WRONG_PARAM_COUNT; + } + + convert_to_string_ex(ptext); + text = (*ptext)->value.str.val; + + if (ZEND_NUM_ARGS() > 1) { + convert_to_long_ex(plinelength); + linelength = (*plinelength)->value.lval; + } + else { + linelength = 75; + } + + if (ZEND_NUM_ARGS() > 2) { + convert_to_string_ex(pbreakchar); + breakchar = (*pbreakchar)->value.str.val; + breakcharlen = (*pbreakchar)->value.str.len; + } + else { + breakchar = "\n"; + breakcharlen = 1; + } + + /* Special case for a single-character break as it needs no + additional storage space */ + + if (breakcharlen == 1) { + + while (text[i] != '\0') { + /* prescan line to see if it is greater than linelength */ + l = 0; + while (text[i+l] != breakchar[0]) { + if (text[i+l] == '\0') { + l --; + break; + } + l++; + } + if (l > linelength) { + pgr = l; + l = linelength; + /* needs breaking; work backwards to find previous word */ + while (l >= 0) { + if (text[i+l] == ' ') { + text[i+l] = breakchar[0]; + break; + } + l --; + } + if (l == -1) { + /* couldn't break is backwards, try looking forwards */ + l = linelength; + while (l <= pgr) { + if(text[i+l] == ' ') { + text[i+l] = breakchar[0]; + break; + } + l ++; + } + } + } + i += l+1; + } + RETVAL_STRINGL(text, strlen(text), 1); + } + else { + /* Multiple character line break */ + + newtext = emalloc((*ptext)->value.str.len * (breakcharlen+1)); + newtext[0] = '\0'; + + i = 0; + while (text[i] != '\0') { + /* prescan line to see if it is greater than linelength */ + l = 0; + while (text[i+l] != '\0') { + if (text[i+l] == breakchar[0]) { + if (breakcharlen == 1 || strncmp(text+i+l, breakchar, breakcharlen)==0) + break; + } + l ++; + } + if (l > linelength) { + pgr = l; + l = linelength; + + /* needs breaking; work backwards to find previous word */ + while (l >= 0) { + if (text[i+l] == ' ') { + strncat(newtext, text+last, i+l-last); + strcat(newtext, breakchar); + last = i + l + 1; + break; + } + l --; + } + if (l == -1) { + /* couldn't break it backwards, try looking forwards */ + l = linelength; + while (l <= pgr) { + if (text[i+l] == ' ') { + strncat(newtext, text+last, i+l-last); + strcat(newtext, breakchar); + last = i + l + 1; + break; + } + l ++; + } + } + i += l+1; + } + else { + i += (l ? l : 1); + } + } + + if (i+l > last) { + strcat(newtext, text+last); + } + + RETVAL_STRINGL(newtext, strlen(newtext), 1); + efree(newtext); + } +} +/* }}} */ + PHPAPI void php_explode(zval *delim, zval *str, zval *return_value, int limit) {

« previous php.cvs (#854) next »