From 97c24ec2d266bba135e060e82417290c0f316dc2 Mon Sep 17 00:00:00 2001 From: Rafael Kitover Date: Fri, 23 Sep 2022 23:43:31 +0000 Subject: [PATCH] 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 --- src/wx/autoupdater/wxmsw/autoupdater.cpp | 2 +- src/wx/opts.cpp | 12 +-- src/wx/strutils.cpp | 13 ++-- src/wx/strutils.h | 9 ++- src/wx/tests/strutils.cpp | 95 ++++++++++++------------ src/wx/widgets/gamecontrol.cpp | 2 +- src/wx/widgets/joyedit.cpp | 2 +- src/wx/widgets/keyedit.cpp | 4 +- src/wx/widgets/userinput.cpp | 2 +- src/wx/wxvbam.cpp | 4 +- 10 files changed, 73 insertions(+), 72 deletions(-) diff --git a/src/wx/autoupdater/wxmsw/autoupdater.cpp b/src/wx/autoupdater/wxmsw/autoupdater.cpp index 1f4c7173..ca6ec78e 100644 --- a/src/wx/autoupdater/wxmsw/autoupdater.cpp +++ b/src/wx/autoupdater/wxmsw/autoupdater.cpp @@ -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 diff --git a/src/wx/opts.cpp b/src/wx/opts.cpp index 656a3715..02b402df 100644 --- a/src/wx/opts.cpp +++ b/src/wx/opts.cpp @@ -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); diff --git a/src/wx/strutils.cpp b/src/wx/strutils.cpp index bf6a42eb..a0edf623 100644 --- a/src/wx/strutils.cpp +++ b/src/wx/strutils.cpp @@ -1,10 +1,13 @@ #include "strutils.h" + #include +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 diff --git a/src/wx/strutils.h b/src/wx/strutils.h index 9fa4eff3..6e006710 100644 --- a/src/wx/strutils.h +++ b/src/wx/strutils.h @@ -7,15 +7,16 @@ #include #include +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 diff --git a/src/wx/tests/strutils.cpp b/src/wx/tests/strutils.cpp index f6c1df2a..d0fbebba 100644 --- a/src/wx/tests/strutils.cpp +++ b/src/wx/tests/strutils.cpp @@ -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] == "|-|"); } - diff --git a/src/wx/widgets/gamecontrol.cpp b/src/wx/widgets/gamecontrol.cpp index 9b6c5aff..33c1aeb4 100644 --- a/src/wx/widgets/gamecontrol.cpp +++ b/src/wx/widgets/gamecontrol.cpp @@ -135,7 +135,7 @@ nonstd::optional 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; diff --git a/src/wx/widgets/joyedit.cpp b/src/wx/widgets/joyedit.cpp index 403a10d9..ca3f6c36 100644 --- a/src/wx/widgets/joyedit.cpp +++ b/src/wx/widgets/joyedit.cpp @@ -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)); diff --git a/src/wx/widgets/keyedit.cpp b/src/wx/widgets/keyedit.cpp index 93ed7e3a..8ad22a1d 100644 --- a/src/wx/widgets/keyedit.cpp +++ b/src/wx/widgets/keyedit.cpp @@ -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)); diff --git a/src/wx/widgets/userinput.cpp b/src/wx/widgets/userinput.cpp index 92c34662..275180e0 100644 --- a/src/wx/widgets/userinput.cpp +++ b/src/wx/widgets/userinput.cpp @@ -43,7 +43,7 @@ std::set 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(); diff --git a/src/wx/wxvbam.cpp b/src/wx/wxvbam.cpp index 94b92d81..db987262 100644 --- a/src/wx/wxvbam.cpp +++ b/src/wx/wxvbam.cpp @@ -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]);