diff options
author | ShadowNinja <shadowninja@minetest.net> | 2016-03-19 12:08:24 -0400 |
---|---|---|
committer | ShadowNinja <shadowninja@minetest.net> | 2016-03-19 21:27:57 -0400 |
commit | 93887043d9443ba6627fea68921994d0f1896517 (patch) | |
tree | 7199a98377b19afc01517ab795bc4d6dfa073130 | |
parent | eb7db21d50f866d1e847d7556f83995294a7e54f (diff) | |
download | minetest-93887043d9443ba6627fea68921994d0f1896517.tar.gz minetest-93887043d9443ba6627fea68921994d0f1896517.tar.bz2 minetest-93887043d9443ba6627fea68921994d0f1896517.zip |
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.
-rw-r--r-- | src/ban.cpp | 2 | ||||
-rw-r--r-- | src/chat.cpp | 4 | ||||
-rw-r--r-- | src/client/tile.cpp | 4 | ||||
-rw-r--r-- | src/craftdef.cpp | 4 | ||||
-rw-r--r-- | src/guiFormSpecMenu.cpp | 2 | ||||
-rw-r--r-- | src/inventory.cpp | 6 | ||||
-rw-r--r-- | src/inventorymanager.cpp | 2 | ||||
-rw-r--r-- | src/mods.cpp | 4 | ||||
-rw-r--r-- | src/network/clientpackethandler.cpp | 4 | ||||
-rw-r--r-- | src/settings.cpp | 2 | ||||
-rw-r--r-- | src/shader.cpp | 1 | ||||
-rw-r--r-- | src/strfnd.h | 176 | ||||
-rw-r--r-- | src/subgame.cpp | 8 | ||||
-rw-r--r-- | src/util/strfnd.h | 82 |
14 files changed, 103 insertions, 198 deletions
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 <sstream> #include <set> -#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 <cctype> #include <sstream> #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 <IGUICheckBox.h> #include <IGUIEditBox.h> #include <IGUIButton.h> 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 <sstream> #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 <fstream> #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 <iostream> #include <fstream> #include <sstream> 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 <celeron55@gmail.com> - -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 <string> - -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<<"\" plop=\""<<plop<<"\""<<std::endl; - size_t n; - std::string palautus; - if (p < tek.size()) - { - //std::cout<<"\tp<tek.size()"<<std::endl; - if ((n = tek.find(plop, p)) == std::string::npos || plop == "") - { - //std::cout<<"\t\tn == string::npos || plop == \"\""<<std::endl; - n = tek.size(); - } - else - { - //std::cout<<"\t\tn != string::npos"<<std::endl; - } - palautus = tek.substr(p, n-p); - p = n + plop.length(); - } - //else - //std::cout<<"\tp>=tek.size()"<<std::endl; - //std::cout<<"palautus=\""<<palautus<<"\""<<std::endl; - return palautus; - } - - // Returns substr of tek up to the next occurence of plop that isn't escaped with '\' - std::string next_esc(std::string plop) { - size_t n, realp; - - if (p >= 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<chars.size(); i++){ - if(chars[i] == tek[p]){ - is = true; - break; - } - } - if(!is) break; - p++; - } - } - bool atend(){ - if(p>=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<<"\" plop=\""<<plop<<"\""<<std::endl; - size_t n; - std::wstring palautus; - if (p < tek.size()) - { - //std::cout<<"\tp<tek.size()"<<std::endl; - if ((n = tek.find(plop, p)) == std::wstring::npos || plop == L"") - { - //std::cout<<"\t\tn == string::npos || plop == \"\""<<std::endl; - n = tek.size(); - } - else - { - //std::cout<<"\t\tn != string::npos"<<std::endl; - } - palautus = tek.substr(p, n-p); - p = n + plop.length(); - } - //else - //std::cout<<"\tp>=tek.size()"<<std::endl; - //std::cout<<"palautus=\""<<palautus<<"\""<<std::endl; - return palautus; - } - - std::wstring next_esc(std::wstring plop) { - size_t n, realp; - - if (p >= 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<std::string> getAvailableGameIds() Strfnd search_paths(getSubgamePathEnv()); - while (!search_paths.atend()) + while (!search_paths.at_end()) gamespaths.insert(search_paths.next(PATH_DELIM)); for (std::set<std::string>::const_iterator i = gamespaths.begin(); @@ -230,7 +230,7 @@ std::vector<WorldSpec> 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 <celeron55@gmail.com> + +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 <string> + +template <typename T> +class BasicStrfnd { + typedef std::basic_string<T> 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<T>('\\')) + { + 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<char> Strfnd; +typedef BasicStrfnd<wchar_t> WStrfnd; + +#endif + |