CVS update: php3/functions
| From: | sas | Date: | Sun, 20 Dec 1998 07:32:29 +0000 |
| Subject: | CVS update: php3/functions | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-2903@lists.php.net to get a copy of this message | ||
Date: Sunday December 20, 1998 @ 2:32
Author: sas
Update of /repository/php3/functions
In directory asf:/u/temp/cvs-serv8175/functions
Modified Files:
php3_string.h basic_functions.c string.c
Log Message:
added str_replace
improved quotemeta
updated changelog
changed documentation for chr_replace
Index: php3/functions/php3_string.h
diff -c php3/functions/php3_string.h:1.25 php3/functions/php3_string.h:1.26
*** php3/functions/php3_string.h:1.25 Sat Dec 19 15:44:12 1998
--- php3/functions/php3_string.h Sun Dec 20 02:32:28 1998
***************
*** 29,35 ****
*/
! /* $Id: php3_string.h,v 1.25 1998/12/19 20:44:12 sas Exp $ */
#ifndef _PHPSTRING_H
#define _PHPSTRING_H
--- 29,35 ----
*/
! /* $Id: php3_string.h,v 1.26 1998/12/20 07:32:28 sas Exp $ */
#ifndef _PHPSTRING_H
#define _PHPSTRING_H
***************
*** 43,48 ****
--- 43,49 ----
extern void php3_strspn(INTERNAL_FUNCTION_PARAMETERS);
extern void php3_strcspn(INTERNAL_FUNCTION_PARAMETERS);
extern void php3_strcasecmp(INTERNAL_FUNCTION_PARAMETERS);
+ extern void php3_str_replace(INTERNAL_FUNCTION_PARAMETERS);
extern void php3_chop(INTERNAL_FUNCTION_PARAMETERS);
extern void php3_trim(INTERNAL_FUNCTION_PARAMETERS);
extern void php3_ltrim(INTERNAL_FUNCTION_PARAMETERS);
Index: php3/functions/basic_functions.c
diff -c php3/functions/basic_functions.c:1.239 php3/functions/basic_functions.c:1.240
*** php3/functions/basic_functions.c:1.239 Sat Dec 19 15:44:11 1998
--- php3/functions/basic_functions.c Sun Dec 20 02:32:28 1998
***************
*** 141,146 ****
--- 141,147 ----
{"addslashes", php3_addslashes, NULL},
{"chop", php3_chop, NULL},
+ {"str_replace", php3_str_replace, NULL},
{"chunk_split", php3_chunk_split, NULL},
{"trim", php3_trim, NULL},
{"ltrim", php3_ltrim, NULL},
Index: php3/functions/string.c
diff -c php3/functions/string.c:1.152 php3/functions/string.c:1.153
*** php3/functions/string.c:1.152 Sat Dec 19 17:11:02 1998
--- php3/functions/string.c Sun Dec 20 02:32:29 1998
***************
*** 30,36 ****
*/
! /* $Id: string.c,v 1.152 1998/12/19 22:11:02 rasmus Exp $ */
#ifdef THREAD_SAFE
#include "tls.h"
#endif
--- 30,36 ----
*/
! /* $Id: string.c,v 1.153 1998/12/20 07:32:29 sas Exp $ */
#ifdef THREAD_SAFE
#include "tls.h"
#endif
***************
*** 855,862 ****
void php3_quotemeta(INTERNAL_FUNCTION_PARAMETERS)
{
pval *arg;
- register int x, y;
char *str, *old;
TLS_VARS;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg) == FAILURE) {
--- 855,863 ----
void php3_quotemeta(INTERNAL_FUNCTION_PARAMETERS)
{
pval *arg;
char *str, *old;
+ char *p, *q;
+ char c;
TLS_VARS;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg) == FAILURE) {
***************
*** 869,877 ****
if (!*old) {
RETURN_FALSE;
}
str = emalloc(2 * arg->value.str.len + 1);
! for (x = 0, y = 0; old[x]; x++, y++) {
! switch (old[x]) {
case '.':
case '\\':
case '+':
--- 870,880 ----
if (!*old) {
RETURN_FALSE;
}
+
str = emalloc(2 * arg->value.str.len + 1);
!
! for(p = old, q = str; (c = *p); p++) {
! switch(c) {
case '.':
case '\\':
case '+':
***************
*** 883,895 ****
case '$':
case '(':
case ')':
! str[y++] = '\\';
}
- str[y] = old[x];
}
! str[y] = '\0';
! RETVAL_STRING(str,1);
! efree(str);
}
/* }}} */
--- 886,899 ----
case '$':
case '(':
case ')':
! *q++ = '\\';
! /* break is missing _intentionally_ */
! default:
! *q++ = c;
}
}
! *q = 0;
! RETVAL_STRING(erealloc(str, q - str + 1), 0);
}
/* }}} */
***************
*** 1145,1173 ****
PHPAPI char *_php3_addslashes(char *str, int length, int *new_length, int should_free)
{
! char *new_str = (char *) emalloc((length?length:strlen(str))*2+1); /* maximum string length,
worst case situation */
! register char *source,*target;
char *end;
! if (new_length) {
! *new_length=0;
! }
! for (source=str,end=source+length,target=new_str; *source || source<end; source++) {
! switch(*source) {
case '\0':
*target++ = '\\';
*target++ = '0';
- if (new_length) {
- (*new_length) +=2;
- }
break;
case '\'':
if (php3_ini.magic_quotes_sybase) {
*target++ = '\'';
*target++ = '\'';
- if (new_length) {
- (*new_length) += 2;
- }
break;
}
/* break is missing *intentionally* */
--- 1149,1170 ----
PHPAPI char *_php3_addslashes(char *str, int length, int *new_length, int should_free)
{
! /* maximum string length, worst case situation */
! char *new_str = (char *) emalloc((length?length:strlen(str))*2+1);
! char *source,*target;
char *end;
+ char c;
! for (source=str,end=source+length,target=new_str; (c = *source) || source<end; source++) {
! switch(c) {
case '\0':
*target++ = '\\';
*target++ = '0';
break;
case '\'':
if (php3_ini.magic_quotes_sybase) {
*target++ = '\'';
*target++ = '\'';
break;
}
/* break is missing *intentionally* */
***************
*** 1175,1194 ****
case '\\':
if (!php3_ini.magic_quotes_sybase) {
*target++ = '\\';
- if (new_length) {
- (*new_length)++;
- }
}
/* break is missing *intentionally* */
default:
! *target++ = *source;
! if (new_length) {
! (*new_length)++;
! }
break;
}
}
*target = 0;
if (should_free) {
STR_FREE(str);
}
--- 1172,1186 ----
case '\\':
if (!php3_ini.magic_quotes_sybase) {
*target++ = '\\';
}
/* break is missing *intentionally* */
default:
! *target++ = c;
break;
}
}
*target = 0;
+ if(new_length) *new_length = target - new_str;
if (should_free) {
STR_FREE(str);
}
***************
*** 1238,1244 ****
--- 1230,1324 ----
*target = 0;
}
+ /*
+ * this is a binary safe equivalent to strnstr
+ * note that we don't check for the end in str_to_str but here
+ */
+
+ static inline char *
+ _php3_memnstr(void *haystack, char *needle, int needle_len, void *end)
+ {
+ void *p;
+ void *s;
+
+ for(p = haystack; p < end - needle_len + 1&&
+ (s = memchr(p, *needle, end - haystack)); p = s + 1) {
+ if(memcmp(s, needle, needle_len) == 0)
+ return s;
+ }
+ 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)
+ {
+ char *p, *q;
+ char *r, *s;
+ char *end = haystack + length;
+ char *new;
+
+ new = malloc(length);
+ /* we jump through haystack searching for the needle. hurray! */
+ for(p = haystack, q = new;
+ (r = _php3_memnstr((void*) p, needle, needle_len, (void*) end));) {
+ /* this ain't optimal. you could call it `efficient memory usage' */
+ realloc(new, (q - new) + (r - p) + (str_len) + 1);
+ memcpy(q, p, r - p);
+ q += r - p;
+ memcpy(q, str, str_len);
+ q += str_len;
+ p = r + needle_len;
+ }
+ /* if there is a rest, copy it */
+ if((end - p)) {
+ s = (q) + (end - p);
+ new = realloc(new, s - new + 1);
+ memcpy(q, p, end - p);
+ q = s;
+ }
+ *q = '\0';
+ if(_new_length) *_new_length = q - new;
+ return new;
+ }
+
+ /* {{{ proto string str_replace(string needle, string str, string haystack)
+ Replaces all occurences of needle in haystack with str */
+ void php3_str_replace(INTERNAL_FUNCTION_PARAMETERS)
+ {
+ pval *haystack, *needle, *str;
+ char *new;
+
+ 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(needle->value.str.len == 1 && isalnum(*(needle->value.str.val))) {
+ /* argh, chr_replace isn't binary safe */
+ return php3_chr_replace(INTERNAL_FUNCTION_PARAM_PASSTHRU);
+ }
+
+ 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,
+ &return_value->value.str.len);
+ return_value->value.str.val = emalloc(return_value->value.str.len + 1);
+ memcpy(return_value->value.str.val, new,
+ return_value->value.str.len + 1);
+ free(new);
+ return_value->type = IS_STRING;
+ }
+ /* }}} */
/* Converts Logical Hebrew text (Hebrew Windows style) to Visual text
* Cheers/complaints/flames - Zeev Suraski <zeev@php.net>
--
PHP Development Mailing List http://www.php.net/
To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net
For help: php-dev-help@lists.php.net