From 08cce5ebe56f1335b6d19c9b3344123610beaba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Tue, 21 Feb 2017 23:08:11 +0100 Subject: [PATCH] Update basic_string with missing C++11 and C++17 interfaces: - Default npos arguments in append, insert, compare - Initializer list support - Non-const data() --- include/boost/container/string.hpp | 110 +++++++++++++++++++++++++---- test/string_test.cpp | 4 ++ 2 files changed, 101 insertions(+), 13 deletions(-) diff --git a/include/boost/container/string.hpp b/include/boost/container/string.hpp index cb219c5..b1fced8 100644 --- a/include/boost/container/string.hpp +++ b/include/boost/container/string.hpp @@ -37,15 +37,18 @@ #include #include #include +#include +#include + +#include #include #include -#include -#include -#include -#include -#include +#include +#include +#include +#include #include #include //bind2nd, etc. @@ -56,8 +59,12 @@ #include #include #include -#include -#include + +//std +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) +#include //for std::initializer_list +#endif + namespace boost { namespace container { @@ -791,6 +798,17 @@ class basic_string this->assign(f, l); } + #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Same as basic_string(il.begin(), il.end(), a). + //! + basic_string(std::initializer_list il, const allocator_type& a = allocator_type()) + : base_t(a) + { + this->priv_terminate_string(); + this->assign(il.begin(), il.end()); + } + #endif + //! Effects: Destroys the basic_string. All used memory is deallocated. //! //! Throws: Nothing. @@ -867,7 +885,7 @@ class basic_string basic_string& operator=(const CharT* s) { return this->assign(s, s + Traits::length(s)); } - //! Effects: Returns: *this = basic_string(1, c). + //! Effects: Returns *this = basic_string(1, c). //! basic_string& operator=(CharT c) { return this->assign(static_cast(1), c); } @@ -878,6 +896,15 @@ class basic_string basic_string& operator=(BasicStringView sv) { return this->assign(sv.data(), sv.size()); } + #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Returns *this = basic_string(il); + //! + basic_string& operator=(std::initializer_list il) + { + return this->assign(il.begin(), il.end()); + } + #endif + //! Effects: Returns a copy of the internal allocator. //! //! Throws: If allocator's copy constructor throws. @@ -1295,6 +1322,15 @@ class basic_string basic_string& operator+=(CharT c) { this->push_back(c); return *this; } + #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Returns append(il) + //! + basic_string& operator+=(std::initializer_list il) + { + return this->append(il); + } + #endif + //! Effects: Calls append(str.data(), str.size()). //! //! Returns: *this @@ -1315,7 +1351,7 @@ class basic_string //! Throws: If memory allocation throws and out_of_range if pos > str.size() //! //! Returns: *this - basic_string& append(const basic_string& s, size_type pos, size_type n) + basic_string& append(const basic_string& s, size_type pos, size_type n = npos) { if (pos > s.size()) throw_out_of_range("basic_string::append out of range position"); @@ -1359,6 +1395,15 @@ class basic_string basic_string& append(InputIter first, InputIter last) { this->insert(this->end(), first, last); return *this; } + #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Returns append(il.begin(), il.size()). + //! + basic_string& append(std::initializer_list il) + { + return this->append(il.begin(), il.size()); + } + #endif + //! Effects: Equivalent to append(static_cast(1), c). //! void push_back(CharT c) @@ -1481,6 +1526,15 @@ class basic_string return *this; } + #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Returns assign(il.begin(), il.size()). + //! + basic_string& assign(std::initializer_list il) + { + return this->assign(il.begin(), il.size()); + } + #endif + //! Requires: pos <= size(). //! //! Effects: Calls insert(pos, str.data(), str.size()). @@ -1507,7 +1561,7 @@ class basic_string //! Throws: If memory allocation throws or out_of_range if pos1 > size() or pos2 > str.size(). //! //! Returns: *this - basic_string& insert(size_type pos1, const basic_string& s, size_type pos2, size_type n) + basic_string& insert(size_type pos1, const basic_string& s, size_type pos2, size_type n = npos) { const size_type sz = this->size(); const size_type str_size = s.size(); @@ -1741,6 +1795,17 @@ class basic_string } #endif + #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: As if by insert(p, il.begin(), il.end()). + //! + //! Returns: An iterator which refers to the copy of the first inserted + //! character, or p if i1 is empty. + iterator insert(const_iterator p, std::initializer_list il) + { + return this->insert(p, il.begin(), il.end()); + } + #endif + //! Effects: Removes the last element from the container. //! //! Throws: Nothing. @@ -2067,10 +2132,23 @@ class basic_string template