Add some C++1 missing functions to string and a couple of debug assertions

This commit is contained in:
Ion Gaztañaga
2015-09-18 14:38:38 +02:00
parent f637fada71
commit 3f4a5dec6e
2 changed files with 108 additions and 15 deletions

View File

@@ -1,6 +1,6 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost
// (C) Copyright Ion Gaztanaga 2005-2015. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
@@ -1124,6 +1124,62 @@ class basic_string
//
//////////////////////////////////////////////
//! <b>Requires</b>: !empty()
//!
//! <b>Effects</b>: Returns a reference to the first
//! element of the container.
//!
//! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
reference front() BOOST_NOEXCEPT_OR_NOTHROW
{
BOOST_ASSERT(!this->empty());
return *this->priv_addr();
}
//! <b>Requires</b>: !empty()
//!
//! <b>Effects</b>: Returns a const reference to the first
//! element of the container.
//!
//! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
const_reference front() const BOOST_NOEXCEPT_OR_NOTHROW
{
BOOST_ASSERT(!this->empty());
return *this->priv_addr();
}
//! <b>Requires</b>: !empty()
//!
//! <b>Effects</b>: Returns a reference to the last
//! element of the container.
//!
//! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
reference back() BOOST_NOEXCEPT_OR_NOTHROW
{
BOOST_ASSERT(!this->empty());
return *(this->priv_addr() + (this->size() - 1u) );
}
//! <b>Requires</b>: !empty()
//!
//! <b>Effects</b>: Returns a const reference to the last
//! element of the container.
//!
//! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant.
const_reference back() const BOOST_NOEXCEPT_OR_NOTHROW
{
BOOST_ASSERT(!this->empty());
return *(this->priv_addr() + (this->size() - 1u) );
}
//! <b>Requires</b>: size() > n.
//!
//! <b>Effects</b>: Returns a reference to the nth element
@@ -1133,7 +1189,10 @@ class basic_string
//!
//! <b>Complexity</b>: Constant.
reference operator[](size_type n) BOOST_NOEXCEPT_OR_NOTHROW
{ return *(this->priv_addr() + n); }
{
BOOST_ASSERT(this->size() > n);
return *(this->priv_addr() + n);
}
//! <b>Requires</b>: size() > n.
//!
@@ -1144,7 +1203,10 @@ class basic_string
//!
//! <b>Complexity</b>: Constant.
const_reference operator[](size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
{ return *(this->priv_addr() + n); }
{
BOOST_ASSERT(this->size() > n);
return *(this->priv_addr() + n);
}
//! <b>Requires</b>: size() > n.
//!
@@ -1626,6 +1688,18 @@ class basic_string
}
#endif
//! <b>Effects</b>: Removes the last element from the container.
//!
//! <b>Throws</b>: Nothing.
//!
//! <b>Complexity</b>: Constant time.
void pop_back() BOOST_NOEXCEPT_OR_NOTHROW
{
BOOST_ASSERT(!this->empty());
iterator p = this->end();
this->erase(--p);
}
//! <b>Requires</b>: pos <= size()
//!
//! <b>Effects</b>: Determines the effective length xlen of the string to be removed as the smaller of n and size() - pos.
@@ -1687,18 +1761,6 @@ class basic_string
return iterator(f);
}
//! <b>Requires</b>: !empty()
//!
//! <b>Throws</b>: Nothing
//!
//! <b>Effects</b>: Equivalent to erase(size() - 1, 1).
void pop_back() BOOST_NOEXCEPT_OR_NOTHROW
{
const size_type old_size = this->priv_size();
Traits::assign(this->priv_addr()[old_size-1], CharT(0));
this->priv_size(old_size-1);;
}
//! <b>Effects</b>: Erases all the elements of the vector.
//!
//! <b>Throws</b>: Nothing.

View File

@@ -440,6 +440,37 @@ int string_test()
if(!StringEqual()(bs4, ss4)){
return 1;
}
//Check front/back/begin/end
if(bs4.front() != *ss4.begin())
return 1;
if(bs4.back() != *(ss4.end()-1))
return 1;
bs4.pop_back();
ss4.erase(ss4.end()-1);
if(!StringEqual()(bs4, ss4)){
return 1;
}
if(*bs4.begin() != *ss4.begin())
return 1;
if(*bs4.cbegin() != *ss4.begin())
return 1;
if(*bs4.rbegin() != *ss4.rbegin())
return 1;
if(*bs4.crbegin() != *ss4.rbegin())
return 1;
if(*(bs4.end()-1) != *(ss4.end()-1))
return 1;
if(*(bs4.cend()-1) != *(ss4.end()-1))
return 1;
if(*(bs4.rend()-1) != *(ss4.rend()-1))
return 1;
if(*(bs4.crend()-1) != *(ss4.rend()-1))
return 1;
}
//When done, delete vector