From patchwork Tue Nov 15 14:33:49 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 82354 Delivered-To: patch@linaro.org Received: by 10.140.97.165 with SMTP id m34csp1559449qge; Tue, 15 Nov 2016 06:34:37 -0800 (PST) X-Received: by 10.99.146.76 with SMTP id s12mr38201688pgn.8.1479220477390; Tue, 15 Nov 2016 06:34:37 -0800 (PST) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id b139si25546764pfb.162.2016.11.15.06.34.37 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 15 Nov 2016 06:34:37 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-return-441457-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org; spf=pass (google.com: domain of gcc-patches-return-441457-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-441457-patch=linaro.org@gcc.gnu.org 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=vJSLSW/05vQV5LqqtxIgJsyw9mjWdDzLWWxg1D+gEh6bTBsVw9T7M 5G8EFI8k4OHTbv8vRr+Tkbsq397uZgJbsbqsnLSFfFB+fbt/zwLN+uZy8yJyvy3Y V63N6WOZJ+NGJVzwxPMcgwAtVZY0D0n6Cn1hxYbNZJqCNwqu/oLdOg= 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=Ks5S2W0jU294CPTDIlnyLlH/oho=; b=bcMM/h7hOenc9Bj4M+kI pQOXwUq17YbB/3WLG0tcJnGVD94Hc1zgU3Iqz/KGTUwUxjWtlInEszpD6g8slT3Y C4Lbhsx1mVU1dm0LJc7XNqjIUhyNk3fMX3pTSXLqwr6KaqBHavHp7TKJ3nN43YBY SgXANMVSNVHwWK4/3ekTnBQ= Received: (qmail 80065 invoked by alias); 15 Nov 2016 14:34:06 -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 79871 invoked by uid 89); 15 Nov 2016 14:34:01 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-4.7 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=UD:basic_string.h, basic_stringh, basic_string.h, __t X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 15 Nov 2016 14:33:51 +0000 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 20C903B3C7; Tue, 15 Nov 2016 14:33:50 +0000 (UTC) Received: from localhost (ovpn-116-110.ams2.redhat.com [10.36.116.110]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id uAFEXnCQ023575; Tue, 15 Nov 2016 09:33:49 -0500 Date: Tue, 15 Nov 2016 14:33:49 +0000 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] Add std::string constructor for substring of string_view (LWG 2742) Message-ID: <20161115143349.GG3145@redhat.com> MIME-Version: 1.0 Content-Disposition: inline X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.7.1 (2016-10-04) This is another issue resolution for C++17 features that was approved at the recent meeting. I think this resolution is wrong too, but in this case the fix is obvious so I've gone ahead and done it. * doc/xml/manual/intro.xml: Document LWG 2742 status. * doc/html/*: Regenerate. * include/bits/basic_string.h (basic_string(const T&, size_type, size_type, const Allocator&)): Add constructor for substring of basic_string_view, as per LWG 2742 but with additional constraint to fix ambiguity. * testsuite/21_strings/basic_string/cons/char/9.cc: New test. * testsuite/21_strings/basic_string/cons/wchar_t/9.cc: New test. Tested powerpc64le-linux, comitted to trunk. commit d8d7a6fba221a205f28212a8bb6288aa724d5c63 Author: Jonathan Wakely Date: Tue Nov 15 12:52:42 2016 +0000 Add std::string constructor for substring of string_view (LWG 2742) * doc/xml/manual/intro.xml: Document LWG 2742 status. * doc/html/*: Regenerate. * include/bits/basic_string.h (basic_string(const T&, size_type, size_type, const Allocator&)): Add constructor for substring of basic_string_view, as per LWG 2742 but with additional constraint to fix ambiguity. * testsuite/21_strings/basic_string/cons/char/9.cc: New test. * testsuite/21_strings/basic_string/cons/wchar_t/9.cc: New test. diff --git a/libstdc++-v3/doc/xml/manual/intro.xml b/libstdc++-v3/doc/xml/manual/intro.xml index 0df24bb..7f2586d 100644 --- a/libstdc++-v3/doc/xml/manual/intro.xml +++ b/libstdc++-v3/doc/xml/manual/intro.xml @@ -1107,6 +1107,14 @@ requirements of the license of GCC. Define the value_compare typedef. + 2742: + Inconsistent string interface taking string_view + + + Add the new constructor and additionally constrain it + to avoid ambiguities with non-const charT*. + + 2748: swappable traits for optionals diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index b80e270..943e88d 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -586,12 +586,27 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 #if __cplusplus > 201402L /** + * @brief Construct string from a substring of a string_view. + * @param __t Source string view. + * @param __pos The index of the first character to copy from __t. + * @param __n The number of characters to copy from __t. + * @param __a Allocator to use. + */ + template, + __not_>>> + basic_string(const _Tp& __t, size_type __pos, size_type __n, + const _Alloc& __a = _Alloc()) + : basic_string(__sv_type(__t).substr(__pos, __n), __a) { } + + /** * @brief Construct string from a string_view. * @param __sv Source string view. * @param __a Allocator to use (default is default allocator). */ - explicit basic_string(__sv_type __sv, const _Alloc& __a = _Alloc()) - : basic_string(__sv.data(), __sv.size(), __a) {} + explicit + basic_string(__sv_type __sv, const _Alloc& __a = _Alloc()) + : basic_string(__sv.data(), __sv.size(), __a) { } #endif // C++17 /** diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/9.cc b/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/9.cc new file mode 100644 index 0000000..0024ffc --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/9.cc @@ -0,0 +1,46 @@ +// Copyright (C) 2016 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 +// . + +// { dg-options "-std=gnu++17" } +// { dg-do run { target c++1z } } + +#include +#include + +void +test01() +{ + using C = char; + using string_type = std::basic_string; + using view_type = std::basic_string_view; + + std::allocator alloc; + VERIFY( string_type(view_type("string")) == "string" ); + VERIFY( string_type(view_type("string"), alloc) == "string" ); + + // LWG 2742 + VERIFY( string_type("substring", 3, 6) == "string" ); + VERIFY( string_type("substring", 3, 6, alloc) == "string" ); + VERIFY( string_type(view_type("substring"), 3, 6) == "string" ); + VERIFY( string_type(view_type("substring"), 3, 6, alloc) == "string" ); +} + +int +main() +{ + test01(); +} diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/cons/wchar_t/9.cc b/libstdc++-v3/testsuite/21_strings/basic_string/cons/wchar_t/9.cc new file mode 100644 index 0000000..bf4b440 --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/cons/wchar_t/9.cc @@ -0,0 +1,46 @@ +// Copyright (C) 2016 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 +// . + +// { dg-options "-std=gnu++17" } +// { dg-do run { target c++1z } } + +#include +#include + +void +test01() +{ + using C = wchar_t; + using string_type = std::basic_string; + using view_type = std::basic_string_view; + + std::allocator alloc; + VERIFY( string_type(view_type(L"string")) == L"string" ); + VERIFY( string_type(view_type(L"string"), alloc) == L"string" ); + + // LWG 2742 + VERIFY( string_type(L"substring", 3, 6) == L"string" ); + VERIFY( string_type(L"substring", 3, 6, alloc) == L"string" ); + VERIFY( string_type(view_type(L"substring"), 3, 6) == L"string" ); + VERIFY( string_type(view_type(L"substring"), 3, 6, alloc) == L"string" ); +} + +int +main() +{ + test01(); +}