mirror of
https://github.com/KartKrewDev/RingRacers.git
synced 2026-05-10 19:01:50 +00:00
EnsurePlayerNameIsGood: Only evaluate strlen once
Also has a slightly better chance of conflict avoidance for its absolute worst case scenario
This commit is contained in:
parent
9e549d5607
commit
1ef3063772
1 changed files with 6 additions and 7 deletions
|
|
@ -602,11 +602,11 @@ static boolean AllowedPlayerNameChar(char ch)
|
||||||
|
|
||||||
boolean EnsurePlayerNameIsGood(char *name, INT32 playernum)
|
boolean EnsurePlayerNameIsGood(char *name, INT32 playernum)
|
||||||
{
|
{
|
||||||
INT32 ix;
|
size_t ix, len = strlen(name);
|
||||||
|
|
||||||
if (strlen(name) == 0 || strlen(name) > MAXPLAYERNAME)
|
if (len == 0 || len > MAXPLAYERNAME)
|
||||||
return false; // Empty or too long.
|
return false; // Empty or too long.
|
||||||
if (name[0] == ' ' || name[strlen(name)-1] == ' ')
|
if (name[0] == ' ' || name[len-1] == ' ')
|
||||||
return false; // Starts or ends with a space.
|
return false; // Starts or ends with a space.
|
||||||
if (isdigit(name[0]))
|
if (isdigit(name[0]))
|
||||||
return false; // Starts with a digit.
|
return false; // Starts with a digit.
|
||||||
|
|
@ -620,20 +620,19 @@ boolean EnsurePlayerNameIsGood(char *name, INT32 playernum)
|
||||||
|
|
||||||
// Also, anything over 0x80 is disallowed too, since compilers love to
|
// Also, anything over 0x80 is disallowed too, since compilers love to
|
||||||
// differ on whether they're printable characters or not.
|
// differ on whether they're printable characters or not.
|
||||||
for (ix = 0; name[ix] != '\0'; ix++)
|
for (ix = 0; ix < len; ix++)
|
||||||
if (!AllowedPlayerNameChar(name[ix]))
|
if (!AllowedPlayerNameChar(name[ix]))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Check if a player is currently using the name, case-insensitively.
|
// Check if a player is currently using the name, case-insensitively.
|
||||||
for (ix = 0; ix < MAXPLAYERS; ix++)
|
for (ix = 0; ix < MAXPLAYERS; ix++)
|
||||||
{
|
{
|
||||||
if (ix != playernum && playeringame[ix]
|
if (ix != (size_t)playernum && playeringame[ix]
|
||||||
&& strcasecmp(name, player_names[ix]) == 0)
|
&& strcasecmp(name, player_names[ix]) == 0)
|
||||||
{
|
{
|
||||||
// We shouldn't kick people out just because
|
// We shouldn't kick people out just because
|
||||||
// they joined the game with the same name
|
// they joined the game with the same name
|
||||||
// as someone else -- modify the name instead.
|
// as someone else -- modify the name instead.
|
||||||
size_t len = strlen(name);
|
|
||||||
|
|
||||||
// Recursion!
|
// Recursion!
|
||||||
// Slowly strip characters off the end of the
|
// Slowly strip characters off the end of the
|
||||||
|
|
@ -647,7 +646,7 @@ boolean EnsurePlayerNameIsGood(char *name, INT32 playernum)
|
||||||
else if (len == 1) // Agh!
|
else if (len == 1) // Agh!
|
||||||
{
|
{
|
||||||
// Last ditch effort...
|
// Last ditch effort...
|
||||||
sprintf(name, "%d", M_RandomKey(10));
|
sprintf(name, "%d", 'A' + M_RandomKey(26));
|
||||||
if (!EnsurePlayerNameIsGood (name, playernum))
|
if (!EnsurePlayerNameIsGood (name, playernum))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue