diff options
Diffstat (limited to 'Source/cmOutputConverter.cxx')
-rw-r--r-- | Source/cmOutputConverter.cxx | 175 |
1 files changed, 90 insertions, 85 deletions
diff --git a/Source/cmOutputConverter.cxx b/Source/cmOutputConverter.cxx index d7bcf7e..da7f8bc 100644 --- a/Source/cmOutputConverter.cxx +++ b/Source/cmOutputConverter.cxx @@ -6,7 +6,6 @@ #include <assert.h> #include <ctype.h> #include <set> -#include <string.h> #include <vector> #include "cmState.h" @@ -38,10 +37,10 @@ std::string cmOutputConverter::ConvertToOutputForExisting( return this->ConvertToOutputFormat(remote, format); } -std::string cmOutputConverter::ConvertToOutputFormat(const std::string& source, +std::string cmOutputConverter::ConvertToOutputFormat(cm::string_view source, OutputFormat output) const { - std::string result = source; + std::string result(source); // Convert it to an output path. if (output == SHELL || output == WATCOMQUOTE) { result = this->ConvertDirectorySeparatorsForShell(source); @@ -53,9 +52,9 @@ std::string cmOutputConverter::ConvertToOutputFormat(const std::string& source, } std::string cmOutputConverter::ConvertDirectorySeparatorsForShell( - const std::string& source) const + cm::string_view source) const { - std::string result = source; + std::string result(source); // For the MSYS shell convert drive letters to posix paths, so // that c:/some/path becomes /c/some/path. This is needed to // avoid problems with the shell path translation. @@ -71,21 +70,21 @@ std::string cmOutputConverter::ConvertDirectorySeparatorsForShell( return result; } -static bool cmOutputConverterIsShellOperator(const std::string& str) +static bool cmOutputConverterIsShellOperator(cm::string_view str) { - static std::set<std::string> const shellOperators{ + static std::set<cm::string_view> const shellOperators{ "<", ">", "<<", ">>", "|", "||", "&&", "&>", "1>", "2>", "2>&1", "1>&2" }; return (shellOperators.count(str) != 0); } -std::string cmOutputConverter::EscapeForShell(const std::string& str, +std::string cmOutputConverter::EscapeForShell(cm::string_view str, bool makeVars, bool forEcho, bool useWatcomQuote) const { // Do not escape shell operators. if (cmOutputConverterIsShellOperator(str)) { - return str; + return std::string(str); } // Compute the flags for the target shell environment. @@ -117,46 +116,44 @@ std::string cmOutputConverter::EscapeForShell(const std::string& str, flags |= Shell_Flag_IsUnix; } - return Shell__GetArgument(str.c_str(), flags); + return Shell__GetArgument(str, flags); } -std::string cmOutputConverter::EscapeForCMake(const std::string& str) +std::string cmOutputConverter::EscapeForCMake(cm::string_view str) { // Always double-quote the argument to take care of most escapes. std::string result = "\""; - for (const char* c = str.c_str(); *c; ++c) { - if (*c == '"') { + for (const char c : str) { + if (c == '"') { // Escape the double quote to avoid ending the argument. result += "\\\""; - } else if (*c == '$') { + } else if (c == '$') { // Escape the dollar to avoid expanding variables. result += "\\$"; - } else if (*c == '\\') { + } else if (c == '\\') { // Escape the backslash to avoid other escapes. result += "\\\\"; } else { // Other characters will be parsed correctly. - result += *c; + result += c; } } result += "\""; return result; } -std::string cmOutputConverter::EscapeWindowsShellArgument(const char* arg, +std::string cmOutputConverter::EscapeWindowsShellArgument(cm::string_view arg, int shell_flags) { return Shell__GetArgument(arg, shell_flags); } cmOutputConverter::FortranFormat cmOutputConverter::GetFortranFormat( - const char* value) + cm::string_view value) { FortranFormat format = FortranFormatNone; - if (value && *value) { - std::vector<std::string> fmt; - cmSystemTools::ExpandListArgument(value, fmt); - for (std::string const& fi : fmt) { + if (!value.empty()) { + for (std::string const& fi : cmSystemTools::ExpandedListArgument(value)) { if (fi == "FIXED") { format = FortranFormatFixed; } @@ -168,6 +165,15 @@ cmOutputConverter::FortranFormat cmOutputConverter::GetFortranFormat( return format; } +cmOutputConverter::FortranFormat cmOutputConverter::GetFortranFormat( + const char* value) +{ + if (!value) { + return FortranFormatNone; + } + return GetFortranFormat(cm::string_view(value)); +} + void cmOutputConverter::SetLinkScriptShell(bool linkScriptShell) { this->LinkScriptShell = linkScriptShell; @@ -213,12 +219,12 @@ use the caret character itself (^), use two in a row (^^). */ /* Some helpers to identify character classes */ -static int Shell__CharIsWhitespace(char c) +static bool Shell__CharIsWhitespace(char c) { return ((c == ' ') || (c == '\t')); } -static int Shell__CharNeedsQuotesOnUnix(char c) +static bool Shell__CharNeedsQuotesOnUnix(char c) { return ((c == '\'') || (c == '`') || (c == ';') || (c == '#') || (c == '&') || (c == '$') || (c == '(') || (c == ')') || (c == '~') || @@ -226,51 +232,52 @@ static int Shell__CharNeedsQuotesOnUnix(char c) (c == '\\')); } -static int Shell__CharNeedsQuotesOnWindows(char c) +static bool Shell__CharNeedsQuotesOnWindows(char c) { return ((c == '\'') || (c == '#') || (c == '&') || (c == '<') || (c == '>') || (c == '|') || (c == '^')); } -static int Shell__CharIsMakeVariableName(char c) +static bool Shell__CharIsMakeVariableName(char c) { return c && (c == '_' || isalpha((static_cast<int>(c)))); } -int cmOutputConverter::Shell__CharNeedsQuotes(char c, int flags) +bool cmOutputConverter::Shell__CharNeedsQuotes(char c, int flags) { /* On Windows the built-in command shell echo never needs quotes. */ if (!(flags & Shell_Flag_IsUnix) && (flags & Shell_Flag_EchoWindows)) { - return 0; + return false; } /* On all platforms quotes are needed to preserve whitespace. */ if (Shell__CharIsWhitespace(c)) { - return 1; + return true; } if (flags & Shell_Flag_IsUnix) { /* On UNIX several special characters need quotes to preserve them. */ if (Shell__CharNeedsQuotesOnUnix(c)) { - return 1; + return true; } } else { /* On Windows several special characters need quotes to preserve them. */ if (Shell__CharNeedsQuotesOnWindows(c)) { - return 1; + return true; } } - return 0; + return false; } -const char* cmOutputConverter::Shell__SkipMakeVariables(const char* c) +cm::string_view::iterator cmOutputConverter::Shell__SkipMakeVariables( + cm::string_view::iterator c, cm::string_view::iterator end) { - while (*c == '$' && *(c + 1) == '(') { - const char* skip = c + 2; - while (Shell__CharIsMakeVariableName(*skip)) { + while ((c != end && (c + 1) != end) && (*c == '$' && *(c + 1) == '(')) { + cm::string_view::iterator skip = c + 2; + while ((skip != end) && Shell__CharIsMakeVariableName(*skip)) { ++skip; } - if (*skip == ')') { + if ((skip != end) && *skip == ')') { c = skip + 1; } else { break; @@ -302,63 +309,60 @@ flag later when we understand applications of this better. */ #define KWSYS_SYSTEM_SHELL_QUOTE_MAKE_VARIABLES 0 -int cmOutputConverter::Shell__ArgumentNeedsQuotes(const char* in, int flags) +bool cmOutputConverter::Shell__ArgumentNeedsQuotes(cm::string_view in, + int flags) { /* The empty string needs quotes. */ - if (!*in) { - return 1; + if (in.empty()) { + return true; } /* Scan the string for characters that require quoting. */ - { - const char* c; - for (c = in; *c; ++c) { - /* Look for $(MAKEVAR) syntax if requested. */ - if (flags & Shell_Flag_AllowMakeVariables) { + for (cm::string_view::iterator cit = in.begin(), cend = in.end(); + cit != cend; ++cit) { + /* Look for $(MAKEVAR) syntax if requested. */ + if (flags & Shell_Flag_AllowMakeVariables) { #if KWSYS_SYSTEM_SHELL_QUOTE_MAKE_VARIABLES - const char* skip = Shell__SkipMakeVariables(c); - if (skip != c) { - /* We need to quote make variable references to preserve the - string with contents substituted in its place. */ - return 1; - } + cm::string_view::iterator skip = Shell__SkipMakeVariables(cit, cend); + if (skip != cit) { + /* We need to quote make variable references to preserve the + string with contents substituted in its place. */ + return true; + } #else - /* Skip over the make variable references if any are present. */ - c = Shell__SkipMakeVariables(c); + /* Skip over the make variable references if any are present. */ + cit = Shell__SkipMakeVariables(cit, cend); - /* Stop if we have reached the end of the string. */ - if (!*c) { - break; - } -#endif + /* Stop if we have reached the end of the string. */ + if (cit == cend) { + break; } +#endif + } - /* Check whether this character needs quotes. */ - if (Shell__CharNeedsQuotes(*c, flags)) { - return 1; - } + /* Check whether this character needs quotes. */ + if (Shell__CharNeedsQuotes(*cit, flags)) { + return true; } } /* On Windows some single character arguments need quotes. */ - if (flags & Shell_Flag_IsUnix && *in && !*(in + 1)) { - char c = *in; + if (flags & Shell_Flag_IsUnix && in.size() == 1) { + char c = in[0]; if ((c == '?') || (c == '&') || (c == '^') || (c == '|') || (c == '#')) { - return 1; + return true; } } - return 0; + return false; } -std::string cmOutputConverter::Shell__GetArgument(const char* in, int flags) +std::string cmOutputConverter::Shell__GetArgument(cm::string_view in, + int flags) { /* Output will be at least as long as input string. */ std::string out; - out.reserve(strlen(in)); - - /* String iterator. */ - const char* c; + out.reserve(in.size()); /* Keep track of how many backslashes have been encountered in a row. */ int windows_backslashes = 0; @@ -378,14 +382,15 @@ std::string cmOutputConverter::Shell__GetArgument(const char* in, int flags) } /* Scan the string for characters that require escaping or quoting. */ - for (c = in; *c; ++c) { + for (cm::string_view::iterator cit = in.begin(), cend = in.end(); + cit != cend; ++cit) { /* Look for $(MAKEVAR) syntax if requested. */ if (flags & Shell_Flag_AllowMakeVariables) { - const char* skip = Shell__SkipMakeVariables(c); - if (skip != c) { + cm::string_view::iterator skip = Shell__SkipMakeVariables(cit, cend); + if (skip != cit) { /* Copy to the end of the make variable references. */ - while (c != skip) { - out += *c++; + while (cit != skip) { + out += *cit++; } /* The make variable reference eliminates any escaping needed @@ -393,7 +398,7 @@ std::string cmOutputConverter::Shell__GetArgument(const char* in, int flags) windows_backslashes = 0; /* Stop if we have reached the end of the string. */ - if (!*c) { + if (cit == cend) { break; } } @@ -403,7 +408,7 @@ std::string cmOutputConverter::Shell__GetArgument(const char* in, int flags) if (flags & Shell_Flag_IsUnix) { /* On Unix a few special characters need escaping even inside a quoted argument. */ - if (*c == '\\' || *c == '"' || *c == '`' || *c == '$') { + if (*cit == '\\' || *cit == '"' || *cit == '`' || *cit == '$') { /* This character needs a backslash to escape it. */ out += '\\'; } @@ -411,10 +416,10 @@ std::string cmOutputConverter::Shell__GetArgument(const char* in, int flags) /* On Windows the built-in command shell echo never needs escaping. */ } else { /* On Windows only backslashes and double-quotes need escaping. */ - if (*c == '\\') { + if (*cit == '\\') { /* Found a backslash. It may need to be escaped later. */ ++windows_backslashes; - } else if (*c == '"') { + } else if (*cit == '"') { /* Found a double-quote. Escape all immediately preceding backslashes. */ while (windows_backslashes > 0) { @@ -432,7 +437,7 @@ std::string cmOutputConverter::Shell__GetArgument(const char* in, int flags) } /* Check whether this character needs escaping for a make tool. */ - if (*c == '$') { + if (*cit == '$') { if (flags & Shell_Flag_Make) { /* In Makefiles a dollar is written $$. The make tool will replace it with just $ before passing it to the shell. */ @@ -449,7 +454,7 @@ std::string cmOutputConverter::Shell__GetArgument(const char* in, int flags) /* Otherwise a dollar is written just $. */ out += '$'; } - } else if (*c == '#') { + } else if (*cit == '#') { if ((flags & Shell_Flag_Make) && (flags & Shell_Flag_WatcomWMake)) { /* In Watcom WMake makefiles a pound is written $#. The make tool will replace it with just # before passing it to the @@ -459,7 +464,7 @@ std::string cmOutputConverter::Shell__GetArgument(const char* in, int flags) /* Otherwise a pound is written just #. */ out += '#'; } - } else if (*c == '%') { + } else if (*cit == '%') { if ((flags & Shell_Flag_VSIDE) || ((flags & Shell_Flag_Make) && ((flags & Shell_Flag_MinGWMake) || (flags & Shell_Flag_NMake)))) { @@ -469,7 +474,7 @@ std::string cmOutputConverter::Shell__GetArgument(const char* in, int flags) /* Otherwise a percent is written just %. */ out += '%'; } - } else if (*c == ';') { + } else if (*cit == ';') { if (flags & Shell_Flag_VSIDE) { /* In a VS IDE a semicolon is written ";". If this is written in an un-quoted argument it starts a quoted segment, @@ -483,7 +488,7 @@ std::string cmOutputConverter::Shell__GetArgument(const char* in, int flags) } } else { /* Store this character. */ - out += *c; + out += *cit; } } |