| 60 | #endif |
| 61 | |
| 62 | class Tokens { |
| 63 | public: |
| 64 | Tokens(std::vector<std::string> tokens, bool isParsingArgv = true) |
| 65 | : fTokens(std::move(tokens)), |
| 66 | fIsParsingArgv(isParsingArgv) |
| 67 | {} |
| 68 | |
| 69 | explicit operator bool() const { |
| 70 | return fIndex < fTokens.size(); |
| 71 | } |
| 72 | |
| 73 | static Tokens from_pattern(std::string const& source) { |
| 74 | static const std::regex re_separators { |
| 75 | "(?:\\s*)" // any spaces (non-matching subgroup) |
| 76 | "(" |
| 77 | "[\\[\\]\\(\\)\\|]" // one character of brackets or parens or pipe character |
| 78 | "|" |
| 79 | "\\.\\.\\." // elipsis |
| 80 | ")" }; |
| 81 | |
| 82 | static const std::regex re_strings { |
| 83 | "(?:\\s*)" // any spaces (non-matching subgroup) |
| 84 | "(" |
| 85 | "\\S*<.*?>" // strings, but make sure to keep "< >" strings together |
| 86 | "|" |
| 87 | "[^<>\\s]+" // string without <> |
| 88 | ")" }; |
| 89 | |
| 90 | // We do two stages of regex matching. The '[]()' and '...' are strong delimeters |
| 91 | // and need to be split out anywhere they occur (even at the end of a token). We |
| 92 | // first split on those, and then parse the stuff between them to find the string |
| 93 | // tokens. This is a little harder than the python version, since they have regex.split |
| 94 | // and we dont have anything like that. |
| 95 | |
| 96 | std::vector<std::string> tokens; |
| 97 | std::for_each(std::sregex_iterator{ source.begin(), source.end(), re_separators }, |
| 98 | std::sregex_iterator{}, |
| 99 | [&](std::smatch const& match) |
| 100 | { |
| 101 | // handle anything before the separator (this is the "stuff" between the delimeters) |
| 102 | if (match.prefix().matched) { |
| 103 | std::for_each(std::sregex_iterator{match.prefix().first, match.prefix().second, re_strings}, |
| 104 | std::sregex_iterator{}, |
| 105 | [&](std::smatch const& m) |
| 106 | { |
| 107 | tokens.push_back(m[1].str()); |
| 108 | }); |
| 109 | } |
| 110 | |
| 111 | // handle the delimter token itself |
| 112 | if (match[1].matched) { |
| 113 | tokens.push_back(match[1].str()); |
| 114 | } |
| 115 | }); |
| 116 | |
| 117 | return Tokens(tokens, false); |
| 118 | } |
| 119 |
no outgoing calls
no test coverage detected