diff --git a/lib/utils/mu-utils.cc b/lib/utils/mu-utils.cc index 287d26be..5afd7ab4 100644 --- a/lib/utils/mu-utils.cc +++ b/lib/utils/mu-utils.cc @@ -1,5 +1,5 @@ /* -** Copyright (C) 2017-2021 Dirk-Jan C. Binnema +** Copyright (C) 2017-2022 Dirk-Jan C. Binnema ** ** This library is free software; you can redistribute it and/or ** modify it under the terms of the GNU Lesser General Public License @@ -171,16 +171,33 @@ Mu::remove_ctrl(const std::string& str) std::vector Mu::split(const std::string& str, const std::string& sepa) { - char** parts = g_strsplit(str.c_str(), sepa.c_str(), -1); std::vector vec; - for (auto part = parts; part && *part; ++part) - vec.push_back(*part); + size_t b = 0, e = 0; - g_strfreev(parts); + /* special cases */ + if (str.empty()) + return vec; + else if (sepa.empty()) { + for (auto&& c: str) + vec.emplace_back(1, c); + return vec; + } + + while (true) { + if (e = str.find(sepa, b); e != std::string::npos) { + vec.emplace_back(str.substr(b, e - b)); + b = e + sepa.length(); + } else { + vec.emplace_back(str.substr(b)); + break; + } + } return vec; } + + std::string Mu::quote(const std::string& str) { @@ -331,12 +348,12 @@ special_date(const std::string& d, bool is_first) } midnight = g_date_time_add_full(dt, - 0, - 0, - 0, - -g_date_time_get_hour(dt), - -g_date_time_get_minute(dt), - -g_date_time_get_second(dt)); + 0, + 0, + 0, + -g_date_time_get_hour(dt), + -g_date_time_get_minute(dt), + -g_date_time_get_second(dt)); time_t t = MAX(0, (gint64)g_date_time_to_unix(midnight)); g_date_time_unref(dt); g_date_time_unref(midnight); @@ -409,11 +426,11 @@ Mu::date_to_time_t_string(const std::string& dstr, bool is_first) fixup_month(&tbuf); dtime = g_date_time_new_local(tbuf.tm_year + 1900, - tbuf.tm_mon + 1, - tbuf.tm_mday, - tbuf.tm_hour, - tbuf.tm_min, - tbuf.tm_sec); + tbuf.tm_mon + 1, + tbuf.tm_mday, + tbuf.tm_hour, + tbuf.tm_min, + tbuf.tm_sec); if (!dtime) { g_warning("invalid %s date '%s'", is_first ? "lower" : "upper", date.c_str()); return date_boundary(is_first); diff --git a/lib/utils/mu-utils.hh b/lib/utils/mu-utils.hh index 5ff63131..8b024b49 100644 --- a/lib/utils/mu-utils.hh +++ b/lib/utils/mu-utils.hh @@ -1,5 +1,5 @@ /* -** Copyright (C) 2020-2021 Dirk-Jan C. Binnema +** Copyright (C) 2020-2022 Dirk-Jan C. Binnema ** ** This library is free software; you can redistribute it and/or ** modify it under the terms of the GNU Lesser General Public License @@ -68,7 +68,8 @@ std::string utf8_clean(const std::string& dirty); std::string remove_ctrl(const std::string& str); /** - * Split a string in parts + * Split a string in parts. As a special case, splitting an empty string + * yields an empty vector (not a vector with a single empty element) * * @param str a string * @param sepa the separator @@ -314,23 +315,11 @@ private: * @param s2 string2 */ #define assert_equal(s1__,s2__) do { \ - std::string s1s__(s1__), s2s__(s2__); \ + std::string s1s__(s1__), s2s__(s2__); \ g_assert_cmpstr(s1s__.c_str(), ==, s2s__.c_str()); \ } while(0) -/** - * For unit tests, assert that to containers are the same. - * - * @param c1 container1 - * @param c2 container2 - */ -#define assert_equal_svec(svec1__,svec2__) do { \ - g_assert_cmpuint((svec1__).size(), ==, (svec2__).size()); \ - for (auto i = 0U; i != (svec1__).size(); ++i) \ - g_assert_cmpstr((svec1__)[i].c_str(), ==, (svec2__)[i].c_str()); \ - } while (0) - /** * For unit-tests, allow warnings in the current function. * diff --git a/lib/utils/tests/test-utils.cc b/lib/utils/tests/test-utils.cc index 811c882a..a774861a 100644 --- a/lib/utils/tests/test-utils.cc +++ b/lib/utils/tests/test-utils.cc @@ -1,5 +1,5 @@ /* -** Copyright (C) 2017 Dirk-Jan C. Binnema +** Copyright (C) 2017-2022 Dirk-Jan C. Binnema ** ** This library is free software; you can redistribute it and/or ** modify it under the terms of the GNU Lesser General Public License @@ -58,19 +58,19 @@ test_date_basic() g_setenv("TZ", "Europe/Helsinki", TRUE); CaseVec cases = {{"2015-09-18T09:10:23", true, "1442556623"}, - {"1972-12-14T09:10:23", true, "0093165023"}, - {"1854-11-18T17:10:23", true, "0000000000"}, + {"1972-12-14T09:10:23", true, "0093165023"}, + {"1854-11-18T17:10:23", true, "0000000000"}, - {"2000-02-31T09:10:23", true, "0951861599"}, - {"2000-02-29T23:59:59", true, "0951861599"}, + {"2000-02-31T09:10:23", true, "0951861599"}, + {"2000-02-29T23:59:59", true, "0951861599"}, - {"2016", true, "1451599200"}, - {"2016", false, "1483221599"}, + {"2016", true, "1451599200"}, + {"2016", false, "1483221599"}, - {"fnorb", true, "0000000000"}, - {"fnorb", false, "9999999999"}, - {"", false, "9999999999"}, - {"", true, "0000000000"}}; + {"fnorb", true, "0000000000"}, + {"fnorb", false, "9999999999"}, + {"", false, "9999999999"}, + {"", true, "0000000000"}}; test_cases(cases, [](auto s, auto f) { return date_to_time_t_string(s, f); }); } @@ -83,11 +83,11 @@ test_date_ymwdhMs(void) long diff; int tolerance; } tests[] = {{"3h", 3 * 60 * 60, 1}, - {"21d", 21 * 24 * 60 * 60, 3600 + 1}, - {"2w", 2 * 7 * 24 * 60 * 60, 3600 + 1}, + {"21d", 21 * 24 * 60 * 60, 3600 + 1}, + {"2w", 2 * 7 * 24 * 60 * 60, 3600 + 1}, - {"2y", 2 * 365 * 24 * 60 * 60, 24 * 3600 + 1}, - {"3m", 3 * 30 * 24 * 60 * 60, 3 * 24 * 3600 + 1}}; + {"2y", 2 * 365 * 24 * 60 * 60, 24 * 3600 + 1}, + {"3m", 3 * 30 * 24 * 60 * 60, 3 * 24 * 3600 + 1}}; for (auto i = 0; i != G_N_ELEMENTS(tests); ++i) { const auto diff = @@ -160,6 +160,26 @@ test_format() g_assert_true(format("hello %s, %u", "world", 123) == "hello world, 123"); } +static void + +test_split() +{ + using svec = std::vector; + auto assert_equal_svec=[](const svec& sv1, const svec& sv2) { + g_assert_cmpuint(sv1.size(),==,sv2.size()); + for (auto i = 0U; i != sv1.size(); ++i) + g_assert_cmpstr(sv1[i].c_str(),==,sv2[i].c_str()); + }; + + + assert_equal_svec(split("axbxc", "x"), {"a", "b", "c"}); + assert_equal_svec(split("axbxcx", "x"), {"a", "b", "c", ""}); + assert_equal_svec(split("", "boo"), {}); + assert_equal_svec(split("ayybyyc", "yy"), {"a", "b", "c"}); + assert_equal_svec(split("abc", ""), {"a", "b", "c"}); + assert_equal_svec(split("", "boo"), {}); +} + enum struct Bits { None = 0, Bit1 = 1 << 0, Bit2 = 1 << 1 }; MU_ENABLE_BITOPS(Bits); @@ -188,6 +208,7 @@ test_define_bitmap() } } + int main(int argc, char* argv[]) { @@ -200,6 +221,7 @@ main(int argc, char* argv[]) g_test_add_func("/utils/remove-ctrl", test_remove_ctrl); g_test_add_func("/utils/clean", test_clean); g_test_add_func("/utils/format", test_format); + g_test_add_func("/utils/split", test_split); g_test_add_func("/utils/define-bitmap", test_define_bitmap); return g_test_run();