Commit e03a6fb7 by Paolo Carlini Committed by Paolo Carlini

basic_string.h (_M_check): Change to return a checked __pos and take an…

basic_string.h (_M_check): Change to return a checked __pos and take an additional const char* argument.

2004-01-21  Paolo Carlini  <pcarlini@suse.de>

	* include/bits/basic_string.h (_M_check): Change to return
	a checked __pos and take an additional const char* argument.
	(_M_fold): Rename to _M_limit, change to return a size_type,
	corresponding to the __off limited to the actual length.
	(insert(size_type, size_type, _CharT)): Update call, call
	replace.
	(insert(iterator, _CharT)): Call replace(iterator, iterator,
	size_type, _CharT) instead.
	(erase(size_type, size_type)): Update calls.
	(replace(size_type, size_type, size_type, _CharT)): Ditto.
	(substr(size_type, size_type)): Use _M_check.
	* include/bits/basic_string.tcc (basic_string(const basic_string&,
	size_type, size_type)): Update calls.
	(basic_string(const basic_string&, size_type, size_type,
	const _Alloc&)): Ditto.
	(assign(const basic_string&, size_type, size_type)): Use the
	new _M_check and _M_limit.
	(insert(size_type, const basic_string&, size_type, size_type):
	Ditto.
	(insert(size_type, const _CharT*, size_type)): Ditto.
	(replace(size_type, size_type, const _CharT*, size_type): Ditto.
	(replace(size_type, size_type, const basic_string&,
	size_type, size_type)): Ditto.
	(append(const basic_string&)): Ditto.
	(append(const basic_string&, size_type, size_type)): Ditto.
	(copy(_CharT*, size_type, size_type)): Ditto.
	(compare(size_type, size_type, const basic_string&)): Ditto.
	(compare(size_type, size_type, const basic_string&,size_type,
	size_type)): Ditto.
	(compare(size_type, size_type, const _CharT*)): Ditto.
	(compare(size_type, size_type, const _CharT*, size_type)): Ditto.

From-SVN: r76274
parent adc04486
2004-01-21 Paolo Carlini <pcarlini@suse.de>
* include/bits/basic_string.h (_M_check): Change to return
a checked __pos and take an additional const char* argument.
(_M_fold): Rename to _M_limit, change to return a size_type,
corresponding to the __off limited to the actual length.
(insert(size_type, size_type, _CharT)): Update call, call
replace.
(insert(iterator, _CharT)): Call replace(iterator, iterator,
size_type, _CharT) instead.
(erase(size_type, size_type)): Update calls.
(replace(size_type, size_type, size_type, _CharT)): Ditto.
(substr(size_type, size_type)): Use _M_check.
* include/bits/basic_string.tcc (basic_string(const basic_string&,
size_type, size_type)): Update calls.
(basic_string(const basic_string&, size_type, size_type,
const _Alloc&)): Ditto.
(assign(const basic_string&, size_type, size_type)): Use the
new _M_check and _M_limit.
(insert(size_type, const basic_string&, size_type, size_type):
Ditto.
(insert(size_type, const _CharT*, size_type)): Ditto.
(replace(size_type, size_type, const _CharT*, size_type): Ditto.
(replace(size_type, size_type, const basic_string&,
size_type, size_type)): Ditto.
(append(const basic_string&)): Ditto.
(append(const basic_string&, size_type, size_type)): Ditto.
(copy(_CharT*, size_type, size_type)): Ditto.
(compare(size_type, size_type, const basic_string&)): Ditto.
(compare(size_type, size_type, const basic_string&,size_type,
size_type)): Ditto.
(compare(size_type, size_type, const _CharT*)): Ditto.
(compare(size_type, size_type, const _CharT*, size_type)): Ditto.
2004-01-19 Stefan Olsson <stefan@snon.net> 2004-01-19 Stefan Olsson <stefan@snon.net>
* include/ext/mt_allocator.h: If a thread, when it dies, still has * include/ext/mt_allocator.h: If a thread, when it dies, still has
......
...@@ -283,21 +283,20 @@ namespace std ...@@ -283,21 +283,20 @@ namespace std
_M_leak_hard(); _M_leak_hard();
} }
iterator size_type
_M_check(size_type __pos) const _M_check(size_type __pos, const char* __s) const
{ {
if (__pos > this->size()) if (__pos > this->size())
__throw_out_of_range(__N("basic_string::_M_check")); __throw_out_of_range(__N(__s));
return _M_ibegin() + __pos; return __pos;
} }
// NB: _M_fold doesn't check for a bad __pos1 value. // NB: _M_limit doesn't check for a bad __pos value.
iterator size_type
_M_fold(size_type __pos, size_type __off) const _M_limit(size_type __pos, size_type __off) const
{ {
const bool __testoff = __off < this->size() - __pos; const bool __testoff = __off < this->size() - __pos;
const size_type __newoff = __testoff ? __off : this->size() - __pos; return __testoff ? __off : this->size() - __pos;
return (_M_ibegin() + __pos + __newoff);
} }
// _S_copy_chars is a separate template to permit specialization // _S_copy_chars is a separate template to permit specialization
...@@ -979,9 +978,10 @@ namespace std ...@@ -979,9 +978,10 @@ namespace std
*/ */
basic_string& basic_string&
insert(size_type __pos, size_type __n, _CharT __c) insert(size_type __pos, size_type __n, _CharT __c)
{ {
this->insert(_M_check(__pos), __n, __c); const iterator __iterator = this->_M_ibegin()
return *this; + _M_check(__pos, "basic_string::insert");
return this->replace(__iterator, __iterator, __n, __c);
} }
/** /**
...@@ -1002,7 +1002,7 @@ namespace std ...@@ -1002,7 +1002,7 @@ namespace std
{ {
_GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend()); _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
const size_type __pos = __p - _M_ibegin(); const size_type __pos = __p - _M_ibegin();
this->insert(_M_check(__pos), size_type(1), __c); this->replace(__p, __p, size_type(1), __c);
_M_rep()->_M_set_leaked(); _M_rep()->_M_set_leaked();
return this->_M_ibegin() + __pos; return this->_M_ibegin() + __pos;
} }
...@@ -1043,7 +1043,8 @@ namespace std ...@@ -1043,7 +1043,8 @@ namespace std
basic_string& basic_string&
erase(size_type __pos = 0, size_type __n = npos) erase(size_type __pos = 0, size_type __n = npos)
{ {
return this->replace(_M_check(__pos), _M_fold(__pos, __n), return this->replace(_M_ibegin() + _M_check(__pos, "basic_string::erase"),
_M_ibegin() + __pos + _M_limit(__pos, __n),
_M_data(), _M_data()); _M_data(), _M_data());
} }
...@@ -1195,7 +1196,9 @@ namespace std ...@@ -1195,7 +1196,9 @@ namespace std
*/ */
basic_string& basic_string&
replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c) replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
{ return this->replace(_M_check(__pos), _M_fold(__pos, __n1), __n2, __c); } { return this->replace(_M_ibegin() + _M_check(__pos, "basic_string::replace"),
_M_ibegin() + __pos + _M_limit(__pos, __n1),
__n2, __c); }
/** /**
* @brief Replace range of characters with string. * @brief Replace range of characters with string.
...@@ -1843,11 +1846,7 @@ namespace std ...@@ -1843,11 +1846,7 @@ namespace std
*/ */
basic_string basic_string
substr(size_type __pos = 0, size_type __n = npos) const substr(size_type __pos = 0, size_type __n = npos) const
{ { return basic_string(*this, _M_check(__pos, "basic_string::substr"), __n); }
if (__pos > this->size())
__throw_out_of_range(__N("basic_string::substr"));
return basic_string(*this, __pos, __n);
}
/** /**
* @brief Compare to a string. * @brief Compare to a string.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment