unique_ptr->optional in MakeRegion

Change-Id: I7295e337ca45761dc986c2dc660ea9a7432280b3
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/138151
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
This commit is contained in:
Noel Grandin 2022-08-08 19:43:21 +02:00 committed by Noel Grandin
parent b0ec87af1b
commit 8fa80d7439
5 changed files with 70 additions and 70 deletions

View file

@ -918,7 +918,8 @@ bool FindAttrImpl(SwPaM & rSearchPam,
bool bCharAttr = isCHRATR(nWhich) || isTXTATR(nWhich);
assert(isTXTATR(nWhich)); // sw_redlinehide: only works for non-formatting hints such as needed in UpdateFields; use FindAttrsImpl for others
std::unique_ptr<SwPaM> pPam(sw::MakeRegion(fnMove, rRegion));
std::optional<SwPaM> oPam;
sw::MakeRegion(fnMove, rRegion, oPam);
bool bFound = false;
bool bFirst = true;
@ -927,18 +928,18 @@ bool FindAttrImpl(SwPaM & rSearchPam,
// if at beginning/end then move it out of the node
if( bSrchForward
? pPam->GetPoint()->GetContentIndex() == pPam->GetContentNode()->Len()
: !pPam->GetPoint()->GetContentIndex() )
? oPam->GetPoint()->GetContentIndex() == oPam->GetContentNode()->Len()
: !oPam->GetPoint()->GetContentIndex() )
{
if( !(*fnMove.fnNds)( &pPam->GetPoint()->nNode, false ))
if( !(*fnMove.fnNds)( &oPam->GetPoint()->nNode, false ))
{
return false;
}
SwContentNode *pNd = pPam->GetContentNode();
pPam->GetPoint()->nContent.Assign( pNd, bSrchForward ? 0 : pNd->Len() );
SwContentNode *pNd = oPam->GetContentNode();
oPam->GetPoint()->nContent.Assign( pNd, bSrchForward ? 0 : pNd->Len() );
}
while (nullptr != (pNode = ::GetNode(*pPam, bFirst, fnMove, bInReadOnly, pLayout)))
while (nullptr != (pNode = ::GetNode(*oPam, bFirst, fnMove, bInReadOnly, pLayout)))
{
if( bCharAttr )
{
@ -960,9 +961,9 @@ bool FindAttrImpl(SwPaM & rSearchPam,
pAttr = iter.NextAttr(&pAttrNode);
}
while (pAttr
&& (pAttrNode->GetIndex() < pPam->GetPoint()->GetNodeIndex()
|| (pAttrNode->GetIndex() == pPam->GetPoint()->GetNodeIndex()
&& pAttr->GetStart() < pPam->GetPoint()->GetContentIndex())
&& (pAttrNode->GetIndex() < oPam->GetPoint()->GetNodeIndex()
|| (pAttrNode->GetIndex() == oPam->GetPoint()->GetNodeIndex()
&& pAttr->GetStart() < oPam->GetPoint()->GetContentIndex())
|| pAttr->Which() != nWhich));
}
else
@ -973,22 +974,22 @@ bool FindAttrImpl(SwPaM & rSearchPam,
pAttr = iter.PrevAttr(&pAttrNode);
}
while (pAttr
&& (pPam->GetPoint()->GetNodeIndex() < pAttrNode->GetIndex()
|| (pPam->GetPoint()->GetNodeIndex() == pAttrNode->GetIndex()
&& pPam->GetPoint()->GetContentIndex() <= pAttr->GetStart())
&& (oPam->GetPoint()->GetNodeIndex() < pAttrNode->GetIndex()
|| (oPam->GetPoint()->GetNodeIndex() == pAttrNode->GetIndex()
&& oPam->GetPoint()->GetContentIndex() <= pAttr->GetStart())
|| pAttr->Which() != nWhich));
}
if (pAttr)
{
assert(pAttrNode);
pPam->GetPoint()->nNode = *pAttrNode;
lcl_SetAttrPam(*pPam, pAttr->GetStart(), pAttr->End(), bSrchForward);
oPam->GetPoint()->nNode = *pAttrNode;
lcl_SetAttrPam(*oPam, pAttr->GetStart(), pAttr->End(), bSrchForward);
bFound = true;
break;
}
}
else if (!pLayout && pNode->GetTextNode()->HasHints() &&
lcl_SearchAttr(*pNode->GetTextNode(), *pPam, rAttr, fnMove))
lcl_SearchAttr(*pNode->GetTextNode(), *oPam, rAttr, fnMove))
{
bFound = true;
}
@ -996,8 +997,8 @@ bool FindAttrImpl(SwPaM & rSearchPam,
{
// set to the values of the attribute
rSearchPam.SetMark();
*rSearchPam.GetPoint() = *pPam->GetPoint();
*rSearchPam.GetMark() = *pPam->GetMark();
*rSearchPam.GetPoint() = *oPam->GetPoint();
*rSearchPam.GetMark() = *oPam->GetMark();
break;
}
else if (isTXTATR(nWhich))
@ -1044,7 +1045,8 @@ static bool FindAttrsImpl(SwPaM & rSearchPam,
const SwPaM & rRegion, bool bInReadOnly, bool bMoveFirst,
SwRootFrame const*const pLayout)
{
std::unique_ptr<SwPaM> pPam(sw::MakeRegion(fnMove, rRegion));
std::optional<SwPaM> oPam;
sw::MakeRegion(fnMove, rRegion, oPam);
bool bFound = false;
bool bFirst = true;
@ -1064,18 +1066,18 @@ static bool FindAttrsImpl(SwPaM & rSearchPam,
// if at beginning/end then move it out of the node
if( bMoveFirst &&
( bSrchForward
? pPam->GetPoint()->GetContentIndex() == pPam->GetContentNode()->Len()
: !pPam->GetPoint()->GetContentIndex() ) )
? oPam->GetPoint()->GetContentIndex() == oPam->GetContentNode()->Len()
: !oPam->GetPoint()->GetContentIndex() ) )
{
if( !(*fnMove.fnNds)( &pPam->GetPoint()->nNode, false ))
if( !(*fnMove.fnNds)( &oPam->GetPoint()->nNode, false ))
{
return false;
}
SwContentNode *pNd = pPam->GetContentNode();
pPam->GetPoint()->nContent.Assign( pNd, bSrchForward ? 0 : pNd->Len() );
SwContentNode *pNd = oPam->GetContentNode();
oPam->GetPoint()->nContent.Assign( pNd, bSrchForward ? 0 : pNd->Len() );
}
while (nullptr != (pNode = ::GetNode(*pPam, bFirst, fnMove, bInReadOnly, pLayout)))
while (nullptr != (pNode = ::GetNode(*oPam, bFirst, fnMove, bInReadOnly, pLayout)))
{
SwTextFrame const*const pFrame(pLayout && pNode->IsTextNode()
? static_cast<SwTextFrame const*>(pNode->getLayoutFrame(pLayout))
@ -1100,8 +1102,8 @@ static bool FindAttrsImpl(SwPaM & rSearchPam,
sw::MergedPara const*const pMergedPara(pFrame ? pFrame->GetMergedPara() : nullptr);
if (pMergedPara)
{
SwPosition const& rStart(*pPam->Start());
SwPosition const& rEnd(*pPam->End());
SwPosition const& rStart(*oPam->Start());
SwPosition const& rEnd(*oPam->End());
// no extents? fall back to searching index 0 of propsnode
// to find its node items
if (pMergedPara->extents.empty())
@ -1113,7 +1115,7 @@ static bool FindAttrsImpl(SwPaM & rSearchPam,
bFound = (*fnSearch)(*pNode->GetTextNode(), aCmpArr, tmp);
if (bFound)
{
*pPam = tmp;
*oPam = tmp;
}
}
}
@ -1153,7 +1155,7 @@ static bool FindAttrsImpl(SwPaM & rSearchPam,
bFound = (*fnSearch)(*rExtent.pNode, aCmpArr, tmp);
if (bFound)
{
*pPam = tmp;
*oPam = tmp;
break;
}
}
@ -1161,14 +1163,14 @@ static bool FindAttrsImpl(SwPaM & rSearchPam,
}
else
{
bFound = (*fnSearch)(*pNode->GetTextNode(), aCmpArr, *pPam);
bFound = (*fnSearch)(*pNode->GetTextNode(), aCmpArr, *oPam);
}
if (bFound)
{
// set to the values of the attribute
rSearchPam.SetMark();
*rSearchPam.GetPoint() = *pPam->GetPoint();
*rSearchPam.GetMark() = *pPam->GetMark();
*rSearchPam.GetPoint() = *oPam->GetPoint();
*rSearchPam.GetMark() = *oPam->GetMark();
break;
}
continue; // text attribute
@ -1192,14 +1194,14 @@ static bool FindAttrsImpl(SwPaM & rSearchPam,
// BACKWARD: SPoint at the beginning, GetMark at the end of the node
if (pFrame)
{
*rSearchPam.GetPoint() = *pPam->GetPoint();
*rSearchPam.GetPoint() = *oPam->GetPoint();
rSearchPam.SetMark();
*rSearchPam.GetMark() = pFrame->MapViewToModelPos(
TextFrameIndex(bSrchForward ? pFrame->GetText().getLength() : 0));
}
else
{
*rSearchPam.GetPoint() = *pPam->GetPoint();
*rSearchPam.GetPoint() = *oPam->GetPoint();
rSearchPam.SetMark();
if (bSrchForward)
{

View file

@ -32,24 +32,25 @@ bool FindFormatImpl(SwPaM & rSearchPam,
{
bool bFound = false;
const bool bSrchForward = &fnMove == &fnMoveForward;
std::unique_ptr<SwPaM> pPam(MakeRegion( fnMove, rRegion ));
std::optional<SwPaM> oPam;
MakeRegion( fnMove, rRegion, oPam );
// if at beginning/end then move it out of the node
if( bSrchForward
? pPam->GetPoint()->GetContentIndex() == pPam->GetContentNode()->Len()
: !pPam->GetPoint()->GetContentIndex() )
? oPam->GetPoint()->GetContentIndex() == oPam->GetContentNode()->Len()
: !oPam->GetPoint()->GetContentIndex() )
{
if( !(*fnMove.fnNds)( &pPam->GetPoint()->nNode, false ))
if( !(*fnMove.fnNds)( &oPam->GetPoint()->nNode, false ))
{
return false;
}
SwContentNode *pNd = pPam->GetPoint()->GetNode().GetContentNode();
pPam->GetPoint()->nContent.Assign( pNd, bSrchForward ? 0 : pNd->Len() );
SwContentNode *pNd = oPam->GetPoint()->GetNode().GetContentNode();
oPam->GetPoint()->nContent.Assign( pNd, bSrchForward ? 0 : pNd->Len() );
}
bool bFirst = true;
SwContentNode* pNode;
while (nullptr != (pNode = ::GetNode(*pPam, bFirst, fnMove, bInReadOnly, pLayout)))
while (nullptr != (pNode = ::GetNode(*oPam, bFirst, fnMove, bInReadOnly, pLayout)))
{
SwTextFrame const*const pFrame(pLayout && pNode->IsTextNode()
? static_cast<SwTextFrame const*>(pNode->getLayoutFrame(pLayout))
@ -68,14 +69,14 @@ bool FindFormatImpl(SwPaM & rSearchPam,
// always: incl. start and incl. end
if (pFrame)
{
*rSearchPam.GetPoint() = *pPam->GetPoint();
*rSearchPam.GetPoint() = *oPam->GetPoint();
rSearchPam.SetMark();
*rSearchPam.GetMark() = pFrame->MapViewToModelPos(
TextFrameIndex(bSrchForward ? pFrame->GetText().getLength() : 0));
}
else
{
*rSearchPam.GetPoint() = *pPam->GetPoint();
*rSearchPam.GetPoint() = *oPam->GetPoint();
rSearchPam.SetMark();
pNode->MakeEndIndex( &rSearchPam.GetPoint()->nContent );
rSearchPam.GetMark()->nContent = 0;

View file

@ -376,10 +376,11 @@ bool FindTextImpl(SwPaM & rSearchPam,
if( rSearchOpt.searchString.isEmpty() )
return false;
std::unique_ptr<SwPaM> pPam = sw::MakeRegion(fnMove, rRegion);
std::optional<SwPaM> oPam;
sw::MakeRegion(fnMove, rRegion, oPam);
const bool bSrchForward = &fnMove == &fnMoveForward;
SwNodeIndex& rNdIdx = pPam->GetPoint()->nNode;
SwContentIndex& rContentIdx = pPam->GetPoint()->nContent;
SwNodeIndex& rNdIdx = oPam->GetPoint()->nNode;
SwContentIndex& rContentIdx = oPam->GetPoint()->nContent;
// If bFound is true then the string was found and is between nStart and nEnd
bool bFound = false;
@ -401,7 +402,7 @@ bool FindTextImpl(SwPaM & rSearchPam,
}
// LanguageType eLastLang = 0;
while (nullptr != (pNode = ::GetNode(*pPam, bFirst, fnMove, bInReadOnly, pLayout)))
while (nullptr != (pNode = ::GetNode(*oPam, bFirst, fnMove, bInReadOnly, pLayout)))
{
if( pNode->IsTextNode() )
{
@ -421,16 +422,16 @@ bool FindTextImpl(SwPaM & rSearchPam,
}
AmbiguousIndex nEnd;
if (pLayout
? FrameContainsNode(*pFrame, pPam->GetMark()->GetNodeIndex())
: rNdIdx == pPam->GetMark()->nNode)
? FrameContainsNode(*pFrame, oPam->GetMark()->GetNodeIndex())
: rNdIdx == oPam->GetMark()->nNode)
{
if (pLayout)
{
nEnd.SetFrameIndex(pFrame->MapModelToViewPos(*pPam->GetMark()));
nEnd.SetFrameIndex(pFrame->MapModelToViewPos(*oPam->GetMark()));
}
else
{
nEnd.SetModelIndex(pPam->GetMark()->GetContentIndex());
nEnd.SetModelIndex(oPam->GetMark()->GetContentIndex());
}
}
else
@ -454,7 +455,7 @@ bool FindTextImpl(SwPaM & rSearchPam,
AmbiguousIndex nStart;
if (pLayout)
{
nStart.SetFrameIndex(pFrame->MapModelToViewPos(*pPam->GetPoint()));
nStart.SetFrameIndex(pFrame->MapModelToViewPos(*oPam->GetPoint()));
}
else
{
@ -679,7 +680,7 @@ bool FindTextImpl(SwPaM & rSearchPam,
bRegSearch, bChkEmptyPara, bChkParaEnd,
nStartInside, nEndInside, nTextLen,
pNode->GetTextNode(), pFrame, pLayout,
pPam.get() );
oPam ? &*oPam : nullptr );
if ( bFound )
break;
else
@ -712,7 +713,7 @@ bool FindTextImpl(SwPaM & rSearchPam,
bRegSearch, bChkEmptyPara, bChkParaEnd,
nStart, nEnd, nTextLen,
pNode->GetTextNode(), pFrame, pLayout,
pPam.get() );
oPam ? &*oPam : nullptr );
}
if (bFound)
break;

View file

@ -629,22 +629,17 @@ namespace sw {
@param fnMove Contains information if beginning or end of document.
@param pOrigRg The given region.
@return Newly created range, in Ring with parameter pOrigRg.
@param rPam returns newly created range, in Ring with parameter pOrigRg.
*/
std::unique_ptr<SwPaM> MakeRegion(SwMoveFnCollection const & fnMove,
const SwPaM & rOrigRg)
void MakeRegion(SwMoveFnCollection const & fnMove,
const SwPaM & rOrigRg, std::optional<SwPaM>& rPam)
{
std::unique_ptr<SwPaM> pPam;
{
pPam.reset(new SwPaM(rOrigRg, const_cast<SwPaM*>(&rOrigRg))); // given search range
// make sure that SPoint is on the "real" start position
// FORWARD: SPoint always smaller than GetMark
// BACKWARD: SPoint always bigger than GetMark
if( (pPam->GetMark()->*fnMove.fnCmpOp)( *pPam->GetPoint() ) )
pPam->Exchange();
}
return pPam;
rPam.emplace(rOrigRg, const_cast<SwPaM*>(&rOrigRg)); // given search range
// make sure that SPoint is on the "real" start position
// FORWARD: SPoint always smaller than GetMark
// BACKWARD: SPoint always bigger than GetMark
if( (rPam->GetMark()->*fnMove.fnCmpOp)( *rPam->GetPoint() ) )
rPam->Exchange();
}
} // namespace sw

View file

@ -23,6 +23,7 @@
#include <unotools/textsearch.hxx>
#include <memory>
#include <optional>
class SwpHints;
struct SwPosition;
@ -87,8 +88,8 @@ SwContentNode* GetNode(SwPaM&, bool&, SwMoveFnCollection const &,
namespace sw {
std::unique_ptr<SwPaM> MakeRegion(SwMoveFnCollection const & fnMove,
const SwPaM & rOrigRg);
void MakeRegion(SwMoveFnCollection const & fnMove,
const SwPaM & rOrigRg, std::optional<SwPaM>& rDestinaton);
/// Search.
bool FindTextImpl(SwPaM & rSearchPam,