1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
19 #if _LIBCPP_STD_VER > 11
21 bool is_skipws ( const std::istream *is ) {
22 return ( is->flags() & std::ios_base::skipws ) != 0;
26 bool is_skipws ( const std::wistream *is ) {
27 return ( is->flags() & std::ios_base::skipws ) != 0;
30 void both_ways ( const char *p ) {
32 auto q = std::quoted(str);
35 bool skippingws = is_skipws ( &ss );
40 void round_trip ( const char *p ) {
42 bool skippingws = is_skipws ( &ss );
47 assert ( skippingws == is_skipws ( &ss ));
50 void round_trip_ws ( const char *p ) {
53 bool skippingws = is_skipws ( &ss );
58 assert ( skippingws == is_skipws ( &ss ));
61 void round_trip_d ( const char *p, char delim ) {
63 ss << std::quoted(p, delim);
65 ss >> std::quoted(s, delim);
69 void round_trip_e ( const char *p, char escape ) {
71 ss << std::quoted(p, '"', escape );
73 ss >> std::quoted(s, '"', escape );
79 std::string quote ( const char *p, char delim='"', char escape='\\' ) {
81 ss << std::quoted(p, delim, escape);
87 std::string unquote ( const char *p, char delim='"', char escape='\\' ) {
91 ss >> std::quoted(s, delim, escape);
95 void test_padding () {
98 ss << std::left << std::setw(10) << std::setfill('!') << std::quoted("abc", '`');
99 assert ( ss.str() == "`abc`!!!!!" );
103 std::stringstream ss;
104 ss << std::right << std::setw(10) << std::setfill('!') << std::quoted("abc", '`');
105 assert ( ss.str() == "!!!!!`abc`" );
110 void round_trip ( const wchar_t *p ) {
111 std::wstringstream ss;
112 bool skippingws = is_skipws ( &ss );
113 ss << std::quoted(p);
115 ss >> std::quoted(s);
117 assert ( skippingws == is_skipws ( &ss ));
121 void round_trip_ws ( const wchar_t *p ) {
122 std::wstringstream ss;
123 std::noskipws ( ss );
124 bool skippingws = is_skipws ( &ss );
125 ss << std::quoted(p);
127 ss >> std::quoted(s);
129 assert ( skippingws == is_skipws ( &ss ));
132 void round_trip_d ( const wchar_t *p, wchar_t delim ) {
133 std::wstringstream ss;
134 ss << std::quoted(p, delim);
136 ss >> std::quoted(s, delim);
140 void round_trip_e ( const wchar_t *p, wchar_t escape ) {
141 std::wstringstream ss;
142 ss << std::quoted(p, wchar_t('"'), escape );
144 ss >> std::quoted(s, wchar_t('"'), escape );
149 std::wstring quote ( const wchar_t *p, wchar_t delim='"', wchar_t escape='\\' ) {
150 std::wstringstream ss;
151 ss << std::quoted(p, delim, escape);
157 std::wstring unquote ( const wchar_t *p, wchar_t delim='"', wchar_t escape='\\' ) {
158 std::wstringstream ss;
161 ss >> std::quoted(s, delim, escape);
167 both_ways ( "" ); // This is a compilation check
170 round_trip_ws ( "" );
171 round_trip_d ( "", 'q' );
172 round_trip_e ( "", 'q' );
175 round_trip_ws ( L"" );
176 round_trip_d ( L"", 'q' );
177 round_trip_e ( L"", 'q' );
180 round_trip_ws ( "Hi" );
181 round_trip_d ( "Hi", '!' );
182 round_trip_e ( "Hi", '!' );
183 assert ( quote ( "Hi", '!' ) == "!Hi!" );
184 assert ( quote ( "Hi!", '!' ) == R"(!Hi\!!)" );
186 round_trip ( L"Hi" );
187 round_trip_ws ( L"Hi" );
188 round_trip_d ( L"Hi", '!' );
189 round_trip_e ( L"Hi", '!' );
190 assert ( quote ( L"Hi", '!' ) == L"!Hi!" );
191 assert ( quote ( L"Hi!", '!' ) == LR"(!Hi\!!)" );
193 round_trip ( "Hi Mom" );
194 round_trip_ws ( "Hi Mom" );
195 round_trip ( L"Hi Mom" );
196 round_trip_ws ( L"Hi Mom" );
198 assert ( quote ( "" ) == "\"\"" );
199 assert ( quote ( L"" ) == L"\"\"" );
200 assert ( quote ( "a" ) == "\"a\"" );
201 assert ( quote ( L"a" ) == L"\"a\"" );
203 // missing end quote - must not hang
204 assert ( unquote ( "\"abc" ) == "abc" );
205 assert ( unquote ( L"\"abc" ) == L"abc" );
207 assert ( unquote ( "abc" ) == "abc" ); // no delimiter
208 assert ( unquote ( L"abc" ) == L"abc" ); // no delimiter
209 assert ( unquote ( "abc def" ) == "abc" ); // no delimiter
210 assert ( unquote ( L"abc def" ) == L"abc" ); // no delimiter
212 assert ( unquote ( "" ) == "" ); // nothing there
213 assert ( unquote ( L"" ) == L"" ); // nothing there