1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 #include <ToxWhitespaceStripper.hxx>
12 #include <o3tl/safeint.hxx>
13 #include <rtl/ustrbuf.hxx>
14 #include <sal/log.hxx>
19 ToxWhitespaceStripper::ToxWhitespaceStripper(std::u16string_view inputString
)
21 OUStringBuffer buffer
;
23 bool lastCharacterWasWhitespace
= false;
24 for (size_t pos
= 0; pos
< inputString
.size(); ++pos
) {
25 sal_Unicode cur
= inputString
[pos
];
27 if (cur
== ' ' || cur
== '\n' || cur
== '\t') {
28 // merge consecutive whitespaces (and translate them to spaces)
29 if (!lastCharacterWasWhitespace
) {
32 lastCharacterWasWhitespace
= true;
36 lastCharacterWasWhitespace
= false;
38 mNewPositions
.push_back(buffer
.getLength()-1);
40 // strip the last whitespace (if there was one)
41 if (lastCharacterWasWhitespace
) {
42 buffer
.truncate(buffer
.getLength() - 1);
44 mNewPositions
.push_back(buffer
.getLength());
45 mStripped
= buffer
.makeStringAndClear();
50 ToxWhitespaceStripper::GetPositionInStrippedString(sal_Int32 pos
) const
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();
59 return mNewPositions
.at(pos
);