| 1 |
// -*-c++-*- |
| 2 |
// vim: set ft=cpp: |
| 3 |
|
| 4 |
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
| 5 |
file LICENSE.rst or https://cmake.org/licensing for details. */ |
| 6 |
#pragma once |
| 7 |
|
| 8 |
#include <iomanip> // IWYU pragma: export |
| 9 |
#if __cplusplus < 201402L || defined(_MSVC_LANG) && _MSVC_LANG < 201402L |
| 10 |
# include <ios> |
| 11 |
# include <iostream> |
| 12 |
# include <sstream> |
| 13 |
# include <string> |
| 14 |
# include <type_traits> |
| 15 |
#endif |
| 16 |
#if __cplusplus < 201703L || defined(_MSVC_LANG) && _MSVC_LANG < 201703L |
| 17 |
# include <cm/string_view> |
| 18 |
#endif |
| 19 |
|
| 20 |
namespace cm { |
| 21 |
|
| 22 |
#if __cplusplus >= 201402L || defined(_MSVC_LANG) && _MSVC_LANG >= 201402L |
| 23 |
|
| 24 |
using std::quoted; |
| 25 |
|
| 26 |
# if __cplusplus < 201703L || defined(_MSVC_LANG) && _MSVC_LANG < 201703L |
| 27 |
|
| 28 |
inline auto quoted(cm::string_view str, char delim = '"', char escape = '\\') |
| 29 |
{ |
| 30 |
return std::quoted(static_cast<std::string>(str), delim, escape); |
| 31 |
} |
| 32 |
|
| 33 |
# endif |
| 34 |
|
| 35 |
#else |
| 36 |
|
| 37 |
namespace internals { |
| 38 |
|
| 39 |
// Struct for delimited strings. |
| 40 |
template <typename String, typename Char> |
| 41 |
struct quoted_string |
| 42 |
{ |
| 43 |
static_assert(std::is_reference<String>::value || |
| 44 |
std::is_pointer<String>::value, |
| 45 |
"String type must be pointer or reference"); |
| 46 |
|
| 47 |
quoted_string(String str, Char del, Char esc) |
| 48 |
: string_(str) |
| 49 |
, delim_{ del } |
| 50 |
, escape_{ esc } |
| 51 |
{ |
| 52 |
} |
| 53 |
|
| 54 |
quoted_string& operator=(quoted_string&) = delete; |
| 55 |
|
| 56 |
String string_; |
| 57 |
Char delim_; |
| 58 |
Char escape_; |
| 59 |
}; |
| 60 |
|
| 61 |
template <> |
| 62 |
struct quoted_string<cm::string_view, char> |
| 63 |
{ |
| 64 |
quoted_string(cm::string_view str, char del, char esc) |
| 65 |
: string_(str) |
| 66 |
, delim_{ del } |
| 67 |
, escape_{ esc } |
| 68 |
{ |
| 69 |
} |
| 70 |
|
| 71 |
quoted_string& operator=(quoted_string&) = delete; |
| 72 |
|
| 73 |
cm::string_view string_; |
| 74 |
char delim_; |
| 75 |
char escape_; |
| 76 |
}; |
| 77 |
|
| 78 |
template <typename Char, typename Traits> |
| 79 |
std::basic_ostream<Char, Traits>& operator<<( |
| 80 |
std::basic_ostream<Char, Traits>& os, |
| 81 |
quoted_string<Char const*, Char> const& str) |
| 82 |
{ |
| 83 |
std::basic_ostringstream<Char, Traits> ostr; |
| 84 |
ostr << str.delim_; |
| 85 |
for (Char const* c = str.string_; *c; ++c) { |
| 86 |
if (*c == str.delim_ || *c == str.escape_) |
| 87 |
ostr << str.escape_; |
| 88 |
ostr << *c; |
| 89 |
} |
| 90 |
ostr << str.delim_; |
| 91 |
|
| 92 |
return os << ostr.str(); |
| 93 |
} |
| 94 |
|
| 95 |
template <typename Char, typename Traits, typename String> |
| 96 |
std::basic_ostream<Char, Traits>& operator<<( |
| 97 |
std::basic_ostream<Char, Traits>& os, quoted_string<String, Char> const& str) |
| 98 |
{ |
| 99 |
std::basic_ostringstream<Char, Traits> ostr; |
| 100 |
ostr << str.delim_; |
| 101 |
for (auto c : str.string_) { |
| 102 |
if (c == str.delim_ || c == str.escape_) |
| 103 |
ostr << str.escape_; |
| 104 |
ostr << c; |
| 105 |
} |
| 106 |
ostr << str.delim_; |
| 107 |
|
| 108 |
return os << ostr.str(); |
| 109 |
} |
| 110 |
|
| 111 |
template <typename Char, typename Traits, typename Alloc> |
| 112 |
std::basic_istream<Char, Traits>& operator>>( |
| 113 |
std::basic_istream<Char, Traits>& is, |
| 114 |
quoted_string<std::basic_string<Char, Traits, Alloc>&, Char> const& str) |
| 115 |
{ |
| 116 |
Char c; |
| 117 |
is >> c; |
| 118 |
if (!is.good()) |
| 119 |
return is; |
| 120 |
if (c != str.delim_) { |
| 121 |
is.unget(); |
| 122 |
is >> str.string_; |
| 123 |
return is; |
| 124 |
} |
| 125 |
str.string_.clear(); |
| 126 |
std::ios_base::fmtflags flags = |
| 127 |
is.flags(is.flags() & ~std::ios_base::skipws); |
| 128 |
do { |
| 129 |
is >> c; |
| 130 |
if (!is.good()) |
| 131 |
break; |
| 132 |
if (c == str.escape_) { |
| 133 |
is >> c; |
| 134 |
if (!is.good()) |
| 135 |
break; |
| 136 |
} else if (c == str.delim_) |
| 137 |
break; |
| 138 |
str.string_ += c; |
| 139 |
} while (true); |
| 140 |
is.setf(flags); |
| 141 |
|
| 142 |
return is; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
template <typename Char> |
| 147 |
inline internals::quoted_string<Char const*, Char> quoted( |
| 148 |
Char const* str, Char delim = Char('"'), Char escape = Char('\\')) |
| 149 |
{ |
| 150 |
return internals::quoted_string<Char const*, Char>(str, delim, escape); |
| 151 |
} |
| 152 |
|
| 153 |
template <typename Char, typename Traits, typename Alloc> |
| 154 |
inline internals::quoted_string<std::basic_string<Char, Traits, Alloc> const&, |
| 155 |
Char> |
| 156 |
quoted(std::basic_string<Char, Traits, Alloc> const& str, |
| 157 |
Char delim = Char('"'), Char escape = Char('\\')) |
| 158 |
{ |
| 159 |
return internals::quoted_string< |
| 160 |
std::basic_string<Char, Traits, Alloc> const&, Char>(str, delim, escape); |
| 161 |
} |
| 162 |
|
| 163 |
template <typename Char, typename Traits, typename Alloc> |
| 164 |
inline internals::quoted_string<std::basic_string<Char, Traits, Alloc>&, Char> |
| 165 |
quoted(std::basic_string<Char, Traits, Alloc>& str, Char delim = Char('"'), |
| 166 |
Char escape = Char('\\')) |
| 167 |
{ |
| 168 |
return internals::quoted_string<std::basic_string<Char, Traits, Alloc>&, |
| 169 |
Char>(str, delim, escape); |
| 170 |
} |
| 171 |
|
| 172 |
inline internals::quoted_string<cm::string_view, char> quoted( |
| 173 |
cm::string_view str, char delim = '"', char escape = '\\') |
| 174 |
{ |
| 175 |
return internals::quoted_string<cm::string_view, char>(str, delim, escape); |
| 176 |
} |
| 177 |
|
| 178 |
#endif |
| 179 |
|
| 180 |
} // namespace cm |