From patchwork Fri Jan 13 15:53:14 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 91455 Delivered-To: patch@linaro.org Received: by 10.182.3.34 with SMTP id 2csp195494obz; Fri, 13 Jan 2017 07:53:47 -0800 (PST) X-Received: by 10.99.151.1 with SMTP id n1mr24944294pge.122.1484322827077; Fri, 13 Jan 2017 07:53:47 -0800 (PST) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id k6si13056712pla.288.2017.01.13.07.53.46 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 13 Jan 2017 07:53:47 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-return-446103-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-446103-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-446103-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=XgM4eJb7DupbnsPpQh8cHqyK6nv8IjDBc7Ln2LIIUctev3AkZA0Hh y7wtdqE8zM8GkUl1AUfpwxvqLpsVAUjDmCnoRmZlo+yg+pK8AcLVv4ziCRBfSiD7 a5kqMt83TY37Zsc2Dy5ddfnlgy8eq429XZ6d9UfRIN0SHOi8YIcPeo= 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=EtdVAKz9GuVCOxHy9Gnqs5WIIGo=; b=IhlPScBDx39BTcSKoXUz o7YH8zAtkw3wZDSu+LpjBuS/7YW9v0x3xGIRc3LMUhDiD+e7LlCZJYDDmDKvHQgf JMhB0g+d18++NHj5wjWqGq1XImeV5jKOkqQd6WkQREeBw8qJIzuRZ8YK83VwI/Zc d1KrKcjmubukDHwK/e9pfso= Received: (qmail 55710 invoked by alias); 13 Jan 2017 15:53:27 -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 55676 invoked by uid 89); 13 Jan 2017 15:53:27 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-4.1 required=5.0 tests=BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy= 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; Fri, 13 Jan 2017 15:53:16 +0000 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (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 9D5E3C0567B1; Fri, 13 Jan 2017 15:53:16 +0000 (UTC) Received: from localhost (ovpn-116-26.ams2.redhat.com [10.36.116.26]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v0DFrFGQ011202; Fri, 13 Jan 2017 10:53:16 -0500 Date: Fri, 13 Jan 2017 15:53:14 +0000 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] Add string_view support to COW std::string Message-ID: <20170113155314.GA13348@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 adds the C++17 changes to make std::string_view interoperate with std::string. The code is mostly just copied from the new std::string, with two adjustments needed where the names of internal member functions differ. With this we have no more test failures when using the old ABI (either via running tests with -D_GLIBCXX_USE_CXX11_ABI=0 or when configured so that's the default). PR libstdc++/79075 * include/bits/basic_string.h [_GLIBCXX_USE_CXX11_ABI] (basic_string): Make _If_sv private. [!_GLIBCXX_USE_CXX11_ABI] (basic_string): Add member functions taking basic_string_view arguments. Tested powerpc64le-linux, committed to trunk. commit 587d97a197fdb2216861a80c04ad70e0d7cef14a Author: Jonathan Wakely Date: Fri Jan 13 15:26:46 2017 +0000 Add string_view support to COW std::string PR libstdc++/79075 * include/bits/basic_string.h [_GLIBCXX_USE_CXX11_ABI] (basic_string): Make _If_sv private. [!_GLIBCXX_USE_CXX11_ABI] (basic_string): Add member functions taking basic_string_view arguments. diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index fe8f7e6..9dffcf9 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -111,6 +111,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 #if __cplusplus > 201402L // A helper type for avoiding boiler-plate. typedef basic_string_view<_CharT, _Traits> __sv_type; + + template + using _If_sv = enable_if_t< + __and_, + __not_>>::value, + _Res>; #endif // Use empty-base optimization: http://www.cantrip.org/emptyopt.html @@ -585,12 +591,6 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { _M_construct(__beg, __end); } #if __cplusplus > 201402L - template - using _If_sv = enable_if_t< - __and_, - __not_>>::value, - _Res>; - /** * @brief Construct string from a substring of a string_view. * @param __t Source string view. @@ -3320,6 +3320,17 @@ _GLIBCXX_END_NAMESPACE_CXX11 _S_empty_rep() _GLIBCXX_NOEXCEPT { return _Rep::_S_empty_rep(); } +#if __cplusplus > 201402L + // A helper type for avoiding boiler-plate. + typedef basic_string_view<_CharT, _Traits> __sv_type; + + template + using _If_sv = enable_if_t< + __and_, + __not_>>::value, + _Res>; +#endif + public: // Construct/copy/destroy: // NB: We overload ctors in some cases instead of using default @@ -3441,6 +3452,29 @@ _GLIBCXX_END_NAMESPACE_CXX11 basic_string(_InputIterator __beg, _InputIterator __end, const _Alloc& __a = _Alloc()); +#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> + 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) { } +#endif // C++17 + /** * @brief Destroy the string instance. */ @@ -3506,6 +3540,23 @@ _GLIBCXX_END_NAMESPACE_CXX11 } #endif // C++11 +#if __cplusplus > 201402L + /** + * @brief Set value to string constructed from a string_view. + * @param __sv A string_view. + */ + basic_string& + operator=(__sv_type __sv) + { return this->assign(__sv); } + + /** + * @brief Convert to a string_view. + * @return A string_view. + */ + operator __sv_type() const noexcept + { return __sv_type(data(), size()); } +#endif // C++17 + // Iterators: /** * Returns a read/write iterator that points to the first character in @@ -3910,6 +3961,17 @@ _GLIBCXX_END_NAMESPACE_CXX11 { return this->append(__l.begin(), __l.size()); } #endif // C++11 +#if __cplusplus > 201402L + /** + * @brief Append a string_view. + * @param __sv The string_view to be appended. + * @return Reference to this string. + */ + basic_string& + operator+=(__sv_type __sv) + { return this->append(__sv); } +#endif // C++17 + /** * @brief Append a string to this string. * @param __str The string to append. @@ -3990,6 +4052,34 @@ _GLIBCXX_END_NAMESPACE_CXX11 append(_InputIterator __first, _InputIterator __last) { return this->replace(_M_iend(), _M_iend(), __first, __last); } +#if __cplusplus > 201402L + /** + * @brief Append a string_view. + * @param __sv The string_view to be appended. + * @return Reference to this string. + */ + basic_string& + append(__sv_type __sv) + { return this->append(__sv.data(), __sv.size()); } + + /** + * @brief Append a range of characters from a string_view. + * @param __sv The string_view to be appended from. + * @param __pos The position in the string_view to append from. + * @param __n The number of characters to append from the string_view. + * @return Reference to this string. + */ + template + _If_sv<_Tp, basic_string&> + append(const _Tp& __svt, size_type __pos, size_type __n = npos) + { + __sv_type __sv = __svt; + return append(__sv.data() + + __sv._M_check(__pos, "basic_string::append"), + __sv._M_limit(__pos, __n)); + } +#endif // C++17 + /** * @brief Append a single character. * @param __c Character to append. @@ -4115,6 +4205,34 @@ _GLIBCXX_END_NAMESPACE_CXX11 { return this->assign(__l.begin(), __l.size()); } #endif // C++11 +#if __cplusplus > 201402L + /** + * @brief Set value from a string_view. + * @param __sv The source string_view. + * @return Reference to this string. + */ + basic_string& + assign(__sv_type __sv) + { return this->assign(__sv.data(), __sv.size()); } + + /** + * @brief Set value from a range of characters in a string_view. + * @param __sv The source string_view. + * @param __pos The position in the string_view to assign from. + * @param __n The number of characters to assign. + * @return Reference to this string. + */ + template + _If_sv<_Tp, basic_string&> + assign(const _Tp& __svt, size_type __pos, size_type __n = npos) + { + __sv_type __sv = __svt; + return assign(__sv.data() + + __sv._M_check(__pos, "basic_string::assign"), + __sv._M_limit(__pos, __n)); + } +#endif // C++17 + /** * @brief Insert multiple characters. * @param __p Iterator referencing location in string to insert at. @@ -4290,6 +4408,38 @@ _GLIBCXX_END_NAMESPACE_CXX11 return iterator(_M_data() + __pos); } +#if __cplusplus > 201402L + /** + * @brief Insert a string_view. + * @param __pos Iterator referencing position in string to insert at. + * @param __sv The string_view to insert. + * @return Reference to this string. + */ + basic_string& + insert(size_type __pos, __sv_type __sv) + { return this->insert(__pos, __sv.data(), __sv.size()); } + + /** + * @brief Insert a string_view. + * @param __pos Iterator referencing position in string to insert at. + * @param __sv The string_view to insert from. + * @param __pos Iterator referencing position in string_view to insert + * from. + * @param __n The number of characters to insert. + * @return Reference to this string. + */ + template + _If_sv<_Tp, basic_string&> + insert(size_type __pos1, const _Tp& __svt, + size_type __pos2, size_type __n = npos) + { + __sv_type __sv = __svt; + return this->replace(__pos1, size_type(0), __sv.data() + + __sv._M_check(__pos2, "basic_string::insert"), + __sv._M_limit(__pos2, __n)); + } +#endif // C++17 + /** * @brief Remove characters. * @param __pos Index of first character to remove (default 0). @@ -4644,6 +4794,52 @@ _GLIBCXX_END_NAMESPACE_CXX11 { return this->replace(__i1, __i2, __l.begin(), __l.end()); } #endif // C++11 +#if __cplusplus > 201402L + /** + * @brief Replace range of characters with string_view. + * @param __pos The position to replace at. + * @param __n The number of characters to replace. + * @param __sv The string_view to insert. + * @return Reference to this string. + */ + basic_string& + replace(size_type __pos, size_type __n, __sv_type __sv) + { return this->replace(__pos, __n, __sv.data(), __sv.size()); } + + /** + * @brief Replace range of characters with string_view. + * @param __pos1 The position to replace at. + * @param __n1 The number of characters to replace. + * @param __sv The string_view to insert from. + * @param __pos2 The position in the string_view to insert from. + * @param __n2 The number of characters to insert. + * @return Reference to this string. + */ + template + _If_sv<_Tp, basic_string&> + replace(size_type __pos1, size_type __n1, const _Tp& __svt, + size_type __pos2, size_type __n2 = npos) + { + __sv_type __sv = __svt; + return this->replace(__pos1, __n1, __sv.data() + + __sv._M_check(__pos2, "basic_string::replace"), + __sv._M_limit(__pos2, __n2)); + } + + /** + * @brief Replace range of characters with string_view. + * @param __i1 An iterator referencing the start position + to replace at. + * @param __i2 An iterator referencing the end position + for the replace. + * @param __sv The string_view to insert from. + * @return Reference to this string. + */ + basic_string& + replace(const_iterator __i1, const_iterator __i2, __sv_type __sv) + { return this->replace(__i1 - begin(), __i2 - __i1, __sv); } +#endif // C++17 + private: template basic_string& @@ -4843,6 +5039,18 @@ _GLIBCXX_END_NAMESPACE_CXX11 size_type find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT; +#if __cplusplus > 201402L + /** + * @brief Find position of a string_view. + * @param __sv The string_view to locate. + * @param __pos Index of character to search from (default 0). + * @return Index of start of first occurrence. + */ + size_type + find(__sv_type __sv, size_type __pos = 0) const noexcept + { return this->find(__sv.data(), __pos, __sv.size()); } +#endif // C++17 + /** * @brief Find last position of a string. * @param __str String to locate. @@ -4904,6 +5112,18 @@ _GLIBCXX_END_NAMESPACE_CXX11 size_type rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT; +#if __cplusplus > 201402L + /** + * @brief Find last position of a string_view. + * @param __sv The string_view to locate. + * @param __pos Index of character to search back from (default end). + * @return Index of start of last occurrence. + */ + size_type + rfind(__sv_type __sv, size_type __pos = npos) const noexcept + { return this->rfind(__sv.data(), __pos, __sv.size()); } +#endif // C++17 + /** * @brief Find position of a character of string. * @param __str String containing characters to locate. @@ -4970,6 +5190,18 @@ _GLIBCXX_END_NAMESPACE_CXX11 find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT { return this->find(__c, __pos); } +#if __cplusplus > 201402L + /** + * @brief Find position of a character of a string_view. + * @param __sv A string_view containing characters to locate. + * @param __pos Index of character to search from (default 0). + * @return Index of first occurrence. + */ + size_type + find_first_of(__sv_type __sv, size_type __pos = 0) const noexcept + { return this->find_first_of(__sv.data(), __pos, __sv.size()); } +#endif // C++17 + /** * @brief Find last position of a character of string. * @param __str String containing characters to locate. @@ -5036,6 +5268,18 @@ _GLIBCXX_END_NAMESPACE_CXX11 find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT { return this->rfind(__c, __pos); } +#if __cplusplus > 201402L + /** + * @brief Find last position of a character of string. + * @param __sv A string_view containing characters to locate. + * @param __pos Index of character to search back from (default end). + * @return Index of last occurrence. + */ + size_type + find_last_of(__sv_type __sv, size_type __pos = npos) const noexcept + { return this->find_last_of(__sv.data(), __pos, __sv.size()); } +#endif // C++17 + /** * @brief Find position of a character not in string. * @param __str String containing characters to avoid. @@ -5099,6 +5343,18 @@ _GLIBCXX_END_NAMESPACE_CXX11 find_first_not_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT; +#if __cplusplus > 201402L + /** + * @brief Find position of a character not in a string_view. + * @param __sv A string_view containing characters to avoid. + * @param __pos Index of character to search from (default 0). + * @return Index of first occurrence. + */ + size_type + find_first_not_of(__sv_type __sv, size_type __pos = 0) const noexcept + { return this->find_first_not_of(__sv.data(), __pos, __sv.size()); } +#endif // C++17 + /** * @brief Find last position of a character not in string. * @param __str String containing characters to avoid. @@ -5163,6 +5419,18 @@ _GLIBCXX_END_NAMESPACE_CXX11 find_last_not_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT; +#if __cplusplus > 201402L + /** + * @brief Find last position of a character not in a string_view. + * @param __sv A string_view containing characters to avoid. + * @param __pos Index of character to search back from (default end). + * @return Index of last occurrence. + */ + size_type + find_last_not_of(__sv_type __sv, size_type __pos = npos) const noexcept + { return this->find_last_not_of(__sv.data(), __pos, __sv.size()); } +#endif // C++17 + /** * @brief Get a substring. * @param __pos Index of first character (default 0). @@ -5207,6 +5475,56 @@ _GLIBCXX_END_NAMESPACE_CXX11 return __r; } +#if __cplusplus > 201402L + /** + * @brief Compare to a string_view. + * @param __sv A string_view to compare against. + * @return Integer < 0, 0, or > 0. + */ + int + compare(__sv_type __sv) const + { + const size_type __size = this->size(); + const size_type __osize = __sv.size(); + const size_type __len = std::min(__size, __osize); + + int __r = traits_type::compare(_M_data(), __sv.data(), __len); + if (!__r) + __r = _S_compare(__size, __osize); + return __r; + } + + /** + * @brief Compare to a string_view. + * @param __pos A position in the string to start comparing from. + * @param __n The number of characters to compare. + * @param __sv A string_view to compare against. + * @return Integer < 0, 0, or > 0. + */ + int + compare(size_type __pos, size_type __n, __sv_type __sv) const + { return __sv_type(*this).substr(__pos, __n).compare(__sv); } + + /** + * @brief Compare to a string_view. + * @param __pos1 A position in the string to start comparing from. + * @param __n1 The number of characters to compare. + * @param __sv A string_view to compare against. + * @param __pos2 A position in the string_view to start comparing from. + * @param __n2 The number of characters to compare. + * @return Integer < 0, 0, or > 0. + */ + template + _If_sv<_Tp, int> + compare(size_type __pos1, size_type __n1, const _Tp& __svt, + size_type __pos2, size_type __n2 = npos) const + { + __sv_type __sv = __svt; + return __sv_type(*this) + .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2)); + } +#endif // C++17 + /** * @brief Compare substring to a string. * @param __pos Index of first character of substring.