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.
This commit is contained in:
ShadowNinja 2016-03-19 12:08:24 -04:00
parent eb7db21d50
commit 93887043d9
14 changed files with 103 additions and 198 deletions

@ -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"

@ -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);

@ -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(":");

@ -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;
}

@ -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>

@ -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);

@ -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<<")"

@ -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"

@ -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);

@ -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>

@ -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"
/*

@ -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

@ -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");

82
src/util/strfnd.h Normal file

@ -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