From 30dd49235d0091ee93ed86562f5191c28bb4de74 Mon Sep 17 00:00:00 2001 From: mniip Date: Mon, 14 May 2018 05:09:44 +0300 Subject: [PATCH] Add ToLower/ToUpper --- src/client/Client.cpp | 8 +--- src/common/String.h | 44 +++++++++++++++++++ .../elementsearch/ElementSearchActivity.cpp | 6 +-- src/gui/filebrowser/FileBrowserActivity.cpp | 6 +-- src/gui/preview/PreviewView.cpp | 3 +- src/lua/LegacyLuaAPI.cpp | 4 +- src/lua/LuaScriptInterface.cpp | 23 +++------- 7 files changed, 56 insertions(+), 38 deletions(-) diff --git a/src/client/Client.cpp b/src/client/Client.cpp index a43741dc1..0d459006e 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -468,9 +468,7 @@ std::vector Client::DirectorySearch(ByteString directory, ByteString { std::vector extensions; extensions.push_back(extension); - for (ByteString::iterator iter = search.begin(); iter != search.end(); ++iter) - *iter = toupper(*iter); - return DirectorySearch(directory, search, extensions); + return DirectorySearch(directory, search.ToUpper(), extensions); } std::vector Client::DirectorySearch(ByteString directory, ByteString search, std::vector extensions) @@ -531,12 +529,10 @@ std::vector Client::DirectorySearch(ByteString directory, ByteString if(filename.EndsWith(*extIter)) { extensionMatch = true; - tempfilename = filename.SubstrFromEnd(0, (*extIter).size()); + tempfilename = filename.SubstrFromEnd(0, (*extIter).size()).ToUpper(); break; } } - for (ByteString::iterator iter = tempfilename.begin(); iter != tempfilename.end(); ++iter) - *iter = toupper(*iter); bool searchMatch = !search.size(); if(search.size() && tempfilename.Contains(search)) searchMatch = true; diff --git a/src/common/String.h b/src/common/String.h index ea024be01..bf4550824 100644 --- a/src/common/String.h +++ b/src/common/String.h @@ -4,6 +4,7 @@ #include #include #include +#include #include /* @@ -56,6 +57,11 @@ Contains(value_type infix) Self-explanatory. + ToLower() + ToUpper() + Lowercases/Uppercases characters in the string. Only works on + characters in the ASCII range. + ByteString::FromUtf8(bool ignoreError = true) Decodes UTF-8 byte sequences into Unicode codepoints. If ignoreError is true, then invalid byte sequences are widened @@ -324,6 +330,24 @@ public: inline ByteString &Erase(size_t pos, size_t count) { super::erase(pos, count); return *this; } inline ByteString &EraseBetween(size_t from, size_t to) { if(from < to) super::erase(from, to - from); return *this; } + inline ByteString ToLower() const + { + std::locale const &loc = std::locale::classic(); + ByteString value(*this); + for(value_type &ch : value) + ch = std::tolower(ch, loc); + return value; + } + + inline ByteString ToUpper() const + { + std::locale const &loc = std::locale::classic(); + ByteString value(*this); + for(value_type &ch : value) + ch = std::toupper(ch, loc); + return value; + } + String FromUtf8(bool ignoreError = true) const; inline String FromAscii() const; template static ByteString Build(Ts&&... args); @@ -479,6 +503,26 @@ public: inline String &Erase(size_t pos, size_t count) { super::erase(pos, count); return *this; } inline String &EraseBetween(size_t from, size_t to) { if(from < to) super::erase(from, to - from); return *this; } + inline String ToLower() const + { + std::locale const &loc = std::locale::classic(); + String value(*this); + for(value_type &ch : value) + if(ch <= std::numeric_limits::max()) + ch = std::tolower(ch, loc); + return value; + } + + inline String ToUpper() const + { + std::locale const &loc = std::locale::classic(); + String value(*this); + for(value_type &ch : value) + if(ch <= std::numeric_limits::max()) + ch = std::toupper(ch, loc); + return value; + } + ByteString ToUtf8() const; ByteString ToAscii() const; template static String Build(Ts&&... args); diff --git a/src/gui/elementsearch/ElementSearchActivity.cpp b/src/gui/elementsearch/ElementSearchActivity.cpp index 9ad8d0853..6f23220fb 100644 --- a/src/gui/elementsearch/ElementSearchActivity.cpp +++ b/src/gui/elementsearch/ElementSearchActivity.cpp @@ -104,8 +104,7 @@ void ElementSearchActivity::searchTools(String query) ui::Point viewPosition = searchField->Position + ui::Point(2+0, searchField->Size.Y+2+8); ui::Point current = ui::Point(0, 0); - ByteString queryLower = query.ToAscii(); - std::transform(queryLower.begin(), queryLower.end(), queryLower.begin(), ::tolower); + ByteString queryLower = query.ToUtf8().ToLower(); std::vector matches; std::vector frontmatches; @@ -113,8 +112,7 @@ void ElementSearchActivity::searchTools(String query) for(std::vector::const_iterator iter = tools.begin(), end = tools.end(); iter != end; ++iter) { - ByteString nameLower = (*iter)->GetName(); - std::transform(nameLower.begin(), nameLower.end(), nameLower.begin(), ::tolower); + ByteString nameLower = (*iter)->GetName().ToLower(); if(nameLower == queryLower) exactmatches.push_back(*iter); else if(nameLower.BeginsWith(queryLower)) diff --git a/src/gui/filebrowser/FileBrowserActivity.cpp b/src/gui/filebrowser/FileBrowserActivity.cpp index e130e9960..920d5b5ee 100644 --- a/src/gui/filebrowser/FileBrowserActivity.cpp +++ b/src/gui/filebrowser/FileBrowserActivity.cpp @@ -58,11 +58,7 @@ class LoadFilesTask: public Task virtual bool doWork() { std::vector files = Client::Ref().DirectorySearch(directory, search, ".cps"); - std::sort(files.rbegin(), files.rend(), [](ByteString a, ByteString b) { - std::transform(a.begin(), a.end(), a.begin(), ::tolower); - std::transform(b.begin(), b.end(), b.begin(), ::tolower); - return a < b; - }); + std::sort(files.rbegin(), files.rend(), [](ByteString a, ByteString b) { return a.ToLower() < b.ToLower(); }); notifyProgress(-1); for(std::vector::iterator iter = files.begin(), end = files.end(); iter != end; ++iter) diff --git a/src/gui/preview/PreviewView.cpp b/src/gui/preview/PreviewView.cpp index 05140c921..c68783dfd 100644 --- a/src/gui/preview/PreviewView.cpp +++ b/src/gui/preview/PreviewView.cpp @@ -289,8 +289,7 @@ void PreviewView::CheckComment() { if (!commentWarningLabel) return; - String text = addCommentBox->GetText(); - std::transform(text.begin(), text.end(), text.begin(), ::tolower); + String text = addCommentBox->GetText().ToLower(); if (!userIsAuthor && (text.Contains("stolen") || text.Contains("copied"))) { if (!commentHelpText) diff --git a/src/lua/LegacyLuaAPI.cpp b/src/lua/LegacyLuaAPI.cpp index f126cd05e..652a7f153 100644 --- a/src/lua/LegacyLuaAPI.cpp +++ b/src/lua/LegacyLuaAPI.cpp @@ -56,9 +56,7 @@ void initLegacyProps() else { - ByteString temp = ByteString(prop.Name); - std::transform(temp.begin(), temp.end(), temp.begin(), ::tolower); - legacyPropNames.insert(std::pair(temp, prop)); + legacyPropNames.insert(std::pair(prop.Name.ToLower(), prop)); } } } diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index caf7b66c2..a4fc601f2 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -138,7 +138,6 @@ LuaScriptInterface::LuaScriptInterface(GameController * c, GameModel * m): initPlatformAPI(); //Old TPT API - char tmpname[12]; int currentElementMeta, currentElement; const static struct luaL_Reg tptluaapi [] = { {"test", &luatpt_test}, @@ -295,10 +294,6 @@ tpt.partsdata = nil"); tptElements = lua_gettop(l); for (int i = 1; i < PT_NUM; i++) { - for (size_t j = 0; j < luacon_sim->elements[i].Name.size(); j++) - tmpname[j] = tolower(luacon_sim->elements[i].Name[j]); - tmpname[luacon_sim->elements[i].Name.size()] = 0; - lua_newtable(l); currentElement = lua_gettop(l); lua_pushinteger(l, i); @@ -312,7 +307,7 @@ tpt.partsdata = nil"); lua_setfield(l, currentElementMeta, "__index"); lua_setmetatable(l, currentElement); - lua_setfield(l, tptElements, tmpname); + lua_setfield(l, tptElements, luacon_sim->elements[i].Name.ToLower().c_str()); } lua_setfield(l, tptProperties, "el"); @@ -320,10 +315,6 @@ tpt.partsdata = nil"); tptElementTransitions = lua_gettop(l); for (int i = 1; i < PT_NUM; i++) { - for (size_t j = 0; j < luacon_sim->elements[i].Name.size(); j++) - tmpname[j] = tolower(luacon_sim->elements[i].Name[j]); - tmpname[luacon_sim->elements[i].Name.size()] = 0; - lua_newtable(l); currentElement = lua_gettop(l); lua_newtable(l); @@ -336,7 +327,7 @@ tpt.partsdata = nil"); lua_setfield(l, currentElementMeta, "__index"); lua_setmetatable(l, currentElement); - lua_setfield(l, tptElementTransitions, tmpname); + lua_setfield(l, tptElementTransitions, luacon_sim->elements[i].Name.ToLower().c_str()); } lua_setfield(l, tptProperties, "eltransition"); @@ -810,10 +801,8 @@ void LuaScriptInterface::initSimulationAPI() particleProperties = new StructProperty[particlePropertiesV.size()]; for(std::vector::iterator iter = particlePropertiesV.begin(), end = particlePropertiesV.end(); iter != end; ++iter) { - ByteString propertyName = (*iter).Name; - std::transform(propertyName.begin(), propertyName.end(), propertyName.begin(), ::toupper); lua_pushinteger(l, particlePropertiesCount); - lua_setfield(l, -2, ("FIELD_"+propertyName).c_str()); + lua_setfield(l, -2, ("FIELD_" + (*iter).Name.ToUpper()).c_str()); particleProperties[particlePropertiesCount++] = *iter; } @@ -2533,10 +2522,8 @@ int LuaScriptInterface::elements_allocate(lua_State * l) ByteString group, id, identifier; luaL_checktype(l, 1, LUA_TSTRING); luaL_checktype(l, 2, LUA_TSTRING); - group = ByteString(lua_tostring(l, 1)); - std::transform(group.begin(), group.end(), group.begin(), ::toupper); - id = ByteString(lua_tostring(l, 2)); - std::transform(id.begin(), id.end(), id.begin(), ::toupper); + group = ByteString(lua_tostring(l, 1)).ToUpper(); + id = ByteString(lua_tostring(l, 2)).ToUpper(); if(group == "DEFAULT") return luaL_error(l, "You cannot create elements in the 'default' group.");