From 93887043d9443ba6627fea68921994d0f1896517 Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Sat, 19 Mar 2016 12:08:24 -0400 Subject: [PATCH] Clean up Strfnd Changes: * Fix indentation. * Pass strings by const reference. * Merge Strfnd and WStrfnd into one class instead of copying them. * Remove trailing spaces. * Fix variable names. * Move to util. * Other miscellaneous style fixes. --- src/ban.cpp | 2 +- src/chat.cpp | 4 +- src/client/tile.cpp | 4 +- src/craftdef.cpp | 4 +- src/guiFormSpecMenu.cpp | 2 +- src/inventory.cpp | 6 +- src/inventorymanager.cpp | 2 +- src/mods.cpp | 4 +- src/network/clientpackethandler.cpp | 4 +- src/settings.cpp | 2 +- src/shader.cpp | 1 - src/strfnd.h | 176 ---------------------------- src/subgame.cpp | 8 +- src/util/strfnd.h | 82 +++++++++++++ 14 files changed, 103 insertions(+), 198 deletions(-) delete mode 100644 src/strfnd.h create mode 100644 src/util/strfnd.h diff --git a/src/ban.cpp b/src/ban.cpp index 57b9f49a5..5fa430702 100644 --- a/src/ban.cpp +++ b/src/ban.cpp @@ -22,7 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "threading/mutex_auto_lock.h" #include #include -#include "strfnd.h" +#include "util/strfnd.h" #include "util/string.h" #include "log.h" #include "filesys.h" diff --git a/src/chat.cpp b/src/chat.cpp index 495e3450b..7a5196ed5 100644 --- a/src/chat.cpp +++ b/src/chat.cpp @@ -19,7 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "chat.h" #include "debug.h" -#include "strfnd.h" +#include "util/strfnd.h" #include #include #include "util/string.h" @@ -684,7 +684,7 @@ void ChatBackend::addMessage(std::wstring name, std::wstring text) // Note: A message may consist of multiple lines, for example the MOTD. WStrfnd fnd(text); - while (!fnd.atend()) + while (!fnd.at_end()) { std::wstring line = fnd.next(L"\n"); m_console_buffer.addLine(name, line); diff --git a/src/client/tile.cpp b/src/client/tile.cpp index 995526ea8..7a9bc0159 100644 --- a/src/client/tile.cpp +++ b/src/client/tile.cpp @@ -31,7 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "mesh.h" #include "log.h" #include "gamedef.h" -#include "strfnd.h" +#include "util/strfnd.h" #include "util/string.h" // for parseColorString() #include "imagefilters.h" #include "guiscalingfilter.h" @@ -1242,7 +1242,7 @@ bool TextureSource::generateImagePart(std::string part_of_name, baseimg = driver->createImage(video::ECF_A8R8G8B8, dim); baseimg->fill(video::SColor(0,0,0,0)); } - while (sf.atend() == false) { + while (sf.at_end() == false) { u32 x = stoi(sf.next(",")); u32 y = stoi(sf.next("=")); std::string filename = sf.next(":"); diff --git a/src/craftdef.cpp b/src/craftdef.cpp index 67c3ae62a..d3f1edaf9 100644 --- a/src/craftdef.cpp +++ b/src/craftdef.cpp @@ -29,7 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/serialize.h" #include "util/string.h" #include "util/numeric.h" -#include "strfnd.h" +#include "util/strfnd.h" #include "exceptions.h" inline bool isGroupRecipeStr(const std::string &rec_name) @@ -90,7 +90,7 @@ static bool inputItemMatchesRecipe(const std::string &inp_name, all_groups_match = false; break; } - } while (!f.atend()); + } while (!f.at_end()); if (all_groups_match) return true; } diff --git a/src/guiFormSpecMenu.cpp b/src/guiFormSpecMenu.cpp index 2e9421b3e..6492eb9d9 100644 --- a/src/guiFormSpecMenu.cpp +++ b/src/guiFormSpecMenu.cpp @@ -28,7 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "constants.h" #include "gamedef.h" #include "keycode.h" -#include "strfnd.h" +#include "util/strfnd.h" #include #include #include diff --git a/src/inventory.cpp b/src/inventory.cpp index e89993e63..fce8575e7 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -23,7 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include "log.h" #include "itemdef.h" -#include "strfnd.h" +#include "util/strfnd.h" #include "content_mapnode.h" // For loading legacy MaterialItems #include "nameidmapping.h" // For loading legacy MaterialItems #include "util/serialize.h" @@ -218,7 +218,7 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef) Strfnd fnd(all); fnd.next("\""); // If didn't skip to end, we have ""s - if(!fnd.atend()){ + if(!fnd.at_end()){ name = fnd.next("\""); } else { // No luck, just read a word then fnd.start(all); @@ -246,7 +246,7 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef) Strfnd fnd(all); fnd.next("\""); // If didn't skip to end, we have ""s - if(!fnd.atend()){ + if(!fnd.at_end()){ name = fnd.next("\""); } else { // No luck, just read a word then fnd.start(all); diff --git a/src/inventorymanager.cpp b/src/inventorymanager.cpp index 5b29b6f17..3d8513492 100644 --- a/src/inventorymanager.cpp +++ b/src/inventorymanager.cpp @@ -25,7 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "settings.h" #include "craftdef.h" #include "rollback_interface.h" -#include "strfnd.h" +#include "util/strfnd.h" #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")" diff --git a/src/mods.cpp b/src/mods.cpp index be6e1e5d3..1b1bdb07b 100644 --- a/src/mods.cpp +++ b/src/mods.cpp @@ -21,11 +21,11 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include "mods.h" #include "filesys.h" -#include "strfnd.h" +#include "util/strfnd.h" #include "log.h" #include "subgame.h" #include "settings.h" -#include "strfnd.h" +#include "util/strfnd.h" #include "convert_json.h" #include "exceptions.h" diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index 28d147d38..0498f4048 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -28,7 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "nodedef.h" #include "serialization.h" #include "server.h" -#include "strfnd.h" +#include "util/strfnd.h" #include "network/clientopcodes.h" #include "util/serialize.h" #include "util/srp.h" @@ -641,7 +641,7 @@ void Client::handleCommand_AnnounceMedia(NetworkPacket* pkt) *pkt >> str; Strfnd sf(str); - while(!sf.atend()) { + while(!sf.at_end()) { std::string baseurl = trim(sf.next(",")); if (baseurl != "") m_media_downloader->addRemoteServer(baseurl); diff --git a/src/settings.cpp b/src/settings.cpp index 8ea687d1c..56afa6133 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -21,7 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "irrlichttypes_bloated.h" #include "exceptions.h" #include "threading/mutex_auto_lock.h" -#include "strfnd.h" +#include "util/strfnd.h" #include #include #include diff --git a/src/shader.cpp b/src/shader.cpp index ed6585bd6..e13ab8df3 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -35,7 +35,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "EShaderTypes.h" #include "log.h" #include "gamedef.h" -#include "strfnd.h" // trim() #include "client/tile.h" /* diff --git a/src/strfnd.h b/src/strfnd.h deleted file mode 100644 index 3142cc10d..000000000 --- a/src/strfnd.h +++ /dev/null @@ -1,176 +0,0 @@ -/* -Minetest -Copyright (C) 2013 celeron55, Perttu Ahola - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation; either version 2.1 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Lesser General Public License for more details. - -You should have received a copy of the GNU Lesser General Public License along -with this program; if not, write to the Free Software Foundation, Inc., -51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -*/ - -#ifndef STRFND_HEADER -#define STRFND_HEADER - -#include - -class Strfnd{ - std::string tek; - unsigned int p; -public: - void start(std::string niinq){ - tek = niinq; - p=0; - } - unsigned int where(){ - return p; - } - void to(unsigned int i){ - p = i; - } - std::string what(){ - return tek; - } - std::string next(std::string plop){ - //std::cout<<"tek=\""<=tek.size()"<= tek.size()) - return ""; - - realp = p; - do { - n = tek.find(plop, p); - if (n == std::string::npos || plop == "") - n = tek.length(); - p = n + plop.length(); - } while (n > 0 && tek[n - 1] == '\\'); - - return tek.substr(realp, n - realp); - } - - void skip_over(std::string chars){ - while(p < tek.size()){ - bool is = false; - for(unsigned int i=0; i=tek.size()) return true; - return false; - } - Strfnd(std::string s){ - start(s); - } -}; - -class WStrfnd{ - std::wstring tek; - unsigned int p; -public: - void start(std::wstring niinq){ - tek = niinq; - p=0; - } - unsigned int where(){ - return p; - } - void to(unsigned int i){ - p = i; - } - std::wstring what(){ - return tek; - } - std::wstring next(std::wstring plop){ - //std::cout<<"tek=\""<=tek.size()"<= tek.size()) - return L""; - - realp = p; - do { - n = tek.find(plop, p); - if (n == std::wstring::npos || plop == L"") - n = tek.length(); - p = n + plop.length(); - } while (n > 0 && tek[n - 1] == '\\'); - - return tek.substr(realp, n - realp); - } - - bool atend(){ - if(p>=tek.size()) return true; - return false; - } - WStrfnd(std::wstring s){ - start(s); - } -}; - -#endif - diff --git a/src/subgame.cpp b/src/subgame.cpp index 20f5116e2..7e9a0b368 100644 --- a/src/subgame.cpp +++ b/src/subgame.cpp @@ -22,7 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "filesys.h" #include "settings.h" #include "log.h" -#include "strfnd.h" +#include "util/strfnd.h" #include "defaultsettings.h" // for override_default_settings #include "mapgen.h" // for MapgenParams #include "util/string.h" @@ -79,7 +79,7 @@ SubgameSpec findSubgame(const std::string &id) Strfnd search_paths(getSubgamePathEnv()); - while (!search_paths.atend()) { + while (!search_paths.at_end()) { std::string path = search_paths.next(PATH_DELIM); find_paths.push_back(GameFindPath( path + DIR_DELIM + id, false)); @@ -153,7 +153,7 @@ std::set getAvailableGameIds() Strfnd search_paths(getSubgamePathEnv()); - while (!search_paths.atend()) + while (!search_paths.at_end()) gamespaths.insert(search_paths.next(PATH_DELIM)); for (std::set::const_iterator i = gamespaths.begin(); @@ -230,7 +230,7 @@ std::vector getAvailableWorlds() Strfnd search_paths(getWorldPathEnv()); - while (!search_paths.atend()) + while (!search_paths.at_end()) worldspaths.insert(search_paths.next(PATH_DELIM)); worldspaths.insert(porting::path_user + DIR_DELIM + "worlds"); diff --git a/src/util/strfnd.h b/src/util/strfnd.h new file mode 100644 index 000000000..a7cd2badb --- /dev/null +++ b/src/util/strfnd.h @@ -0,0 +1,82 @@ +/* +Minetest +Copyright (C) 2013 celeron55, Perttu Ahola + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#ifndef STRFND_HEADER +#define STRFND_HEADER + +#include + +template +class BasicStrfnd { + typedef std::basic_string String; + String str; + size_t pos; +public: + BasicStrfnd(const String &s) : str(s), pos(0) {} + void start(const String &s) { str = s; pos = 0; } + size_t where() { return pos; } + void to(size_t i) { pos = i; } + bool at_end() { return pos >= str.size(); } + String what() { return str; } + + String next(const String &sep) + { + if (pos >= str.size()) + return String(); + + size_t n; + if (sep.empty() || (n = str.find(sep, pos)) == String::npos) { + n = str.size(); + } + String ret = str.substr(pos, n - pos); + pos = n + sep.size(); + return ret; + } + + // Returns substr up to the next occurence of sep that isn't escaped with esc ('\\') + String next_esc(const String &sep, T esc=static_cast('\\')) + { + if (pos >= str.size()) + return String(); + + size_t n, old_p = pos; + do { + if (sep.empty() || (n = str.find(sep, pos)) == String::npos) { + pos = n = str.size(); + break; + } + pos = n + sep.length(); + } while (n > 0 && str[n - 1] == esc); + + return str.substr(old_p, n - old_p); + } + + void skip_over(const String &chars) + { + size_t p = str.find_first_not_of(chars, pos); + if (p != String::npos) + pos = p; + } +}; + +typedef BasicStrfnd Strfnd; +typedef BasicStrfnd WStrfnd; + +#endif +