1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <iomanip>
11 
12 // quoted
13 
14 #include <iomanip>
15 #include <sstream>
16 #include <string>
17 #include <cassert>
18 
19 #if _LIBCPP_STD_VER > 11
20 
21 bool is_skipws ( const std::istream *is ) {
22     return ( is->flags() & std::ios_base::skipws ) != 0;
23     }
24 
25 
26 bool is_skipws ( const std::wistream *is ) {
27     return ( is->flags() & std::ios_base::skipws ) != 0;
28     }
29 
30 void both_ways ( const char *p ) {
31 	std::string str(p);
32 	auto q = std::quoted(str);
33 
34     std::stringstream ss;
35     bool skippingws = is_skipws ( &ss );
36 	ss << q;
37 	ss >> q;
38     }
39 
40 void round_trip ( const char *p ) {
41     std::stringstream ss;
42     bool skippingws = is_skipws ( &ss );
43     ss << std::quoted(p);
44     std::string s;
45     ss >> std::quoted(s);
46     assert ( s == p );
47     assert ( skippingws == is_skipws ( &ss ));
48     }
49 
50 void round_trip_ws ( const char *p ) {
51     std::stringstream ss;
52     std::noskipws ( ss );
53     bool skippingws = is_skipws ( &ss );
54     ss << std::quoted(p);
55     std::string s;
56     ss >> std::quoted(s);
57     assert ( s == p );
58     assert ( skippingws == is_skipws ( &ss ));
59     }
60 
61 void round_trip_d ( const char *p, char delim ) {
62     std::stringstream ss;
63     ss << std::quoted(p, delim);
64     std::string s;
65     ss >> std::quoted(s, delim);
66     assert ( s == p );
67     }
68 
69 void round_trip_e ( const char *p, char escape ) {
70     std::stringstream ss;
71     ss << std::quoted(p, '"', escape );
72     std::string s;
73     ss >> std::quoted(s, '"', escape );
74     assert ( s == p );
75     }
76 
77 
78 
79 std::string quote ( const char *p, char delim='"', char escape='\\' ) {
80     std::stringstream ss;
81     ss << std::quoted(p, delim, escape);
82     std::string s;
83     ss >> s;    // no quote
84     return s;
85 }
86 
87 std::string unquote ( const char *p, char delim='"', char escape='\\' ) {
88     std::stringstream ss;
89     ss << p;
90     std::string s;
91     ss >> std::quoted(s, delim, escape);
92     return s;
93 }
94 
95 
96 void round_trip ( const wchar_t *p ) {
97     std::wstringstream ss;
98     bool skippingws = is_skipws ( &ss );
99     ss << std::quoted(p);
100     std::wstring s;
101     ss >> std::quoted(s);
102     assert ( s == p );
103     assert ( skippingws == is_skipws ( &ss ));
104     }
105 
106 
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);
112     std::wstring s;
113     ss >> std::quoted(s);
114     assert ( s == p );
115     assert ( skippingws == is_skipws ( &ss ));
116     }
117 
118 void round_trip_d ( const wchar_t *p, wchar_t delim ) {
119     std::wstringstream ss;
120     ss << std::quoted(p, delim);
121     std::wstring s;
122     ss >> std::quoted(s, delim);
123     assert ( s == p );
124     }
125 
126 void round_trip_e ( const wchar_t *p, wchar_t escape ) {
127     std::wstringstream ss;
128     ss << std::quoted(p, wchar_t('"'), escape );
129     std::wstring s;
130     ss >> std::quoted(s, wchar_t('"'), escape );
131     assert ( s == p );
132     }
133 
134 
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);
138     std::wstring s;
139     ss >> s;    // no quote
140     return s;
141 }
142 
143 std::wstring unquote ( const wchar_t *p, wchar_t delim='"', wchar_t escape='\\' ) {
144     std::wstringstream ss;
145     ss << p;
146     std::wstring s;
147     ss >> std::quoted(s, delim, escape);
148     return s;
149 }
150 
151 int main()
152 {
153     both_ways ( "" );   // This is a compilation check
154 
155     round_trip    (  "" );
156     round_trip_ws (  "" );
157     round_trip_d  (  "", 'q' );
158     round_trip_e  (  "", 'q' );
159 
160     round_trip    ( L"" );
161     round_trip_ws ( L"" );
162     round_trip_d  ( L"", 'q' );
163     round_trip_e  ( L"", 'q' );
164 
165     round_trip    (  "Hi" );
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\!!)" );
171 
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\!!)" );
178 
179     round_trip    (  "Hi Mom" );
180     round_trip_ws (  "Hi Mom" );
181     round_trip    ( L"Hi Mom" );
182     round_trip_ws ( L"Hi Mom" );
183 
184     assert ( quote (  "" )  ==  "\"\"" );
185     assert ( quote ( L"" )  == L"\"\"" );
186     assert ( quote (  "a" ) ==  "\"a\"" );
187     assert ( quote ( L"a" ) == L"\"a\"" );
188 
189 //  missing end quote - must not hang
190     assert ( unquote (  "\"abc" ) ==  "abc" );
191     assert ( unquote ( L"\"abc" ) == L"abc" );
192 
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
197 
198     assert ( unquote (  "" ) ==  "" ); // nothing there
199     assert ( unquote ( L"" ) == L"" ); // nothing there
200     }
201 
202 #else
203 int main() {}
204 #endif
205