Added quoted() string helper.

This commit is contained in:
Patrick Wuttke 2025-08-30 00:31:27 +02:00
parent 0e988a4d9e
commit 4d19752964

View File

@ -1020,6 +1020,29 @@ bool convertStringType(const TFrom* strFrom, std::basic_string<TTo, TToTraits, T
{ {
return convertStringType(std::basic_string_view<TFrom>(strFrom), outString); return convertStringType(std::basic_string_view<TFrom>(strFrom), outString);
} }
template<typename TChar, typename TTraits, typename TAlloc = MIJIN_DEFAULT_ALLOCATOR<TChar>>
std::basic_string<TChar, TTraits, TAlloc> quoted(std::basic_string_view<TChar, TTraits> input)
{
std::basic_string<TChar, TTraits> result;
result.reserve(input.size() + 2);
result.push_back(TChar('"'));
for (const TChar chr : input)
{
if (chr == TChar('"') || chr == TChar('\\')) {
result.push_back(TChar('\\'));
}
result.push_back(chr);
}
result.push_back(TChar('"'));
return result;
}
template<typename TChar, typename TTraits, typename TAlloc>
std::basic_string<TChar, TTraits, TAlloc> quoted(const std::basic_string<TChar, TTraits, TAlloc>& input)
{
return quoted<TChar, TTraits, TAlloc>(std::basic_string_view(input));
}
} // namespace mijin } // namespace mijin
#endif // !defined(MIJIN_UTIL_STRING_HPP_INCLUDED) #endif // !defined(MIJIN_UTIL_STRING_HPP_INCLUDED)