cvs: php4 /ext/standard string.c
| From: | Sascha Schumann | Date: | Thu, 12 Oct 2000 00:50:28 +0000 |
| Subject: | cvs: php4 /ext/standard string.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-2025@lists.php.net to get a copy of this message | ||
sas Wed Oct 11 17:50:28 2000 EDT
Modified files:
/php4/ext/standard string.c
Log:
Rewrite php_str_to_str using the smart_str API.
This removes much complexity from the function.
Speed is improved by pre-allocating memory instead of reallocating
the buffer each time the pattern is found.
Index: php4/ext/standard/string.c
diff -u php4/ext/standard/string.c:1.161 php4/ext/standard/string.c:1.162
--- php4/ext/standard/string.c:1.161 Tue Oct 3 09:47:25 2000
+++ php4/ext/standard/string.c Wed Oct 11 17:50:28 2000
@@ -18,7 +18,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: string.c,v 1.161 2000/10/03 16:47:25 andi Exp $ */
+/* $Id: string.c,v 1.162 2000/10/12 00:50:28 sas Exp $ */
/* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */
@@ -2048,55 +2048,31 @@
*target = 0;
}
+#include "php_smart_str.h"
PHPAPI char *php_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 *p;
+ char *r;
char *end = haystack + length;
- char *result;
- char *off;
-
- result = emalloc(length);
- /* we jump through haystack searching for the needle. hurray! */
- for(p = haystack, q = result;
- (r = php_memnstr(p, needle, needle_len, end));) {
- /* this ain't optimal. you could call it `efficient memory usage' */
- off = erealloc(result, (q - result) + (r - p) + (str_len) + 1);
- if(off != result) {
- if(!off) {
- goto finish;
- }
- q += off - result;
- result = off;
- }
- 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) > 0) {
- s = (q) + (end - p);
- off = erealloc(result, s - result + 1);
- if(off != result) {
- if(!off) {
- goto finish;
- }
- q += off - result;
- result = off;
- s = q + (end - p);
- }
- memcpy(q, p, end - p);
- q = s;
+ smart_str result = {0};
+
+ for (p = haystack;
+ (r = php_memnstr(p, needle, needle_len, end));
+ p = r + needle_len) {
+ smart_str_appendl(&result, p, r - p);
+ smart_str_appendl(&result, str, str_len);
}
-finish:
- *q = '\0';
- if(_new_length) *_new_length = q - result;
- return result;
+
+ if (p < end)
+ smart_str_appendl(&result, p, end - p);
+
+ smart_str_0(&result);
+
+ if (_new_length) *_new_length = result.len;
+
+ return result.c;
}