LibreOffice Module sw (master)  1
SwGrammarMarkUp.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <SwGrammarMarkUp.hxx>
21 
23 {
24 }
25 
27 {
28  SwWrongList* pClone = new SwGrammarMarkUp();
29  pClone->CopyFrom( *this );
30  return pClone;
31 }
32 
34 {
35  maSentence = static_cast<const SwGrammarMarkUp&>(rCopy).maSentence;
36  SwWrongList::CopyFrom( rCopy );
37 }
38 
39 void SwGrammarMarkUp::MoveGrammar( sal_Int32 nPos, sal_Int32 nDiff )
40 {
41  Move( nPos, nDiff );
42  if( maSentence.empty() )
43  return;
44  auto pIter = std::find_if(maSentence.begin(), maSentence.end(),
45  [nPos](const sal_Int32& rPos) { return rPos >= nPos; });
46  const sal_Int32 nEnd = nDiff < 0 ? nPos-nDiff : nPos;
47  while( pIter != maSentence.end() )
48  {
49  if( *pIter >= nEnd )
50  *pIter += nDiff;
51  else
52  *pIter = nPos;
53  ++pIter;
54  }
55 }
56 
58 {
59  SwGrammarMarkUp* pNew = static_cast<SwGrammarMarkUp*>(SplitList( nSplitPos ));
60  if( maSentence.empty() )
61  return pNew;
62  auto pIter = std::find_if(maSentence.begin(), maSentence.end(),
63  [nSplitPos](const sal_Int32& rPos) { return rPos >= nSplitPos; });
64  if( pIter != maSentence.begin() )
65  {
66  if( !pNew ) {
67  pNew = new SwGrammarMarkUp();
68  pNew->SetInvalid( 0, COMPLETE_STRING );
69  }
70  pNew->maSentence.insert( pNew->maSentence.begin(), maSentence.begin(), pIter );
71  maSentence.erase( maSentence.begin(), pIter );
72  }
73  return pNew;
74 }
75 
76 void SwGrammarMarkUp::JoinGrammarList( SwGrammarMarkUp* pNext, sal_Int32 nInsertPos )
77 {
78  JoinList( pNext, nInsertPos );
79  if (pNext)
80  {
81  if( pNext->maSentence.empty() )
82  return;
83  for( auto& rPos : pNext->maSentence )
84  {
85  rPos += nInsertPos;
86  }
87  maSentence.insert( maSentence.end(), pNext->maSentence.begin(), pNext->maSentence.end() );
88  }
89 }
90 
91 void SwGrammarMarkUp::ClearGrammarList( sal_Int32 nSentenceEnd )
92 {
93  if( COMPLETE_STRING == nSentenceEnd ) {
94  ClearList();
95  maSentence.clear();
96  Validate();
97  } else if( GetBeginInv() <= nSentenceEnd ) {
98  std::vector< sal_Int32 >::iterator pIter = maSentence.begin();
99  sal_Int32 nStart = 0;
100  while( pIter != maSentence.end() && *pIter < GetBeginInv() )
101  {
102  nStart = *pIter;
103  ++pIter;
104  }
105  auto pLast = std::find_if(pIter, maSentence.end(),
106  [nSentenceEnd](const sal_Int32& rPos) { return rPos > nSentenceEnd; });
107  maSentence.erase( pIter, pLast );
108  RemoveEntry( nStart, nSentenceEnd );
109  SetInvalid( nSentenceEnd + 1, COMPLETE_STRING );
110  }
111 }
112 
113 void SwGrammarMarkUp::setSentence( sal_Int32 nStart )
114 {
115  auto pIter = std::find_if(maSentence.begin(), maSentence.end(),
116  [nStart](const sal_Int32& rPos) { return rPos >= nStart; });
117  if( pIter == maSentence.end() || *pIter > nStart )
118  maSentence.insert( pIter, nStart );
119 }
120 
121 sal_Int32 SwGrammarMarkUp::getSentenceStart( sal_Int32 nPos )
122 {
123  if( maSentence.empty() )
124  return 0;
125  auto pIter = std::find_if(maSentence.begin(), maSentence.end(),
126  [nPos](const sal_Int32& rPos) { return rPos >= nPos; });
127  if( pIter != maSentence.begin() )
128  --pIter;
129  if( pIter != maSentence.end() && *pIter < nPos )
130  return *pIter;
131  return 0;
132 }
133 
134 sal_Int32 SwGrammarMarkUp::getSentenceEnd( sal_Int32 nPos )
135 {
136  if( maSentence.empty() )
137  return COMPLETE_STRING;
138  auto pIter = std::find_if(maSentence.begin(), maSentence.end(),
139  [nPos](const sal_Int32& rPos) { return rPos > nPos; });
140  if( pIter != maSentence.end() )
141  return *pIter;
142  return COMPLETE_STRING;
143 }
144 
145 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void Move(sal_Int32 nPos, sal_Int32 nDiff)
Change all values after the given position.
Definition: wrong.cxx:266
void RemoveEntry(sal_Int32 nBegin, sal_Int32 nEnd)
Definition: wrong.cxx:585
virtual void CopyFrom(const SwWrongList &rCopy)
Definition: wrong.cxx:72
void JoinList(SwWrongList *pNext, sal_Int32 nInsertPos)
Definition: wrong.cxx:489
virtual ~SwGrammarMarkUp() override
void MoveGrammar(sal_Int32 nPos, sal_Int32 nDiff)
SwWrongList * SplitList(sal_Int32 nSplitPos)
Definition: wrong.cxx:445
virtual SwWrongList * Clone() override
virtual void CopyFrom(const SwWrongList &rCopy) override
void JoinGrammarList(SwGrammarMarkUp *pNext, sal_Int32 nInsertPos)
SwGrammarMarkUp * SplitGrammarList(sal_Int32 nSplitPos)
sal_Int32 getSentenceStart(sal_Int32 nPos)
sal_Int32 GetBeginInv() const
Definition: wrong.hxx:292
void ClearList()
Definition: wrong.cxx:85
sal_Int32 getSentenceEnd(sal_Int32 nPos)
void ClearGrammarList(sal_Int32 nSentenceEnd=COMPLETE_STRING)
void setSentence(sal_Int32 nStart)
std::vector< sal_Int32 > maSentence
void Validate()
Definition: wrong.hxx:295
const sal_Int32 COMPLETE_STRING
Definition: swtypes.hxx:61
void SetInvalid(sal_Int32 nBegin, sal_Int32 nEnd)
Definition: wrong.cxx:253