Clean up Strfnd
authorShadowNinja <shadowninja@minetest.net>
Sat, 19 Mar 2016 16:08:24 +0000 (12:08 -0400)
committerShadowNinja <shadowninja@minetest.net>
Sun, 20 Mar 2016 01:27:57 +0000 (21:27 -0400)
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.

14 files changed:
src/ban.cpp
src/chat.cpp
src/client/tile.cpp
src/craftdef.cpp
src/guiFormSpecMenu.cpp
src/inventory.cpp
src/inventorymanager.cpp
src/mods.cpp
src/network/clientpackethandler.cpp
src/settings.cpp
src/shader.cpp
src/strfnd.h [deleted file]
src/subgame.cpp
src/util/strfnd.h [new file with mode: 0644]

index 57b9f49a57aba6f02e33a940cd384d2bbe72fa98..5fa430702724feee2b94fa205559be5759ea50e8 100644 (file)
@@ -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"
index 495e3450b2d132d6203a383176ae551b5d5e31fa..7a5196ed506bb307966f9aedb2f8b9af017e181d 100644 (file)
@@ -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);
index 995526ea8c2a5f9c624ff3cabe81835280fa063c..7a9bc0159e4872b7cabf0508a4e8355123cbaed5 100644 (file)
@@ -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(":");
index 67c3ae62ac39205be2f37abc7f15457c1aba58d2..d3f1edaf9ae9707455c04ecbad8387a1d53984e7 100644 (file)
@@ -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;
        }
index 2e9421b3ee91b0b31ef811fe4d95b97367204b2b..6492eb9d915896076df68d04b0303c09318c0b72 100644 (file)
@@ -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>
index e89993e63d773f43cd610ee54cb9a1ab26be12db..fce8575e79ade5fa3d870d44981a0108028177a9 100644 (file)
@@ -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);
index 5b29b6f173531ccb1d2622e780f1867c41cccbf1..3d85134923a87beee47bb48b1403c74d3cb907a2 100644 (file)
@@ -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<<")"
 
index be6e1e5d30f06a1dba0b77053a346a944fc5e474..1b1bdb07b69ca7a420fed58fd58602adb3b82e60 100644 (file)
@@ -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"
 
index 28d147d38e3356622d5ebb54ee97618e3a70194b..0498f4048eb7bd1a2e916c97ce1a5e6baaa3a45e 100644 (file)
@@ -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);
index 8ea687d1c8b7cff41c7fb6c6490b3df9bd164477..56afa613391b41b73be76f991e79286727f47823 100644 (file)
@@ -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>
index ed6585bd6f71309f2a390b2351491c282b41e721..e13ab8df30ef264282f2e823851a4c6fc16d2d5c 100644 (file)
@@ -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 (file)
index 3142cc1..0000000
+++ /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
-
index 20f5116e2b7772238c4a00a4e08e192137d3d70c..7e9a0b368e3452f99821c7a39359c5b163086ea2 100644 (file)
@@ -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 (file)
index 0000000..a7cd2ba
--- /dev/null
@@ -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
+