History

Change nullptr return value in debugger to an exception. Pull out the split() function from the debugger and push it into utils.h.

arr committed 9 years ago

Commit 2e5c41c1 parent 551c5002 files · +40 −40Browse files

Showing the changes to src/include (1 of 2 files). Show all

Unified · Split

src/include/utils.h+37−3
@@ -9,28 +9,62 @@ · 8 unmodified lines
99
1010 #include <cstdint>
1111 #include <type_traits>
12+#include <string>
13+#include <cstring>
14+#include <vector>
1215
1316 namespace disvm
1417 {
1518 namespace util
1619 {
1720 template<typename Flags>
18− inline bool has_flag(const Flags flags, const Flags flag)
21+ bool has_flag(const Flags flags, const Flags flag)
1922 {
2023 return (static_cast<std::underlying_type_t<Flags>>(flags) & static_cast<std::underlying_type_t<Flags>>(flag)) != 0;
2124 }
2225
2326 template <typename T1, typename T2>
24− inline bool are_equal(const T1 &l, const T2 &r)
27+ bool are_equal(const T1 &l, const T2 &r)
2528 {
2629 return (l == r);
2730 }
2831
2932 template <typename T1, typename T2, typename... Others>
30− inline bool are_equal(const T1 &l, const T2 &r, const Others &... args)
33+ bool are_equal(const T1 &l, const T2 &r, const Others &... args)
3134 {
3235 return (l == r) && are_equal(r, args...);
3336 }
37+
38+ // split the string using the delimiters contained in the supplied string
39+ inline std::vector<std::string> split(const std::string &str, const char *delims)
40+ {
41+ auto split_string = std::vector<std::string>{};
42+
43+ auto curr = str.begin();
44+ auto end = str.cend();
45+ while (curr != end)
46+ {
47+ auto curr_begin = curr;
48+
49+ while (curr != end && (nullptr == std::strchr(delims, *curr)))
50+ ++curr;
51+
52+ if (curr_begin != curr)
53+ split_string.push_back(std::move(std::string{ curr_begin, curr }));
54+
55+ if (curr == end)
56+ break;
57+
58+ ++curr;
59+ }
60+
61+ return split_string;
62+ }
63+
64+ inline std::vector<std::string> split(const std::string &str)
65+ {
66+ return split(str, " ");
67+ }
3468 }
3569 }
3670