From 52f69b39403b1ac0df0fdf45698e80e60c0f2def Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Tue, 21 Mar 2017 13:42:38 -0700 Subject: Make CPVT_WordPlace more conformant with the prevailing idioms. Add relational operators to replace CmpWord(). Leave CmpLine() for the moment as it saves some work. Remove CmpSec() and just inline comparison of the one member. Invert IsExist() as IsEmpty(). There is a big philosophical discussion as to whether things that are empty exist. I say they do, but they're just empty. Rename Default() to Reset(). Default sounds like a noun. Add AdvanceSection() method and call as appropriate. Change-Id: I01d9479f63a4860e0fd228255540d6d94dc24a9f Reviewed-on: https://pdfium-review.googlesource.com/3139 Commit-Queue: dsinclair Reviewed-by: dsinclair --- core/fpdfdoc/cpdf_variabletext.cpp | 15 +++---- core/fpdfdoc/cpvt_wordplace.h | 80 +++++++++++++++++++------------------- core/fpdfdoc/cpvt_wordrange.h | 38 +++++++++--------- core/fpdfdoc/csection.cpp | 6 +-- 4 files changed, 68 insertions(+), 71 deletions(-) (limited to 'core') diff --git a/core/fpdfdoc/cpdf_variabletext.cpp b/core/fpdfdoc/cpdf_variabletext.cpp index 24dd9b065d..4e46969dc4 100644 --- a/core/fpdfdoc/cpdf_variabletext.cpp +++ b/core/fpdfdoc/cpdf_variabletext.cpp @@ -432,9 +432,7 @@ void CPDF_VariableText::SetText(const CFX_WideString& swText) { if (swText.GetAt(i + 1) == 0x0A) i += 1; - wp.nSecIndex++; - wp.nLineIndex = 0; - wp.nWordIndex = -1; + wp.AdvanceSection(); AddSection(wp, secinfo); } break; @@ -443,9 +441,7 @@ void CPDF_VariableText::SetText(const CFX_WideString& swText) { if (swText.GetAt(i + 1) == 0x0D) i += 1; - wp.nSecIndex++; - wp.nLineIndex = 0; - wp.nWordIndex = -1; + wp.AdvanceSection(); AddSection(wp, secinfo); } break; @@ -537,7 +533,7 @@ CPVT_WordPlace CPDF_VariableText::GetPrevWordPlace( return GetEndWordPlace(); CSection* pSection = m_SectionArray[place.nSecIndex].get(); - if (place.WordCmp(pSection->GetBeginWordPlace()) > 0) + if (place > pSection->GetBeginWordPlace()) return pSection->GetPrevWordPlace(place); if (!pdfium::IndexInBounds(m_SectionArray, place.nSecIndex - 1)) return GetBeginWordPlace(); @@ -552,7 +548,7 @@ CPVT_WordPlace CPDF_VariableText::GetNextWordPlace( return GetEndWordPlace(); CSection* pSection = m_SectionArray[place.nSecIndex].get(); - if (place.WordCmp(pSection->GetEndWordPlace()) < 0) + if (place < pSection->GetEndWordPlace()) return pSection->GetNextWordPlace(place); if (!pdfium::IndexInBounds(m_SectionArray, place.nSecIndex + 1)) return GetEndWordPlace(); @@ -638,8 +634,7 @@ CPVT_WordPlace CPDF_VariableText::GetDownWordPlace( return pSection->SearchWordPlace(pt.x - pSection->m_SecInfo.rcSection.left, temp); } - ++temp.nSecIndex; - temp.nLineIndex = 0; + temp.AdvanceSection(); if (!pdfium::IndexInBounds(m_SectionArray, temp.nSecIndex)) return place; diff --git a/core/fpdfdoc/cpvt_wordplace.h b/core/fpdfdoc/cpvt_wordplace.h index f4a6b087b9..c0a1a9cd5c 100644 --- a/core/fpdfdoc/cpvt_wordplace.h +++ b/core/fpdfdoc/cpvt_wordplace.h @@ -14,55 +14,55 @@ struct CPVT_WordPlace { CPVT_WordPlace(int32_t other_nSecIndex, int32_t other_nLineIndex, - int32_t other_nWordIndex) { - nSecIndex = other_nSecIndex; - nLineIndex = other_nLineIndex; - nWordIndex = other_nWordIndex; + int32_t other_nWordIndex) + : nSecIndex(other_nSecIndex), + nLineIndex(other_nLineIndex), + nWordIndex(other_nWordIndex) {} + + void Reset() { + nSecIndex = -1; + nLineIndex = -1; + nWordIndex = -1; } - void Default() { nSecIndex = nLineIndex = nWordIndex = -1; } + void AdvanceSection() { + nSecIndex++; + nLineIndex = 0; + nWordIndex = -1; + } - bool operator==(const CPVT_WordPlace& wp) const { + inline bool operator==(const CPVT_WordPlace& wp) const { return wp.nSecIndex == nSecIndex && wp.nLineIndex == nLineIndex && wp.nWordIndex == nWordIndex; } - - bool operator!=(const CPVT_WordPlace& wp) const { return !(*this == wp); } - - inline int32_t WordCmp(const CPVT_WordPlace& wp) const { - if (nSecIndex > wp.nSecIndex) - return 1; - if (nSecIndex < wp.nSecIndex) - return -1; - if (nLineIndex > wp.nLineIndex) - return 1; - if (nLineIndex < wp.nLineIndex) - return -1; - if (nWordIndex > wp.nWordIndex) - return 1; - if (nWordIndex < wp.nWordIndex) - return -1; - return 0; + inline bool operator!=(const CPVT_WordPlace& wp) const { + return !(*this == wp); } - - inline int32_t LineCmp(const CPVT_WordPlace& wp) const { - if (nSecIndex > wp.nSecIndex) - return 1; - if (nSecIndex < wp.nSecIndex) - return -1; - if (nLineIndex > wp.nLineIndex) - return 1; - if (nLineIndex < wp.nLineIndex) - return -1; - return 0; + inline bool operator<(const CPVT_WordPlace& wp) const { + if (nSecIndex != wp.nSecIndex) + return nSecIndex < wp.nSecIndex; + if (nLineIndex != wp.nLineIndex) + return nLineIndex < wp.nLineIndex; + return nWordIndex < wp.nWordIndex; + } + inline bool operator>(const CPVT_WordPlace& wp) const { + if (nSecIndex != wp.nSecIndex) + return nSecIndex > wp.nSecIndex; + if (nLineIndex != wp.nLineIndex) + return nLineIndex > wp.nLineIndex; + return nWordIndex > wp.nWordIndex; + } + inline bool operator<=(const CPVT_WordPlace& wp) const { + return *this < wp || *this == wp; + } + inline bool operator>=(const CPVT_WordPlace& wp) const { + return *this > wp || *this == wp; } - inline int32_t SecCmp(const CPVT_WordPlace& wp) const { - if (nSecIndex > wp.nSecIndex) - return 1; - if (nSecIndex < wp.nSecIndex) - return -1; - return 0; + inline int32_t LineCmp(const CPVT_WordPlace& wp) const { + if (nSecIndex != wp.nSecIndex) + return nSecIndex - wp.nSecIndex; + return nLineIndex - wp.nLineIndex; } int32_t nSecIndex; diff --git a/core/fpdfdoc/cpvt_wordrange.h b/core/fpdfdoc/cpvt_wordrange.h index b291e50e53..ed06d6f0de 100644 --- a/core/fpdfdoc/cpvt_wordrange.h +++ b/core/fpdfdoc/cpvt_wordrange.h @@ -7,49 +7,51 @@ #ifndef CORE_FPDFDOC_CPVT_WORDRANGE_H_ #define CORE_FPDFDOC_CPVT_WORDRANGE_H_ +#include + #include "core/fpdfdoc/cpvt_wordplace.h" #include "core/fxcrt/fx_system.h" struct CPVT_WordRange { CPVT_WordRange() {} - CPVT_WordRange(const CPVT_WordPlace& begin, const CPVT_WordPlace& end) { - Set(begin, end); + CPVT_WordRange(const CPVT_WordPlace& begin, const CPVT_WordPlace& end) + : BeginPos(begin), EndPos(end) { + Normalize(); } - void Default() { - BeginPos.Default(); - EndPos.Default(); + void Reset() { + BeginPos.Reset(); + EndPos.Reset(); } void Set(const CPVT_WordPlace& begin, const CPVT_WordPlace& end) { BeginPos = begin; EndPos = end; - SwapWordPlace(); + Normalize(); } void SetBeginPos(const CPVT_WordPlace& begin) { BeginPos = begin; - SwapWordPlace(); + Normalize(); } void SetEndPos(const CPVT_WordPlace& end) { EndPos = end; - SwapWordPlace(); + Normalize(); } - bool IsExist() const { return BeginPos != EndPos; } - - bool operator!=(const CPVT_WordRange& wr) const { - return wr.BeginPos != BeginPos || wr.EndPos != EndPos; + inline bool IsEmpty() const { return BeginPos == EndPos; } + inline bool operator==(const CPVT_WordRange& wr) const { + return wr.BeginPos == BeginPos && wr.EndPos == EndPos; + } + inline bool operator!=(const CPVT_WordRange& wr) const { + return !(*this == wr); } - void SwapWordPlace() { - if (BeginPos.WordCmp(EndPos) > 0) { - CPVT_WordPlace place = EndPos; - EndPos = BeginPos; - BeginPos = place; - } + void Normalize() { + if (BeginPos > EndPos) + std::swap(BeginPos, EndPos); } CPVT_WordPlace BeginPos; diff --git a/core/fpdfdoc/csection.cpp b/core/fpdfdoc/csection.cpp index 6198d780fd..b6f45a274c 100644 --- a/core/fpdfdoc/csection.cpp +++ b/core/fpdfdoc/csection.cpp @@ -237,14 +237,14 @@ void CSection::ClearMidWords(int32_t nBeginIndex, int32_t nEndIndex) { void CSection::ClearWords(const CPVT_WordRange& PlaceRange) { CPVT_WordPlace SecBeginPos = GetBeginWordPlace(); CPVT_WordPlace SecEndPos = GetEndWordPlace(); - if (PlaceRange.BeginPos.WordCmp(SecBeginPos) >= 0) { - if (PlaceRange.EndPos.WordCmp(SecEndPos) <= 0) { + if (PlaceRange.BeginPos >= SecBeginPos) { + if (PlaceRange.EndPos <= SecEndPos) { ClearMidWords(PlaceRange.BeginPos.nWordIndex, PlaceRange.EndPos.nWordIndex); } else { ClearRightWords(PlaceRange.BeginPos.nWordIndex); } - } else if (PlaceRange.EndPos.WordCmp(SecEndPos) <= 0) { + } else if (PlaceRange.EndPos <= SecEndPos) { ClearLeftWords(PlaceRange.EndPos.nWordIndex); } else { m_WordArray.clear(); -- cgit v1.2.3