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