From patchwork Wed Jun 6 06:04:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 925743 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-479160-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="Wt1sm/Zu"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 410ynq2DNPzB3sg for ; Wed, 6 Jun 2018 16:05:16 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=SXZz4XMyHlqrwKAgG1ozYoFMJlMGCIchlN5NsXDi6MMGrN37Kh1ga a663H3ThE5Jye7yaiEX9QvanUieHOKFGzdNhhLyXoaLxq80DoUzb+IouSnLnLx2R YmmJOORAq6Lv5TxnUANr290rXvsmpCZp3KdkCvXs/xN+D63cleDmUU= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=Fuh0J2XZE2gdXxGESaTg44tDDho=; b=Wt1sm/ZuMwmba9m54FiL Ba5tMfkjUSPyjPCoLO1Enttjaq+K1JR9BSPqLgcOXZuVBdk4UcqPltbg+De1IIDM yW7AMO/7zwlwkdnIdnqDYjT6re0FTIZDsZ+Gdej/X7oYufh+9644XqaCGm4ULdxn bMyPd5Sv3MO8HfChAXZVn9c= Received: (qmail 24540 invoked by alias); 6 Jun 2018 06:05:00 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 24464 invoked by uid 89); 6 Jun 2018 06:04:58 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.9 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, KAM_SHORT, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=L, sc, SC, basic_ostream X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx3-rdu2.redhat.com (HELO mx1.redhat.com) (66.187.233.73) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 06 Jun 2018 06:04:56 +0000 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A38BC818BAF7; Wed, 6 Jun 2018 06:04:54 +0000 (UTC) Received: from localhost (unknown [10.33.36.5]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4CFF1210C6D2; Wed, 6 Jun 2018 06:04:54 +0000 (UTC) Date: Wed, 6 Jun 2018 07:04:53 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] PR libstdc++/86008 add std::quoted support for string_view Message-ID: <20180606060453.GA17988@redhat.com> MIME-Version: 1.0 Content-Disposition: inline X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.9.2 (2017-12-15) PR libstdc++/86008 * include/bits/quoted_string.h (_Quoted_string): Define new partial specialization. * include/std/iomanip (quoted(basic_string_view, C, C)): Define new overload. (operator<<(basic_ostream&, const _Quoted_string&)): Use value not reference for iteration. * testsuite/27_io/manipulators/standard/char/quoted.cc: Adjust comment. * testsuite/27_io/manipulators/standard/char/quoted_sv.cc: New test. * testsuite/27_io/manipulators/standard/wchar_t/quoted.cc: Adjust comment. Tested powerpc64le-linux, committed to trunk. commit 3914358048981798c18801b9885576529534d2bd Author: Jonathan Wakely Date: Tue Jun 5 14:28:12 2018 +0100 PR libstdc++/86008 add std::quoted support for string_view PR libstdc++/86008 * include/bits/quoted_string.h (_Quoted_string): Define new partial specialization. * include/std/iomanip (quoted(basic_string_view, C, C)): Define new overload. (operator<<(basic_ostream&, const _Quoted_string&)): Use value not reference for iteration. * testsuite/27_io/manipulators/standard/char/quoted.cc: Adjust comment. * testsuite/27_io/manipulators/standard/char/quoted_sv.cc: New test. * testsuite/27_io/manipulators/standard/wchar_t/quoted.cc: Adjust comment. diff --git a/libstdc++-v3/include/bits/quoted_string.h b/libstdc++-v3/include/bits/quoted_string.h index b6e707a8a6e..f6134da5a43 100644 --- a/libstdc++-v3/include/bits/quoted_string.h +++ b/libstdc++-v3/include/bits/quoted_string.h @@ -64,6 +64,24 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _CharT _M_escape; }; +#if __cplusplus >= 201703L + template + struct _Quoted_string, _CharT> + { + _Quoted_string(basic_string_view<_CharT, _Traits> __str, + _CharT __del, _CharT __esc) + : _M_string(__str), _M_delim{__del}, _M_escape{__esc} + { } + + _Quoted_string& + operator=(_Quoted_string&) = delete; + + basic_string_view<_CharT, _Traits> _M_string; + _CharT _M_delim; + _CharT _M_escape; + }; +#endif // C++17 + /** * @brief Inserter for quoted strings. * @@ -101,7 +119,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { std::basic_ostringstream<_CharT, _Traits> __ostr; __ostr << __str._M_delim; - for (auto& __c : __str._M_string) + for (auto __c : __str._M_string) { if (__c == __str._M_delim || __c == __str._M_escape) __ostr << __str._M_escape; diff --git a/libstdc++-v3/include/std/iomanip b/libstdc++-v3/include/std/iomanip index db8670ed56a..9c1f63edb61 100644 --- a/libstdc++-v3/include/std/iomanip +++ b/libstdc++-v3/include/std/iomanip @@ -446,7 +446,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __is; } -#if __cplusplus > 201103L +#if __cplusplus >= 201402L #define __cpp_lib_quoted_string_io 201304 @@ -471,8 +471,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\')) { return __detail::_Quoted_string< - const basic_string<_CharT, _Traits, _Alloc>&, _CharT>( - __string, __delim, __escape); + const basic_string<_CharT, _Traits, _Alloc>&, _CharT>( + __string, __delim, __escape); } template @@ -481,11 +481,23 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\')) { return __detail::_Quoted_string< - basic_string<_CharT, _Traits, _Alloc>&, _CharT>( - __string, __delim, __escape); + basic_string<_CharT, _Traits, _Alloc>&, _CharT>( + __string, __delim, __escape); } -#endif // __cplusplus > 201103L +#if __cplusplus >= 201703L + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 2785. quoted should work with basic_string_view + template + inline auto + quoted(basic_string_view<_CharT, _Traits> __sv, + _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\')) + { + return __detail::_Quoted_string< + basic_string_view<_CharT, _Traits>, _CharT>(__sv, __delim, __escape); + } +#endif // C++17 +#endif // C++14 #endif // __cplusplus >= 201103L diff --git a/libstdc++-v3/testsuite/27_io/manipulators/standard/char/quoted.cc b/libstdc++-v3/testsuite/27_io/manipulators/standard/char/quoted.cc index 50f56f280d1..7d1da6b731e 100644 --- a/libstdc++-v3/testsuite/27_io/manipulators/standard/char/quoted.cc +++ b/libstdc++-v3/testsuite/27_io/manipulators/standard/char/quoted.cc @@ -17,7 +17,7 @@ // with this library; see the file COPYING3. If not see // . -// 27.7.6 - Quoted manipulators [quoted.manip] +// C++14 27.7.6 - Quoted manipulators [quoted.manip] #include #include diff --git a/libstdc++-v3/testsuite/27_io/manipulators/standard/char/quoted_sv.cc b/libstdc++-v3/testsuite/27_io/manipulators/standard/char/quoted_sv.cc new file mode 100644 index 00000000000..512bdf25533 --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/manipulators/standard/char/quoted_sv.cc @@ -0,0 +1,73 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run { target c++17 } } + +// Copyright (C) 2018 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// C++17 30.7.8 - Quoted manipulators [quoted.manip] + +#include +#include +#include +#include + +void +test01() +{ + std::stringstream ss; + const std::string_view original = R"(This "string" will be \"quoted\")"; + std::string raw, round_trip; + ss << std::quoted(original); + raw = ss.str(); + VERIFY( raw == R"("This \"string\" will be \\\"quoted\\\"")" ); + ss >> std::quoted(round_trip); + VERIFY( original == round_trip ); +} + +void +test02() +{ + std::stringstream ss; + const std::string_view original = R"(This "string" will be \"quoted\")"; + std::string raw, round_trip; + ss << std::quoted(original, '\'', '!'); + raw = ss.str(); + VERIFY( raw == R"('This "string" will be \"quoted\"')" ); + ss >> std::quoted(round_trip, '\'', '!'); + VERIFY( original == round_trip ); +} + +void +test03() +{ + std::stringstream ss; + const std::string_view original = R"(This 'string' will be !'quoted!')"; + std::string raw, round_trip; + ss << std::quoted(original, '\'', '!'); + raw = ss.str(); + VERIFY( raw == R"('This !'string!' will be !!!'quoted!!!'')" ); + ss >> std::quoted(round_trip, '\'', '!'); + VERIFY( original == round_trip ); +} + +int +main() +{ + test01(); + test02(); + test03(); +} diff --git a/libstdc++-v3/testsuite/27_io/manipulators/standard/wchar_t/quoted.cc b/libstdc++-v3/testsuite/27_io/manipulators/standard/wchar_t/quoted.cc index 4c5896372f4..d8b05037aae 100644 --- a/libstdc++-v3/testsuite/27_io/manipulators/standard/wchar_t/quoted.cc +++ b/libstdc++-v3/testsuite/27_io/manipulators/standard/wchar_t/quoted.cc @@ -17,7 +17,7 @@ // with this library; see the file COPYING3. If not see // . -// 27.7.6 - Quoted manipulators [quoted.manip] +// C++14 27.7.6 - Quoted manipulators [quoted.manip] #include #include diff --git a/libstdc++-v3/testsuite/27_io/manipulators/standard/wchar_t/quoted_sv.cc b/libstdc++-v3/testsuite/27_io/manipulators/standard/wchar_t/quoted_sv.cc new file mode 100644 index 00000000000..59de59c1eaf --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/manipulators/standard/wchar_t/quoted_sv.cc @@ -0,0 +1,73 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run { target c++17 } } + +// Copyright (C) 2018 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// C++17 30.7.8 - Quoted manipulators [quoted.manip] + +#include +#include +#include +#include + +void +test01() +{ + std::wstringstream ss; + const std::wstring_view original = LR"(This "string" will be \"quoted\")"; + std::wstring raw, round_trip; + ss << std::quoted(original); + raw = ss.str(); + VERIFY( raw == LR"("This \"string\" will be \\\"quoted\\\"")" ); + ss >> std::quoted(round_trip); + VERIFY( original == round_trip ); +} + +void +test02() +{ + std::wstringstream ss; + const std::wstring_view original = LR"(This "string" will be \"quoted\")"; + std::wstring raw, round_trip; + ss << std::quoted(original, L'\'', L'!'); + raw = ss.str(); + VERIFY( raw == LR"('This "string" will be \"quoted\"')" ); + ss >> std::quoted(round_trip, L'\'', L'!'); + VERIFY( original == round_trip ); +} + +void +test03() +{ + std::wstringstream ss; + const std::wstring_view original = LR"(This 'string' will be !'quoted!')"; + std::wstring raw, round_trip; + ss << std::quoted(original, L'\'', L'!'); + raw = ss.str(); + VERIFY( raw == LR"('This !'string!' will be !!!'quoted!!!'')" ); + ss >> std::quoted(round_trip, L'\'', L'!'); + VERIFY( original == round_trip ); +} + +int +main() +{ + test01(); + test02(); + test03(); +}