Skip to content

Commit 9ab15fa

Browse files
KyleFromKitwarekwrobot
authored andcommitted
Merge topic 'cmRemoveQuotes'
2709009 cmStringAlgorithms: Add cmRemoveQuotes Acked-by: Kitware Robot <[email protected]> Merge-request: !3665
2 parents 9e9766d + 2709009 commit 9ab15fa

File tree

5 files changed

+40
-16
lines changed

5 files changed

+40
-16
lines changed

Source/cmGlobalGenerator.cxx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2270,14 +2270,6 @@ bool cmGlobalGenerator::NameResolvesToFramework(
22702270
return false;
22712271
}
22722272

2273-
inline std::string removeQuotes(const std::string& s)
2274-
{
2275-
if (s.front() == '\"' && s.back() == '\"') {
2276-
return s.substr(1, s.size() - 2);
2277-
}
2278-
return s;
2279-
}
2280-
22812273
bool cmGlobalGenerator::CheckCMP0037(std::string const& targetName,
22822274
std::string const& reason) const
22832275
{

Source/cmStringAlgorithms.cxx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ std::string cmTrimWhitespace(cm::string_view str)
2121
return std::string(start, stop + 1);
2222
}
2323

24+
std::string cmRemoveQuotes(cm::string_view str)
25+
{
26+
// We process only strings that have two quotes at least.
27+
// Also front() and back() are only defined behavior on non empty strings.
28+
if (str.size() >= 2 && //
29+
str.front() == '"' && //
30+
str.back() == '"') {
31+
// Remove a quote from the front and back
32+
str.remove_prefix(1);
33+
str.remove_suffix(1);
34+
}
35+
return std::string(str);
36+
}
37+
2438
std::string cmEscapeQuotes(cm::string_view str)
2539
{
2640
std::string result;

Source/cmStringAlgorithms.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ inline bool cmIsSpace(char ch)
4141
/** Returns a string that has whitespace removed from the start and the end. */
4242
std::string cmTrimWhitespace(cm::string_view str);
4343

44+
/** Returns a string that has quotes removed from the start and the end. */
45+
std::string cmRemoveQuotes(cm::string_view str);
46+
4447
/** Escape quotes in a string. */
4548
std::string cmEscapeQuotes(cm::string_view str);
4649

Source/cmake.cxx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2270,14 +2270,6 @@ void cmake::TruncateOutputLog(const char* fname)
22702270
}
22712271
}
22722272

2273-
inline std::string removeQuotes(const std::string& s)
2274-
{
2275-
if (s.front() == '\"' && s.back() == '\"') {
2276-
return s.substr(1, s.size() - 2);
2277-
}
2278-
return s;
2279-
}
2280-
22812273
void cmake::MarkCliAsUsed(const std::string& variable)
22822274
{
22832275
this->UsedCliVariables[variable] = true;

Tests/CMakeLib/testStringAlgorithms.cxx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,29 @@ int testStringAlgorithms(int /*unused*/, char* /*unused*/ [])
5050
"cmTrimWhitespace front and back");
5151
}
5252

53+
// ----------------------------------------------------------------------
54+
// Test cmRemoveQuotes
55+
{
56+
auto test = [&assert_string](cm::string_view source,
57+
cm::string_view expected,
58+
cm::string_view title) {
59+
assert_string(cmRemoveQuotes(source), expected, title);
60+
};
61+
62+
test("", "", "cmRemoveQuotes empty");
63+
test("\"", "\"", "cmRemoveQuotes single quote");
64+
test("\"\"", "", "cmRemoveQuotes double quote");
65+
test("\"a", "\"a", "cmRemoveQuotes quote char");
66+
test("\"ab", "\"ab", "cmRemoveQuotes quote char char");
67+
test("a\"", "a\"", "cmRemoveQuotes char quote");
68+
test("ab\"", "ab\"", "cmRemoveQuotes char char quote");
69+
test("a", "a", "cmRemoveQuotes single char");
70+
test("ab", "ab", "cmRemoveQuotes two chars");
71+
test("abc", "abc", "cmRemoveQuotes three chars");
72+
test("\"abc\"", "abc", "cmRemoveQuotes quoted chars");
73+
test("\"\"abc\"\"", "\"abc\"", "cmRemoveQuotes quoted quoted chars");
74+
}
75+
5376
// ----------------------------------------------------------------------
5477
// Test cmEscapeQuotes
5578
{

0 commit comments

Comments
 (0)