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
17namespace sw {
18
19ToxWhitespaceStripper::ToxWhitespaceStripper(std::u16string_view inputString)
20{
21 OUStringBuffer buffer;
22
23 bool lastCharacterWasWhitespace = false;
24 for (size_t pos = 0; pos < inputString.size(); ++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.makeStringAndClear();
46}
47
48
49sal_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}
std::vector< sal_Int32 > mNewPositions
ToxWhitespaceStripper(std::u16string_view)
sal_Int32 GetPositionInStrippedString(sal_Int32 pos) const
#define SAL_WARN(area, stream)
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
Dialog to specify the properties of date form field.
sal_uInt16 sal_Unicode
size_t pos