kblib 0.2.3
General utilities library for modern C++
stringops.cpp
Go to the documentation of this file.
1#include "kblib/stringops.h"
2#include "catch.hpp"
3
4using namespace std::literals;
5
6#if KBLIB_USE_CXX17
7TEST_CASE("concat") {
8 REQUIRE(kblib::concat("a") == "a");
9 REQUIRE(kblib::concat(1) == "1");
10 REQUIRE(kblib::concat(1, "a") == "1a");
11 REQUIRE(kblib::concat("a"sv) == "a");
12 REQUIRE(kblib::concat(1, "a"sv) == "1a");
13 REQUIRE(kblib::concat(1, 2, 3, 4) == "1234");
14 REQUIRE(kblib::concat('a', 'b', 'c') == "abc");
15 REQUIRE(kblib::concat("a", 'b', 'c') == "abc");
16 REQUIRE(kblib::concat('a', "b", 'c') == "abc");
17 REQUIRE(kblib::concat("a", "b", "c") == "abc");
18 REQUIRE(kblib::concat("a"sv, 'b', 'c') == "abc");
19 REQUIRE(kblib::concat("a", "b"sv, 'c') == "abc");
20 REQUIRE(kblib::concat("a"sv, "b", 'c') == "abc");
21 REQUIRE(kblib::concat("a", "b"sv, "c") == "abc");
22 REQUIRE(kblib::concat("a"sv, "b"sv, 'c') == "abc");
23 REQUIRE(kblib::concat("a"sv, "b"sv, "c") == "abc");
24 REQUIRE(kblib::concat("ab", "cd", 12, 3, 4) == "abcd1234");
25 static_assert(std::is_same_v<std::string, kblib::detail::str_type_t<double>>,
26 "arithmetic types are converted to std::string");
27 REQUIRE(kblib::concat(1.0) == "1.000000");
28 REQUIRE(kblib::concat(1.5) == "1.500000");
29 REQUIRE(kblib::concat(0.0) == "0.000000");
30 {
31 std::string val = "1";
32 const char* type = "bool";
33 REQUIRE(kblib::concat("\"", val, "\" is not a ", type)
34 == "\"1\" is not a bool");
35 }
36 {
37 std::string_view val = "1";
38 const char* type = "bool";
39 REQUIRE(kblib::concat("\"", val, "\" is not a ", type)
40 == "\"1\" is not a bool");
41 }
42}
43
44TEST_CASE("join and split") {
45 std::vector<std::string> vec{"Hello", "world!", "How", "do", "you", "do?"};
46 auto joined = kblib::join(vec, " "s);
47 REQUIRE(joined == "Hello world! How do you do?");
48 auto split = kblib::split_tokens(joined);
49 REQUIRE(split == vec);
50}
51
52TEST_CASE("split") {
53 std::vector<std::tuple<std::string, std::vector<std::string>>> tests{
54 {"", {}},
55 {"test", {"test"}},
56 {" ", {}},
57 {"abc def", {"abc", "def"}},
58 {" abc", {"abc"}},
59 {"abc ", {"abc"}},
60 {" abc ", {"abc"}},
61 {"abc def", {"abc", "def"}},
62 {"abc def ", {"abc", "def"}},
63 {"\fabc", {"abc"}},
64 {"abc\n", {"abc"}},
65 {"\tabc\v", {"abc"}},
66 {"abc\t\tdef", {"abc", "def"}},
67 {"abc\tdef\t", {"abc", "def"}},
68 {" \tabc", {"abc"}},
69 {"abc \f", {"abc"}},
70 {"\v abc\t ", {"abc"}},
71 {"abc \t def", {"abc", "def"}},
72 {"abc\n def ", {"abc", "def"}},
73 {" abc def ", {"abc", "def"}},
74 {" abc def", {"abc", "def"}},
75 {" abc def ", {"abc", "def"}},
76 {" abc def ghi", {"abc", "def", "ghi"}},
77 };
78 for (const auto& [test, result] : tests) {
79 REQUIRE(kblib::split_tokens(test) == result);
80 }
81}
82TEST_CASE("split2") {
83 std::vector<std::pair<std::string, std::vector<std::string>>> tests{
84 {"", {}},
85 {"test", {"test"}},
86 {" ", {}},
87 {"abc def", {"abc", "def"}},
88 {" abc", {"abc"}},
89 {"abc ", {"abc"}},
90 {" abc ", {"abc"}},
91 {"abc def", {"abc", "def"}},
92 {"abc def ", {"abc", "def"}},
93 {" abc def ", {"abc", "def"}},
94 {" abc def", {"abc", "def"}},
95 {" abc def ", {"abc", "def"}},
96 {" abc def ghi", {"abc", "def", "ghi"}},
97 };
98 for (const auto& [test, result] : tests) {
99 REQUIRE(kblib::kbsplit2(test) == result);
100 }
101}
102
103TEST_CASE("split_dsv") {
104 std::vector<std::pair<std::string, std::vector<std::string>>> tests{
105 {"", {""}},
106 {"test", {"test"}},
107 {",", {"", ""}},
108 {"abc,def", {"abc", "def"}},
109 {",abc", {"", "abc"}},
110 {"abc,", {"abc", ""}},
111 {",abc,", {"", "abc", ""}},
112 {"abc,,def", {"abc", "", "def"}},
113 {"abc,def,", {"abc", "def", ""}},
114 {",abc,,def,", {"", "abc", "", "def", ""}},
115 {",abc,def", {"", "abc", "def"}},
116 {",,abc,,,,def,,", {"", "", "abc", "", "", "", "def", "", ""}},
117 {",abc,def,ghi", {"", "abc", "def", "ghi"}},
118 };
119 for (const auto& [test, result] : tests) {
120 REQUIRE(kblib::split_dsv(test, ',') == result);
121 }
122}
123
124#endif
constexpr auto test() noexcept -> bool
Definition: main.cpp:68
auto split_tokens(const String &in, Predicate spacer) -> return_assert_t< is_callable< Predicate, typename Container::value_type::value_type >::value, Container >
Split a string on all condensed delimiters.
Definition: stringops.h:397
auto kbsplit2(const String &in, char delim=' ') -> Container
Definition: stringops.h:474
auto concat(F &&f, S &&... ins) -> string
Returns a string consisting of the concatenation of all arguments.
Definition: stringops.h:305
auto join(const range &in, const string &joiner="")
Concatenates all elements of a range together with an optional joiner.
Definition: stringops.h:370
auto split_dsv(const String &str, char delim) -> Container
Split a string on all instances of delim.
Definition: stringops.h:501
TEST_CASE("concat")
Definition: stringops.cpp:7
Provides utilities for performing common operations on strings.