Skip to content

Commit

Permalink
refactoring: extract caseinsensitive find_substr function
Browse files Browse the repository at this point in the history
  • Loading branch information
HarpyWar committed Aug 9, 2014
1 parent c2b838b commit cdd0129
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
10 changes: 2 additions & 8 deletions src/bnetd/account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
27 changes: 27 additions & 0 deletions src/common/xstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <iomanip>

#include "compat/strdup.h"
#include "compat/strcasecmp.h"
#include "common/xalloc.h"
#include "common/setup_after.h"

Expand Down Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions src/common/xstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down

0 comments on commit cdd0129

Please sign in to comment.