[PATCH] D12355: [libcxx] Optimize away unneeded length calculation in basic_string::compare(const char*)

Eric Fiselier via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 26 14:38:33 PDT 2015


EricWF updated this revision to Diff 33253.
EricWF added a comment.

Fix debug assertion.


http://reviews.llvm.org/D12355

Files:
  include/string

Index: include/string
===================================================================
--- include/string
+++ include/string
@@ -3795,7 +3795,10 @@
 operator==(const _CharT* __lhs,
            const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
 {
-    return __rhs.compare(__lhs) == 0;
+    _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
+    size_t __lhs_len = _Traits::length(__lhs);
+    if (__lhs_len != __rhs.size()) return false;
+    return _Traits::compare(__rhs.data(), __lhs, _VSTD::min(__lhs_len, __rhs.size())) == 0;
 }
 
 template<class _CharT, class _Traits, class _Allocator>
@@ -3804,7 +3807,10 @@
 operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
            const _CharT* __rhs) _NOEXCEPT
 {
-    return __lhs.compare(__rhs) == 0;
+    _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
+    size_t __rhs_len = _Traits::length(__rhs);
+    if (__rhs_len != __lhs.size()) return false;
+    return _Traits::compare(__lhs.data(), __rhs, _VSTD::min(__rhs_len, __lhs.size())) == 0;
 }
 
 // operator!=


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D12355.33253.patch
Type: text/x-patch
Size: 1126 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150826/7b84cd62/attachment.bin>


More information about the cfe-commits mailing list