Added option to quoted() function to replace newlines.
This commit is contained in:
parent
7da2f7b7f4
commit
3891c0f8ce
@ -1021,7 +1021,12 @@ bool convertStringType(const TFrom* strFrom, std::basic_string<TTo, TToTraits, T
|
||||
return convertStringType(std::basic_string_view<TFrom>(strFrom), outString);
|
||||
}
|
||||
|
||||
template<typename TChar, typename TTraits, typename TAlloc = MIJIN_DEFAULT_ALLOCATOR<TChar>>
|
||||
struct StringQuoteOptions
|
||||
{
|
||||
bool replaceNewlines = false;
|
||||
};
|
||||
|
||||
template<StringQuoteOptions options = {}, 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;
|
||||
@ -1029,8 +1034,28 @@ std::basic_string<TChar, TTraits, TAlloc> quoted(std::basic_string_view<TChar, T
|
||||
result.push_back(TChar('"'));
|
||||
for (const TChar chr : input)
|
||||
{
|
||||
if (chr == TChar('"') || chr == TChar('\\')) {
|
||||
switch (chr)
|
||||
{
|
||||
case TChar('"'):
|
||||
case TChar('\\'):
|
||||
result.push_back(TChar('\\'));
|
||||
break;
|
||||
case TChar('\n'):
|
||||
if constexpr (options.replaceNewlines)
|
||||
{
|
||||
result.push_back(TChar('\\'));
|
||||
result.push_back(TChar('n'));
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
case TChar('\r'):
|
||||
if constexpr (options.replaceNewlines)
|
||||
{
|
||||
result.push_back(TChar('\\'));
|
||||
result.push_back(TChar('r'));
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
result.push_back(chr);
|
||||
}
|
||||
@ -1038,10 +1063,10 @@ std::basic_string<TChar, TTraits, TAlloc> quoted(std::basic_string_view<TChar, T
|
||||
return result;
|
||||
}
|
||||
|
||||
template<typename TChar, typename TTraits, typename TAlloc>
|
||||
template<StringQuoteOptions options = {}, 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));
|
||||
return quoted<options, TChar, TTraits, TAlloc>(std::basic_string_view(input));
|
||||
}
|
||||
} // namespace mijin
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user