From d7a3f981fdc6bf9aef306854505638db7b4b51c7 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Mon, 6 Nov 2017 19:00:21 +0000 Subject: Improve ByteString{View}::operator<(). Make it possible to compare: 1) const char* < ByteStringView 2) ByteString < const char* 3) ByteString < ByteStringView 4) const char* < ByteString Do the same for WideString{View}. With new unit test cases. Change-Id: I31da526b1ab78b8d51ec1c99bf29046fe13754d5 Reviewed-on: https://pdfium-review.googlesource.com/17872 Commit-Queue: Lei Zhang Reviewed-by: Tom Sepez --- core/fxcrt/bytestring.cpp | 50 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 14 deletions(-) (limited to 'core/fxcrt/bytestring.cpp') diff --git a/core/fxcrt/bytestring.cpp b/core/fxcrt/bytestring.cpp index 37121a7da9..704cdd46aa 100644 --- a/core/fxcrt/bytestring.cpp +++ b/core/fxcrt/bytestring.cpp @@ -251,13 +251,39 @@ bool ByteString::operator==(const ByteString& other) const { m_pData->m_nDataLength) == 0; } -bool ByteString::operator<(const ByteString& str) const { - if (m_pData == str.m_pData) +bool ByteString::operator<(const char* ptr) const { + if (!m_pData && !ptr) return false; + if (c_str() == ptr) + return false; + + size_t len = GetLength(); + size_t other_len = ptr ? strlen(ptr) : 0; + int result = memcmp(c_str(), ptr, std::min(len, other_len)); + return result < 0 || (result == 0 && len < other_len); +} +bool ByteString::operator<(const ByteStringView& str) const { + if (!m_pData && !str.unterminated_c_str()) + return false; + if (c_str() == str.unterminated_c_str()) + return false; + + size_t len = GetLength(); + size_t other_len = str.GetLength(); int result = - memcmp(c_str(), str.c_str(), std::min(GetLength(), str.GetLength())); - return result < 0 || (result == 0 && GetLength() < str.GetLength()); + memcmp(c_str(), str.unterminated_c_str(), std::min(len, other_len)); + return result < 0 || (result == 0 && len < other_len); +} + +bool ByteString::operator<(const ByteString& other) const { + if (m_pData == other.m_pData) + return false; + + size_t len = GetLength(); + size_t other_len = other.GetLength(); + int result = memcmp(c_str(), other.c_str(), std::min(len, other_len)); + return result < 0 || (result == 0 && len < other_len); } bool ByteString::EqualNoCase(const ByteStringView& str) const { @@ -668,26 +694,22 @@ ByteString ByteString::FromUnicode(const WideString& str) { } int ByteString::Compare(const ByteStringView& str) const { - if (!m_pData) { + if (!m_pData) return str.IsEmpty() ? 0 : -1; - } + size_t this_len = m_pData->m_nDataLength; size_t that_len = str.GetLength(); size_t min_len = std::min(this_len, that_len); for (size_t i = 0; i < min_len; i++) { - if (static_cast(m_pData->m_String[i]) < str[i]) { + if (static_cast(m_pData->m_String[i]) < str[i]) return -1; - } - if (static_cast(m_pData->m_String[i]) > str[i]) { + if (static_cast(m_pData->m_String[i]) > str[i]) return 1; - } } - if (this_len < that_len) { + if (this_len < that_len) return -1; - } - if (this_len > that_len) { + if (this_len > that_len) return 1; - } return 0; } -- cgit v1.2.3