1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-06 04:31:29 +00:00

Optimize utility function removechar. Closes #670 by shikharsrivastava

This commit is contained in:
dmiller
2017-03-01 20:41:15 +00:00
parent dd4f367036
commit 3ddb5c9f81

View File

@@ -316,26 +316,18 @@ int bitcmp(u8 *a, u8*b, int len){
/** Removes every instance of the character stored in parameter "c" in the /** Removes every instance of the character stored in parameter "c" in the
* supplied string. * supplied string.
* @warning the supplied buffer is modified by this function. Whenever a * @warning the supplied buffer is modified by this function. */
* colon is found, the rest of the string is moved one position to the left
* so the colon gets overwritten. */
int removechar(char *string, char c){ int removechar(char *string, char c){
size_t len=0, i=0, j=0; size_t i=0, j=0;
if(string==NULL) if(string==NULL)
return OP_FAILURE; return OP_FAILURE;
len=strlen(string);
for(i=0; i<len; i++){ while(string[j] != '\0') {
/* Found the character, move everything one position to the left */ if(string[j] != c)
if( string[i]== c ){ string[i++] = string[j];
for(j=i; j<len-1; j++) j++;
string[j]=string[j+1];
len-=1;
string[len]='\0';
/* Start again from the beginning because otherwise we don't catch
* consecutive colons */
i=-1; /* (get incremented by one by the loop control) */
}
} }
string[i] = '\0';
return OP_SUCCESS; return OP_SUCCESS;
} /* End of removechar() */ } /* End of removechar() */