diff options
Diffstat (limited to 'ext/standard/string.c')
-rw-r--r-- | ext/standard/string.c | 616 |
1 files changed, 153 insertions, 463 deletions
diff --git a/ext/standard/string.c b/ext/standard/string.c index 9f29036eb7..bfc66735a9 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -2,7 +2,7 @@ +----------------------------------------------------------------------+ | PHP HTML Embedded Scripting Language Version 3.0 | +----------------------------------------------------------------------+ - | Copyright (c) 1997-1999 PHP Development Team (See Credits file) | + | Copyright (c) 1997,1998 PHP Development Team (See Credits file) | +----------------------------------------------------------------------+ | This program is free software; you can redistribute it and/or modify | | it under the terms of one of the following licenses: | @@ -24,20 +24,24 @@ | contact core@php.net. | +----------------------------------------------------------------------+ | Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> | - | Stig Sæther Bakken <ssb@fast.no> | + | Stig Sæther Bakken <ssb@guardian.no> | | Zeev Suraski <bourbon@nevision.net.il> | +----------------------------------------------------------------------+ */ + /* $Id$ */ #include <stdio.h> #include "php.h" -#include "php_globals.h" -#include "php3_standard.h" +#include "reg.h" +#include "post.h" +#include "php3_string.h" #if HAVE_SETLOCALE #include <locale.h> #endif +#include "zend_execute.h" +#include "php_globals.h" static char hexconvtab[] = "0123456789abcdef"; @@ -84,13 +88,11 @@ PHP_FUNCTION(bin2hex) RETURN_STRINGL(new, newlen, 0); } - /* {{{ proto int strlen(string str) Get string length */ -void php3_strlen(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(strlen) { pval *str; - PLS_FETCH(); if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &str) == FAILURE) { WRONG_PARAM_COUNT; @@ -102,7 +104,7 @@ void php3_strlen(INTERNAL_FUNCTION_PARAMETERS) /* {{{ proto int strcmp(string str1, string str2) Binary safe string comparison */ -void php3_strcmp(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(strcmp) { pval *s1,*s2; @@ -117,7 +119,7 @@ void php3_strcmp(INTERNAL_FUNCTION_PARAMETERS) /* {{{ proto int strcasecmp(string str1, string str2) Binary safe case-insensitive string comparison */ -void php3_strcasecmp(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(strcasecmp) { pval *s1,*s2; @@ -126,13 +128,13 @@ void php3_strcasecmp(INTERNAL_FUNCTION_PARAMETERS) } convert_to_string(s1); convert_to_string(s2); - RETURN_LONG(zend_binary_strcasecmp(s1,s2)); + RETURN_LONG(strcasecmp(s1->value.str.val,s2->value.str.val)); } /* }}} */ /* {{{ proto int strspn(string str, string mask) Find length of initial segment consisting entirely of characters found in mask */ -void php3_strspn(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(strspn) { pval *s1,*s2; @@ -147,7 +149,7 @@ void php3_strspn(INTERNAL_FUNCTION_PARAMETERS) /* {{{ proto int strcspn(string str, string mask) Find length of initial segment consisting entirely of characters not found in mask */ -void php3_strcspn(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(strcspn) { pval *s1,*s2; @@ -160,17 +162,12 @@ void php3_strcspn(INTERNAL_FUNCTION_PARAMETERS) } /* }}} */ -/* {{{ proto string rtrim(string str) - An alias for chop */ -/* }}} */ - /* {{{ proto string chop(string str) Remove trailing whitespace */ -void php3_chop(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(chop) { pval *str; register int i; - PLS_FETCH(); if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &str) == FAILURE) { WRONG_PARAM_COUNT; @@ -195,41 +192,12 @@ void php3_chop(INTERNAL_FUNCTION_PARAMETERS) } /* }}} */ -PHPAPI void _php3_trim(pval *str, pval * return_value) -{ - register int i; - - int len = str->value.str.len; - int trimmed = 0; - char *c = str->value.str.val; - for (i = 0; i < len; i++) { - if (c[i] == ' ' || c[i] == '\n' || c[i] == '\r' || - c[i] == '\t' || c[i] == '\v') { - trimmed++; - } else { - break; - } - } - len-=trimmed; - c+=trimmed; - for (i = len - 1; i >= 0; i--) { - if (c[i] == ' ' || c[i] == '\n' || c[i] == '\r' || - c[i] == '\t' || c[i] == '\v') { - len--; - } else { - break; - } - } - RETVAL_STRINGL(c, len, 1); -} - /* {{{ proto string trim(string str) Strip whitespace from the beginning and end of a string */ -void php3_trim(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(trim) { pval *str; register int i; - PLS_FETCH(); if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &str) == FAILURE) { WRONG_PARAM_COUNT; @@ -267,28 +235,57 @@ void php3_trim(INTERNAL_FUNCTION_PARAMETERS) /* {{{ proto string ltrim(string str) Strip whitespace from the beginning of a string */ -void php3_ltrim(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(ltrim) { pval *str; - PLS_FETCH(); + register int i; if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &str) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string(str); + if (str->type == IS_STRING) { - _php3_trim(str, return_value); + int len = str->value.str.len; + int trimmed = 0; + char *c = str->value.str.val; + for (i = 0; i < len; i++) { + if (c[i] == ' ' || c[i] == '\n' || c[i] == '\r' || + c[i] == '\t' || c[i] == '\v') { + trimmed++; + } else { + break; + } + } + RETVAL_STRINGL(c+trimmed, len-trimmed, 1); return; } RETURN_FALSE; } /* }}} */ -void _php3_explode(pval *delim, pval *str, pval *return_value) +/* {{{ proto array(string separator, string str) + Split a string on string separator and return array of components */ +PHP_FUNCTION(explode) { + pval *str, *delim; char *work_str, *p1, *p2; int i = 0; + if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &delim, &str) == FAILURE) { + WRONG_PARAM_COUNT; + } + convert_to_string(str); + convert_to_string(delim); + + if (strlen(delim->value.str.val)==0) { + /* the delimiter must be a valid C string that's at least 1 character long */ + php3_error(E_WARNING,"Empty delimiter"); + RETURN_FALSE; + } + if (array_init(return_value) == FAILURE) { + return; + } work_str = p1 = estrndup(str->value.str.val,str->value.str.len); p2 = strstr(p1, delim->value.str.val); if (p2 == NULL) { @@ -303,47 +300,38 @@ void _php3_explode(pval *delim, pval *str, pval *return_value) } efree(work_str); } +/* }}} */ -/* {{{ proto array explode(string separator, string str) - Split a string on string separator and return array of components */ -void php3_explode(INTERNAL_FUNCTION_PARAMETERS) +/* {{{ proto string implode(array src, string glue) + Join array elements placing glue string between items and return one string */ +PHP_FUNCTION(implode) { - pval *str, *delim; - - if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &delim, &str) == FAILURE) { + pval *arg1, *arg2, *delim, **tmp, *arr; + int len = 0, count = 0; + + if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &arg1, &arg2) == FAILURE) { WRONG_PARAM_COUNT; } - convert_to_string(str); - convert_to_string(delim); - - if (strlen(delim->value.str.val)==0) { - /* the delimiter must be a valid C string that's at least 1 character long */ - php3_error(E_WARNING,"Empty delimiter"); - RETURN_FALSE; - } - if (array_init(return_value) == FAILURE) { + if (arg1->type == IS_ARRAY && arg2->type == IS_STRING) { + arr = arg1; + delim = arg2; + } else if (arg2->type == IS_ARRAY) { + convert_to_string(arg1); + arr = arg2; + delim = arg1; + } else { + php3_error(E_WARNING, "Bad arguments to %s()", + get_active_function_name()); return; } - _php3_explode(delim, str, return_value); -} -/* }}} */ - -/* {{{ proto string join(array src, string glue) - An alias for implode */ -/* }}} */ -void _php3_implode(pval *delim, pval *arr, pval *return_value) -{ - pval *tmp; - int len = 0, count = 0; - PLS_FETCH(); /* convert everything to strings, and calculate length */ _php3_hash_internal_pointer_reset(arr->value.ht); while (_php3_hash_get_current_data(arr->value.ht, (void **) &tmp) == SUCCESS) { - convert_to_string(tmp); - if (tmp->type == IS_STRING && tmp->value.str.val != undefined_variable_string) { - len += tmp->value.str.len; + convert_to_string(*tmp); + if ((*tmp)->type == IS_STRING) { + len += (*tmp)->value.str.len; if (count>0) { len += delim->value.str.len; } @@ -358,9 +346,9 @@ void _php3_implode(pval *delim, pval *arr, pval *return_value) return_value->value.str.val[len] = '\0'; _php3_hash_internal_pointer_reset(arr->value.ht); while (_php3_hash_get_current_data(arr->value.ht, (void **) &tmp) == SUCCESS) { - if (tmp->type == IS_STRING && tmp->value.str.val != undefined_variable_string) { + if ((*tmp)->type == IS_STRING) { count--; - strcat(return_value->value.str.val, tmp->value.str.val); + strcat(return_value->value.str.val, (*tmp)->value.str.val); if (count > 0) { strcat(return_value->value.str.val, delim->value.str.val); } @@ -368,44 +356,19 @@ void _php3_implode(pval *delim, pval *arr, pval *return_value) _php3_hash_move_forward(arr->value.ht); } return_value->type = IS_STRING; + return_value->refcount = 1; + return_value->is_ref = 0; return_value->value.str.len = len; } - - -/* {{{ proto string implode(array src, string glue) - Join array elements placing glue string between items and return one string */ -void php3_implode(INTERNAL_FUNCTION_PARAMETERS) -{ - pval *arg1, *arg2, *delim, *arr; - - if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &arg1, &arg2) == FAILURE) { - WRONG_PARAM_COUNT; - } - - if (arg1->type == IS_ARRAY && arg2->type == IS_STRING) { - arr = arg1; - delim = arg2; - } else if (arg2->type == IS_ARRAY) { - convert_to_string(arg1); - arr = arg2; - delim = arg1; - } else { - php3_error(E_WARNING, "Bad arguments to %s()", - get_active_function_name()); - return; - } - _php3_implode(delim, arr, return_value) ; -} /* }}} */ - #ifndef THREAD_SAFE char *strtok_string; #endif /* {{{ proto string strtok([string str,] string token) Tokenize a string */ -void php3_strtok(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(strtok) { pval *str, *tok; #ifndef THREAD_SAFE @@ -415,7 +378,6 @@ void php3_strtok(INTERNAL_FUNCTION_PARAMETERS) char *token = NULL, *tokp=NULL; char *first = NULL; int argc; - PLS_FETCH(); argc = ARG_COUNT(ht); @@ -447,7 +409,7 @@ void php3_strtok(INTERNAL_FUNCTION_PARAMETERS) if (strtok_pos2) { *strtok_pos2 = '\0'; } - RETVAL_STRING(strtok_pos1, 1); + RETVAL_STRING(strtok_pos1,1); #if 0 /* skip 'token' white space for next call to strtok */ while (strtok_pos2 && @@ -480,11 +442,10 @@ PHPAPI char *_php3_strtoupper(char *s) /* {{{ proto string strtoupper(string str) Make a string uppercase */ -void php3_strtoupper(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(strtoupper) { pval *arg; char *ret; - PLS_FETCH(); if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg)) { WRONG_PARAM_COUNT; @@ -512,11 +473,10 @@ PHPAPI char *_php3_strtolower(char *s) /* {{{ proto string strtolower(string str) Make a string lowercase */ -void php3_strtolower(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(strtolower) { pval *str; char *ret; - PLS_FETCH(); if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &str)) { WRONG_PARAM_COUNT; @@ -530,11 +490,10 @@ void php3_strtolower(INTERNAL_FUNCTION_PARAMETERS) /* {{{ proto string basename(string path) Return the filename component of the path */ -void php3_basename(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(basename) { pval *str; char *ret, *c; - PLS_FETCH(); if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &str)) { WRONG_PARAM_COUNT; @@ -583,11 +542,10 @@ PHPAPI void _php3_dirname(char *str, int len) { /* {{{ proto string dirname(string path) Return the directory name component of the path */ -void php3_dirname(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(dirname) { pval *str; char *ret; - PLS_FETCH(); if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &str)) { WRONG_PARAM_COUNT; @@ -601,7 +559,7 @@ void php3_dirname(INTERNAL_FUNCTION_PARAMETERS) /* }}} */ -/* case insensitive strstr */ +/* case-insensitve strstr */ PHPAPI char *php3i_stristr(unsigned char *s, unsigned char *t) { int i, j, k, l; @@ -616,13 +574,12 @@ PHPAPI char *php3i_stristr(unsigned char *s, unsigned char *t) return NULL; } -/* {{{ proto string stristr(string haystack, string needle) +/* {{{ proto string strstr(string haystack, string needle) Find first occurrence of a string within another, case insensitive */ -void php3_stristr(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(stristr) { pval *haystack, *needle; char *found = NULL; - PLS_FETCH(); if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &haystack, &needle) == FAILURE) { @@ -645,17 +602,12 @@ void php3_stristr(INTERNAL_FUNCTION_PARAMETERS) } /* }}} */ -/* {{{ proto string strchr(string haystack, string needle) - An alias for strstr */ -/* }}} */ - /* {{{ proto string strstr(string haystack, string needle) Find first occurrence of a string within another */ -void php3_strstr(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(strstr) { pval *haystack, *needle; char *found = NULL; - PLS_FETCH(); if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &haystack, &needle) == FAILURE) { @@ -683,14 +635,13 @@ void php3_strstr(INTERNAL_FUNCTION_PARAMETERS) } /* }}} */ -/* {{{ proto int strpos(string haystack, string needle [, int offset]) +/* {{{ proto int strpos(string haystack, string needle) Find position of first occurrence of a string within another */ -void php3_strpos(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(strpos) { pval *haystack, *needle, *OFFSET; int offset = 0; char *found = NULL; - PLS_FETCH(); switch(ARG_COUNT(ht)) { case 2: @@ -735,11 +686,10 @@ void php3_strpos(INTERNAL_FUNCTION_PARAMETERS) /* {{{ proto int strrpos(string haystack, string needle) Find the last occurrence of a character in a string within another */ -void php3_strrpos(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(strrpos) { pval *haystack, *needle; char *found = NULL; - PLS_FETCH(); if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &haystack, &needle) == FAILURE) { WRONG_PARAM_COUNT; @@ -763,11 +713,10 @@ void php3_strrpos(INTERNAL_FUNCTION_PARAMETERS) /* {{{ proto string strrchr(string haystack, string needle) Find the last occurrence of a character in a string within another */ -void php3_strrchr(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(strrchr) { pval *haystack, *needle; char *found = NULL; - PLS_FETCH(); if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &haystack, &needle) == FAILURE) { @@ -793,8 +742,7 @@ void php3_strrchr(INTERNAL_FUNCTION_PARAMETERS) /* }}} */ static char * -_php3_chunk_split(char *src, int srclen, char *end, int endlen, - int chunklen, int *destlen) +_php3_chunk_split(char *src, int srclen, char *end, int endlen, int chunklen) { char *dest; char *p, *q; @@ -822,14 +770,13 @@ _php3_chunk_split(char *src, int srclen, char *end, int endlen, } *q = '\0'; - if(destlen) *destlen = q - dest; return(dest); } /* {{{ proto string chunk_split(string str [, int chunklen [, string ending]]) Return split line */ -void php3_chunk_split(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(chunk_split) { pval *p_str, *p_chunklen, *p_ending; int argc; @@ -837,13 +784,13 @@ void php3_chunk_split(INTERNAL_FUNCTION_PARAMETERS) char *end = "\r\n"; int endlen = 2; int chunklen = 76; - int result_len; - PLS_FETCH(); argc = ARG_COUNT(ht); - - if(argc < 1 || argc > 3 || getParameters(ht, argc, &p_str, - &p_chunklen, &p_ending) == FAILURE) { + + if(!((argc == 1 && getParameters(ht, 1, &p_str) != FAILURE) || + (argc == 2 && getParameters(ht, 2, &p_str, &p_chunklen) != FAILURE) || + (argc == 3 && getParameters(ht, 3, &p_str, &p_chunklen, + &p_ending) != FAILURE))) { WRONG_PARAM_COUNT; } @@ -865,10 +812,10 @@ void php3_chunk_split(INTERNAL_FUNCTION_PARAMETERS) } result = _php3_chunk_split(p_str->value.str.val, p_str->value.str.len, - end, endlen, chunklen, &result_len); + end, endlen, chunklen); if(result) { - RETVAL_STRINGL(result, result_len, 0); + RETVAL_STRING(result, 0); } else { RETURN_FALSE; } @@ -877,12 +824,11 @@ void php3_chunk_split(INTERNAL_FUNCTION_PARAMETERS) /* {{{ proto string substr(string str, int start [, int length]) Return part of a string */ -void php3_substr(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(substr) { pval *string, *from, *len; int argc, l; int f; - PLS_FETCH(); argc = ARG_COUNT(ht); @@ -926,23 +872,22 @@ void php3_substr(INTERNAL_FUNCTION_PARAMETERS) RETURN_FALSE; } - /* Adjust l to match the actual string segment to be returned */ if((f+l) > (int)string->value.str.len) { l = (int)string->value.str.len - f; } - RETVAL_STRINGL(string->value.str.val + f,l,1); + + RETVAL_STRINGL(string->value.str.val + f, l, 1); } /* }}} */ /* {{{ proto string quotemeta(string str) Quote meta characters */ -void php3_quotemeta(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(quotemeta) { pval *arg; char *str, *old; char *p, *q; char c; - PLS_FETCH(); if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg) == FAILURE) { WRONG_PARAM_COUNT; @@ -983,10 +928,9 @@ void php3_quotemeta(INTERNAL_FUNCTION_PARAMETERS) /* {{{ proto int ord(string character) Return ASCII value of character */ -void php3_ord(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(ord) { pval *str; - PLS_FETCH(); if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &str) == FAILURE) { WRONG_PARAM_COUNT; @@ -998,11 +942,10 @@ void php3_ord(INTERNAL_FUNCTION_PARAMETERS) /* {{{ proto string chr(int ascii) Convert ASCII code to a character */ -void php3_chr(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(chr) { pval *num; char temp[2]; - PLS_FETCH(); if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &num) == FAILURE) { WRONG_PARAM_COUNT; @@ -1014,9 +957,9 @@ void php3_chr(INTERNAL_FUNCTION_PARAMETERS) } /* }}} */ -/* {{{ proto string ucfirst(string str) +/* {{{ proto string(string str) Make a string's first character uppercase */ -void php3_ucfirst(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(ucfirst) { pval *arg; @@ -1035,7 +978,7 @@ void php3_ucfirst(INTERNAL_FUNCTION_PARAMETERS) /* {{{ proto string ucwords(string str) Uppercase the first character of every word in a string */ -void php3_ucwords(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(ucwords) { pval *arg; char *r; @@ -1060,33 +1003,14 @@ void php3_ucwords(INTERNAL_FUNCTION_PARAMETERS) } /* }}} */ -PHPAPI char *_php3_strtr(char *string, int len, char *str_from, char *str_to, int trlen) -{ - int i; - unsigned char xlat[256]; - - if ((trlen < 1) || (len < 1)) - return string; - - for (i = 0; i < 256; xlat[i] = i, i++); - - for (i = 0; i < trlen; i++) { - xlat[(unsigned char) str_from[i]] = str_to[i]; - } - - for (i = 0; i < len; i++) { - string[i] = xlat[(unsigned char) string[i]]; - } - - return string; -} - /* {{{ proto string strtr(string str, string from, string to) Translate characters in str using given translation tables */ -void php3_strtr(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(strtr) { /* strtr(STRING,FROM,TO) */ pval *str, *from, *to; - PLS_FETCH(); + unsigned char xlat[256]; + unsigned char *str_from, *str_to, *string; + int i, len1, len2; if (ARG_COUNT(ht) != 3 || getParameters(ht, 3, &str, &from, &to) == FAILURE) { @@ -1096,19 +1020,35 @@ void php3_strtr(INTERNAL_FUNCTION_PARAMETERS) convert_to_string(from); convert_to_string(to); - RETVAL_STRING(_php3_strtr(str->value.str.val, - str->value.str.len, - from->value.str.val, - to->value.str.val, - MIN(from->value.str.len,to->value.str.len)), - 1); + string = (unsigned char*) str->value.str.val; + str_from = (unsigned char*) from->value.str.val; + str_to = (unsigned char*) to->value.str.val; + + len1 = from->value.str.len; + len2 = to->value.str.len; + + if (len1 > len2) { + str_from[len2] = '\0'; + len1 = len2; + } + for (i = 0; i < 256; xlat[i] = i, i++); + + for (i = 0; i < len1; i++) { + xlat[(unsigned char) str_from[i]] = str_to[i]; + } + + for (i = 0; i < str->value.str.len; i++) { + string[i] = xlat[(unsigned char) string[i]]; + } + + RETVAL_STRING((char *)string,1); } /* }}} */ /* {{{ proto string strrev(string str) Reverse a string */ -void php3_strrev(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(strrev) { pval *str; int i,len; @@ -1133,87 +1073,14 @@ void php3_strrev(INTERNAL_FUNCTION_PARAMETERS) } /* }}} */ -static void _php3_similar_str(const char *txt1, int len1, const char *txt2, - int len2, int *pos1, int *pos2, int *max) -{ - char *p, *q; - char *end1 = (char *) txt1 + len1; - char *end2 = (char *) txt2 + len2; - int l; - - *max = 0; - for (p = (char *) txt1; p < end1; p++) { - for (q = (char *) txt2; q < end2; q++) { - for (l = 0; (p + l < end1) && (q + l < end2) && (p[l] == q[l]); - l++); - if (l > *max) { - *max = l; - *pos1 = p - txt1; - *pos2 = q - txt2; - } - } - } -} - -static int _php3_similar_char(const char *txt1, int len1, - const char *txt2, int len2) -{ - int sum; - int pos1, pos2, max; - - _php3_similar_str(txt1, len1, txt2, len2, &pos1, &pos2, &max); - if ((sum = max)) { - if (pos1 && pos2) - sum += _php3_similar_char(txt1, pos1, txt2, pos2); - if ((pos1 + max < len1) && (pos2 + max < len2)) - sum += _php3_similar_char(txt1 + pos1 + max, len1 - pos1 - max, - txt2 + pos2 + max, len2 - pos2 -max); - } - return sum; -} -/* {{{ proto int similar_text(string str1, string str2 [, double percent]) - Calculates the similarity between two strings */ -void php3_similar_text(INTERNAL_FUNCTION_PARAMETERS) -{ - pval *t1, *t2, *percent; - int ac = ARG_COUNT(ht); - int sim; - - if (ac < 2 || ac > 3 || - getParameters(ht, ac, &t1, &t2, &percent) == FAILURE) { - WRONG_PARAM_COUNT; - } - - convert_to_string(t1); - convert_to_string(t2); - if(ac > 2) { - convert_to_double(percent); - } - - if((t1->value.str.len + t2->value.str.len) == 0) { - if(ac > 2) - percent->value.dval = 0; - RETURN_LONG(0); - } - - sim = _php3_similar_char(t1->value.str.val, t1->value.str.len, - t2->value.str.val, t2->value.str.len); - - if (ac > 2) { - percent->value.dval = sim * 200.0 / (t1->value.str.len + t2->value.str.len); - } - - RETURN_LONG(sim); -} -/* }}} */ - /* be careful, this edits the string in-place */ PHPAPI void _php3_stripslashes(char *string, int *len) { char *s, *t; int l; char escape_char='\\'; + PLS_FETCH(); if (PG(magic_quotes_sybase)) { escape_char='\''; @@ -1258,7 +1125,7 @@ PHPAPI void _php3_stripslashes(char *string, int *len) /* {{{ proto string addslashes(string str) Escape single quote, double quotes and backslash characters in a string with backslashes */ -void php3_addslashes(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(addslashes) { pval *str; @@ -1273,10 +1140,9 @@ void php3_addslashes(INTERNAL_FUNCTION_PARAMETERS) /* {{{ proto string stripslashes(string str) Strip backslashes from a string */ -void php3_stripslashes(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(stripslashes) { pval *str; - PLS_FETCH(); if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &str) == FAILURE) { WRONG_PARAM_COUNT; @@ -1314,6 +1180,7 @@ PHPAPI char *_php3_addslashes(char *str, int length, int *new_length, int should char *source,*target; char *end; char c; + PLS_FETCH(); for (source=str,end=source+length,target=new_str; (c = *source) || source<end; source++) { switch(c) { @@ -1340,7 +1207,9 @@ PHPAPI char *_php3_addslashes(char *str, int length, int *new_length, int should } } *target = 0; - if(new_length) *new_length = target - new_str; + if (new_length) { + *new_length = target - new_str; + } if (should_free) { STR_FREE(str); } @@ -1354,7 +1223,7 @@ PHPAPI char *_php3_addslashes(char *str, int length, int *new_length, int should #define _isblank(c) (((((unsigned char) c)==' ' || ((unsigned char) c)=='\t')) ? 1 : 0) #define _isnewline(c) (((((unsigned char) c)=='\n' || ((unsigned char) c)=='\r')) ? 1 : 0) -PHPAPI void _php3_char_to_str(char *str,uint len,char from,char *to,int to_len,pval *result) +static void _php3_char_to_str(char *str,uint len,char from,char *to,int to_len,pval *result) { int char_count=0; char *source,*target,*tmp,*source_end=str+len, *tmp_end=NULL; @@ -1390,7 +1259,6 @@ PHPAPI void _php3_char_to_str(char *str,uint len,char from,char *to,int to_len,p *target = 0; } -#if 1 /* * this is a binary safe equivalent to strnstr * note that we don't check for the end in str_to_str but here @@ -1410,12 +1278,6 @@ _php3_memnstr(char *haystack, char *needle, int needle_len, char *end) return NULL; } -/* - * because of efficiency we use malloc/realloc/free here - * erealloc _will_ move your data around - it took me some time - * to find out ... Sascha Schumann <sas@schell.de> 981220 - */ - static char *_php3_str_to_str(char *haystack, int length, char *needle, int needle_len, char *str, int str_len, int *_new_length) { @@ -1466,79 +1328,22 @@ finish: return new; } -#else - -static char *_php3_memstr(char *s, char *c, size_t n, size_t m) -{ - char *p; - - for(p = s; (p - s) < n; p++) - if(memcmp(p, c, m) == 0) - return p; - return NULL; -} - -#define ATTCHSTR(st, sz) \ - nl += sz; \ - n = erealloc(n, nl + 1); \ - memcpy(n + no, st, sz); \ - no += sz - - -static char *_php3_str_to_str(char *a, int al, char *b, int bl, char *c, int cl, - int *newlen) -{ - char *n = NULL, *p, *q; - int nl = 0; - int no = 0; - - /* run through all occurences of b in a */ - for(p = q = a; (p = _php3_memstr(p, b, al - (p - a), bl)); q = p) { - /* attach everything between the previous occ. and this one */ - ATTCHSTR(q, p - q); - /* attach the replacement string c */ - ATTCHSTR(c, cl); - /* jump over string b in a */ - p += bl; - } - - /* anything left over ? */ - if((al - (q - a)) > 0) { - ATTCHSTR(q, al - (q - a)); - } - - if(newlen) *newlen = nl; - n[nl] = '\0'; - - return n; -} - -#undef ATTCHSTR -#endif - /* {{{ proto string str_replace(string needle, string str, string haystack) Replace all occurrences of needle in haystack with str */ -void php3_str_replace(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(str_replace) { pval *haystack, *needle, *str; char *new; int len = 0; - if(ARG_COUNT(ht) != 3 || + if(ARG_COUNT(ht) != 3 || getParameters(ht, 3, &needle, &str, &haystack) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string(haystack); convert_to_string(needle); - convert_to_string(str); - - if(haystack->value.str.len == 0) { - RETURN_STRING(empty_string,1); - } - - if(needle->value.str.len == 1) { - _php3_char_to_str(haystack->value.str.val,haystack->value.str.len,needle->value.str.val[0],str->value.str.val, str->value.str.len ,return_value); + convert_to_string(str); if(needle->value.str.len == 1) { _php3_char_to_str(haystack->value.str.val,haystack->value.str.len,needle->value.str.val[0],str->value.str.val, str->value.str.len ,return_value); return; } @@ -1547,10 +1352,10 @@ void php3_str_replace(INTERNAL_FUNCTION_PARAMETERS) RETURN_FALSE; } - new = _php3_str_to_str(haystack->value.str.val, haystack->value.str.len, - needle->value.str.val, needle->value.str.len, - str->value.str.val, str->value.str.len, - &len); + new = _php3_str_to_str(haystack->value.str.val, haystack->value.str.len, + needle->value.str.val, needle->value.str.len, + str->value.str.val, str->value.str.len, + &len); RETURN_STRINGL(new, len, 0); } /* }}} */ @@ -1724,15 +1529,15 @@ static void _php3_hebrev(INTERNAL_FUNCTION_PARAMETERS,int convert_newlines) /* {{{ proto string hebrev(string str [, int max_chars_per_line]) Convert logical Hebrew text to visual text */ -void php3_hebrev(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(hebrev) { _php3_hebrev(INTERNAL_FUNCTION_PARAM_PASSTHRU,0); } /* }}} */ -/* {{{ proto string hebrevc(string str [, int max_chars_per_line]) +/* {{{ proto string hebrev(string str [, int max_chars_per_line]) Convert logical Hebrew text to visual text with newline conversion */ -void php3_hebrev_with_conversion(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(hebrev_with_conversion) { _php3_hebrev(INTERNAL_FUNCTION_PARAM_PASSTHRU,1); } @@ -1740,7 +1545,7 @@ void php3_hebrev_with_conversion(INTERNAL_FUNCTION_PARAMETERS) /* {{{ proto string nl2br(string str) Converts newlines to HTML line breaks */ -void php3_newline_to_br(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(newline_to_br) { pval *str; @@ -1754,27 +1559,9 @@ void php3_newline_to_br(INTERNAL_FUNCTION_PARAMETERS) } /* }}} */ -/* {{{ proto string strip_tags(string str) - Strips HTML and PHP tags from a string */ -void php3_strip_tags(INTERNAL_FUNCTION_PARAMETERS) -{ - char *buf; - pval *str; - - if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &str)==FAILURE) { - WRONG_PARAM_COUNT; - } - convert_to_string(str); - buf=estrdup(str->value.str.val); - _php3_strip_tags(buf,0); - RETURN_STRING(buf,0); -} -/* }}} */ - - /* {{{ proto string setlocale(string category, string locale) Set locale information */ -void php3_setlocale(INTERNAL_FUNCTION_PARAMETERS) +PHP_FUNCTION(setlocale) { pval *category, *locale; int cat; @@ -1815,102 +1602,6 @@ void php3_setlocale(INTERNAL_FUNCTION_PARAMETERS) } /* }}} */ -/* A simple little state-machine to strip out html and php tags - - State 0 is the output state, State 1 means we are inside a - normal html tag and state 2 means we are inside a php tag. - - The state variable is passed in to allow a function like fgetss - to maintain state across calls to the function. - - lc holds the last significant character read and br is a bracket - counter. -*/ -void _php3_strip_tags(char *rbuf, int state) { - char *buf, *p, *rp, c, lc; - int br; - - buf = estrdup(rbuf); - c = *buf; - lc = '\0'; - p = buf; - rp = rbuf; - br = 0; - - while (c) { /* This is not binary-safe. Don't see why it should be */ - switch (c) { - case '<': - if (state == 0) { - lc = '<'; - state = 1; - } - break; - - case '(': - if (state == 2) { - if (lc != '\"') { - lc = '('; - br++; - } - } else if (state == 0) { - *(rp++) = c; - } - break; - - case ')': - if (state == 2) { - if (lc != '\"') { - lc = ')'; - br--; - } - } else if (state == 0) { - *(rp++) = c; - } - break; - - case '>': - if (state == 1) { - lc = '>'; - state = 0; - } else if (state == 2) { - if (!br && lc != '\"' && *(p-1)=='?') { - state = 0; - } - } - break; - - case '\"': - if (state == 2) { - if (lc == '\"') { - lc = '\0'; - } else if (lc != '\\') { - lc = '\"'; - } - } else if (state == 0) { - *(rp++) = c; - } - break; - - case '?': - if (state==1 && *(p-1)=='<') { - br=0; - state=2; - break; - } - /* fall-through */ - - default: - if (state == 0) { - *(rp++) = c; - } - break; - } - c = *(++p); - } - *rp = '\0'; - efree(buf); -} - /* {{{ proto void parsestr(string encoded_string) Parses GET/POST/COOKIE data and sets global variables. */ PHP_FUNCTION(parsestr) @@ -1927,7 +1618,6 @@ PHP_FUNCTION(parsestr) } php3_treat_data(PARSE_STRING, res); } -/* }}} */ /* * Local variables: |