LCOV - code coverage report
Current view: top level - libreoffice/svl/source/misc - lngmisc.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 51 53 96.2 %
Date: 2012-12-27 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          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             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : 
      21             : #include "svl/lngmisc.hxx"
      22             : 
      23             : #include <comphelper/string.hxx>
      24             : #include <rtl/ustrbuf.hxx>
      25             : #include <tools/debug.hxx>
      26             : 
      27             : namespace linguistic
      28             : {
      29           8 :     sal_Int32 GetNumControlChars(const rtl::OUString &rTxt)
      30             :     {
      31           8 :         sal_Int32 nCnt = 0;
      32         100 :         for (sal_Int32 i = 0; i < rTxt.getLength(); ++i)
      33          92 :             if (IsControlChar(rTxt[i]))
      34          66 :                 ++nCnt;
      35           8 :         return nCnt;
      36             :     }
      37             : 
      38           4 :     bool RemoveHyphens(rtl::OUString &rTxt)
      39             :     {
      40           4 :         sal_Int32 n = rTxt.getLength();
      41           4 :         rTxt = comphelper::string::remove(rTxt, SVT_SOFT_HYPHEN);
      42           4 :         rTxt = comphelper::string::remove(rTxt, SVT_HARD_HYPHEN);
      43           4 :         return n != rTxt.getLength();
      44             :     }
      45             : 
      46           4 :     bool RemoveControlChars(rtl::OUString &rTxt)
      47             :     {
      48           4 :         sal_Int32 nSize = rTxt.getLength() - GetNumControlChars(rTxt);
      49           4 :         if(nSize == rTxt.getLength())
      50           2 :             return false;
      51             : 
      52           2 :         rtl::OUStringBuffer aBuf(nSize);
      53           2 :         aBuf.setLength(nSize);
      54          44 :         for (sal_Int32 i = 0, j = 0; i < rTxt.getLength() && j < nSize; ++i)
      55          42 :             if (!IsControlChar(rTxt[i]))
      56           9 :                 aBuf[j++] = rTxt[i];
      57             : 
      58           2 :         rTxt = aBuf.makeStringAndClear();
      59             :         DBG_ASSERT(rTxt.getLength() == nSize, "GetNumControlChars returned a different number of control characters than were actually removed.");
      60             : 
      61           2 :         return true;
      62             :     }
      63             : 
      64           4 :     bool ReplaceControlChars(rtl::OUString &rTxt)
      65             :     {
      66             :         // non breaking field character
      67             :         static const sal_Char CH_TXTATR_INWORD = static_cast<sal_Char>(0x02);
      68             : 
      69             :         // the resulting string looks like this:
      70             :         // 1. non breaking field characters get removed
      71             :         // 2. remaining control characters will be replaced by ' '
      72             : 
      73           4 :         if (GetNumControlChars(rTxt) == 0)
      74           2 :             return false;
      75             : 
      76           2 :         sal_Int32 n = rTxt.getLength();
      77             : 
      78           2 :         rtl::OUStringBuffer aBuf(n);
      79           2 :         aBuf.setLength(n);
      80             : 
      81           2 :         sal_Int32 j = 0;
      82          44 :         for (sal_Int32 i = 0; i < n && j < n; ++i)
      83             :         {
      84          42 :             if (CH_TXTATR_INWORD == rTxt[i])
      85           1 :                 continue;
      86             : 
      87          41 :             aBuf[j++] = IsControlChar(rTxt[i]) ? ' ' : rTxt[i];
      88             :         }
      89             : 
      90           2 :         aBuf.setLength(j);
      91           2 :         rTxt = aBuf.makeStringAndClear();
      92             : 
      93           2 :         return true;
      94             :     }
      95             : 
      96           8 :     ::rtl::OUString GetThesaurusReplaceText(const ::rtl::OUString &rText)
      97             :     {
      98             :         // The strings for synonyms returned by the thesaurus sometimes have some
      99             :         // explanation text put in between '(' and ')' or a trailing '*'.
     100             :         // These parts should not be put in the ReplaceEdit Text that may get
     101             :         // inserted into the document. Thus we strip them from the text.
     102             : 
     103           8 :         ::rtl::OUString aText(rText);
     104             : 
     105           8 :         sal_Int32 nPos = aText.indexOf('(');
     106           8 :         while (nPos >= 0)
     107             :         {
     108           4 :             sal_Int32 nEnd = aText.indexOf(')', nPos);
     109           4 :             if (nEnd >= 0)
     110             :             {
     111           4 :                 ::rtl::OUStringBuffer aTextBuf(aText);
     112           4 :                 aTextBuf.remove(nPos, nEnd - nPos + 1);
     113           4 :                 aText = aTextBuf.makeStringAndClear();
     114             :             }
     115             :             else
     116           0 :                 break;
     117           4 :             nPos = aText.indexOf('(');
     118             :         }
     119             : 
     120           8 :         nPos = aText.indexOf('*');
     121           8 :         if(nPos == 0)
     122           0 :             return ::rtl::OUString();
     123           8 :         else if(nPos > 0)
     124           5 :             aText = aText.copy(0, nPos);
     125             : 
     126             :         // remove any possible remaining ' ' that may confuse the thesaurus
     127             :         // when it gets called with the text
     128           8 :         return comphelper::string::strip(aText, ' ');
     129             :     }
     130             : } // namespace linguistic
     131             : 
     132             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10