LibreOffice Module sw (master)  1
ToxWhitespaceStripper.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 
11 
12 #include <o3tl/safeint.hxx>
13 #include <rtl/ustrbuf.hxx>
14 #include <sal/log.hxx>
15 
16 
17 namespace sw {
18 
20 {
21  OUStringBuffer buffer;
22 
23  bool lastCharacterWasWhitespace = false;
24  for (sal_Int32 pos = 0; pos < inputString.getLength(); ++pos) {
25  sal_Unicode cur = inputString[pos];
26 
27  if (cur == ' ' || cur == '\n' || cur == '\t') {
28  // merge consecutive whitespaces (and translate them to spaces)
29  if (!lastCharacterWasWhitespace) {
30  buffer.append(' ');
31  }
32  lastCharacterWasWhitespace = true;
33  }
34  else {
35  buffer.append(cur);
36  lastCharacterWasWhitespace = false;
37  }
38  mNewPositions.push_back(buffer.getLength()-1);
39  }
40  // strip the last whitespace (if there was one)
41  if (lastCharacterWasWhitespace) {
42  buffer.truncate(buffer.getLength() - 1);
43  }
44  mNewPositions.push_back(buffer.getLength());
45  mStripped = buffer.getStr();
46 }
47 
48 
49 sal_Int32
51 {
52  assert(0 <= pos);
53  if (o3tl::make_unsigned(pos) >= mNewPositions.size()) {
54  // TODO probably this should assert, not just warn?
55  SAL_WARN("sw.core", "Requested position of TOX entry text which does not exist. "
56  "Maybe the formatting hint is corrupt?");
57  return mNewPositions.back();
58  }
59  return mNewPositions.at(pos);
60 }
61 
62 
63 }
Dialog to specify the properties of date form field.
sal_uInt16 sal_Unicode
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
size_t pos
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
ToxWhitespaceStripper(const OUString &)
sal_Int32 GetPositionInStrippedString(sal_Int32 pos) const
#define SAL_WARN(area, stream)
std::vector< sal_Int32 > mNewPositions