aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/include/bits/basic_string.tcc
diff options
context:
space:
mode:
Diffstat (limited to 'libstdc++-v3/include/bits/basic_string.tcc')
-rw-r--r--libstdc++-v3/include/bits/basic_string.tcc35
1 files changed, 19 insertions, 16 deletions
diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc
index dce9b38fcb4..4a22d896792 100644
--- a/libstdc++-v3/include/bits/basic_string.tcc
+++ b/libstdc++-v3/include/bits/basic_string.tcc
@@ -142,6 +142,10 @@ namespace std
if (__beg == __end && __a == _Alloc())
return _S_empty_rep()._M_refcopy();
+ // NB: Not required, but considered best practice.
+ if (__builtin_expect(__beg == _InIter(), 0))
+ __throw_logic_error("attempt to create string with null pointer");
+
// Check for out_of_range and length_error exceptions.
_Rep* __r = _Rep::_S_create(__dnew, __a);
try
@@ -219,7 +223,8 @@ namespace std
template<typename _CharT, typename _Traits, typename _Alloc>
basic_string<_CharT, _Traits, _Alloc>::
basic_string(const _CharT* __s, const _Alloc& __a)
- : _M_dataplus(_S_construct(__s, __s + traits_type::length(__s), __a), __a)
+ : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) :
+ __s + npos, __a), __a)
{ }
template<typename _CharT, typename _Traits, typename _Alloc>
@@ -438,7 +443,7 @@ namespace std
void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
_Rep *__p = new (__place) _Rep;
__p->_M_capacity = __capacity;
- __p->_M_set_sharable(); // one reference
+ __p->_M_set_sharable(); // One reference.
__p->_M_length = 0;
return __p;
}
@@ -492,14 +497,10 @@ namespace std
this->erase(__n);
// else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
}
-
- // This is the general replace helper, which gets instantiated both
- // for input-iterators and forward-iterators. It buffers internally and
- // then calls _M_replace_safe. For input-iterators this is almost the
- // best we can do, but for forward-iterators many optimizations could be
- // conceived: f.i., when source and destination ranges do not overlap
- // buffering is not really needed. In order to easily implement them, it
- // could become useful to add an _M_replace(forward_iterator_tag)
+
+ // This is the general replace helper, which currently gets instantiated both
+ // for input iterators and reverse iterators. It buffers internally and then
+ // calls _M_replace_safe.
template<typename _CharT, typename _Traits, typename _Alloc>
template<typename _InputIter>
basic_string<_CharT, _Traits, _Alloc>&
@@ -513,10 +514,8 @@ namespace std
}
// This is a special replace helper, which does not buffer internally
- // and can be used in the "safe" situations involving forward-iterators,
+ // and can be used in "safe" situations involving forward iterators,
// i.e., when source and destination ranges are known to not overlap.
- // Presently, is called by _M_replace, by the various append and by
- // the assigns.
template<typename _CharT, typename _Traits, typename _Alloc>
template<typename _ForwardIter>
basic_string<_CharT, _Traits, _Alloc>&
@@ -885,9 +884,11 @@ namespace std
compare(const _CharT* __s) const
{
size_type __size = this->size();
- int __r = traits_type::compare(_M_data(), __s, __size);
+ size_type __osize = traits_type::length(__s);
+ size_type __len = min(__size, __osize);
+ int __r = traits_type::compare(_M_data(), __s, __len);
if (!__r)
- __r = __size - traits_type::length(__s);
+ __r = __size - __osize;
return __r;
}
@@ -945,7 +946,7 @@ namespace std
// which are defined via explicit instantiations elsewhere.
// NB: This syntax is a GNU extension.
extern template class basic_string<char>;
- extern template
+ extern template
basic_istream<char>&
operator>>(basic_istream<char>&, string&);
extern template
@@ -958,6 +959,7 @@ namespace std
basic_istream<char>&
getline(basic_istream<char>&, string&);
+#ifdef _GLIBCPP_USE_WCHAR_T
extern template class basic_string<wchar_t>;
extern template
basic_istream<wchar_t>&
@@ -971,6 +973,7 @@ namespace std
extern template
basic_istream<wchar_t>&
getline(basic_istream<wchar_t>&, wstring&);
+#endif
} // namespace std
#endif