quoted.pass.cpp revision 62f34be0baf276c2b310db8bda0d358841ebab9a
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
21bool is_skipws ( const std::istream *is ) {
22    return ( is->flags() & std::ios_base::skipws ) != 0;
23    }
24
25
26bool is_skipws ( const std::wistream *is ) {
27    return ( is->flags() & std::ios_base::skipws ) != 0;
28    }
29
30void round_trip ( const char *p ) {
31    std::stringstream ss;
32    bool skippingws = is_skipws ( &ss );
33    ss << std::quoted(p);
34    std::string s;
35    ss >> std::quoted(s);
36    assert ( s == p );
37    assert ( skippingws == is_skipws ( &ss ));
38    }
39
40void round_trip_ws ( const char *p ) {
41    std::stringstream ss;
42    std::noskipws ( ss );
43    bool skippingws = is_skipws ( &ss );
44    ss << std::quoted(p);
45    std::string s;
46    ss >> std::quoted(s);
47    assert ( s == p );
48    assert ( skippingws == is_skipws ( &ss ));
49    }
50
51void round_trip_d ( const char *p, char delim ) {
52    std::stringstream ss;
53    ss << std::quoted(p, delim);
54    std::string s;
55    ss >> std::quoted(s, delim);
56    assert ( s == p );
57    }
58
59void round_trip_e ( const char *p, char escape ) {
60    std::stringstream ss;
61    ss << std::quoted(p, '"', escape );
62    std::string s;
63    ss >> std::quoted(s, '"', escape );
64    assert ( s == p );
65    }
66
67
68
69std::string quote ( const char *p, char delim='"', char escape='\\' ) {
70    std::stringstream ss;
71    ss << std::quoted(p, delim, escape);
72    std::string s;
73    ss >> s;    // no quote
74    return s;
75}
76
77std::string unquote ( const char *p, char delim='"', char escape='\\' ) {
78    std::stringstream ss;
79    ss << p;
80    std::string s;
81    ss >> std::quoted(s, delim, escape);
82    return s;
83}
84
85
86void round_trip ( const wchar_t *p ) {
87    std::wstringstream ss;
88    bool skippingws = is_skipws ( &ss );
89    ss << std::quoted(p);
90    std::wstring s;
91    ss >> std::quoted(s);
92    assert ( s == p );
93    assert ( skippingws == is_skipws ( &ss ));
94    }
95
96
97void round_trip_ws ( const wchar_t *p ) {
98    std::wstringstream ss;
99    std::noskipws ( ss );
100    bool skippingws = is_skipws ( &ss );
101    ss << std::quoted(p);
102    std::wstring s;
103    ss >> std::quoted(s);
104    assert ( s == p );
105    assert ( skippingws == is_skipws ( &ss ));
106    }
107
108void round_trip_d ( const wchar_t *p, wchar_t delim ) {
109    std::wstringstream ss;
110    ss << std::quoted(p, delim);
111    std::wstring s;
112    ss >> std::quoted(s, delim);
113    assert ( s == p );
114    }
115
116void round_trip_e ( const wchar_t *p, wchar_t escape ) {
117    std::wstringstream ss;
118    ss << std::quoted(p, wchar_t('"'), escape );
119    std::wstring s;
120    ss >> std::quoted(s, wchar_t('"'), escape );
121    assert ( s == p );
122    }
123
124
125std::wstring quote ( const wchar_t *p, wchar_t delim='"', wchar_t escape='\\' ) {
126    std::wstringstream ss;
127    ss << std::quoted(p, delim, escape);
128    std::wstring s;
129    ss >> s;    // no quote
130    return s;
131}
132
133std::wstring unquote ( const wchar_t *p, wchar_t delim='"', wchar_t escape='\\' ) {
134    std::wstringstream ss;
135    ss << p;
136    std::wstring s;
137    ss >> std::quoted(s, delim, escape);
138    return s;
139}
140
141int main()
142{
143    round_trip    (  "" );
144    round_trip_ws (  "" );
145    round_trip_d  (  "", 'q' );
146    round_trip_e  (  "", 'q' );
147
148    round_trip    ( L"" );
149    round_trip_ws ( L"" );
150    round_trip_d  ( L"", 'q' );
151    round_trip_e  ( L"", 'q' );
152
153    round_trip    (  "Hi" );
154    round_trip_ws (  "Hi" );
155    round_trip_d  (  "Hi", '!' );
156    round_trip_e  (  "Hi", '!' );
157    assert ( quote ( "Hi", '!' ) == "!Hi!" );
158    assert ( quote ( "Hi!", '!' ) == R"(!Hi\!!)" );
159
160    round_trip    ( L"Hi" );
161    round_trip_ws ( L"Hi" );
162    round_trip_d  ( L"Hi", '!' );
163    round_trip_e  ( L"Hi", '!' );
164    assert ( quote ( L"Hi", '!' )  == L"!Hi!" );
165    assert ( quote ( L"Hi!", '!' ) == LR"(!Hi\!!)" );
166
167    round_trip    (  "Hi Mom" );
168    round_trip_ws (  "Hi Mom" );
169    round_trip    ( L"Hi Mom" );
170    round_trip_ws ( L"Hi Mom" );
171
172    assert ( quote (  "" )  ==  "\"\"" );
173    assert ( quote ( L"" )  == L"\"\"" );
174    assert ( quote (  "a" ) ==  "\"a\"" );
175    assert ( quote ( L"a" ) == L"\"a\"" );
176
177//  missing end quote - must not hang
178    assert ( unquote (  "\"abc" ) ==  "abc" );
179    assert ( unquote ( L"\"abc" ) == L"abc" );
180
181    assert ( unquote (  "abc" ) == "abc" ); // no delimiter
182    assert ( unquote ( L"abc" ) == L"abc" ); // no delimiter
183    assert ( unquote (  "abc def" ) ==  "abc" ); // no delimiter
184    assert ( unquote ( L"abc def" ) == L"abc" ); // no delimiter
185
186    assert ( unquote (  "" ) ==  "" ); // nothing there
187    assert ( unquote ( L"" ) == L"" ); // nothing there
188    }
189
190#else
191int main() {}
192#endif
193