LCOV - code coverage report
Current view: top level - unotools/source/i18n - textsearch.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 85 164 51.8 %
Date: 2014-04-11 Functions: 12 16 75.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             : #include <i18nlangtag/languagetag.hxx>
      21             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      22             : #include <com/sun/star/util/TextSearch.hpp>
      23             : #include <com/sun/star/util/SearchFlags.hpp>
      24             : #include <com/sun/star/i18n/TransliterationModules.hpp>
      25             : #include <unotools/charclass.hxx>
      26             : #include <comphelper/processfactory.hxx>
      27             : #include <unotools/textsearch.hxx>
      28             : #include <rtl/instance.hxx>
      29             : #include <rtl/ustrbuf.hxx>
      30             : 
      31             : using namespace ::com::sun::star::util;
      32             : using namespace ::com::sun::star::uno;
      33             : using namespace ::com::sun::star::lang;
      34             : 
      35             : namespace utl
      36             : {
      37             : 
      38           6 : SearchParam::SearchParam( const OUString &rText,
      39             :                                 SearchType eType,
      40             :                                 bool bCaseSensitive,
      41             :                                 bool bWrdOnly,
      42           6 :                                 bool bSearchInSel )
      43             : {
      44           6 :     sSrchStr        = rText;
      45           6 :     m_eSrchType     = eType;
      46             : 
      47           6 :     m_bWordOnly     = bWrdOnly;
      48           6 :     m_bSrchInSel    = bSearchInSel;
      49           6 :     m_bCaseSense    = bCaseSensitive;
      50             : 
      51           6 :     nTransliterationFlags = 0;
      52             : 
      53             :     // Parameters for weighted Levenshtein distance
      54           6 :     bLEV_Relaxed    = true;
      55           6 :     nLEV_OtherX     = 2;
      56           6 :     nLEV_ShorterY   = 1;
      57           6 :     nLEV_LongerZ    = 3;
      58           6 : }
      59             : 
      60           0 : SearchParam::SearchParam( const SearchParam& rParam )
      61             : {
      62           0 :     sSrchStr        = rParam.sSrchStr;
      63           0 :     sReplaceStr     = rParam.sReplaceStr;
      64           0 :     m_eSrchType     = rParam.m_eSrchType;
      65             : 
      66           0 :     m_bWordOnly     = rParam.m_bWordOnly;
      67           0 :     m_bSrchInSel    = rParam.m_bSrchInSel;
      68           0 :     m_bCaseSense    = rParam.m_bCaseSense;
      69             : 
      70           0 :     bLEV_Relaxed    = rParam.bLEV_Relaxed;
      71           0 :     nLEV_OtherX     = rParam.nLEV_OtherX;
      72           0 :     nLEV_ShorterY   = rParam.nLEV_ShorterY;
      73           0 :     nLEV_LongerZ    = rParam.nLEV_LongerZ;
      74             : 
      75           0 :     nTransliterationFlags = rParam.nTransliterationFlags;
      76           0 : }
      77             : 
      78           6 : SearchParam::~SearchParam() {}
      79             : 
      80          87 : static bool lcl_Equals( const SearchOptions& rSO1, const SearchOptions& rSO2 )
      81             : {
      82         171 :     return rSO1.algorithmType == rSO2.algorithmType &&
      83         162 :         rSO1.searchFlag == rSO2.searchFlag &&
      84         117 :         rSO1.searchString.equals(rSO2.searchString) &&
      85          75 :         rSO1.replaceString.equals(rSO2.replaceString) &&
      86          72 :         rSO1.changedChars == rSO2.changedChars &&
      87          72 :         rSO1.deletedChars == rSO2.deletedChars &&
      88          72 :         rSO1.insertedChars == rSO2.insertedChars &&
      89          72 :         rSO1.Locale.Language == rSO2.Locale.Language &&
      90          72 :         rSO1.Locale.Country == rSO2.Locale.Country &&
      91         159 :         rSO1.Locale.Variant == rSO2.Locale.Variant &&
      92         123 :         rSO1.transliterateFlags == rSO2.transliterateFlags;
      93             : }
      94             : 
      95             : namespace
      96             : {
      97          14 :     struct CachedTextSearch
      98             :     {
      99             :         ::osl::Mutex mutex;
     100             :         ::com::sun::star::util::SearchOptions Options;
     101             :         ::com::sun::star::uno::Reference< ::com::sun::star::util::XTextSearch > xTextSearch;
     102             :     };
     103             : 
     104             :     struct theCachedTextSearch
     105             :         : public rtl::Static< CachedTextSearch, theCachedTextSearch > {};
     106             : }
     107             : 
     108          87 : Reference<XTextSearch> TextSearch::getXTextSearch( const SearchOptions& rPara )
     109             : {
     110          87 :     CachedTextSearch &rCache = theCachedTextSearch::get();
     111             : 
     112          87 :     osl::MutexGuard aGuard(rCache.mutex);
     113             : 
     114          87 :     if ( lcl_Equals(rCache.Options, rPara) )
     115          36 :         return rCache.xTextSearch;
     116             : 
     117         102 :     Reference< XComponentContext > xContext = ::comphelper::getProcessComponentContext();
     118          51 :     rCache.xTextSearch.set( ::TextSearch::create(xContext) );
     119          51 :     rCache.xTextSearch->setOptions( rPara );
     120          51 :     rCache.Options = rPara;
     121             : 
     122         138 :     return rCache.xTextSearch;
     123             : }
     124             : 
     125           0 : TextSearch::TextSearch(const SearchParam & rParam, LanguageType eLang )
     126             : {
     127           0 :     if( LANGUAGE_NONE == eLang )
     128           0 :         eLang = LANGUAGE_SYSTEM;
     129           0 :     ::com::sun::star::lang::Locale aLocale( LanguageTag::convertToLocale( eLang ) );
     130             : 
     131           0 :     Init( rParam, aLocale);
     132           0 : }
     133             : 
     134           6 : TextSearch::TextSearch(const SearchParam & rParam, const CharClass& rCClass )
     135             : {
     136           6 :     Init( rParam, rCClass.getLanguageTag().getLocale() );
     137           6 : }
     138             : 
     139          81 : TextSearch::TextSearch( const SearchOptions& rPara )
     140             : {
     141          81 :     xTextSearch = getXTextSearch( rPara );
     142          81 : }
     143             : 
     144           6 : void TextSearch::Init( const SearchParam & rParam,
     145             :                         const ::com::sun::star::lang::Locale& rLocale )
     146             : {
     147             :     // convert SearchParam to the UNO SearchOptions
     148           6 :     SearchOptions aSOpt;
     149             : 
     150           6 :     switch( rParam.GetSrchType() )
     151             :     {
     152             :     case SearchParam::SRCH_REGEXP:
     153           5 :         aSOpt.algorithmType = SearchAlgorithms_REGEXP;
     154           5 :         if( rParam.IsSrchInSelection() )
     155             :             aSOpt.searchFlag |= SearchFlags::REG_NOT_BEGINOFLINE |
     156           0 :                                 SearchFlags::REG_NOT_ENDOFLINE;
     157           5 :         break;
     158             : 
     159             :     case SearchParam::SRCH_LEVDIST:
     160           0 :         aSOpt.algorithmType = SearchAlgorithms_APPROXIMATE;
     161           0 :         aSOpt.changedChars = rParam.GetLEVOther();
     162           0 :         aSOpt.deletedChars = rParam.GetLEVLonger();
     163           0 :         aSOpt.insertedChars = rParam.GetLEVShorter();
     164           0 :         if( rParam.IsSrchRelaxed() )
     165           0 :             aSOpt.searchFlag |= SearchFlags::LEV_RELAXED;
     166           0 :         break;
     167             : 
     168             : //  case SearchParam::SRCH_NORMAL:
     169             :     default:
     170           1 :         aSOpt.algorithmType = SearchAlgorithms_ABSOLUTE;
     171           1 :         if( rParam.IsSrchWordOnly() )
     172           0 :             aSOpt.searchFlag |= SearchFlags::NORM_WORD_ONLY;
     173           1 :         break;
     174             :     }
     175           6 :     aSOpt.searchString = rParam.GetSrchStr();
     176           6 :     aSOpt.replaceString = rParam.GetReplaceStr();
     177           6 :     aSOpt.Locale = rLocale;
     178           6 :     aSOpt.transliterateFlags = rParam.GetTransliterationFlags();
     179           6 :     if( !rParam.IsCaseSensitive() )
     180             :     {
     181           6 :         aSOpt.searchFlag |= SearchFlags::ALL_IGNORE_CASE;
     182           6 :         aSOpt.transliterateFlags |= ::com::sun::star::i18n::TransliterationModules_IGNORE_CASE;
     183             :     }
     184             : 
     185           6 :     xTextSearch = getXTextSearch( aSOpt );
     186           6 : }
     187             : 
     188           0 : void TextSearch::SetLocale( const ::com::sun::star::util::SearchOptions& rOptions,
     189             :                             const ::com::sun::star::lang::Locale& rLocale )
     190             : {
     191             :     // convert SearchParam to the UNO SearchOptions
     192           0 :     SearchOptions aSOpt( rOptions );
     193           0 :     aSOpt.Locale = rLocale;
     194             : 
     195           0 :     xTextSearch = getXTextSearch( aSOpt );
     196           0 : }
     197             : 
     198          87 : TextSearch::~TextSearch()
     199             : {
     200          87 : }
     201             : 
     202             : /*
     203             :  * General search methods. These methods will call the respective
     204             :  * methods, such as ordinary string searching or regular expression
     205             :  * matching, using the method pointer.
     206             :  */
     207        1184 : bool TextSearch::SearchForward( const OUString &rStr,
     208             :                     sal_Int32* pStart, sal_Int32* pEnd,
     209             :                     ::com::sun::star::util::SearchResult* pRes)
     210             : {
     211        1184 :     bool nRet = false;
     212             :     try
     213             :     {
     214        1184 :         if( xTextSearch.is() )
     215             :         {
     216        1184 :             SearchResult aRet( xTextSearch->searchForward( rStr, *pStart, *pEnd ));
     217        1184 :             if( aRet.subRegExpressions > 0 )
     218             :             {
     219         778 :                 nRet = true;
     220             :                 // the XTextsearch returns in startOffset the higher position
     221             :                 // and the endposition is always exclusive.
     222             :                 // The caller of this function will have in startPos the
     223             :                 // lower pos. and end
     224         778 :                 *pStart = aRet.startOffset[ 0 ];
     225         778 :                 *pEnd = aRet.endOffset[ 0 ];
     226         778 :                 if( pRes )
     227         686 :                     *pRes = aRet;
     228        1184 :             }
     229             :         }
     230             :     }
     231           0 :     catch ( Exception& )
     232             :     {
     233             :         SAL_WARN( "unotools.i18n", "SearchForward: Exception caught!" );
     234             :     }
     235        1184 :     return nRet;
     236             : }
     237             : 
     238           0 : bool TextSearch::SearchBackward( const OUString & rStr, sal_Int32* pStart,
     239             :                                 sal_Int32* pEnde, SearchResult* pRes )
     240             : {
     241           0 :     bool nRet = false;
     242             :     try
     243             :     {
     244           0 :         if( xTextSearch.is() )
     245             :         {
     246           0 :             SearchResult aRet( xTextSearch->searchBackward( rStr, *pStart, *pEnde ));
     247           0 :             if( aRet.subRegExpressions )
     248             :             {
     249           0 :                 nRet = true;
     250             :                 // the XTextsearch returns in startOffset the higher position
     251             :                 // and the endposition is always exclusive.
     252             :                 // The caller of this function will have in startPos the
     253             :                 // lower pos. and end
     254           0 :                 *pEnde = aRet.startOffset[ 0 ];
     255           0 :                 *pStart = aRet.endOffset[ 0 ];
     256           0 :                 if( pRes )
     257           0 :                     *pRes = aRet;
     258           0 :             }
     259             :         }
     260             :     }
     261           0 :     catch ( Exception& )
     262             :     {
     263             :         SAL_WARN( "unotools.i18n", "SearchBackward: Exception caught!" );
     264             :     }
     265           0 :     return nRet;
     266             : }
     267             : 
     268         269 : void TextSearch::ReplaceBackReferences( OUString& rReplaceStr, const OUString &rStr, const SearchResult& rResult )
     269             : {
     270         269 :     if( rResult.subRegExpressions > 0 )
     271             :     {
     272             :         sal_Unicode sFndChar;
     273             :         sal_Int32 i;
     274         269 :         OUStringBuffer sBuff(rReplaceStr.getLength()*4);
     275        2233 :         for(i = 0; i < rReplaceStr.getLength(); i++)
     276             :         {
     277        1964 :             if( rReplaceStr[i] == '&')
     278             :             {
     279           0 :                 sal_Int32 nStart = rResult.startOffset[0];
     280           0 :                 sal_Int32 nLength = rResult.endOffset[0] - rResult.startOffset[0];
     281           0 :                 sBuff.append(rStr.getStr() + nStart, nLength);
     282             :             }
     283        1964 :             else if((i < rReplaceStr.getLength() - 1) && rReplaceStr[i] == '$')
     284             :             {
     285           0 :                 sFndChar = rReplaceStr[ i + 1 ];
     286           0 :                 switch(sFndChar)
     287             :                 {   // placeholder for a backward reference?
     288             :                 case '0':
     289             :                 case '1':
     290             :                 case '2':
     291             :                 case '3':
     292             :                 case '4':
     293             :                 case '5':
     294             :                 case '6':
     295             :                 case '7':
     296             :                 case '8':
     297             :                 case '9':
     298             :                     {
     299           0 :                         int j = sFndChar - '0'; // index
     300           0 :                         if(j < rResult.subRegExpressions)
     301             :                         {
     302           0 :                             sal_Int32 nSttReg = rResult.startOffset[j];
     303           0 :                             sal_Int32 nRegLen = rResult.endOffset[j];
     304           0 :                             if( nRegLen > nSttReg )
     305             :                             {
     306           0 :                                 nRegLen = nRegLen - nSttReg;
     307             :                             }
     308             :                             else
     309             :                             {
     310           0 :                                 nRegLen = nSttReg - nRegLen;
     311           0 :                                 nSttReg = rResult.endOffset[j];
     312             :                             }
     313             :                             // Copy reference from found string
     314           0 :                             sBuff.append(rStr.getStr() + nSttReg, nRegLen);
     315             :                         }
     316           0 :                         i += 1;
     317             :                     }
     318           0 :                     break;
     319             :                 default:
     320           0 :                     sBuff.append(rReplaceStr[i]);
     321           0 :                     sBuff.append(rReplaceStr[i+1]);
     322           0 :                     i += 1;
     323           0 :                     break;
     324             :                 }
     325             :             }
     326        1964 :             else if((i < rReplaceStr.getLength() - 1) && rReplaceStr[i] == '\\')
     327             :             {
     328           0 :                 sFndChar = rReplaceStr[ i+1 ];
     329           0 :                 switch(sFndChar)
     330             :                 {
     331             :                 case '\\':
     332             :                 case '&':
     333             :                 case '$':
     334           0 :                     sBuff.append(sFndChar);
     335           0 :                     i+=1;
     336           0 :                     break;
     337             :                 case 't':
     338           0 :                     sBuff.append('\t');
     339           0 :                     i += 1;
     340           0 :                     break;
     341             :                 default:
     342           0 :                     sBuff.append(rReplaceStr[i]);
     343           0 :                     sBuff.append(rReplaceStr[i+1]);
     344           0 :                     i += 1;
     345           0 :                     break;
     346             :                 }
     347             :             }
     348             :             else
     349             :             {
     350        1964 :                 sBuff.append(rReplaceStr[i]);
     351             :             }
     352             :         }
     353         269 :         rReplaceStr = sBuff.makeStringAndClear();
     354             :     }
     355         269 : }
     356             : 
     357             : }   // namespace utl
     358             : 
     359             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10