From cdd0129cac4628ae3180585b93086602d6554561 Mon Sep 17 00:00:00 2001 From: HarpyWar Date: Sat, 9 Aug 2014 20:07:22 +0400 Subject: [PATCH] refactoring: extract caseinsensitive find_substr function --- src/bnetd/account.cpp | 10 ++-------- src/common/xstring.cpp | 27 +++++++++++++++++++++++++++ src/common/xstring.h | 1 + 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/bnetd/account.cpp b/src/bnetd/account.cpp index b1cff2792..ee4f6c056 100644 --- a/src/bnetd/account.cpp +++ b/src/bnetd/account.cpp @@ -53,6 +53,7 @@ #include "storage.h" #include "common/flags.h" #include "common/xalloc.h" +#include "common/xstring.h" #include "common/setup_after.h" namespace pvpgn @@ -570,14 +571,7 @@ namespace pvpgn if (tname = account_get_name(account)) { - char temp[MAX_USERNAME_LEN]; - for (i = 0; i < std::strlen(tname); i++) { - temp[i] = tname[i]; - if (isupper((int)temp[i])) { - temp[i] = tolower((int)temp[i]); - } - } - if (strstr(temp, vague_username)) { + if (find_substr((char*)tname, vague_username)) { return tname; } return NULL; diff --git a/src/common/xstring.cpp b/src/common/xstring.cpp index 1f1670d93..890f431c0 100644 --- a/src/common/xstring.cpp +++ b/src/common/xstring.cpp @@ -27,6 +27,7 @@ #include #include "compat/strdup.h" +#include "compat/strcasecmp.h" #include "common/xalloc.h" #include "common/setup_after.h" @@ -331,4 +332,30 @@ namespace pvpgn } + // search substring in input string + // (case insensitive) + extern bool find_substr(char * input, const char * find) + { + char c1[2], c2[2]; + bool is_found = false; + int pos = 0; + int b = true; + for (int i = 0; i < strlen(input); i++) + { + c1[0] = input[i]; c1[1] = '\0'; + c2[0] = find[pos]; c2[1] = '\0'; + if (strcasecmp(c1, c2) == 0) + { + if (pos == strlen(find) - 1) + { + is_found = true; + break; + } + pos++; + } + else + pos = 0; + } + return is_found; + } } diff --git a/src/common/xstring.h b/src/common/xstring.h index 01c4d112e..924022948 100644 --- a/src/common/xstring.h +++ b/src/common/xstring.h @@ -35,6 +35,7 @@ namespace pvpgn extern char * str_strip_affix(char * str, char const * affix); extern char *str_replace(char *orig, char *rep, char *with); extern std::string str_replace_nl(char const * text); + extern bool find_substr(char * input, const char * find); #define safe_toupper(X) (std::islower((int)X)?std::toupper((int)X):(X))