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 <boost/numeric/conversion/cast.hpp>
      14             : 
      15             : namespace sw {
      16             : 
      17          30 : ToxWhitespaceStripper::ToxWhitespaceStripper(const OUString& inputString)
      18             : {
      19          30 :     OUStringBuffer buffer;
      20             : 
      21          30 :     bool lastCharacterWasWhitespace = false;
      22         242 :     for (sal_Int32 pos = 0; pos < inputString.getLength(); ++pos) {
      23         212 :         sal_Unicode cur = inputString[pos];
      24             : 
      25         212 :         if (cur == ' ' || cur == '\n') {
      26             :             // merge consecutive whitespaces (and translate them to spaces)
      27          48 :             if (!lastCharacterWasWhitespace) {
      28          32 :                 buffer.append(' ');
      29             :             }
      30          48 :             lastCharacterWasWhitespace = true;
      31             :         }
      32             :         else {
      33         164 :             buffer.append(cur);
      34         164 :             lastCharacterWasWhitespace = false;
      35             :         }
      36         212 :         mNewPositions.push_back(buffer.getLength()-1);
      37             :     }
      38             :     // Add one position if the position after the stripped string is requested, e.g., for attributes which
      39             :     // extend beyond the string.
      40          30 :     mNewPositions.push_back(buffer.getLength());
      41             :     // strip the last whitespace (if there was one)
      42          30 :     if (lastCharacterWasWhitespace) {
      43          12 :         buffer.truncate(buffer.getLength() - 1);
      44             :     }
      45          30 :     mStripped = buffer.getStr();
      46          30 : }
      47             : 
      48             : 
      49             : sal_Int32
      50          54 : ToxWhitespaceStripper::GetPositionInStrippedString(sal_Int32 pos) const
      51             : {
      52          54 :     size_t upos = boost::numeric_cast<size_t>(pos);
      53          54 :     return mNewPositions.at(upos);
      54             : }
      55             : 
      56             : 
      57             : }
       |