diff options
| author | Phil Christensen <philc@microsoft.com> | 2019-08-23 10:20:18 -0700 |
|---|---|---|
| committer | Phil Christensen <philc@microsoft.com> | 2019-08-23 10:20:18 -0700 |
| commit | 13c95f16bfab4e516d088e15a1e77733b3f8745f (patch) | |
| tree | 32ae776badfc2eba8bed4689afbacb46eb63322f /toolsrc/src | |
| parent | 051a6fd5b3d83fedc83592236413c9b8c0015c6d (diff) | |
| download | vcpkg-13c95f16bfab4e516d088e15a1e77733b3f8745f.tar.gz vcpkg-13c95f16bfab4e516d088e15a1e77733b3f8745f.zip | |
clean up list parsing logic and add clear warnings
Diffstat (limited to 'toolsrc/src')
| -rw-r--r-- | toolsrc/src/vcpkg/parse.cpp | 88 |
1 files changed, 68 insertions, 20 deletions
diff --git a/toolsrc/src/vcpkg/parse.cpp b/toolsrc/src/vcpkg/parse.cpp index 8f90aef15..91b2b2786 100644 --- a/toolsrc/src/vcpkg/parse.cpp +++ b/toolsrc/src/vcpkg/parse.cpp @@ -3,6 +3,7 @@ #include <vcpkg/parse.h> #include <vcpkg/base/util.h> +#include <vcpkg/base/system.print.h> namespace vcpkg::Parse { @@ -58,26 +59,73 @@ namespace vcpkg::Parse std::vector<std::string> out; - size_t cur = 0; - do - { - auto pos = str.find(',', cur); - if (pos == std::string::npos) - { - out.push_back(str.substr(cur)); - break; - } - out.push_back(str.substr(cur, pos - cur)); - - // skip comma and space - ++pos; - while (is_whitespace(str[pos])) - { - ++pos; - } - - cur = pos; - } while (cur != std::string::npos); + auto iter = str.cbegin(); + + do { + // Trim leading whitespace of each element + while (iter != str.cend() && is_whitespace(*iter)) + { + ++iter; + } + + // Allow commas inside of []. + bool bracket_nesting = false; + + auto element_begin = iter; + auto element_end = iter; + while (iter != str.cend() && (*iter != ',' || bracket_nesting)) + { + char value = *iter; + + // do not support nested [] + if (value == '[') + { + bracket_nesting = true; + } + else if (value == ']') + { + bracket_nesting = false; + } + + ++iter; + + // Trim ending whitespace + if (!is_whitespace(value)) + { + // Update element_end after iter is incremented so it will be one past. + element_end = iter; + } + } + + if (element_begin == element_end) + { + System::print2( System::Color::warning, + "Warning: empty element in list\n" + "> '", str, "'\n" + "> ", std::string(static_cast<int>(element_begin - str.cbegin()), ' '), "^\n" + ); + } + else + { + out.push_back({ element_begin, element_end }); + } + + if (iter != str.cend()) + { + //Not at the end, must be at a comma that needs to be stepped over + ++iter; + + if (iter == str.end()) + { + System::print2(System::Color::warning, + "Warning: empty element in list\n" + "> '", str, "'\n" + "> ", std::string(str.length(), ' '), "^\n" + ); + } + } + + } while (iter != str.cend()); return out; } |
