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

Unified · Split

2 files
  1. src/exec/debugger.cpp +3 −37
  2. src/include/utils.h +37 −3
src/exec/debugger.cpp+3−37
@@ -770,7 +770,7 @@ namespace · 769 unmodified lines
770770 }
771771
772772 // [TODO] Didn't find the cmd, try ignoring case?
773− return nullptr;
773+ throw debug_cmd_error_t{ "Unknown command. '?' for help." };
774774 }
775775
776776 void print_help()
@@ -797,31 +797,6 @@ namespace · 20 unmodified lines
797797 debug_print_info(ss.str());
798798 }
799799
800− // split the string using the supplied delimiter
801− std::vector<std::string> split(const std::string &str, const char delim = ' ')
802− {
803− auto split_string = std::vector<std::string>{};
804−
805− auto curr = str.begin();
806− auto end = str.cend();
807− while (curr != end)
808− {
809− auto curr_begin = curr;
810− while (curr != end && *curr != delim)
811− ++curr;
812−
813− if (curr_begin != curr)
814− split_string.push_back(std::move(std::string{ curr_begin, curr }));
815−
816− if (curr == end)
817− break;
818−
819− ++curr;
820− }
821−
822− return split_string;
823− }
824−
825800 void prompt(debugger &debugger, const vm_registers_t &r)
826801 {
827802 debugger.controller->suspend_all_threads();
@@ -845,22 +820,13 @@ namespace · 17 unmodified lines
845820 << console_modifiers::reset_all;
846821
847822 std::getline(std::cin, cmd);
848− if (cmd.empty())
849− continue;
850−
851− const auto cmd_tokens = split(cmd);
823+ const auto cmd_tokens = util::split(cmd);
852824 if (cmd_tokens.empty())
853825 continue;
854826
855− auto cmd_exec = find_cmd(cmd_tokens[0]);
856− if (cmd_exec == nullptr)
857− {
858− debug_print_error("Unknown command. '?' for help.");
859− continue;
860− }
861−
862827 try
863828 {
829+ auto cmd_exec = find_cmd(cmd_tokens[0]);
864830 cmd_exec(cmd_tokens, cxt);
865831 if (cxt.exit_break)
866832 break;
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