1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11
10 // UNSUPPORTED: no-localization
11 // UNSUPPORTED: !stdlib=libc++ && c++14
19 #include <string_view>
22 #include "test_macros.h"
24 bool is_skipws(const std::istream
* is
) { return (is
->flags() & std::ios_base::skipws
) != 0; }
26 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
27 bool is_skipws(const std::wistream
* is
) { return (is
->flags() & std::ios_base::skipws
) != 0; }
30 void round_trip(const char* p
) {
32 bool skippingws
= is_skipws(&ss
);
33 std::string_view sv
{p
};
35 ss
<< std::quoted(sv
);
39 assert(skippingws
== is_skipws(&ss
));
42 void round_trip_ws(const char* p
) {
45 bool skippingws
= is_skipws(&ss
);
46 std::string_view sv
{p
};
48 ss
<< std::quoted(sv
);
52 assert(skippingws
== is_skipws(&ss
));
55 void round_trip_d(const char* p
, char delim
) {
57 std::string_view sv
{p
};
59 ss
<< std::quoted(sv
, delim
);
61 ss
>> std::quoted(s
, delim
);
65 void round_trip_e(const char* p
, char escape
) {
67 std::string_view sv
{p
};
69 ss
<< std::quoted(sv
, '"', escape
);
71 ss
>> std::quoted(s
, '"', escape
);
75 std::string
quote(const char* p
, char delim
= '"', char escape
= '\\') {
77 ss
<< std::quoted(p
, delim
, escape
);
83 std::string
unquote(const char* p
, char delim
= '"', char escape
= '\\') {
87 ss
>> std::quoted(s
, delim
, escape
);
91 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
92 void round_trip(const wchar_t* p
) {
93 std::wstringstream ss
;
94 bool skippingws
= is_skipws(&ss
);
95 std::wstring_view sv
{p
};
97 ss
<< std::quoted(sv
);
101 assert(skippingws
== is_skipws(&ss
));
104 void round_trip_ws(const wchar_t* p
) {
105 std::wstringstream ss
;
107 bool skippingws
= is_skipws(&ss
);
108 std::wstring_view sv
{p
};
110 ss
<< std::quoted(sv
);
112 ss
>> std::quoted(s
);
114 assert(skippingws
== is_skipws(&ss
));
117 void round_trip_d(const wchar_t* p
, wchar_t delim
) {
118 std::wstringstream ss
;
119 std::wstring_view sv
{p
};
121 ss
<< std::quoted(sv
, delim
);
123 ss
>> std::quoted(s
, delim
);
127 void round_trip_e(const wchar_t* p
, wchar_t escape
) {
128 std::wstringstream ss
;
129 std::wstring_view sv
{p
};
131 ss
<< std::quoted(sv
, wchar_t('"'), escape
);
133 ss
>> std::quoted(s
, wchar_t('"'), escape
);
137 std::wstring
quote(const wchar_t* p
, wchar_t delim
= '"', wchar_t escape
= '\\') {
138 std::wstringstream ss
;
139 std::wstring_view sv
{p
};
141 ss
<< std::quoted(sv
, delim
, escape
);
147 std::wstring
unquote(const wchar_t* p
, wchar_t delim
= '"', wchar_t escape
= '\\') {
148 std::wstringstream ss
;
149 std::wstring_view sv
{p
};
153 ss
>> std::quoted(s
, delim
, escape
);
156 #endif // TEST_HAS_NO_WIDE_CHARACTERS
158 int main(int, char**) {
161 round_trip_d("", 'q');
162 round_trip_e("", 'q');
164 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
167 round_trip_d(L
"", 'q');
168 round_trip_e(L
"", 'q');
173 round_trip_d("Hi", '!');
174 round_trip_e("Hi", '!');
175 assert(quote("Hi", '!') == "!Hi!");
176 assert(quote("Hi!", '!') == R
"(!Hi\!!)");
178 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
180 round_trip_ws(L
"Hi");
181 round_trip_d(L
"Hi", '!');
182 round_trip_e(L
"Hi", '!');
183 assert(quote(L
"Hi", '!') == L
"!Hi!");
184 assert(quote(L
"Hi!", '!') == LR
"(!Hi\!!)");
187 round_trip("Hi Mom");
188 round_trip_ws("Hi Mom");
190 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
191 round_trip(L
"Hi Mom");
192 round_trip_ws(L
"Hi Mom");
195 assert(quote("") == "\"\"");
196 assert(quote("a") == "\"a\"");
197 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
198 assert(quote(L
"") == L
"\"\"");
199 assert(quote(L
"a") == L
"\"a\"");
202 // missing end quote - must not hang
203 assert(unquote("\"abc") == "abc");
204 assert(unquote("abc") == "abc"); // no delimiter
205 assert(unquote("abc def") == "abc"); // no delimiter
206 assert(unquote("") == ""); // nothing there
208 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
209 assert(unquote(L
"\"abc") == L
"abc");
210 assert(unquote(L
"abc") == L
"abc"); // no delimiter
211 assert(unquote(L
"abc def") == L
"abc"); // no delimiter
212 assert(unquote(L
"") == L
""); // nothing there