Added function to trim strings.

This commit is contained in:
ajuaristi 2014-05-22 09:51:59 +02:00
commit 8ebbde3223
2 changed files with 24 additions and 0 deletions

View file

@ -1285,3 +1285,26 @@ int hydra_memsearch(char *haystack, int hlen, char *needle, int nlen) {
return i;
return -1;
}
char * hydra_trim(char * str) {
size_t len = strlen(str);
char * str_ptr[] = {str, NULL};
// Truncate leading spaces
for(; *str_ptr[0] == ' ' && len > 0; str_ptr[0]++)
len--;
if(len == 0)
return NULL;
// Truncate trailing spaces
str_ptr[1] = str + len;
for(; *str_ptr[1] == ' '; str_ptr[1]--)
len--;
*(str_ptr[1] + 1) = 0;
char * trimmed_str = strndup(str_ptr[0], len);
free(str);
return trimmed_str;
}

View file

@ -38,6 +38,7 @@ extern char *hydra_strcasestr(const char *haystack, const char *needle);
extern void hydra_dump_data(unsigned char *buf, int len, char *text);
extern int hydra_memsearch(char *haystack, int hlen, char *needle, int nlen);
extern char *hydra_strrep(char *string, char *oldpiece, char *newpiece);
extern char * hydra_trim(char * str);
#ifdef HAVE_PCRE
int hydra_string_match(char *str, const char *regex);