VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
CommandSyntax.cpp
Go to the documentation of this file.
2
4{
5
6std::vector<std::string> Tokenize(std::string_view text)
7{
8 std::vector<std::string> tokens;
9 std::string current;
10 bool quoted = false;
11 bool started = false; // a `""` is an argument, even though the token is empty
12
13 for (size_t i = 0; i < text.size(); ++i)
14 {
15 const char c = text[i];
16 if (c == '\\' && i + 1 < text.size() && text[i + 1] == '"')
17 {
18 current += '"';
19 ++i;
20 started = true;
21 continue;
22 }
23 if (c == '"')
24 {
25 quoted = !quoted;
26 started = true;
27 continue;
28 }
29 if (c == ' ' && !quoted)
30 {
31 if (started || !current.empty())
32 {
33 tokens.push_back(current);
34 }
35 current.clear();
36 started = false;
37 continue;
38 }
39 current += c;
40 }
41
42 if (started || !current.empty())
43 {
44 tokens.push_back(current);
45 }
46 return tokens;
47}
48
49std::optional<std::string_view> StripPrefix(std::string_view message)
50{
51 if (message.size() > ChatPrefix.size() && message.starts_with(ChatPrefix))
52 {
53 return message.substr(ChatPrefix.size());
54 }
55 return std::nullopt;
56}
57
58} // namespace VoltMod::CommandSyntax
How a chat line is spelled: the prefix, and splitting one into tokens.
std::optional< std::string_view > StripPrefix(std::string_view message)
constexpr std::string_view ChatPrefix
std::vector< std::string > Tokenize(std::string_view text)
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56