--------------------- PatchSet 10280 Date: 2007/12/19 07:56:22 Author: adri Branch: s27_adri Tag: (none) Log: Rewrite a number of String operators to not require NUL-terminted strings; stop NUL-terminting strings. Strings are now "A" terminated. :) Members: libbuf/String.c:1.1.2.14->1.1.2.15 libbuf/String.h:1.1.2.18->1.1.2.19 libbuf/buf.c:1.1.2.5->1.1.2.6 Index: squid/libbuf/String.c =================================================================== RCS file: /cvsroot/squid-sf//squid/libbuf/Attic/String.c,v retrieving revision 1.1.2.14 retrieving revision 1.1.2.15 diff -u -r1.1.2.14 -r1.1.2.15 --- squid/libbuf/String.c 19 Dec 2007 03:58:29 -0000 1.1.2.14 +++ squid/libbuf/String.c 19 Dec 2007 07:56:22 -0000 1.1.2.15 @@ -1,6 +1,6 @@ /* - * $Id: String.c,v 1.1.2.14 2007/12/19 03:58:29 adri Exp $ + * $Id: String.c,v 1.1.2.15 2007/12/19 07:56:22 adri Exp $ * * DEBUG: section 67 String * AUTHOR: Duane Wessels @@ -218,3 +218,160 @@ stringLimitInit(&n, strBuf(s) + start, len); return n; } + +char * +strCDupOffset(String s, int offset) +{ + char *a = xmalloc(strLen2(s) - offset + 1); + memcpy(a, strBuf2(s), strLen2(s) - offset); + a[strLen2(s) - offset] = '\0'; + return a; +} + +int +strChr(String s, char ch) +{ + int i; + for (i = 0; i < strLen2(s); i++) { + if (strGetPos(s, i) == ch) + return i; + } + return -1; +} + +int +strRChr(String s, char ch) +{ + int i; + for (i = strLen2(s) - 1; i >= 0; i++) { + if (strGetPos(s, i) == ch) + return i; + } + return -1; +} + +void +strSet(String s, int offset, char ch) +{ + int a; + + assert(offset < s.len); + stringMakePrivate(&s); + a = buf_put_chr(s.nb, offset, ch); + assert(a); +} + +/* XXX this breaks layering! */ +void +strCut(String s, int pos) +{ + int a; + + stringMakePrivate(&s); + /* XXX this is breaking buf_t layering; please revisit! */ + s.len = s.nb->len = pos; + a = buf_put_chr(s.nb, pos, '\0'); + assert(a); +} + +int +strMatch(const char *search, String m, int maxlen) +{ + int mlen = strLen2(m); + if (maxlen < mlen) + return -1; + return strncmp(search, strBuf2(m), maxlen); +} + + +int +strCmpNull(String a, const char *b) +{ + if (strIsNotNull(a) && b) + return strCmp(a, b); + else if (strIsNotNull(a)) + return 1; + else if (b) + return -1; + return 0; +} + +void +stringInitStatic(String * s, const char *str) +{ + stringClean(s); + stringInit(s, str); +} + +/* + * Compare a Squid string and a normal string. + * + * strCmp() once worked on NUL-terminated Squid strings and used strcmp(); + * this function attempts to emulate that behaviour. + */ +int +strCmp(String s, const char *str) +{ + int l = strlen(str); + if (l != strLen2(s)) + return strLen2(s) - l; + + return strncmp(strBuf2(s), str, strLen2(s)); +} + +int +strNCmp(String s, const char *str, int len) +{ + return strncmp(strBuf2(s), str, len); +} + +int +strCaseCmp(String s, const char *str) +{ + int l = strlen(str); + if (l != strLen2(s)) + return strLen2(s) - l; + + return strncasecmp(strBuf2(s), str, strLen2(s)); +} + +int +strNCaseCmp(String s, const char *str, int len) +{ + if (len != strLen2(s)) + return strLen2(s) - len; + + return strncasecmp(strBuf2(s), str, len); +} + +int +strNCaseCmpOffset(String s, int offset, const char *str, int len) +{ + if (strLen2(s) - offset != len) + return strLen2(s) - offset - len; + return strncasecmp(strBuf2(s) + offset, str, len); +} + +int +strStr(String haystack, const char *needle) +{ + int i; + int nl = strlen(needle); + for (i = 0; i < strLen2(haystack) - nl; i++) { + if (strncmp(strBuf2(haystack) + i, needle, nl) == 0) + return i; + } + return -1; +} + +int +strCStr(const char *haystack, String needle) +{ + int i; + int hl = strlen(haystack); + for (i = 0; i < hl - strLen2(needle); i++) { + if (strncmp(haystack + i, strBuf2(needle), strLen2(needle)) == 0) + return i; + } + return -1; +} Index: squid/libbuf/String.h =================================================================== RCS file: /cvsroot/squid-sf//squid/libbuf/Attic/String.h,v retrieving revision 1.1.2.18 retrieving revision 1.1.2.19 diff -u -r1.1.2.18 -r1.1.2.19 --- squid/libbuf/String.h 18 Dec 2007 11:59:54 -0000 1.1.2.18 +++ squid/libbuf/String.h 19 Dec 2007 07:56:22 -0000 1.1.2.19 @@ -13,110 +13,59 @@ extern void stringMakePrivate(String *s); /* String */ -#define strLen(s) ((/* const */ int)(s).len) - -static inline const char * strBuf(String s) +/* These are "replacements" for strBuf and strLen which indicate that the + * code using them has been changed to not assume NUL-terminated strings */ +#define strLen2(s) ((/* const */ int)(s).len) +static inline const char * strBuf2(String s) { if (s.nb == NULL) return NULL; return buf_buf(s.nb); } -/* These are "replacements" for strBuf and strLen which indicate that the - * code using them has been changed to not assume NUL-terminated strings */ -#define strBuf2(s) strBuf(s) -#define strLen2(s) strLen(s) - -static inline char * -strCDupOffset(String s, int offset) -{ - char *a = xmalloc(strLen2(s) - offset + 1); - memcpy(a, strBuf2(s), strLen2(s) - offset); - a[strLen2(s) - offset] = '\0'; - return a; - -} +#define strBuf(s) strBuf2(s) +#define strLen(s) strLen2(s) -#define strIsNull(s) ((s).nb == NULL) -#define strIsNotNull(s) ((s).nb != NULL) #define strCDup(s) strCDupOffset((s), 0) - -/* XXX None of these functions are non-NUL safe and they need to be later replaced */ -#define strStr(s,str) ((const char*)strstr(strBuf(s), (str))) -#define strCStr(str, s) ((const char *) strstr(str, strBuf(s))) -#define strCmp(s,str) strcmp(strBuf(s), (str)) -#define strNCmp(s,str,n) strncmp(strBuf(s), (str), (n)) -#define strCaseCmp(s,str) strcasecmp(strBuf(s), (str)) -#define strNCaseCmp(s,str,n) strncasecmp(strBuf(s), (str), (n)) - -#define strCmpStr(s1, s2) strncmp(strBuf2(s1), strBuf2(s2), XMIN(strLen2(s1), strLen2(s2))) -#define strCaseCmpStr(s1, s2) strncasecmp(strBuf2(s1), strBuf2(s2), XMIN(strLen2(s1), strLen2(s2))) - -/* XXX this should eventually take the String length + offset under consideration! */ -#define strNCaseCmpOffset(s, o, str, n) strncasecmp(strBuf(s) + (o), (str), (n)) - +extern char * strCDupOffset(String s, int offset); /* XXX this should also take into account the length! */ #define strGetPos(s, o) *(strBuf(s) + o) - -static inline int -strChr(String s, char ch) -{ - const char *t2; - t2 = strchr(strBuf(s), ch); - if (t2 == NULL) - return -1; - return t2 - strBuf(s); -} - -static inline int -strRChr(String s, char ch) -{ - const char *t2; - t2 = strrchr(strBuf(s), ch); - if (t2 == NULL) - return -1; - return t2 - strBuf(s); - -} - -/* These following functions will trigger a copy on write! */ -/* XXX these functions which take a char pointer need to be rewritten to take a pos first! */ -static inline void strSet(String s, int offset, char ch) -{ - int a; - - assert(offset < s.len); - - stringMakePrivate(&s); - a = buf_put_chr(s.nb, offset, ch); - assert(a); -} - -static inline void strCut(String s, int pos) -{ - int a; - - stringMakePrivate(&s); - /* XXX this is breaking buf_t layering; please revisit! */ - s.len = s.nb->len = pos; - a = buf_put_chr(s.nb, pos, '\0'); - assert(a); -} - -#define strCat(s,str) stringAppend(&(s), (str), strlen(str)) -#define strCatStr(s, s2) stringAppend(&(s), strBuf2(s2), strLen2(s2)) +#define strIsNull(s) ((s).nb == NULL) +#define strIsNotNull(s) ((s).nb != NULL) extern void stringInit(String * s, const char *str); extern void stringLimitInit(String * s, const char *str, int len); extern String stringDup(const String * s); extern void stringClean(String * s); extern void stringReset(String * s, const char *str); -extern void stringAppend(String * s, const char *buf, int len); /* extern void stringAppendf(String *s, const char *fmt, ...) PRINTF_FORMAT_ARG2; */ extern int strToOffset(String s, off_t *off, int base); extern int strCSpn(String s, int start, const char *reject); extern String strSubStr(String s, int start, int len); +/* XXX None of these functions are non-NUL safe and they need to be later replaced */ + +/* "Fixed" functions */ +extern int strChr(String s, char ch); +extern int strRChr(String s, char ch); +extern int strCmp(String s, const char *str); +extern int strCaseCmp(String s, const char *str); +extern int strNCmp(String s, const char *str, int len); +extern int strNCaseCmp(String s, const char *str, int len); +extern int strNCaseCmpOffset(String s, int offset, const char *str, int len); +extern int strStr(String s, const char *haystack); +extern int strCStr(const char *n, String h); +#define strCaseCmpStr(s1, s2) (strLen2(s1) == strLen2(s2) ? strncasecmp(strBuf2(s1), strBuf2(s2), XMIN(strLen2(s1), strLen2(s2))) : strLen2(s1) - strLen2(s2)) +#define strCmpStr(s1, s2) (strLen2(s1) == strLen2(s2) ? strncmp(strBuf2(s1), strBuf2(s2), XMIN(strLen2(s1), strLen2(s2))) : strLen2(s1) - strLen2(s2)) + +/* These following functions will trigger a copy on write! */ +/* XXX these functions which take a char pointer need to be rewritten to take a pos first! */ +extern void strSet(String s, int offset, char ch); +extern void strCut(String s, int pos); +#define strCat(s,str) stringAppend(&(s), (str), strlen(str)) +#define strCatStr(s, s2) stringAppend(&(s), strBuf2(s2), strLen2(s2)) +extern void stringAppend(String * s, const char *buf, int len); + typedef struct { int create, reset, clean, dup, make_private; } stringStatsStruct; @@ -129,33 +78,11 @@ /* XXX sort it out later! */ #define strHasWhitespace(s) (StringMapCheck(&strmap_whitespace, strBuf2(s), strLen2(s))) -/* A port of a routine from store.c */ -static int inline -strMatch(const char *search, String m, int maxlen) -{ - int mlen = strLen2(m); - if (maxlen < mlen) - return -1; - return strncmp(search, strBuf2(m), maxlen); -} - -static int inline -strCmpNull(String a, const char *b) -{ - if (strIsNotNull(a) && b) - return strCmp(a, b); - else if (strIsNotNull(a)) - return 1; - else if (b) - return -1; - return 0; -} +/* A port of routines from store.c */ +extern int strMatch(const char *search, String m, int maxlen); +extern int strCmpNull(String a, const char *b); /* XXX this will eventually bypass needing to copy the backend buf_t unless its modified */ -static inline void stringInitStatic(String * s, const char *str) -{ - stringClean(s); - stringInit(s, str); -} +extern void stringInitStatic(String * s, const char *str); #endif /* __LIBBUF_STRING__H_ */ Index: squid/libbuf/buf.c =================================================================== RCS file: /cvsroot/squid-sf//squid/libbuf/Attic/buf.c,v retrieving revision 1.1.2.5 retrieving revision 1.1.2.6 diff -u -r1.1.2.5 -r1.1.2.6 --- squid/libbuf/buf.c 19 Dec 2007 05:47:04 -0000 1.1.2.5 +++ squid/libbuf/buf.c 19 Dec 2007 07:56:22 -0000 1.1.2.6 @@ -165,7 +165,7 @@ int buf_append(buf_t *b, const void *src, size_t len, buf_flags_t flags) { - while (b->size - b->len < len + 1) { + if (b->size - b->len < len + 1) { if (!buf_changesize(b, b->size + b->len + 1)) { /* tsk! */ return -1; @@ -173,8 +173,11 @@ } memcpy(b->b + b->len, src, len); b->len += len; +#if 0 if (flags & BF_APPEND_NUL) b->b[b->len] = '\0'; +#endif + b->b[b->len] = 'A'; return len; }