utils: rework Mu::split
And add some tests, cleanups.
This commit is contained in:
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** Copyright (C) 2017-2021 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
** Copyright (C) 2017-2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||||
**
|
**
|
||||||
** This library is free software; you can redistribute it and/or
|
** This library is free software; you can redistribute it and/or
|
||||||
** modify it under the terms of the GNU Lesser General Public License
|
** 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<std::string>
|
std::vector<std::string>
|
||||||
Mu::split(const std::string& str, const std::string& sepa)
|
Mu::split(const std::string& str, const std::string& sepa)
|
||||||
{
|
{
|
||||||
char** parts = g_strsplit(str.c_str(), sepa.c_str(), -1);
|
|
||||||
std::vector<std::string> vec;
|
std::vector<std::string> vec;
|
||||||
for (auto part = parts; part && *part; ++part)
|
size_t b = 0, e = 0;
|
||||||
vec.push_back(*part);
|
|
||||||
|
|
||||||
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;
|
return vec;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
std::string
|
std::string
|
||||||
Mu::quote(const std::string& str)
|
Mu::quote(const std::string& str)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** Copyright (C) 2020-2021 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
** Copyright (C) 2020-2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||||
**
|
**
|
||||||
** This library is free software; you can redistribute it and/or
|
** This library is free software; you can redistribute it and/or
|
||||||
** modify it under the terms of the GNU Lesser General Public License
|
** 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);
|
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 str a string
|
||||||
* @param sepa the separator
|
* @param sepa the separator
|
||||||
@ -319,18 +320,6 @@ private:
|
|||||||
} while(0)
|
} 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.
|
* For unit-tests, allow warnings in the current function.
|
||||||
*
|
*
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** Copyright (C) 2017 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
** Copyright (C) 2017-2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||||
**
|
**
|
||||||
** This library is free software; you can redistribute it and/or
|
** This library is free software; you can redistribute it and/or
|
||||||
** modify it under the terms of the GNU Lesser General Public License
|
** modify it under the terms of the GNU Lesser General Public License
|
||||||
@ -160,6 +160,26 @@ test_format()
|
|||||||
g_assert_true(format("hello %s, %u", "world", 123) == "hello world, 123");
|
g_assert_true(format("hello %s, %u", "world", 123) == "hello world, 123");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
|
||||||
|
test_split()
|
||||||
|
{
|
||||||
|
using svec = std::vector<std::string>;
|
||||||
|
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 };
|
enum struct Bits { None = 0, Bit1 = 1 << 0, Bit2 = 1 << 1 };
|
||||||
MU_ENABLE_BITOPS(Bits);
|
MU_ENABLE_BITOPS(Bits);
|
||||||
|
|
||||||
@ -188,6 +208,7 @@ test_define_bitmap()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char* argv[])
|
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/remove-ctrl", test_remove_ctrl);
|
||||||
g_test_add_func("/utils/clean", test_clean);
|
g_test_add_func("/utils/clean", test_clean);
|
||||||
g_test_add_func("/utils/format", test_format);
|
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);
|
g_test_add_func("/utils/define-bitmap", test_define_bitmap);
|
||||||
|
|
||||||
return g_test_run();
|
return g_test_run();
|
||||||
|
|||||||
Reference in New Issue
Block a user