From bc48185643b3983fd9620ba2f987ada1478fbdc2 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Tue, 19 Sep 2017 11:35:17 -0400 Subject: Convert FindNextBreakPos to return position This CL changes FindNextBreakPos to return the found index instead of requiring a call to GetAt(). This also fixes the issue that we may return -1 from GetAt which would cause issues in the BoundsForWordAt method when it gets shoved into a size_t variable. Change-Id: I29e09de5d0837921a027208fc2471a9b1de287f1 Reviewed-on: https://pdfium-review.googlesource.com/14293 Reviewed-by: Ryan Harrison Commit-Queue: dsinclair --- xfa/fde/cfde_texteditengine.cpp | 29 +++++++++++------------------ xfa/fde/cfde_texteditengine.h | 5 ++--- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/xfa/fde/cfde_texteditengine.cpp b/xfa/fde/cfde_texteditengine.cpp index 233d958094..c619a984d2 100644 --- a/xfa/fde/cfde_texteditengine.cpp +++ b/xfa/fde/cfde_texteditengine.cpp @@ -975,11 +975,9 @@ std::pair CFDE_TextEditEngine::BoundsForWordAt( CFDE_TextEditEngine::Iterator iter(this); iter.SetAt(idx); - iter.FindNextBreakPos(true); - size_t start_idx = iter.GetAt(); - iter.FindNextBreakPos(false); - size_t end_idx = iter.GetAt(); + size_t start_idx = iter.FindNextBreakPos(true); + size_t end_idx = iter.FindNextBreakPos(false); return {start_idx, end_idx - start_idx + 1}; } @@ -1006,19 +1004,13 @@ wchar_t CFDE_TextEditEngine::Iterator::GetChar() const { return engine_->GetChar(current_position_); } -void CFDE_TextEditEngine::Iterator::SetAt(int32_t nIndex) { - if (nIndex < 0) - current_position_ = 0; - else if (static_cast(nIndex) >= engine_->GetLength()) +void CFDE_TextEditEngine::Iterator::SetAt(size_t nIndex) { + if (static_cast(nIndex) >= engine_->GetLength()) current_position_ = engine_->GetLength(); else current_position_ = nIndex; } -int32_t CFDE_TextEditEngine::Iterator::GetAt() const { - return current_position_; -} - bool CFDE_TextEditEngine::Iterator::IsEOF(bool bPrev) const { return bPrev ? current_position_ == -1 : current_position_ > -1 && @@ -1026,9 +1018,9 @@ bool CFDE_TextEditEngine::Iterator::IsEOF(bool bPrev) const { engine_->GetLength(); } -void CFDE_TextEditEngine::Iterator::FindNextBreakPos(bool bPrev) { +size_t CFDE_TextEditEngine::Iterator::FindNextBreakPos(bool bPrev) { if (IsEOF(bPrev)) - return; + return current_position_ > -1 ? current_position_ : 0; WordBreakProperty ePreType = WordBreakProperty::kNone; if (!IsEOF(!bPrev)) { @@ -1047,14 +1039,14 @@ void CFDE_TextEditEngine::Iterator::FindNextBreakPos(bool bPrev) { if (wBreak) { if (IsEOF(bPrev)) { Next(!bPrev); - return; + break; } if (bFirst) { int32_t nFlags = GetBreakFlagsFor(eCurType, eNextType); if (nFlags > 0) { if (BreakFlagsChanged(nFlags, ePreType)) { Next(!bPrev); - return; + break; } Next(bPrev); wBreak = false; @@ -1064,7 +1056,7 @@ void CFDE_TextEditEngine::Iterator::FindNextBreakPos(bool bPrev) { int32_t nFlags = GetBreakFlagsFor(eNextType, eCurType); if (nFlags <= 0) { Next(!bPrev); - return; + break; } Next(bPrev); @@ -1072,11 +1064,12 @@ void CFDE_TextEditEngine::Iterator::FindNextBreakPos(bool bPrev) { if (BreakFlagsChanged(nFlags, eNextType)) { Next(!bPrev); Next(!bPrev); - return; + break; } } } eCurType = eNextType; bFirst = false; } + return current_position_ > -1 ? current_position_ : 0; } diff --git a/xfa/fde/cfde_texteditengine.h b/xfa/fde/cfde_texteditengine.h index fc5a5fb490..e26487aaf6 100644 --- a/xfa/fde/cfde_texteditengine.h +++ b/xfa/fde/cfde_texteditengine.h @@ -44,9 +44,8 @@ class CFDE_TextEditEngine { void Next(bool bPrev); wchar_t GetChar() const; - void SetAt(int32_t nIndex); - int32_t GetAt() const; - void FindNextBreakPos(bool bPrev); + void SetAt(size_t nIndex); + size_t FindNextBreakPos(bool bPrev); bool IsEOF(bool bPrev) const; private: -- cgit v1.2.3