Line data Source code
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 :
10 : #include "ToxWhitespaceStripper.hxx"
11 :
12 : #include "rtl/ustrbuf.hxx"
13 : #include "sal/log.hxx"
14 :
15 : #include <boost/numeric/conversion/cast.hpp>
16 :
17 : namespace sw {
18 :
19 16 : ToxWhitespaceStripper::ToxWhitespaceStripper(const OUString& inputString)
20 : {
21 16 : OUStringBuffer buffer;
22 :
23 16 : bool lastCharacterWasWhitespace = false;
24 127 : for (sal_Int32 pos = 0; pos < inputString.getLength(); ++pos) {
25 111 : sal_Unicode cur = inputString[pos];
26 :
27 111 : if (cur == ' ' || cur == '\n') {
28 : // merge consecutive whitespaces (and translate them to spaces)
29 26 : if (!lastCharacterWasWhitespace) {
30 17 : buffer.append(' ');
31 : }
32 26 : lastCharacterWasWhitespace = true;
33 : }
34 : else {
35 85 : buffer.append(cur);
36 85 : lastCharacterWasWhitespace = false;
37 : }
38 111 : mNewPositions.push_back(buffer.getLength()-1);
39 : }
40 : // Add one position if the position after the stripped string is requested, e.g., for attributes which
41 : // extend beyond the string.
42 16 : mNewPositions.push_back(buffer.getLength());
43 : // strip the last whitespace (if there was one)
44 16 : if (lastCharacterWasWhitespace) {
45 6 : buffer.truncate(buffer.getLength() - 1);
46 : }
47 16 : mStripped = buffer.getStr();
48 16 : }
49 :
50 :
51 : sal_Int32
52 28 : ToxWhitespaceStripper::GetPositionInStrippedString(sal_Int32 pos) const
53 : {
54 28 : size_t upos = boost::numeric_cast<size_t>(pos);
55 28 : if (upos >= mNewPositions.size()) {
56 : SAL_WARN("sw.core", "Requested position of TOX entry text which does not exist. "
57 : "Maybe the formatting hint is corrupt?");
58 1 : return mNewPositions.back();
59 : }
60 27 : return mNewPositions.at(upos);
61 : }
62 :
63 :
64 : }
|