Skip to content

Commit

Permalink
Merge branch 'string-handling-performance-improvement' of github.com:…
Browse files Browse the repository at this point in the history
…daveyc/zowe-common-c into string-handling-performance-improvement
  • Loading branch information
daveyc committed Feb 10, 2020
2 parents 1c4dff7 + 2b4945e commit fe8d511
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions c/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ int lastIndexOf(const char *str, int len, char c) {
return -1;
}

<<<<<<< HEAD
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);
Expand All @@ -161,6 +162,26 @@ int indexOfString(const char *sourceString, size_t sourceLength, const char *sea
}
return -1;
}
=======
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;
}
>>>>>>> 2b4945e4d4014b46650b1a3a4f2f298a30c9ff70

int lastIndexOfString(char *str, int len, char *searchString) {
int searchLen = strlen(searchString);
Expand Down Expand Up @@ -383,10 +404,17 @@ token* tknGetStandard(char *str, int len, int start){
for (i=start; i<len; i++){
char c = str[i];
if (c == '@' ||
<<<<<<< HEAD
c == '#' ||
c == '$' ||
c == '.' ||
isCharAN(c)){
=======
c == '#' ||
c == '$' ||
c == '.' ||
isCharAN(c)){
>>>>>>> 2b4945e4d4014b46650b1a3a4f2f298a30c9ff70
if (!inToken){
inToken = 1;
tokenStart = i;
Expand Down Expand Up @@ -514,9 +542,15 @@ int tknLength(token *t){
}

static char hexDigits[16] ={ '0', '1', '2', '3',
<<<<<<< HEAD
'4', '5', '6', '7',
'8', '9', 'A', 'B',
'C', 'D', 'E', 'F'};
=======
'4', '5', '6', '7',
'8', '9', 'A', 'B',
'C', 'D', 'E', 'F'};
>>>>>>> 2b4945e4d4014b46650b1a3a4f2f298a30c9ff70

static char hexDigitsLower[16] = { '0', '1', '2', '3',
'4', '5', '6', '7',
Expand Down Expand Up @@ -610,10 +644,17 @@ void dumpBufferToStream(const char *buffer, int length, /* FILE* */void *traceOu
for (pos=0; pos<32; pos++){

if (((index+pos)%4 == 0) && ((index+pos)%32 != 0)){
<<<<<<< HEAD
if ((index+pos)%16 == 0){
lineBuffer[linePos++] = ' ';
}
lineBuffer[linePos++] = ' ';
=======
if ((index+pos)%16 == 0){
lineBuffer[linePos++] = ' ';
}
lineBuffer[linePos++] = ' ';
>>>>>>> 2b4945e4d4014b46650b1a3a4f2f298a30c9ff70
}
if ((index+pos)<length){
linePos = hexFill(lineBuffer,linePos,0,2,0,(0xFF&buffer[index+pos])); /* sprintf(lineBuffer+linePos,"%0.2x",(int)(0xFF&buffer[index+pos])); */
Expand Down Expand Up @@ -663,8 +704,13 @@ void hexdump(char *buffer, int length, int nominalStartAddress, int formatWidth,
memcpy(lineBuffer+8,pad1,pad1Len);
for (pos=0; pos<formatWidth; pos++){
if ((index+pos)<length){
<<<<<<< HEAD
simpleHexFill(lineBuffer+linePos,2,(int)(0xFF&buffer[index+pos]));
linePos += 2;
=======
simpleHexFill(lineBuffer+linePos,2,(int)(0xFF&buffer[index+pos]));
linePos += 2;
>>>>>>> 2b4945e4d4014b46650b1a3a4f2f298a30c9ff70
/* linePos += sprintf(lineBuffer+linePos,"%0.2x",(int)(0xFF&buffer[index+pos])); */
} else{
linePos += sprintf(lineBuffer+linePos," ");
Expand Down Expand Up @@ -714,10 +760,17 @@ void dumpbufferA(const char *buffer, int length){
for (pos=0; pos<32; pos++){

if (((index+pos)%4 == 0) && ((index+pos)%32 != 0)){
<<<<<<< HEAD
if ((index+pos)%16 == 0){
lineBuffer[linePos++] = ' ';
}
lineBuffer[linePos++] = ' ';
=======
if ((index+pos)%16 == 0){
lineBuffer[linePos++] = ' ';
}
lineBuffer[linePos++] = ' ';
>>>>>>> 2b4945e4d4014b46650b1a3a4f2f298a30c9ff70
}
if ((index+pos)<length){
linePos = hexFill(lineBuffer,linePos,0,2,0,(0xFF&buffer[index+pos])); /* sprintf(lineBuffer+linePos,"%0.2x",(int)(0xFF&buffer[index+pos])); */
Expand Down Expand Up @@ -911,6 +964,7 @@ char *cleanURLParamValue(ShortLivedHeap *slh, char *value){
switch (state){
case URL_PARM_NORMAL:
if (c == '%'){
<<<<<<< HEAD
state = URL_PARM_PERCENT;
} else if (c == '+'){
cleanValue[pos++] = ' ';
Expand All @@ -928,11 +982,31 @@ char *cleanURLParamValue(ShortLivedHeap *slh, char *value){
cleanValue[pos++] = '%';
cleanValue[pos++] = c;
state = URL_PARM_NORMAL;
=======
state = URL_PARM_PERCENT;
} else if (c == '+'){
cleanValue[pos++] = ' ';
} else{
cleanValue[pos++] = c;
}
break;
case URL_PARM_PERCENT:
if (c == '%'){
cleanValue[pos++] = '%';
} else if ((c >= '2') && (c <= '7')){
state = URL_PARM_PERCENT_NUMBER;
numChar = c;
} else{
cleanValue[pos++] = '%';
cleanValue[pos++] = c;
state = URL_PARM_NORMAL;
>>>>>>> 2b4945e4d4014b46650b1a3a4f2f298a30c9ff70
}
break;
case URL_PARM_PERCENT_NUMBER:
highDigit = (numChar-'0')*0x10;
if ((c >= '0') && (c <= '9')){
<<<<<<< HEAD
cleanValue[pos++] = ascii[highDigit+(c-'0')];
/* printf("highDigit %x (c-0) %x\n",highDigit,(c-'0')); */
state = URL_PARM_NORMAL;
Expand All @@ -951,6 +1025,26 @@ char *cleanURLParamValue(ShortLivedHeap *slh, char *value){
cleanValue[pos++] = numChar;
cleanValue[pos++] = c;
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;
} else if ((c >= 'A') && (c <= 'F')){
cleanValue[pos++] = ascii[highDigit+10+(c-'A')];
state = URL_PARM_NORMAL;
} else if (c == '%'){
cleanValue[pos++] = '%';
cleanValue[pos++] = numChar;
state = URL_PARM_PERCENT;
} else{
cleanValue[pos++] = '%';
cleanValue[pos++] = numChar;
cleanValue[pos++] = c;
state = URL_PARM_NORMAL;
>>>>>>> 2b4945e4d4014b46650b1a3a4f2f298a30c9ff70
}
break;
}
Expand Down Expand Up @@ -1035,9 +1129,15 @@ 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,
<<<<<<< HEAD
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};
>>>>>>> 2b4945e4d4014b46650b1a3a4f2f298a30c9ff70

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,
Expand Down Expand Up @@ -1588,7 +1688,11 @@ int stringListContains(StringList *list, char *s){
StringListElt *elt = list->head;
while (elt){
if (!strcmp(elt->string,s)){
<<<<<<< HEAD
return 1;
=======
return 1;
>>>>>>> 2b4945e4d4014b46650b1a3a4f2f298a30c9ff70
}
elt = elt->next;
}
Expand Down

0 comments on commit fe8d511

Please sign in to comment.