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);
96 void round_trip ( const wchar_t *p ) {
97 std::wstringstream ss;
98 bool skippingws = is_skipws ( &ss );
101 ss >> std::quoted(s);
103 assert ( skippingws == is_skipws ( &ss ));
107 void round_trip_ws ( const wchar_t *p ) {
108 std::wstringstream ss;
109 std::noskipws ( ss );
110 bool skippingws = is_skipws ( &ss );
111 ss << std::quoted(p);
113 ss >> std::quoted(s);
115 assert ( skippingws == is_skipws ( &ss ));
118 void round_trip_d ( const wchar_t *p, wchar_t delim ) {
119 std::wstringstream ss;
120 ss << std::quoted(p, delim);
122 ss >> std::quoted(s, delim);
126 void round_trip_e ( const wchar_t *p, wchar_t escape ) {
127 std::wstringstream ss;
128 ss << std::quoted(p, wchar_t('"'), escape );
130 ss >> std::quoted(s, wchar_t('"'), escape );
135 std::wstring quote ( const wchar_t *p, wchar_t delim='"', wchar_t escape='\\' ) {
136 std::wstringstream ss;
137 ss << std::quoted(p, delim, escape);
143 std::wstring unquote ( const wchar_t *p, wchar_t delim='"', wchar_t escape='\\' ) {
144 std::wstringstream ss;
147 ss >> std::quoted(s, delim, escape);
153 both_ways ( "" ); // This is a compilation check
156 round_trip_ws ( "" );
157 round_trip_d ( "", 'q' );
158 round_trip_e ( "", 'q' );
161 round_trip_ws ( L"" );
162 round_trip_d ( L"", 'q' );
163 round_trip_e ( L"", 'q' );
166 round_trip_ws ( "Hi" );
167 round_trip_d ( "Hi", '!' );
168 round_trip_e ( "Hi", '!' );
169 assert ( quote ( "Hi", '!' ) == "!Hi!" );
170 assert ( quote ( "Hi!", '!' ) == R"(!Hi\!!)" );
172 round_trip ( L"Hi" );
173 round_trip_ws ( L"Hi" );
174 round_trip_d ( L"Hi", '!' );
175 round_trip_e ( L"Hi", '!' );
176 assert ( quote ( L"Hi", '!' ) == L"!Hi!" );
177 assert ( quote ( L"Hi!", '!' ) == LR"(!Hi\!!)" );
179 round_trip ( "Hi Mom" );
180 round_trip_ws ( "Hi Mom" );
181 round_trip ( L"Hi Mom" );
182 round_trip_ws ( L"Hi Mom" );
184 assert ( quote ( "" ) == "\"\"" );
185 assert ( quote ( L"" ) == L"\"\"" );
186 assert ( quote ( "a" ) == "\"a\"" );
187 assert ( quote ( L"a" ) == L"\"a\"" );
189 // missing end quote - must not hang
190 assert ( unquote ( "\"abc" ) == "abc" );
191 assert ( unquote ( L"\"abc" ) == L"abc" );
193 assert ( unquote ( "abc" ) == "abc" ); // no delimiter
194 assert ( unquote ( L"abc" ) == L"abc" ); // no delimiter
195 assert ( unquote ( "abc def" ) == "abc" ); // no delimiter
196 assert ( unquote ( L"abc def" ) == L"abc" ); // no delimiter
198 assert ( unquote ( "" ) == "" ); // nothing there
199 assert ( unquote ( L"" ) == L"" ); // nothing there