From 1b65779f46f16b4fffd0591f5e58722c1e7cde8d Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Tue, 14 Jun 2022 14:54:27 +0100 Subject: libstdc++: Inline all basic_string::compare overloads [PR59048] Defining the compare member functions inline allows calls to traits_type::length and std::min to be inlined, taking advantage of constant expression arguments. When not inline, the compiler prefers to use the explicit instantiation definitions in libstdc++.so and can't take advantage of constant arguments. libstdc++-v3/ChangeLog: PR libstdc++/59048 * include/bits/basic_string.h (compare): Define inline. * include/bits/basic_string.tcc (compare): Remove out-of-line definitions. * include/bits/cow_string.h (compare): Define inline. * testsuite/21_strings/basic_string/operations/compare/char/3.cc: New test. --- libstdc++-v3/include/bits/basic_string.tcc | 85 ------------------------------ 1 file changed, 85 deletions(-) (limited to 'libstdc++-v3/include/bits/basic_string.tcc') diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc index 0696b96604c..4563c61429a 100644 --- a/libstdc++-v3/include/bits/basic_string.tcc +++ b/libstdc++-v3/include/bits/basic_string.tcc @@ -847,91 +847,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return npos; } - template - _GLIBCXX_STRING_CONSTEXPR - int - basic_string<_CharT, _Traits, _Alloc>:: - compare(size_type __pos, size_type __n, const basic_string& __str) const - { - _M_check(__pos, "basic_string::compare"); - __n = _M_limit(__pos, __n); - const size_type __osize = __str.size(); - const size_type __len = std::min(__n, __osize); - int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len); - if (!__r) - __r = _S_compare(__n, __osize); - return __r; - } - - template - _GLIBCXX_STRING_CONSTEXPR - int - basic_string<_CharT, _Traits, _Alloc>:: - compare(size_type __pos1, size_type __n1, const basic_string& __str, - size_type __pos2, size_type __n2) const - { - _M_check(__pos1, "basic_string::compare"); - __str._M_check(__pos2, "basic_string::compare"); - __n1 = _M_limit(__pos1, __n1); - __n2 = __str._M_limit(__pos2, __n2); - const size_type __len = std::min(__n1, __n2); - int __r = traits_type::compare(_M_data() + __pos1, - __str.data() + __pos2, __len); - if (!__r) - __r = _S_compare(__n1, __n2); - return __r; - } - - template - _GLIBCXX_STRING_CONSTEXPR - int - basic_string<_CharT, _Traits, _Alloc>:: - compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT - { - __glibcxx_requires_string(__s); - const size_type __size = this->size(); - const size_type __osize = traits_type::length(__s); - const size_type __len = std::min(__size, __osize); - int __r = traits_type::compare(_M_data(), __s, __len); - if (!__r) - __r = _S_compare(__size, __osize); - return __r; - } - - template - _GLIBCXX_STRING_CONSTEXPR - int - basic_string <_CharT, _Traits, _Alloc>:: - compare(size_type __pos, size_type __n1, const _CharT* __s) const - { - __glibcxx_requires_string(__s); - _M_check(__pos, "basic_string::compare"); - __n1 = _M_limit(__pos, __n1); - const size_type __osize = traits_type::length(__s); - const size_type __len = std::min(__n1, __osize); - int __r = traits_type::compare(_M_data() + __pos, __s, __len); - if (!__r) - __r = _S_compare(__n1, __osize); - return __r; - } - - template - _GLIBCXX_STRING_CONSTEXPR - int - basic_string <_CharT, _Traits, _Alloc>:: - compare(size_type __pos, size_type __n1, const _CharT* __s, - size_type __n2) const - { - __glibcxx_requires_string_len(__s, __n2); - _M_check(__pos, "basic_string::compare"); - __n1 = _M_limit(__pos, __n1); - const size_type __len = std::min(__n1, __n2); - int __r = traits_type::compare(_M_data() + __pos, __s, __len); - if (!__r) - __r = _S_compare(__n1, __n2); - return __r; - } - #undef _GLIBCXX_STRING_CONSTEXPR // 21.3.7.9 basic_string::getline and operators -- cgit v1.2.1