diff options
author | Mattes D <github@xoft.cz> | 2014-08-04 12:02:57 +0200 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2014-08-04 12:02:57 +0200 |
commit | 7b986e65d2b7d7cad6768599ba4298b62b16c35f (patch) | |
tree | dd627e5e467d4bf0f3ed47aa1201b6ab94e9c35a /src | |
parent | Merge pull request #1283 from Howaner/GlobalFixes (diff) | |
parent | Refactored case-conversion functions. (diff) | |
download | cuberite-7b986e65d2b7d7cad6768599ba4298b62b16c35f.tar cuberite-7b986e65d2b7d7cad6768599ba4298b62b16c35f.tar.gz cuberite-7b986e65d2b7d7cad6768599ba4298b62b16c35f.tar.bz2 cuberite-7b986e65d2b7d7cad6768599ba4298b62b16c35f.tar.lz cuberite-7b986e65d2b7d7cad6768599ba4298b62b16c35f.tar.xz cuberite-7b986e65d2b7d7cad6768599ba4298b62b16c35f.tar.zst cuberite-7b986e65d2b7d7cad6768599ba4298b62b16c35f.zip |
Diffstat (limited to '')
-rw-r--r-- | src/HTTPServer/HTTPMessage.cpp | 3 | ||||
-rw-r--r-- | src/Mobs/Monster.cpp | 3 | ||||
-rw-r--r-- | src/Protocol/MojangAPI.cpp | 52 | ||||
-rw-r--r-- | src/Protocol/MojangAPI.h | 4 | ||||
-rw-r--r-- | src/StringUtils.cpp | 25 | ||||
-rw-r--r-- | src/StringUtils.h | 7 | ||||
-rw-r--r-- | src/WorldStorage/WSSAnvil.cpp | 7 |
7 files changed, 54 insertions, 47 deletions
diff --git a/src/HTTPServer/HTTPMessage.cpp b/src/HTTPServer/HTTPMessage.cpp index 4226352e9..65d73fd87 100644 --- a/src/HTTPServer/HTTPMessage.cpp +++ b/src/HTTPServer/HTTPMessage.cpp @@ -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()) { diff --git a/src/Mobs/Monster.cpp b/src/Mobs/Monster.cpp index 94df991a3..53334753a 100644 --- a/src/Mobs/Monster.cpp +++ b/src/Mobs/Monster.cpp @@ -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; diff --git a/src/Protocol/MojangAPI.cpp b/src/Protocol/MojangAPI.cpp index 9fdf07380..a7aea5490 100644 --- a/src/Protocol/MojangAPI.cpp +++ b/src/Protocol/MojangAPI.cpp @@ -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); } } diff --git a/src/Protocol/MojangAPI.h b/src/Protocol/MojangAPI.h index 08e799c73..6ed37625e 100644 --- a/src/Protocol/MojangAPI.h +++ b/src/Protocol/MojangAPI.h @@ -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); diff --git a/src/StringUtils.cpp b/src/StringUtils.cpp index 0e30e8ebb..5f88cbf64 100644 --- a/src/StringUtils.cpp +++ b/src/StringUtils.cpp @@ -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 } diff --git a/src/StringUtils.h b/src/StringUtils.h index 65363382d..142aaf59b 100644 --- a/src/StringUtils.h +++ b/src/StringUtils.h @@ -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); diff --git a/src/WorldStorage/WSSAnvil.cpp b/src/WorldStorage/WSSAnvil.cpp index 434f1e21f..a9c9ae4b5 100644 --- a/src/WorldStorage/WSSAnvil.cpp +++ b/src/WorldStorage/WSSAnvil.cpp @@ -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()) |