From 3fa212abba3b6977a4e47981116bb0a3f4017221 Mon Sep 17 00:00:00 2001 From: Jane Liu Date: Mon, 21 Aug 2017 10:26:25 -0400 Subject: Converted CFDE_TxtEdt{Buf|Engine}::Insert() to take CFX_WideString Converted CFDE_TxtEdtBuf::Insert(), CFDE_TxtEdtEngine::Insert(), and CFDE_TxtEdtEngine::Inner_Insert() to take in CFX_WideString instead of a wchar_t* and length. Bug=pdfium:757 Change-Id: I6097e9b0d4e584b920b2117b4c5489eedef37767 Reviewed-on: https://pdfium-review.googlesource.com/11411 Commit-Queue: Jane Liu Commit-Queue: dsinclair Reviewed-by: dsinclair --- xfa/fde/cfde_txtedtbuf.cpp | 7 ++++--- xfa/fde/cfde_txtedtbuf.h | 2 +- xfa/fde/cfde_txtedtbuf_unittest.cpp | 11 +++++------ xfa/fde/cfde_txtedtengine.cpp | 28 +++++++++++++--------------- xfa/fde/cfde_txtedtengine.h | 2 +- 5 files changed, 24 insertions(+), 26 deletions(-) (limited to 'xfa/fde') diff --git a/xfa/fde/cfde_txtedtbuf.cpp b/xfa/fde/cfde_txtedtbuf.cpp index 6e712f2b04..3f558ca386 100644 --- a/xfa/fde/cfde_txtedtbuf.cpp +++ b/xfa/fde/cfde_txtedtbuf.cpp @@ -110,10 +110,10 @@ CFX_WideString CFDE_TxtEdtBuf::GetRange(int32_t nBegin, int32_t nLength) const { return wsText; } -void CFDE_TxtEdtBuf::Insert(int32_t nPos, - const wchar_t* lpText, - int32_t nLength) { +void CFDE_TxtEdtBuf::Insert(int32_t nPos, const CFX_WideString& wsText) { ASSERT(nPos >= 0 && nPos <= m_nTotal); + + int32_t nLength = wsText.GetLength(); ASSERT(nLength > 0); int32_t chunkIndex = 0; @@ -137,6 +137,7 @@ void CFDE_TxtEdtBuf::Insert(int32_t nPos, charIndex = 0; } + const wchar_t* lpText = wsText.c_str(); if (chunkIndex != 0) { ChunkHeader* chunk = m_chunks[chunkIndex - 1].get(); if (chunk->nUsed != m_chunkSize) { diff --git a/xfa/fde/cfde_txtedtbuf.h b/xfa/fde/cfde_txtedtbuf.h index f6d9ffc8c9..b28e4a434e 100644 --- a/xfa/fde/cfde_txtedtbuf.h +++ b/xfa/fde/cfde_txtedtbuf.h @@ -49,7 +49,7 @@ class CFDE_TxtEdtBuf { wchar_t GetCharByIndex(int32_t nIndex) const; CFX_WideString GetRange(int32_t nBegin, int32_t nCount) const; - void Insert(int32_t nPos, const wchar_t* lpText, int32_t nLength); + void Insert(int32_t nPos, const CFX_WideString& wsText); void Delete(int32_t nIndex, int32_t nLength); void Clear(bool bRelease); diff --git a/xfa/fde/cfde_txtedtbuf_unittest.cpp b/xfa/fde/cfde_txtedtbuf_unittest.cpp index 020175aa57..74bc4f95b7 100644 --- a/xfa/fde/cfde_txtedtbuf_unittest.cpp +++ b/xfa/fde/cfde_txtedtbuf_unittest.cpp @@ -34,7 +34,7 @@ TEST_F(CFDE_TxtEdtBufTest, InsertAppendChunk) { buf_->SetText(L"Hi"); CFX_WideString end = L" World"; - buf_->Insert(2, end.c_str(), end.GetLength()); + buf_->Insert(2, end); EXPECT_EQ(3UL, ChunkCount()); EXPECT_EQ(8, buf_->GetTextLength()); @@ -47,7 +47,7 @@ TEST_F(CFDE_TxtEdtBufTest, InsertPrependChunk) { buf_->SetText(L"Hi"); CFX_WideString end = L"World "; - buf_->Insert(0, end.c_str(), end.GetLength()); + buf_->Insert(0, end); EXPECT_EQ(3UL, ChunkCount()); EXPECT_EQ(8, buf_->GetTextLength()); @@ -61,7 +61,7 @@ TEST_F(CFDE_TxtEdtBufTest, InsertBetweenChunks) { EXPECT_EQ(3UL, ChunkCount()); CFX_WideString inst = L"there "; - buf_->Insert(6, inst.c_str(), inst.GetLength()); + buf_->Insert(6, inst); EXPECT_EQ(5UL, ChunkCount()); EXPECT_EQ(17, buf_->GetTextLength()); @@ -170,9 +170,8 @@ TEST_F(CFDE_TxtEdtBufTestDeathTest, InsertBadIndexes) { CFX_WideString inst = L"there "; buf_->SetText(L"Hi"); - EXPECT_DEATH(buf_->Insert(-4, inst.c_str(), inst.GetLength()), "Assertion"); - EXPECT_DEATH(buf_->Insert(9999, inst.c_str(), inst.GetLength()), "Assertion"); - EXPECT_DEATH(buf_->Insert(1, inst.c_str(), -6), "Assertion"); + EXPECT_DEATH(buf_->Insert(-4, inst), "Assertion"); + EXPECT_DEATH(buf_->Insert(9999, inst), "Assertion"); } TEST_F(CFDE_TxtEdtBufTestDeathTest, DeleteWithBadIdx) { diff --git a/xfa/fde/cfde_txtedtengine.cpp b/xfa/fde/cfde_txtedtengine.cpp index 92682a19ea..f109083e01 100644 --- a/xfa/fde/cfde_txtedtengine.cpp +++ b/xfa/fde/cfde_txtedtengine.cpp @@ -60,8 +60,7 @@ class InsertOperation : public IFDE_TxtEdtDoRecord { void Redo() const override { CFX_WideString prev = m_pEngine->GetText(0, -1); - m_pEngine->Inner_Insert(m_nCaret, m_wsInsert.c_str(), - m_wsInsert.GetLength()); + m_pEngine->Inner_Insert(m_nCaret, m_wsInsert); m_pEngine->GetParams()->pEventSink->OnTextChanged(prev); m_pEngine->SetCaretPos(m_nCaret, false); } @@ -94,7 +93,7 @@ class DeleteOperation : public IFDE_TxtEdtDoRecord { if (m_pEngine->IsSelect()) m_pEngine->ClearSelection(); - m_pEngine->Inner_Insert(m_nIndex, m_wsRange.c_str(), m_wsRange.GetLength()); + m_pEngine->Inner_Insert(m_nIndex, m_wsRange); if (m_bSel) m_pEngine->AddSelRange(m_nIndex, m_wsRange.GetLength()); @@ -189,7 +188,7 @@ void CFDE_TxtEdtEngine::SetText(const CFX_WideString& wsText) { } m_pTxtBuf->SetText(wsTemp); } - m_pTxtBuf->Insert(nLength, L"\n", 1); + m_pTxtBuf->Insert(nLength, L"\n"); RebuildParagraphs(); } @@ -375,7 +374,7 @@ int32_t CFDE_TxtEdtEngine::Insert(const CFX_WideString& str) { this, m_nCaret, CFX_WideString(lpBuffer, nLength))); CFX_WideString prev = GetText(0, -1); - Inner_Insert(m_nCaret, lpBuffer, nLength); + Inner_Insert(m_nCaret, CFX_WideString(lpBuffer, nLength)); int32_t nStart = m_nCaret; nStart += nLength; @@ -627,8 +626,8 @@ CFX_WideString CFDE_TxtEdtEngine::GetPreReplaceText(int32_t nIndex, } void CFDE_TxtEdtEngine::Inner_Insert(int32_t nStart, - const wchar_t* lpText, - int32_t nLength) { + const CFX_WideString& wsText) { + const int32_t nLength = wsText.GetLength(); ASSERT(nLength > 0); FDE_TXTEDTPARAGPOS ParagPos; TextPos2ParagPos(nStart, ParagPos); @@ -643,28 +642,27 @@ void CFDE_TxtEdtEngine::Inner_Insert(int32_t nStart, int32_t nLeavePart = ParagPos.nCharIndex; int32_t nCutPart = pParag->GetTextLength() - ParagPos.nCharIndex; int32_t nTextStart = 0; - wchar_t wCurChar = L' '; - const wchar_t* lpPos = lpText; + int32_t nCur = 0; bool bFirst = true; int32_t nParagIndex = ParagPos.nParagIndex; - for (int32_t i = 0; i < nLength; i++, lpPos++) { - wCurChar = *lpPos; + for (const auto& wCurChar : wsText) { + ++nCur; if (wCurChar == L'\n') { if (bFirst) { - pParag->SetTextLength(nLeavePart + (i - nTextStart + 1)); + pParag->SetTextLength(nLeavePart + (nCur - nTextStart + 1)); pParag->SetLineCount(-1); nReserveCharStart += pParag->GetTextLength(); bFirst = false; } else { auto pParag2 = pdfium::MakeUnique(this); pParag2->SetLineCount(-1); - pParag2->SetTextLength(i - nTextStart + 1); + pParag2->SetTextLength(nCur - nTextStart + 1); pParag2->SetStartIndex(nReserveCharStart); nReserveCharStart += pParag2->GetTextLength(); m_ParagPtrArray.insert(m_ParagPtrArray.begin() + ++nParagIndex, std::move(pParag2)); } - nTextStart = i + 1; + nTextStart = nCur + 1; } } if (bFirst) { @@ -679,7 +677,7 @@ void CFDE_TxtEdtEngine::Inner_Insert(int32_t nStart, m_ParagPtrArray.insert(m_ParagPtrArray.begin() + ++nParagIndex, std::move(pParag2)); } - m_pTxtBuf->Insert(nStart, lpText, nLength); + m_pTxtBuf->Insert(nStart, wsText); int32_t nTotalLineCount = 0; for (int32_t i = ParagPos.nParagIndex; i <= nParagIndex; i++) { pParag = m_ParagPtrArray[i].get(); diff --git a/xfa/fde/cfde_txtedtengine.h b/xfa/fde/cfde_txtedtengine.h index b22c36f5ec..40251159c0 100644 --- a/xfa/fde/cfde_txtedtengine.h +++ b/xfa/fde/cfde_txtedtengine.h @@ -130,7 +130,7 @@ class CFDE_TxtEdtEngine { bool IsSelect() const { return !m_SelRangePtrArr.empty(); } void Inner_DeleteRange(int32_t nStart, int32_t nCount); - void Inner_Insert(int32_t nStart, const wchar_t* lpText, int32_t nLength); + void Inner_Insert(int32_t nStart, const CFX_WideString& wsText); const FDE_TXTEDTPARAMS* GetParams() const { return &m_Param; } private: -- cgit v1.2.3