Minor refactor of strutils and add 3 new functions

Use namespace strutils:: instead of the `str_` prefix for the functions.

Remove `vec_find` as it's just an alias for `wxArrayString::Index(str)`
and is not a function on a string.

Signed-off-by: Rafael Kitover <rkitover@gmail.com>
This commit is contained in:
Rafael Kitover 2022-09-23 23:43:31 +00:00
parent 2d3610b5c6
commit 97c24ec2d2
No known key found for this signature in database
GPG Key ID: 08AB596679D86240
10 changed files with 73 additions and 72 deletions

View File

@ -7,7 +7,7 @@
void initAutoupdater()
{
// even if we are a nightly, only check latest stable version
wxString version = str_split(vbam_version, '-')[0];
wxString version = strutils::split(vbam_version, '-')[0];
#ifndef NO_HTTPS
win_sparkle_set_appcast_url("https://data.vba-m.com/appcast.xml");
#else

View File

@ -631,12 +631,12 @@ void load_opts()
cfg->Read(opt.opt, opt.stropt, *opt.stropt);
opt.curstr = *opt.stropt;
} else if (!opt.enumvals.empty()) {
auto enum_opts = str_split(opt.enumvals.MakeLower(), wxT("|"));
auto enum_opts = strutils::split(opt.enumvals.MakeLower(), wxT("|"));
opt.curint = *opt.intopt;
bool gotit = cfg->Read(opt.opt, &s); s.MakeLower();
if (gotit && !s.empty()) {
const auto found_pos = vec_find(enum_opts, s);
const auto found_pos = enum_opts.Index(s);
const bool matched = ((int)found_pos != wxNOT_FOUND);
if (!matched) {
@ -795,7 +795,7 @@ void update_opts()
} else if (!opt.enumvals.empty()) {
if (*opt.intopt != opt.curint) {
opt.curint = *opt.intopt;
auto enum_opts = str_split(opt.enumvals.MakeLower(), wxT("|"));
auto enum_opts = strutils::split(opt.enumvals.MakeLower(), wxT("|"));
cfg->Write(opt.opt, enum_opts[opt.curint]);
}
@ -938,9 +938,9 @@ bool opt_set(const wxString& name, const wxString& val)
} else if (!opt->enumvals.empty()) {
wxString s = val; s.MakeLower();
wxString ev = opt->enumvals; ev.MakeLower();
auto enum_opts = str_split(ev, wxT("|"));
auto enum_opts = strutils::split(ev, wxT("|"));
const std::size_t found_pos = vec_find(enum_opts, s);
const std::size_t found_pos = enum_opts.Index(s);
const bool matched = ((int)found_pos != wxNOT_FOUND);
if (!matched) {
@ -1016,7 +1016,7 @@ bool opt_set(const wxString& name, const wxString& val)
if (name.Find(wxT('/')) == wxNOT_FOUND)
return false;
auto parts = str_split(name, wxT("/"));
auto parts = strutils::split(name, wxT("/"));
if (parts[0] != wxT("Keyboard")) {
cmditem* cmd = std::lower_bound(&cmdtab[0], &cmdtab[ncmds], cmditem{parts[1],wxString(),0,0,NULL}, cmditem_lt);

View File

@ -1,10 +1,13 @@
#include "strutils.h"
#include <wx/tokenzr.h>
namespace strutils {
// From: https://stackoverflow.com/a/7408245/262458
//
// Modified to ignore empty tokens or return sep for them.
wxArrayString str_split(const wxString& text, const wxString& sep, bool empty_token_is_sep) {
wxArrayString split(const wxString& text, const wxString& sep, bool empty_token_is_sep) {
wxArrayString tokens;
size_t start = 0, end = 0;
@ -29,11 +32,9 @@ wxArrayString str_split(const wxString& text, const wxString& sep, bool empty_to
return tokens;
}
wxArrayString str_split_with_sep(const wxString& text, const wxString& sep)
wxArrayString split_with_sep(const wxString& text, const wxString& sep)
{
return str_split(text, sep, true);
return split(text, sep, true);
}
size_t vec_find(wxArrayString& opts, const wxString& val) {
return opts.Index(val);
}
} // namespace strutils

View File

@ -7,15 +7,16 @@
#include <wx/string.h>
#include <wx/arrstr.h>
namespace strutils {
// From: https://stackoverflow.com/a/7408245/262458
wxArrayString str_split(const wxString& text, const wxString& sep, bool empty_token_is_sep=false);
wxArrayString split(const wxString& text, const wxString& sep, bool empty_token_is_sep=false);
// Same as above, but it includes the sep dir.
// If "A,,,B" is the text and "," is sep, then
// 'A', ',' and 'B' will be in the output.
wxArrayString str_split_with_sep(const wxString& text, const wxString& sep);
wxArrayString split_with_sep(const wxString& text, const wxString& sep);
// From: https://stackoverflow.com/a/15099743/262458
size_t vec_find(wxArrayString& opts, const wxString& val);
} // namespace strutils
#endif

View File

@ -2,96 +2,95 @@
#include "tests.hpp"
TEST_CASE("str_split() basic test") {
TEST_CASE("strutils::split() basic test") {
wxString foo = "foo|bar|baz";
auto vec = str_split(foo, '|');
auto vec = strutils::split(foo, '|');
REQUIRE(vec.size() == 3);
CHECK(vec.size() == 3);
REQUIRE(vec[0] == "foo");
REQUIRE(vec[1] == "bar");
REQUIRE(vec[2] == "baz");
CHECK(vec[0] == "foo");
CHECK(vec[1] == "bar");
CHECK(vec[2] == "baz");
}
TEST_CASE("str_split() multi-char separator") {
TEST_CASE("strutils::split() multi-char separator") {
wxString foo = "foo|-|bar|-|baz";
auto vec = str_split(foo, "|-|");
auto vec = strutils::split(foo, "|-|");
REQUIRE(vec.size() == 3);
CHECK(vec.size() == 3);
REQUIRE(vec[0] == "foo");
REQUIRE(vec[1] == "bar");
REQUIRE(vec[2] == "baz");
CHECK(vec[0] == "foo");
CHECK(vec[1] == "bar");
CHECK(vec[2] == "baz");
}
TEST_CASE("str_split() skips empty tokens") {
TEST_CASE("strutils::split() skips empty tokens") {
wxString foo = "|-|foo|-||-|bar|-|baz|-|";
auto vec = str_split(foo, "|-|");
auto vec = strutils::split(foo, "|-|");
REQUIRE(vec.size() == 3);
CHECK(vec.size() == 3);
REQUIRE(vec[0] == "foo");
REQUIRE(vec[1] == "bar");
REQUIRE(vec[2] == "baz");
CHECK(vec[0] == "foo");
CHECK(vec[1] == "bar");
CHECK(vec[2] == "baz");
}
TEST_CASE("str_split() empty input") {
TEST_CASE("strutils::split() empty input") {
wxString foo;
auto vec = str_split(foo, "|-|");
auto vec = strutils::split(foo, "|-|");
REQUIRE(vec.size() == 0);
CHECK(vec.size() == 0);
}
TEST_CASE("str_split() no tokens, just separators") {
TEST_CASE("strutils::split() no tokens, just separators") {
wxString foo = "|-||-||-||-||-|";
auto vec = str_split(foo, "|-|");
auto vec = strutils::split(foo, "|-|");
REQUIRE(vec.size() == 0);
CHECK(vec.size() == 0);
}
TEST_CASE("str_split_with_sep() basic test") {
TEST_CASE("strutils::split_with_sep() basic test") {
wxString foo = "foo|bar|baz|";
auto vec = str_split_with_sep(foo, '|');
auto vec = strutils::split_with_sep(foo, '|');
REQUIRE(vec.size() == 4);
CHECK(vec.size() == 4);
REQUIRE(vec[0] == "foo");
REQUIRE(vec[1] == "bar");
REQUIRE(vec[2] == "baz");
REQUIRE(vec[3] == "|");
CHECK(vec[0] == "foo");
CHECK(vec[1] == "bar");
CHECK(vec[2] == "baz");
CHECK(vec[3] == "|");
}
TEST_CASE("str_split_with_sep() multi-char sep") {
TEST_CASE("strutils::split_with_sep() multi-char sep") {
wxString foo = "foo|-|bar|-|baz|-|";
auto vec = str_split_with_sep(foo, "|-|");
auto vec = strutils::split_with_sep(foo, "|-|");
REQUIRE(vec.size() == 4);
CHECK(vec.size() == 4);
REQUIRE(vec[0] == "foo");
REQUIRE(vec[1] == "bar");
REQUIRE(vec[2] == "baz");
REQUIRE(vec[3] == "|-|");
CHECK(vec[0] == "foo");
CHECK(vec[1] == "bar");
CHECK(vec[2] == "baz");
CHECK(vec[3] == "|-|");
}
TEST_CASE("str_split_with_sep() multiple sep tokens") {
TEST_CASE("strutils::split_with_sep() multiple sep tokens") {
wxString foo = "|-|foo|-||-|bar|-|baz|-|";
auto vec = str_split_with_sep(foo, "|-|");
auto vec = strutils::split_with_sep(foo, "|-|");
REQUIRE(vec.size() == 6);
CHECK(vec.size() == 6);
REQUIRE(vec[0] == "|-|");
REQUIRE(vec[1] == "foo");
REQUIRE(vec[2] == "|-|");
REQUIRE(vec[3] == "bar");
REQUIRE(vec[4] == "baz");
REQUIRE(vec[5] == "|-|");
CHECK(vec[0] == "|-|");
CHECK(vec[1] == "foo");
CHECK(vec[2] == "|-|");
CHECK(vec[3] == "bar");
CHECK(vec[4] == "baz");
CHECK(vec[5] == "|-|");
}

View File

@ -135,7 +135,7 @@ nonstd::optional<wxGameControl> wxGameControl::FromString(const wxString &name)
return nonstd::nullopt;
}
auto parts = str_split(name, wxT("/"));
auto parts = strutils::split(name, wxT("/"));
if (parts.size() != 3) {
wxLogDebug("Wrong split size: %d", parts.size());
return nonstd::nullopt;

View File

@ -229,7 +229,7 @@ wxAcceleratorEntry_v wxJoyKeyTextCtrl::ToAccelFromString(const wxString& s, wxCh
if (s.size() == 0)
return empty;
for (const auto& token : str_split_with_sep(s, sep)) {
for (const auto& token : strutils::split_with_sep(s, sep)) {
if (!ParseString(token, token.size(), mod, key, joy))
return empty;
ret.insert(ret.begin(), wxAcceleratorEntryUnicode(token, joy, mod, key));

View File

@ -204,7 +204,7 @@ static bool checkForPairKeyMod(const wxString& s, int& mod, int& key)
{
long ulkey, ulmod;
// key:mod as pair
auto pair = str_split(s, ":");
auto pair = strutils::split(s, ":");
if (pair.size() == 2 && pair[0].ToLong(&ulkey) && pair[1].ToLong(&ulmod))
{
key = (int)ulkey;
@ -309,7 +309,7 @@ wxAcceleratorEntry_v wxKeyTextCtrl::FromString(const wxString& s, wxChar sep)
wxAcceleratorEntry_v ret, empty;
int mod, key;
for (const auto& token : str_split_with_sep(s, sep)) {
for (const auto& token : strutils::split_with_sep(s, sep)) {
if (!ParseString(token, token.size(), mod, key))
return empty;
ret.insert(ret.begin(), wxAcceleratorEntryUnicode(mod, key));

View File

@ -43,7 +43,7 @@ std::set<wxUserInput> wxUserInput::FromString(const wxString& string) {
return user_inputs;
}
for (const auto& token : str_split_with_sep(string, wxT(","))) {
for (const auto& token : strutils::split_with_sep(string, wxT(","))) {
int mod, key, joy;
if (!wxJoyKeyTextCtrl::ParseString(token, token.size(), mod, key, joy)) {
user_inputs.clear();

View File

@ -331,7 +331,7 @@ bool wxvbamApp::OnInit()
// process command-line options
for (size_t i = 0; i < pending_optset.size(); i++) {
auto parts = str_split(pending_optset[i], wxT('='));
auto parts = strutils::split(pending_optset[i], wxT('='));
opt_set(parts[0], parts[1]);
}
@ -680,7 +680,7 @@ bool wxvbamApp::OnCmdLineParsed(wxCmdLineParser& cl)
for (int i = 0; i < nparm; i++) {
auto p = cl.GetParam(i);
auto parts = str_split(p, wxT('='));
auto parts = strutils::split(p, wxT('='));
if (parts.size() > 1) {
opt_set(parts[0], parts[1]);