217 lines · cpp
1//===----------------------------------------------------------------------===//2//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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++1110// UNSUPPORTED: no-localization11// UNSUPPORTED: !stdlib=libc++ && c++1412 13// <iomanip>14 15// quoted16 17#include <iomanip>18#include <sstream>19#include <string_view>20#include <cassert>21 22#include "test_macros.h"23 24bool is_skipws(const std::istream* is) { return (is->flags() & std::ios_base::skipws) != 0; }25 26#ifndef TEST_HAS_NO_WIDE_CHARACTERS27bool is_skipws(const std::wistream* is) { return (is->flags() & std::ios_base::skipws) != 0; }28#endif29 30void round_trip(const char* p) {31 std::stringstream ss;32 bool skippingws = is_skipws(&ss);33 std::string_view sv{p};34 35 ss << std::quoted(sv);36 std::string s;37 ss >> std::quoted(s);38 assert(s == sv);39 assert(skippingws == is_skipws(&ss));40}41 42void round_trip_ws(const char* p) {43 std::stringstream ss;44 std::noskipws(ss);45 bool skippingws = is_skipws(&ss);46 std::string_view sv{p};47 48 ss << std::quoted(sv);49 std::string s;50 ss >> std::quoted(s);51 assert(s == sv);52 assert(skippingws == is_skipws(&ss));53}54 55void round_trip_d(const char* p, char delim) {56 std::stringstream ss;57 std::string_view sv{p};58 59 ss << std::quoted(sv, delim);60 std::string s;61 ss >> std::quoted(s, delim);62 assert(s == sv);63}64 65void round_trip_e(const char* p, char escape) {66 std::stringstream ss;67 std::string_view sv{p};68 69 ss << std::quoted(sv, '"', escape);70 std::string s;71 ss >> std::quoted(s, '"', escape);72 assert(s == sv);73}74 75std::string quote(const char* p, char delim = '"', char escape = '\\') {76 std::stringstream ss;77 ss << std::quoted(p, delim, escape);78 std::string s;79 ss >> s; // no quote80 return s;81}82 83std::string unquote(const char* p, char delim = '"', char escape = '\\') {84 std::stringstream ss;85 ss << p;86 std::string s;87 ss >> std::quoted(s, delim, escape);88 return s;89}90 91#ifndef TEST_HAS_NO_WIDE_CHARACTERS92void round_trip(const wchar_t* p) {93 std::wstringstream ss;94 bool skippingws = is_skipws(&ss);95 std::wstring_view sv{p};96 97 ss << std::quoted(sv);98 std::wstring s;99 ss >> std::quoted(s);100 assert(s == sv);101 assert(skippingws == is_skipws(&ss));102}103 104void round_trip_ws(const wchar_t* p) {105 std::wstringstream ss;106 std::noskipws(ss);107 bool skippingws = is_skipws(&ss);108 std::wstring_view sv{p};109 110 ss << std::quoted(sv);111 std::wstring s;112 ss >> std::quoted(s);113 assert(s == sv);114 assert(skippingws == is_skipws(&ss));115}116 117void round_trip_d(const wchar_t* p, wchar_t delim) {118 std::wstringstream ss;119 std::wstring_view sv{p};120 121 ss << std::quoted(sv, delim);122 std::wstring s;123 ss >> std::quoted(s, delim);124 assert(s == sv);125}126 127void round_trip_e(const wchar_t* p, wchar_t escape) {128 std::wstringstream ss;129 std::wstring_view sv{p};130 131 ss << std::quoted(sv, wchar_t('"'), escape);132 std::wstring s;133 ss >> std::quoted(s, wchar_t('"'), escape);134 assert(s == sv);135}136 137std::wstring quote(const wchar_t* p, wchar_t delim = '"', wchar_t escape = '\\') {138 std::wstringstream ss;139 std::wstring_view sv{p};140 141 ss << std::quoted(sv, delim, escape);142 std::wstring s;143 ss >> s; // no quote144 return s;145}146 147std::wstring unquote(const wchar_t* p, wchar_t delim = '"', wchar_t escape = '\\') {148 std::wstringstream ss;149 std::wstring_view sv{p};150 151 ss << sv;152 std::wstring s;153 ss >> std::quoted(s, delim, escape);154 return s;155}156#endif // TEST_HAS_NO_WIDE_CHARACTERS157 158int main(int, char**) {159 round_trip("");160 round_trip_ws("");161 round_trip_d("", 'q');162 round_trip_e("", 'q');163 164#ifndef TEST_HAS_NO_WIDE_CHARACTERS165 round_trip(L"");166 round_trip_ws(L"");167 round_trip_d(L"", 'q');168 round_trip_e(L"", 'q');169#endif170 171 round_trip("Hi");172 round_trip_ws("Hi");173 round_trip_d("Hi", '!');174 round_trip_e("Hi", '!');175 assert(quote("Hi", '!') == "!Hi!");176 assert(quote("Hi!", '!') == R"(!Hi\!!)");177 178#ifndef TEST_HAS_NO_WIDE_CHARACTERS179 round_trip(L"Hi");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\!!)");185#endif186 187 round_trip("Hi Mom");188 round_trip_ws("Hi Mom");189 190#ifndef TEST_HAS_NO_WIDE_CHARACTERS191 round_trip(L"Hi Mom");192 round_trip_ws(L"Hi Mom");193#endif194 195 assert(quote("") == "\"\"");196 assert(quote("a") == "\"a\"");197#ifndef TEST_HAS_NO_WIDE_CHARACTERS198 assert(quote(L"") == L"\"\"");199 assert(quote(L"a") == L"\"a\"");200#endif201 202 // missing end quote - must not hang203 assert(unquote("\"abc") == "abc");204 assert(unquote("abc") == "abc"); // no delimiter205 assert(unquote("abc def") == "abc"); // no delimiter206 assert(unquote("") == ""); // nothing there207 208#ifndef TEST_HAS_NO_WIDE_CHARACTERS209 assert(unquote(L"\"abc") == L"abc");210 assert(unquote(L"abc") == L"abc"); // no delimiter211 assert(unquote(L"abc def") == L"abc"); // no delimiter212 assert(unquote(L"") == L""); // nothing there213#endif214 215 return 0;216}217