Skip to content

Commit 9388704

Browse files
committedMar 20, 2016
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.
1 parent eb7db21 commit 9388704

14 files changed

+103
-198
lines changed
 

Diff for: ‎src/ban.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2222
#include "threading/mutex_auto_lock.h"
2323
#include <sstream>
2424
#include <set>
25-
#include "strfnd.h"
25+
#include "util/strfnd.h"
2626
#include "util/string.h"
2727
#include "log.h"
2828
#include "filesys.h"

Diff for: ‎src/chat.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
1919

2020
#include "chat.h"
2121
#include "debug.h"
22-
#include "strfnd.h"
22+
#include "util/strfnd.h"
2323
#include <cctype>
2424
#include <sstream>
2525
#include "util/string.h"
@@ -684,7 +684,7 @@ void ChatBackend::addMessage(std::wstring name, std::wstring text)
684684

685685
// Note: A message may consist of multiple lines, for example the MOTD.
686686
WStrfnd fnd(text);
687-
while (!fnd.atend())
687+
while (!fnd.at_end())
688688
{
689689
std::wstring line = fnd.next(L"\n");
690690
m_console_buffer.addLine(name, line);

Diff for: ‎src/client/tile.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3131
#include "mesh.h"
3232
#include "log.h"
3333
#include "gamedef.h"
34-
#include "strfnd.h"
34+
#include "util/strfnd.h"
3535
#include "util/string.h" // for parseColorString()
3636
#include "imagefilters.h"
3737
#include "guiscalingfilter.h"
@@ -1242,7 +1242,7 @@ bool TextureSource::generateImagePart(std::string part_of_name,
12421242
baseimg = driver->createImage(video::ECF_A8R8G8B8, dim);
12431243
baseimg->fill(video::SColor(0,0,0,0));
12441244
}
1245-
while (sf.atend() == false) {
1245+
while (sf.at_end() == false) {
12461246
u32 x = stoi(sf.next(","));
12471247
u32 y = stoi(sf.next("="));
12481248
std::string filename = sf.next(":");

Diff for: ‎src/craftdef.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2929
#include "util/serialize.h"
3030
#include "util/string.h"
3131
#include "util/numeric.h"
32-
#include "strfnd.h"
32+
#include "util/strfnd.h"
3333
#include "exceptions.h"
3434

3535
inline bool isGroupRecipeStr(const std::string &rec_name)
@@ -90,7 +90,7 @@ static bool inputItemMatchesRecipe(const std::string &inp_name,
9090
all_groups_match = false;
9191
break;
9292
}
93-
} while (!f.atend());
93+
} while (!f.at_end());
9494
if (all_groups_match)
9595
return true;
9696
}

Diff for: ‎src/guiFormSpecMenu.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2828
#include "constants.h"
2929
#include "gamedef.h"
3030
#include "keycode.h"
31-
#include "strfnd.h"
31+
#include "util/strfnd.h"
3232
#include <IGUICheckBox.h>
3333
#include <IGUIEditBox.h>
3434
#include <IGUIButton.h>

