From cb4fb245016a4bbe3ac1cf44b76a3eca97f922c8 Mon Sep 17 00:00:00 2001 From: David Crayford Date: Mon, 10 Feb 2020 21:59:25 +0800 Subject: [PATCH] Improve the performance of indexOfString Signed-off-by: David Crayford --- c/utils.c | 112 +++++++++++++++++++++++++++--------------------------- h/utils.h | 2 +- 2 files changed, 58 insertions(+), 56 deletions(-) diff --git a/c/utils.c b/c/utils.c index f506e318e..2d685840f 100644 --- a/c/utils.c +++ b/c/utils.c @@ -143,22 +143,24 @@ int lastIndexOf(const char *str, int len, char c) { return -1; } -int indexOfString(char *str, int len, char *searchString, int startPos){ - int searchLen = strlen(searchString); - int lastPossibleStart = len-searchLen; - int pos = startPos; - - if (startPos > lastPossibleStart){ - return -1; - } - while (pos <= lastPossibleStart){ - if (!memcmp(str+pos,searchString,searchLen)){ - return pos; - } - pos++; +int indexOfString(const char *sourceString, size_t sourceLength, const char *searchString, size_t startPos) { + if (sourceString == NULL || sourceLength == 0 || searchString == NULL) return - 1; + size_t searchLength = strlen(searchString); + if (searchLength == 0) return -1; + const char * currPos = sourceString + startPos; + const char * endPos = sourceString + sourceLength - searchLength; + char firstChar = searchString[0]; + while (currPos < endPos) { + size_t bytesRemaining = endPos - currPos + 1; + currPos = memchr(currPos, firstChar, bytesRemaining); + if (currPos == NULL) break; + if (memcmp(currPos, searchString, searchLength) == 0) { + return currPos - sourceString; + } + currPos++; } return -1; -} +} int lastIndexOfString(char *str, int len, char *searchString) { int searchLen = strlen(searchString); @@ -381,10 +383,10 @@ token* tknGetStandard(char *str, int len, int start){ for (i=start; i= '2') && (c <= '7')){ - state = URL_PARM_PERCENT_NUMBER; - numChar = c; + state = URL_PARM_PERCENT_NUMBER; + numChar = c; } else{ - cleanValue[pos++] = '%'; + cleanValue[pos++] = '%'; cleanValue[pos++] = c; - state = URL_PARM_NORMAL; + state = URL_PARM_NORMAL; } break; case URL_PARM_PERCENT_NUMBER: highDigit = (numChar-'0')*0x10; if ((c >= '0') && (c <= '9')){ - cleanValue[pos++] = ascii[highDigit+(c-'0')]; - /* printf("highDigit %x (c-0) %x\n",highDigit,(c-'0')); */ - state = URL_PARM_NORMAL; + cleanValue[pos++] = ascii[highDigit+(c-'0')]; + /* printf("highDigit %x (c-0) %x\n",highDigit,(c-'0')); */ + state = URL_PARM_NORMAL; } else if ((c >= 'a') && (c <= 'f')){ - cleanValue[pos++] = ascii[highDigit+10+(c-'a')]; - state = URL_PARM_NORMAL; + cleanValue[pos++] = ascii[highDigit+10+(c-'a')]; + state = URL_PARM_NORMAL; } else if ((c >= 'A') && (c <= 'F')){ - cleanValue[pos++] = ascii[highDigit+10+(c-'A')]; - state = URL_PARM_NORMAL; + cleanValue[pos++] = ascii[highDigit+10+(c-'A')]; + state = URL_PARM_NORMAL; } else if (c == '%'){ - cleanValue[pos++] = '%'; + cleanValue[pos++] = '%'; cleanValue[pos++] = numChar; - state = URL_PARM_PERCENT; + state = URL_PARM_PERCENT; } else{ - cleanValue[pos++] = '%'; + cleanValue[pos++] = '%'; cleanValue[pos++] = numChar; - cleanValue[pos++] = c; - state = URL_PARM_NORMAL; + cleanValue[pos++] = c; + state = URL_PARM_NORMAL; } break; } @@ -1033,9 +1035,9 @@ int decodeBase64(char *s, char *result){ } static char binToB64[] ={0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50, - 0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x61,0x62,0x63,0x64,0x65,0x66, - 0x67,0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,0x73,0x74,0x75,0x76, - 0x77,0x78,0x79,0x7A,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x2B,0x2F}; + 0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x61,0x62,0x63,0x64,0x65,0x66, + 0x67,0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,0x73,0x74,0x75,0x76, + 0x77,0x78,0x79,0x7A,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x2B,0x2F}; static char binToEB64[] ={0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7, 0xd8,0xd9,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0x81,0x82,0x83,0x84,0x85,0x86, @@ -1586,7 +1588,7 @@ int stringListContains(StringList *list, char *s){ StringListElt *elt = list->head; while (elt){ if (!strcmp(elt->string,s)){ - return 1; + return 1; } elt = elt->next; } diff --git a/h/utils.h b/h/utils.h index 6d3295ff7..e7906f7cc 100644 --- a/h/utils.h +++ b/h/utils.h @@ -39,7 +39,7 @@ char * strcopy_safe(char * dest, const char * source, int dest_size); int indexOf(char *str, int len, char c, int startPos); int lastIndexOf(const char *str, int len, char c); -int indexOfString(char *str, int len, char *searchString, int startPos); +int indexOfString(const char * sourceString, size_t sourceLength, const char * searchString, size_t startPos); int lastIndexOfString(char *str, int len, char *searchString); int indexOfStringInsensitive(char *str, int len, char *searchString, int startPos);