From ad3183db8cbc15d25b9326b5b479eba01e87f30a Mon Sep 17 00:00:00 2001 From: Sebastian Holtermann Date: Sun, 28 Jul 2019 15:59:22 +0200 Subject: cmSystemTool: Let Expand(ed)ListArgument accept a cm::string_view --- Source/cmSystemTools.cxx | 30 +++++++++++++++++------------- Source/cmSystemTools.h | 4 ++-- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 3ba3640..1e9580c 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -1121,7 +1121,7 @@ void cmSystemTools::GlobDirs(const std::string& path, } } -void cmSystemTools::ExpandListArgument(const std::string& arg, +void cmSystemTools::ExpandListArgument(cm::string_view arg, std::vector& argsOut, bool emptyArgs) { @@ -1129,25 +1129,29 @@ void cmSystemTools::ExpandListArgument(const std::string& arg, if (!emptyArgs && arg.empty()) { return; } + // if there are no ; in the name then just copy the current string - if (arg.find(';') == std::string::npos) { - argsOut.push_back(arg); + if (arg.find(';') == cm::string_view::npos) { + argsOut.emplace_back(arg); return; } + std::string newArg; - const char* last = arg.c_str(); // Break the string at non-escaped semicolons not nested in []. int squareNesting = 0; - for (const char* c = last; *c; ++c) { + cm::string_view::iterator last = arg.begin(); + cm::string_view::iterator const cend = arg.end(); + for (cm::string_view::iterator c = last; c != cend; ++c) { switch (*c) { case '\\': { // We only want to allow escaping of semicolons. Other // escapes should not be processed here. - const char* next = c + 1; - if (*next == ';') { - newArg.append(last, c - last); + cm::string_view::iterator cnext = c + 1; + if ((cnext != cend) && *cnext == ';') { + newArg.append(last, c); // Skip over the escape character - last = c = next; + last = cnext; + c = cnext; } } break; case '[': { @@ -1160,7 +1164,7 @@ void cmSystemTools::ExpandListArgument(const std::string& arg, // Break the string here if we are not nested inside square // brackets. if (squareNesting == 0) { - newArg.append(last, c - last); + newArg.append(last, c); // Skip over the semicolon last = c + 1; if (!newArg.empty() || emptyArgs) { @@ -1175,15 +1179,15 @@ void cmSystemTools::ExpandListArgument(const std::string& arg, } break; } } - newArg.append(last); + newArg.append(last, cend); if (!newArg.empty() || emptyArgs) { // Add the last argument if the string is not empty. - argsOut.push_back(newArg); + argsOut.push_back(std::move(newArg)); } } std::vector cmSystemTools::ExpandedListArgument( - const std::string& arg, bool emptyArgs) + cm::string_view arg, bool emptyArgs) { std::vector argsOut; ExpandListArgument(arg, argsOut, emptyArgs); diff --git a/Source/cmSystemTools.h b/Source/cmSystemTools.h index 1962389..87f0a11 100644 --- a/Source/cmSystemTools.h +++ b/Source/cmSystemTools.h @@ -32,7 +32,7 @@ public: * Expand the ; separated string @a arg into multiple arguments. * All found arguments are appended to @a argsOut. */ - static void ExpandListArgument(const std::string& arg, + static void ExpandListArgument(cm::string_view arg, std::vector& argsOut, bool emptyArgs = false); @@ -54,7 +54,7 @@ public: * Same as ExpandListArgument but a new vector is created containing * the expanded arguments from the string @a arg. */ - static std::vector ExpandedListArgument(const std::string& arg, + static std::vector ExpandedListArgument(cm::string_view arg, bool emptyArgs = false); /** -- cgit v0.12