Diff for: ‎src/inventory.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2323
#include <sstream>
2424
#include "log.h"
2525
#include "itemdef.h"
26-
#include "strfnd.h"
26+
#include "util/strfnd.h"
2727
#include "content_mapnode.h" // For loading legacy MaterialItems
2828
#include "nameidmapping.h" // For loading legacy MaterialItems
2929
#include "util/serialize.h"
@@ -218,7 +218,7 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
218218
Strfnd fnd(all);
219219
fnd.next("\"");
220220
// If didn't skip to end, we have ""s
221-
if(!fnd.atend()){
221+
if(!fnd.at_end()){
222222
name = fnd.next("\"");
223223
} else { // No luck, just read a word then
224224
fnd.start(all);
@@ -246,7 +246,7 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
246246
Strfnd fnd(all);
247247
fnd.next("\"");
248248
// If didn't skip to end, we have ""s
249-
if(!fnd.atend()){
249+
if(!fnd.at_end()){
250250
name = fnd.next("\"");
251251
} else { // No luck, just read a word then
252252
fnd.start(all);

Diff for: ‎src/inventorymanager.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2525
#include "settings.h"
2626
#include "craftdef.h"
2727
#include "rollback_interface.h"
28-
#include "strfnd.h"
28+
#include "util/strfnd.h"
2929

3030
#define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
3131

Diff for: ‎src/mods.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2121
#include <fstream>
2222
#include "mods.h"
2323
#include "filesys.h"
24-
#include "strfnd.h"
24+
#include "util/strfnd.h"
2525
#include "log.h"
2626
#include "subgame.h"
2727
#include "settings.h"
28-
#include "strfnd.h"
28+
#include "util/strfnd.h"
2929
#include "convert_json.h"
3030
#include "exceptions.h"
3131

Diff for: ‎src/network/clientpackethandler.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2828
#include "nodedef.h"
2929
#include "serialization.h"
3030
#include "server.h"
31-
#include "strfnd.h"
31+
#include "util/strfnd.h"
3232
#include "network/clientopcodes.h"
3333
#include "util/serialize.h"
3434
#include "util/srp.h"
@@ -641,7 +641,7 @@ void Client::handleCommand_AnnounceMedia(NetworkPacket* pkt)
641641
*pkt >> str;
642642

643643
Strfnd sf(str);
644-
while(!sf.atend()) {
644+
while(!sf.at_end()) {
645645
std::string baseurl = trim(sf.next(","));
646646
if (baseurl != "")
647647
m_media_downloader->addRemoteServer(baseurl);

Diff for: ‎src/settings.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2121
#include "irrlichttypes_bloated.h"
2222
#include "exceptions.h"
2323
#include "threading/mutex_auto_lock.h"
24-
#include "strfnd.h"
24+
#include "util/strfnd.h"
2525
#include <iostream>
2626
#include <fstream>
2727
#include <sstream>

Diff for: ‎src/shader.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3535
#include "EShaderTypes.h"
3636
#include "log.h"
3737
#include "gamedef.h"
38-
#include "strfnd.h" // trim()
3938
#include "client/tile.h"
4039

4140
/*

Diff for: ‎src/strfnd.h

-176
This file was deleted.

Diff for: ‎src/subgame.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2222
#include "filesys.h"
2323
#include "settings.h"
2424
#include "log.h"
25-
#include "strfnd.h"
25+
#include "util/strfnd.h"
2626
#include "defaultsettings.h" // for override_default_settings
2727
#include "mapgen.h" // for MapgenParams
2828
#include "util/string.h"
@@ -79,7 +79,7 @@ SubgameSpec findSubgame(const std::string &id)
7979

8080
Strfnd search_paths(getSubgamePathEnv());
8181

82-
while (!search_paths.atend()) {
82+
while (!search_paths.at_end()) {
8383
std::string path = search_paths.next(PATH_DELIM);
8484
find_paths.push_back(GameFindPath(
8585
path + DIR_DELIM + id, false));
@@ -153,7 +153,7 @@ std::set<std::string> getAvailableGameIds()
153153

154154
Strfnd search_paths(getSubgamePathEnv());
155155

156-
while (!search_paths.atend())
156+
while (!search_paths.at_end())
157157
gamespaths.insert(search_paths.next(PATH_DELIM));
158158

159159
for (std::set<std::string>::const_iterator i = gamespaths.begin();
@@ -230,7 +230,7 @@ std::vector<WorldSpec> getAvailableWorlds()
230230

231231
Strfnd search_paths(getWorldPathEnv());
232232

233-
while (!search_paths.atend())
233+
while (!search_paths.at_end())
234234
worldspaths.insert(search_paths.next(PATH_DELIM));
235235

236236
worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");

Diff for: ‎src/util/strfnd.h

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Minetest
3+
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4+
5+
This program is free software; you can redistribute it and/or modify
6+
it under the terms of the GNU Lesser General Public License as published by
7+
the Free Software Foundation; either version 2.1 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public License along
16+
with this program; if not, write to the Free Software Foundation, Inc.,
17+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*/
19+
20+
#ifndef STRFND_HEADER
21+
#define STRFND_HEADER
22+
23+
#include <string>
24+
25+
template <typename T>
26+
class BasicStrfnd {
27+
typedef std::basic_string<T> String;
28+
String str;
29+
size_t pos;
30+
public:
31+
BasicStrfnd(const String &s) : str(s), pos(0) {}
32+
void start(const String &s) { str = s; pos = 0; }
33+
size_t where() { return pos; }
34+
void to(size_t i) { pos = i; }
35+
bool at_end() { return pos >= str.size(); }
36+
String what() { return str; }
37+
38+
String next(const String &sep)
39+
{
40+
if (pos >= str.size())
41+
return String();
42+
43+
size_t n;
44+
if (sep.empty() || (n = str.find(sep, pos)) == String::npos) {
45+
n = str.size();
46+
}
47+
String ret = str.substr(pos, n - pos);
48+
pos = n + sep.size();
49+
return ret;
50+
}
51+
52+
// Returns substr up to the next occurence of sep that isn't escaped with esc ('\\')
53+
String next_esc(const String &sep, T esc=static_cast<T>('\\'))
54+
{
55+
if (pos >= str.size())
56+
return String();
57+
58+
size_t n, old_p = pos;
59+
do {
60+
if (sep.empty() || (n = str.find(sep, pos)) == String::npos) {
61+
pos = n = str.size();
62+
break;
63+
}
64+
pos = n + sep.length();
65+
} while (n > 0 && str[n - 1] == esc);
66+
67+
return str.substr(old_p, n - old_p);
68+
}
69+
70+
void skip_over(const String &chars)
71+
{
72+
size_t p = str.find_first_not_of(chars, pos);
73+
if (p != String::npos)
74+
pos = p;
75+
}
76+
};
77+
78+
typedef BasicStrfnd<char> Strfnd;
79+
typedef BasicStrfnd<wchar_t> WStrfnd;
80+
81+
#endif
82+

0 commit comments

Comments
 (0)
Please sign in to comment.