mirror of
https://github.com/NixOS/nix.git
synced 2025-11-08 19:46:02 +01:00
strings: Add optionalBracket helper
This commit is contained in:
parent
f77094715f
commit
5dcfddf997
3 changed files with 112 additions and 0 deletions
|
|
@ -494,4 +494,63 @@ TEST(shellSplitString, testUnbalancedQuotes)
|
|||
ASSERT_THROW(shellSplitString("foo\"bar\\\""), Error);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* optionalBracket
|
||||
* --------------------------------------------------------------------------*/
|
||||
|
||||
TEST(optionalBracket, emptyContent)
|
||||
{
|
||||
ASSERT_EQ(optionalBracket(" (", "", ")"), "");
|
||||
}
|
||||
|
||||
TEST(optionalBracket, nonEmptyContent)
|
||||
{
|
||||
ASSERT_EQ(optionalBracket(" (", "foo", ")"), " (foo)");
|
||||
}
|
||||
|
||||
TEST(optionalBracket, emptyPrefixAndSuffix)
|
||||
{
|
||||
ASSERT_EQ(optionalBracket("", "foo", ""), "foo");
|
||||
}
|
||||
|
||||
TEST(optionalBracket, emptyContentEmptyBrackets)
|
||||
{
|
||||
ASSERT_EQ(optionalBracket("", "", ""), "");
|
||||
}
|
||||
|
||||
TEST(optionalBracket, complexBrackets)
|
||||
{
|
||||
ASSERT_EQ(optionalBracket(" [[[", "content", "]]]"), " [[[content]]]");
|
||||
}
|
||||
|
||||
TEST(optionalBracket, onlyPrefix)
|
||||
{
|
||||
ASSERT_EQ(optionalBracket("prefix", "content", ""), "prefixcontent");
|
||||
}
|
||||
|
||||
TEST(optionalBracket, onlySuffix)
|
||||
{
|
||||
ASSERT_EQ(optionalBracket("", "content", "suffix"), "contentsuffix");
|
||||
}
|
||||
|
||||
TEST(optionalBracket, optionalWithValue)
|
||||
{
|
||||
ASSERT_EQ(optionalBracket(" (", std::optional<std::string>("foo"), ")"), " (foo)");
|
||||
}
|
||||
|
||||
TEST(optionalBracket, optionalNullopt)
|
||||
{
|
||||
ASSERT_EQ(optionalBracket(" (", std::optional<std::string>(std::nullopt), ")"), "");
|
||||
}
|
||||
|
||||
TEST(optionalBracket, optionalEmptyString)
|
||||
{
|
||||
ASSERT_EQ(optionalBracket(" (", std::optional<std::string>(""), ")"), "");
|
||||
}
|
||||
|
||||
TEST(optionalBracket, optionalStringViewWithValue)
|
||||
{
|
||||
ASSERT_EQ(optionalBracket(" (", std::optional<std::string_view>("bar"), ")"), " (bar)");
|
||||
}
|
||||
|
||||
} // namespace nix
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
#include "nix/util/types.hh"
|
||||
|
||||
#include <list>
|
||||
#include <optional>
|
||||
#include <set>
|
||||
#include <string_view>
|
||||
#include <string>
|
||||
|
|
@ -93,6 +94,44 @@ extern template std::string dropEmptyInitThenConcatStringsSep(std::string_view,
|
|||
*/
|
||||
std::list<std::string> shellSplitString(std::string_view s);
|
||||
|
||||
/**
|
||||
* Conditionally wrap a string with prefix and suffix brackets.
|
||||
*
|
||||
* If `content` is empty, returns an empty string.
|
||||
* Otherwise, returns `prefix + content + suffix`.
|
||||
*
|
||||
* Example:
|
||||
* optionalBracket(" (", "foo", ")") == " (foo)"
|
||||
* optionalBracket(" (", "", ")") == ""
|
||||
*
|
||||
* Design note: this would have been called `optionalParentheses`, except this
|
||||
* function is more general and more explicit. Parentheses typically *also* need
|
||||
* to be prefixed with a space in order to fit nicely in a piece of natural
|
||||
* language.
|
||||
*/
|
||||
std::string optionalBracket(std::string_view prefix, std::string_view content, std::string_view suffix);
|
||||
|
||||
/**
|
||||
* Overload for optional content.
|
||||
*
|
||||
* If `content` is nullopt or contains an empty string, returns an empty string.
|
||||
* Otherwise, returns `prefix + *content + suffix`.
|
||||
*
|
||||
* Example:
|
||||
* optionalBracket(" (", std::optional<std::string>("foo"), ")") == " (foo)"
|
||||
* optionalBracket(" (", std::nullopt, ")") == ""
|
||||
* optionalBracket(" (", std::optional<std::string>(""), ")") == ""
|
||||
*/
|
||||
template<typename T>
|
||||
requires std::convertible_to<T, std::string_view>
|
||||
std::string optionalBracket(std::string_view prefix, const std::optional<T> & content, std::string_view suffix)
|
||||
{
|
||||
if (!content || std::string_view(*content).empty()) {
|
||||
return "";
|
||||
}
|
||||
return optionalBracket(prefix, std::string_view(*content), suffix);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hash implementation that can be used for zero-copy heterogenous lookup from
|
||||
* P1690R1[1] in unordered containers.
|
||||
|
|
|
|||
|
|
@ -138,4 +138,18 @@ std::list<std::string> shellSplitString(std::string_view s)
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string optionalBracket(std::string_view prefix, std::string_view content, std::string_view suffix)
|
||||
{
|
||||
if (content.empty()) {
|
||||
return "";
|
||||
}
|
||||
std::string result;
|
||||
result.reserve(prefix.size() + content.size() + suffix.size());
|
||||
result.append(prefix);
|
||||
result.append(content);
|
||||
result.append(suffix);
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace nix
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue