summaryrefslogtreecommitdiff
path: root/unotools/source
diff options
context:
space:
mode:
authorVladimir Glazounov <vg@openoffice.org>2007-12-05 15:41:58 +0000
committerVladimir Glazounov <vg@openoffice.org>2007-12-05 15:41:58 +0000
commitf9bb8ca2afca2eacf67f2bed4c6520007438ef5b (patch)
tree3df3a725a897cf39a80ec03d145c3f9dfd7cc51f /unotools/source
parenta76133c236938b336d7064ec5903064473b0ac43 (diff)
INTEGRATION: CWS regexp02 (1.13.26); FILE MERGED
2007/11/06 13:01:43 ama 1.13.26.2: Fix #i15666#: Backward references in regular expressions 2007/11/01 12:40:16 ama 1.13.26.1: Fix #i15666#: Resolve backreferences in regular expressions
Diffstat (limited to 'unotools/source')
-rw-r--r--unotools/source/i18n/textsearch.cxx103
1 files changed, 100 insertions, 3 deletions
diff --git a/unotools/source/i18n/textsearch.cxx b/unotools/source/i18n/textsearch.cxx
index 032edc452aa5..dd01d03ebadb 100644
--- a/unotools/source/i18n/textsearch.cxx
+++ b/unotools/source/i18n/textsearch.cxx
@@ -4,9 +4,9 @@
*
* $RCSfile: textsearch.cxx,v $
*
- * $Revision: 1.13 $
+ * $Revision: 1.14 $
*
- * last change: $Author: ihi $ $Date: 2007-03-26 12:46:42 $
+ * last change: $Author: vg $ $Date: 2007-12-05 16:41:58 $
*
* The Contents of this file are made available subject to
* the terms of GNU Lesser General Public License Version 2.1.
@@ -249,7 +249,7 @@ int TextSearch::SearchFrwrd( const String & rStr, xub_StrLen* pStart,
{
SearchResult aRet( xTextSearch->searchForward(
rStr, *pStart, *pEnde ));
- if( 1 == aRet.subRegExpressions )
+ if( aRet.subRegExpressions > 0 )
{
nRet = 1;
// the XTextsearch returns in startOffset the higher position
@@ -301,6 +301,103 @@ int TextSearch::SearchBkwrd( const String & rStr, xub_StrLen* pStart,
return nRet;
}
+void TextSearch::ReplaceBackReferences( String& rReplaceStr, const String &rStr, const SearchResult& rResult )
+{
+ if( rResult.subRegExpressions > 0 )
+ {
+ String sTab( '\t' );
+ sal_Unicode sSrchChrs[] = {'\\', '&', '$', 0};
+ String sTmp;
+ xub_StrLen nPos = 0;
+ sal_Unicode sFndChar;
+ while( STRING_NOTFOUND != ( nPos = rReplaceStr.SearchChar( sSrchChrs, nPos )) )
+ {
+ if( rReplaceStr.GetChar( nPos ) == '&')
+ {
+ USHORT nStart = (USHORT)(rResult.startOffset[0]);
+ USHORT nLength = (USHORT)(rResult.endOffset[0] - rResult.startOffset[0]);
+ rReplaceStr.Erase( nPos, 1 ); // delete ampersand
+ // replace by found string
+ rReplaceStr.Insert( rStr, nStart, nLength, nPos );
+ // jump over
+ nPos = nPos + nLength;
+ }
+ else if( rReplaceStr.GetChar( nPos ) == '$')
+ {
+ if( nPos + 1 < rReplaceStr.Len())
+ {
+ sFndChar = rReplaceStr.GetChar( nPos + 1 );
+ switch(sFndChar)
+ { // placeholder for a backward reference?
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ {
+ rReplaceStr.Erase( nPos, 2 ); // delete both
+ int i = sFndChar - '0'; // index
+ if(i < rResult.subRegExpressions)
+ {
+ USHORT nSttReg = (USHORT)(rResult.startOffset[i]);
+ USHORT nRegLen = (USHORT)(rResult.endOffset[i]);
+ if( nRegLen > nSttReg )
+ nRegLen = nRegLen - nSttReg;
+ else
+ {
+ nRegLen = nSttReg - nRegLen;
+ nSttReg = (USHORT)(rResult.endOffset[i]);
+ }
+ // Copy reference from found string
+ sTmp = rStr.Copy((USHORT)nSttReg, (USHORT)nRegLen);
+ // insert
+ rReplaceStr.Insert( sTmp, nPos );
+ // and step over
+ nPos = nPos + sTmp.Len();
+ }
+ }
+ break;
+ default:
+ nPos += 2; // leave both chars unchanged
+ break;
+ }
+ }
+ }
+ else
+ {
+ // at least another character?
+ if( nPos + 1 < rReplaceStr.Len())
+ {
+ sFndChar = rReplaceStr.GetChar( nPos + 1 );
+ switch(sFndChar)
+ {
+ case '\\':
+ case '&':
+ case '$':
+ rReplaceStr.Erase( nPos, 1 );
+ nPos++;
+ break;
+ case 't':
+ rReplaceStr.Erase( nPos, 2 ); // delete both
+ rReplaceStr.Insert( sTab, nPos ); // insert tabulator
+ nPos++; // step over
+ break;
+ default:
+ nPos += 2; // ignore both characters
+ break;
+ }
+ }
+ }
+ }
+ }
+}
+
+
#if defined _MSC_VER
#pragma optimize("", on)
#endif