1
0
Fork 0

Refactored case-conversion functions.

StrToLower() returns a modified copy of the string, InPlaceLowercase() modifies the string in-place.
This commit is contained in:
madmaxoft 2014-08-04 11:16:19 +02:00
parent 054ce9bcc4
commit 1fa210c7f9
7 changed files with 54 additions and 47 deletions

View File

@ -35,8 +35,7 @@ cHTTPMessage::cHTTPMessage(eKind a_Kind) :
void cHTTPMessage::AddHeader(const AString & a_Key, const AString & a_Value)
{
AString Key = a_Key;
StrToLower(Key);
AString Key = StrToLower(a_Key);
cNameValueMap::iterator itr = m_Headers.find(Key);
if (itr == m_Headers.end())
{

View File

@ -710,8 +710,7 @@ AString cMonster::MobTypeToString(cMonster::eType a_MobType)
cMonster::eType cMonster::StringToMobType(const AString & a_Name)
{
AString lcName(a_Name);
StrToLower(lcName);
AString lcName = StrToLower(a_Name);
// Binary-search for the lowercase name:
int lo = 0, hi = ARRAYCOUNT(g_MobTypeNames) - 1;

View File

@ -190,8 +190,7 @@ void cMojangAPI::Start(cIniFile & a_SettingsIni)
AString cMojangAPI::GetUUIDFromPlayerName(const AString & a_PlayerName, bool a_UseOnlyCached)
{
// Convert the playername to lowercase:
AString lcPlayerName(a_PlayerName);
StrToLower(lcPlayerName);
AString lcPlayerName = StrToLower(a_PlayerName);
// Request the cache to query the name if not yet cached:
if (!a_UseOnlyCached)
@ -219,7 +218,7 @@ AString cMojangAPI::GetUUIDFromPlayerName(const AString & a_PlayerName, bool a_U
AString cMojangAPI::GetPlayerNameFromUUID(const AString & a_UUID, bool a_UseOnlyCached)
{
// Normalize the UUID to lowercase short format that is used as the map key:
AString UUID = StrToLower(MakeUUIDShort(a_UUID));
AString UUID = MakeUUIDShort(a_UUID);
// Retrieve from caches:
{
@ -260,8 +259,7 @@ AStringVector cMojangAPI::GetUUIDsFromPlayerNames(const AStringVector & a_Player
AStringVector PlayerNames;
for (AStringVector::const_iterator itr = a_PlayerNames.begin(), end = a_PlayerNames.end(); itr != end; ++itr)
{
AString Lower(*itr);
PlayerNames.push_back(StrToLower(Lower));
PlayerNames.push_back(StrToLower(*itr));
} // for itr - a_PlayerNames[]
// Request the cache to populate any names not yet contained:
@ -292,12 +290,11 @@ AStringVector cMojangAPI::GetUUIDsFromPlayerNames(const AStringVector & a_Player
void cMojangAPI::AddPlayerNameToUUIDMapping(const AString & a_PlayerName, const AString & a_UUID)
{
AString lcName(a_PlayerName);
AString UUID = StrToLower(MakeUUIDShort(a_UUID));
AString UUID = MakeUUIDShort(a_UUID);
Int64 Now = time(NULL);
{
cCSLock Lock(m_CSNameToUUID);
m_NameToUUID[StrToLower(lcName)] = sProfile(a_PlayerName, UUID, "", "", Now);
m_NameToUUID[StrToLower(a_PlayerName)] = sProfile(a_PlayerName, UUID, "", "", Now);
}
{
cCSLock Lock(m_CSUUIDToName);
@ -311,12 +308,11 @@ void cMojangAPI::AddPlayerNameToUUIDMapping(const AString & a_PlayerName, const
void cMojangAPI::AddPlayerProfile(const AString & a_PlayerName, const AString & a_UUID, const Json::Value & a_Properties)
{
AString lcName(a_PlayerName);
AString UUID = StrToLower(MakeUUIDShort(a_UUID));
AString UUID = MakeUUIDShort(a_UUID);
Int64 Now = time(NULL);
{
cCSLock Lock(m_CSNameToUUID);
m_NameToUUID[StrToLower(lcName)] = sProfile(a_PlayerName, UUID, "", "", Now);
m_NameToUUID[StrToLower(a_PlayerName)] = sProfile(a_PlayerName, UUID, "", "", Now);
}
{
cCSLock Lock(m_CSUUIDToName);
@ -395,13 +391,13 @@ AString cMojangAPI::MakeUUIDShort(const AString & a_UUID)
{
case 32:
{
// Already is a short UUID
return a_UUID;
// Already is a short UUID, only lowercase
return StrToLower(a_UUID);
}
case 36:
{
// Remove the dashes from the string:
// Remove the dashes from the string by appending together the parts between them:
AString res;
res.reserve(32);
res.append(a_UUID, 0, 8);
@ -409,7 +405,7 @@ AString cMojangAPI::MakeUUIDShort(const AString & a_UUID)
res.append(a_UUID, 14, 4);
res.append(a_UUID, 19, 4);
res.append(a_UUID, 24, 12);
return res;
return StrToLower(res);
}
}
LOGWARNING("%s: Not an UUID: \"%s\".", __FUNCTION__, a_UUID.c_str());
@ -427,8 +423,8 @@ AString cMojangAPI::MakeUUIDDashed(const AString & a_UUID)
{
case 36:
{
// Already is a dashed UUID
return a_UUID;
// Already is a dashed UUID, only lowercase
return StrToLower(a_UUID);
}
case 32:
@ -445,7 +441,7 @@ AString cMojangAPI::MakeUUIDDashed(const AString & a_UUID)
res.append(a_UUID, 16, 4);
res.push_back('-');
res.append(a_UUID, 20, 12);
return res;
return StrToLower(res);
}
}
LOGWARNING("%s: Not an UUID: \"%s\".", __FUNCTION__, a_UUID.c_str());
@ -487,9 +483,8 @@ void cMojangAPI::LoadCachesFromDisk(void)
AString PlayerName = stmt.getColumn(0);
AString UUID = stmt.getColumn(1);
Int64 DateTime = stmt.getColumn(2);
AString lcPlayerName = PlayerName;
UUID = StrToLower(MakeUUIDShort(UUID));
m_NameToUUID[StrToLower(lcPlayerName)] = sProfile(PlayerName, UUID, "", "", DateTime);
UUID = MakeUUIDShort(UUID);
m_NameToUUID[StrToLower(PlayerName)] = sProfile(PlayerName, UUID, "", "", DateTime);
m_UUIDToName[UUID] = sProfile(PlayerName, UUID, "", "", DateTime);
}
}
@ -502,9 +497,8 @@ void cMojangAPI::LoadCachesFromDisk(void)
AString Textures = stmt.getColumn(2);
AString TexturesSignature = stmt.getColumn(2);
Int64 DateTime = stmt.getColumn(4);
AString lcPlayerName = PlayerName;
UUID = StrToLower(MakeUUIDShort(UUID));
m_UUIDToProfile[StrToLower(lcPlayerName)] = sProfile(PlayerName, UUID, Textures, TexturesSignature, DateTime);
UUID = MakeUUIDShort(UUID);
m_UUIDToProfile[UUID] = sProfile(PlayerName, UUID, Textures, TexturesSignature, DateTime);
}
}
}
@ -669,13 +663,12 @@ void cMojangAPI::CacheNamesToUUIDs(const AStringVector & a_PlayerNames)
{
Json::Value & Val = root[idx];
AString JsonName = Val.get("name", "").asString();
AString JsonUUID = StrToLower(MakeUUIDShort(Val.get("id", "").asString()));
AString JsonUUID = MakeUUIDShort(Val.get("id", "").asString());
if (JsonUUID.empty())
{
continue;
}
AString lcName = JsonName;
m_NameToUUID[StrToLower(lcName)] = sProfile(JsonName, JsonUUID, "", "", Now);
m_NameToUUID[StrToLower(JsonName)] = sProfile(JsonName, JsonUUID, "", "", Now);
} // for idx - root[]
} // cCSLock (m_CSNameToUUID)
@ -686,7 +679,7 @@ void cMojangAPI::CacheNamesToUUIDs(const AStringVector & a_PlayerNames)
{
Json::Value & Val = root[idx];
AString JsonName = Val.get("name", "").asString();
AString JsonUUID = StrToLower(MakeUUIDShort(Val.get("id", "").asString()));
AString JsonUUID = MakeUUIDShort(Val.get("id", "").asString());
if (JsonUUID.empty())
{
continue;
@ -796,9 +789,8 @@ void cMojangAPI::CacheUUIDToProfile(const AString & a_UUID)
m_UUIDToName[a_UUID] = sProfile(PlayerName, a_UUID, Properties, Now);
}
{
AString lcPlayerName(PlayerName);
cCSLock Lock(m_CSNameToUUID);
m_NameToUUID[StrToLower(lcPlayerName)] = sProfile(PlayerName, a_UUID, Properties, Now);
m_NameToUUID[StrToLower(PlayerName)] = sProfile(PlayerName, a_UUID, Properties, Now);
}
}

View File

@ -40,12 +40,12 @@ public:
// tolua_begin
/** Converts the given UUID to its short form (32 bytes, no dashes).
/** Normalizes the given UUID to its short form (32 bytes, no dashes, lowercase).
Logs a warning and returns empty string if not a UUID.
Note: only checks the string's length, not the actual content. */
static AString MakeUUIDShort(const AString & a_UUID);
/** Converts the given UUID to its dashed form (36 bytes, 4 dashes).
/** Normalizes the given UUID to its dashed form (36 bytes, 4 dashes, lowercase).
Logs a warning and returns empty string if not a UUID.
Note: only checks the string's length, not the actual content. */
static AString MakeUUIDDashed(const AString & a_UUID);

View File

@ -196,9 +196,9 @@ AString TrimString(const AString & str)
AString & StrToUpper(AString & s)
AString & InPlaceLowercase(AString & s)
{
std::transform(s.begin(), s.end(), s.begin(), ::toupper);
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
return s;
}
@ -206,9 +206,9 @@ AString & StrToUpper(AString & s)
AString & StrToLower(AString & s)
AString & InPlaceUppercase(AString & s)
{
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
std::transform(s.begin(), s.end(), s.begin(), ::toupper);
return s;
}
@ -227,16 +227,25 @@ AString StrToLower(const AString & s)
AString StrToUpper(const AString & s)
{
AString res(s);
std::transform(res.begin(), res.end(), res.begin(), ::toupper);
return res;
}
int NoCaseCompare(const AString & s1, const AString & s2)
{
#ifdef _MSC_VER
// MSVC has stricmp that compares case-insensitive:
return _stricmp(s1.c_str(), s2.c_str());
#else
// Do it the hard way:
AString s1Copy(s1);
AString s2Copy(s2);
return StrToUpper(s1Copy).compare(StrToUpper(s2Copy));
// Do it the hard way - convert both strings to lowercase:
return StrToLower(s1).compare(StrToLower(s2));
#endif // else _MSC_VER
}

View File

@ -43,10 +43,13 @@ extern AStringVector StringSplitAndTrim(const AString & str, const AString & del
extern AString TrimString(const AString & str); // tolua_export
/// In-place string conversion to uppercase; returns the same string
extern AString & StrToUpper(AString & s);
extern AString & InPlaceUppercase(AString & s);
/// In-place string conversion to lowercase; returns the same string
extern AString & StrToLower(AString & s);
extern AString & InPlaceLowercase(AString & s);
/** Returns an upper-cased copy of the string */
extern AString StrToUpper(const AString & s);
/** Returns a lower-cased copy of the string */
extern AString StrToLower(const AString & s);

View File

@ -2495,7 +2495,7 @@ void cWSSAnvil::LoadWolfOwner(cWolf & a_Wolf, const cParsedNBT & a_NBT, int a_Ta
int OwnerUUIDIdx = a_NBT.FindChildByName(a_TagIdx, "OwnerUUID");
if (OwnerUUIDIdx > 0)
{
OwnerUUID = cMojangAPI::MakeUUIDShort(a_NBT.GetString(OwnerUUIDIdx));
OwnerUUID = a_NBT.GetString(OwnerUUIDIdx);
}
int OwnerIdx = a_NBT.FindChildByName(a_TagIdx, "Owner");
if (OwnerIdx > 0)
@ -2520,6 +2520,11 @@ void cWSSAnvil::LoadWolfOwner(cWolf & a_Wolf, const cParsedNBT & a_NBT, int a_Ta
return;
}
}
else
{
// Normalize the UUID:
OwnerUUID = cMojangAPI::MakeUUIDShort(OwnerUUID);
}
// Convert UUID to name, if needed:
if (OwnerName.empty())