Skip to content

Commit

Permalink
Add ToLower/ToUpper
Browse files Browse the repository at this point in the history
  • Loading branch information
mniip committed May 14, 2018
1 parent 36a5451 commit 30dd492
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 38 deletions.
8 changes: 2 additions & 6 deletions src/client/Client.cpp
Expand Up @@ -468,9 +468,7 @@ std::vector<ByteString> Client::DirectorySearch(ByteString directory, ByteString
{
std::vector<ByteString> 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<ByteString> Client::DirectorySearch(ByteString directory, ByteString search, std::vector<ByteString> extensions)
Expand Down Expand Up @@ -531,12 +529,10 @@ std::vector<ByteString> 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;
Expand Down
44 changes: 44 additions & 0 deletions src/common/String.h
Expand Up @@ -4,6 +4,7 @@
#include <sstream>
#include <vector>
#include <string>
#include <limits>
#include <ios>

/*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -324,6 +330,24 @@ class ByteString : public std::basic_string<char>
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<typename... Ts> static ByteString Build(Ts&&... args);
Expand Down Expand Up @@ -479,6 +503,26 @@ class String : public std::basic_string<char32_t>
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<ByteString::value_type>::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<ByteString::value_type>::max())
ch = std::toupper(ch, loc);
return value;
}

ByteString ToUtf8() const;
ByteString ToAscii() const;
template<typename... Ts> static String Build(Ts&&... args);
Expand Down
6 changes: 2 additions & 4 deletions src/gui/elementsearch/ElementSearchActivity.cpp
Expand Up @@ -104,17 +104,15 @@ 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<Tool *> matches;
std::vector<Tool *> frontmatches;
std::vector<Tool *> exactmatches;

for(std::vector<Tool*>::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))
Expand Down
6 changes: 1 addition & 5 deletions src/gui/filebrowser/FileBrowserActivity.cpp
Expand Up @@ -58,11 +58,7 @@ class LoadFilesTask: public Task
virtual bool doWork()
{
std::vector<ByteString> 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<ByteString>::iterator iter = files.begin(), end = files.end(); iter != end; ++iter)
Expand Down
3 changes: 1 addition & 2 deletions src/gui/preview/PreviewView.cpp
Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions src/lua/LegacyLuaAPI.cpp
Expand Up @@ -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<ByteString, StructProperty>(temp, prop));
legacyPropNames.insert(std::pair<ByteString, StructProperty>(prop.Name.ToLower(), prop));
}
}
}
Expand Down
23 changes: 5 additions & 18 deletions src/lua/LuaScriptInterface.cpp
Expand Up @@ -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},
Expand Down Expand Up @@ -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);
Expand All @@ -312,18 +307,14 @@ 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");

lua_newtable(l);
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);
Expand All @@ -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");

Expand Down Expand Up @@ -810,10 +801,8 @@ void LuaScriptInterface::initSimulationAPI()
particleProperties = new StructProperty[particlePropertiesV.size()];
for(std::vector<StructProperty>::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;
}

Expand Down Expand Up @@ -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.");
Expand Down

0 comments on commit 30dd492

Please sign in to comment.