diff options
Diffstat (limited to 'l10ntools/source/po.cxx')
-rw-r--r-- | l10ntools/source/po.cxx | 237 |
1 files changed, 217 insertions, 20 deletions
diff --git a/l10ntools/source/po.cxx b/l10ntools/source/po.cxx index c59a4f8a4e31..918a76878a8c 100644 --- a/l10ntools/source/po.cxx +++ b/l10ntools/source/po.cxx @@ -8,6 +8,7 @@ */ #include <rtl/ustring.hxx> +#include <rtl/strbuf.hxx> #include <cstring> #include <ctime> @@ -107,21 +108,217 @@ namespace } //Unescape text - static OString lcl_UnEscapeText(const OString& rText, - const OString& rEscaped = POESCAPED, - const OString& rUnEscaped = POUNESCAPED) + static OString lcl_UnEscapeText(const OString& rText) { - assert( rEscaped.getLength() == 2*rUnEscaped.getLength() ); - OString sResult = rText; - int nCount = 0; - for(sal_Int32 nIndex=0; nIndex<rText.getLength()-1; ++nIndex) + sal_Int32 index; + for(index = 0 ; index < rText.getLength() - 1; ++index) { - sal_Int32 nActChar = rEscaped.indexOf(rText.copy(nIndex,2)); - if(nActChar % 2 == 0) - sResult = sResult.replaceAt((nIndex++)-(nCount++),2, - rUnEscaped.copy(nActChar/2,1)); + if(rText[index] == '\\') + { + switch(rText[index + 1]) + { + case '\\': + case 'n': + case 'r': + case 't': + case '"': + OStringBuffer sBuff(rText); + const sal_Char* in = sBuff.getStr() + index; + sal_Char* out = &sBuff[index]; + while(*in) + { + if(*in == '\\') + { + switch(in[1]) + { + case '\\': + *out++ = '\\'; + in += 2; + break; + case 'n': + *out++ = '\n'; + in += 2; + break; + case 'r': + *out++ = '\r'; + in += 2; + break; + case 't': + *out++ = '\t'; + in += 2; + break; + case '"': + *out++ = '"'; + in += 2; + break; + default: + *out++ = *in++; + break; + } + } + else + { + *out++ = *in++; + } + } + *out = 0; + sBuff.setLength((out - sBuff.getStr())); + return sBuff.makeStringAndClear(); + } + } } - return sResult; + return rText; + } + + static OString lcl_UnEscapeTextBlanks(const OString& rText) + { + sal_Int32 index; + for(index = 0 ; index < rText.getLength() - 1; ++index) + { + if(rText[index] == '\\') + { + switch(rText[index + 1]) + { + case 'n': + case 'r': + case 't': + OStringBuffer sBuff(rText); + const sal_Char* in = sBuff.getStr() + index; + sal_Char* out = &sBuff[index]; + while(*in) + { + if(*in == '\\') + { + switch(in[1]) + { + case 'n': + *out++ = '\n'; + in += 2; + break; + case 'r': + *out++ = '\r'; + in += 2; + break; + case 't': + *out++ = '\t'; + in += 2; + break; + default: + *out++ = *in++; + break; + } + } + else + { + *out++ = *in++; + } + } + *out = 0; + sBuff.setLength((out - sBuff.getStr())); + return sBuff.makeStringAndClear(); + } + } + } + return rText; + } + + static OString lcl_EscapeTextBlanks(const OString& rText) + { + sal_Int32 index; + for(index = 0 ; index < rText.getLength() - 1; ++index) + { + switch(rText[index]) + { + case '\n': + case '\r': + case '\t': + OStringBuffer sBuff(rText); + const sal_Char* in = rText.getStr() + index; + sal_Char* out = &sBuff[index]; + while(*in) + { + switch(in[1]) + { + case '\n': + *out++ = '\\'; + *out++ = 'n'; + break; + case '\r': + *out++ = '\\'; + *out++ = 'r'; + break; + case 't': + *out++ = '\\'; + *out++ = 'r'; + break; + default: + *out++ = *in++; + break; + } + } + *out = 0; + sBuff.setLength((out - sBuff.getStr())); + return sBuff.makeStringAndClear(); + } + } + return rText; + } + + static OString lcl_UnEscapeTextHelp(const OString& rText) + { + sal_Int32 index; + for(index = 0 ; index < rText.getLength() - 1; ++index) + { + if(rText[index] == '\\') + { + switch(rText[index + 1]) + { + case '<': + case '>': + case '"': + case '\\': + OStringBuffer sBuff(rText); + const sal_Char* in = sBuff.getStr() + index; + sal_Char* out = &sBuff[index]; + while(*in) + { + if(*in == '\\') + { + switch(in[1]) + { + case '<': + *out++ = '<'; + in += 2; + break; + case '>': + *out++ = '>'; + in += 2; + break; + case '"': + *out++ = '"'; + in += 2; + break; + case '\\': + *out++ = '\\'; + in += 2; + break; + default: + *out++ = *in++; + break; + } + } + else + { + *out++ = *in++; + } + } + *out = 0; + sBuff.setLength((out - sBuff.getStr())); + return sBuff.makeStringAndClear(); + } + } + } + return rText; } //Convert a normal string to msg/po output string @@ -301,9 +498,9 @@ namespace const OString& rText,const bool bHelpText = false ) { if ( bHelpText ) - return lcl_UnEscapeText(rText,"\\<\\>\\\"\\\\","<>\"\\"); + return lcl_UnEscapeTextHelp(rText); else - return lcl_UnEscapeText(rText,"\\n\\t\\r","\n\t\r"); + return lcl_UnEscapeTextBlanks(rText); } //Find all special tag in a string using a regular expression @@ -311,18 +508,18 @@ namespace const OString& rText,std::vector<OString>& o_vFoundTags ) { - UErrorCode nIcuErr = U_ZERO_ERROR; - sal_uInt32 nSearchFlags = UREGEX_DOTALL | UREGEX_CASE_INSENSITIVE; + static UErrorCode nIcuErr = U_ZERO_ERROR; + static sal_uInt32 nSearchFlags = UREGEX_DOTALL | UREGEX_CASE_INSENSITIVE; OUString sLocaleText( OStringToOUString(rText,RTL_TEXTENCODING_UTF8) ); - OUString sPattern("<[/]\?\?[a-z_-]+?(?:| +[a-z]+?=\".*?\") *[/]\?\?>"); - UnicodeString sSearchPat( + static OUString sPattern("<[/]\?\?[a-z_-]+?(?:| +[a-z]+?=\".*?\") *[/]\?\?>"); + static UnicodeString sSearchPat( reinterpret_cast<const UChar*>( sPattern.getStr()), sPattern.getLength() ); UnicodeString sSource( reinterpret_cast<const UChar*>( sLocaleText.getStr()), sLocaleText.getLength() ); - RegexMatcher aRegexMatcher( sSearchPat, nSearchFlags, nIcuErr ); + static RegexMatcher aRegexMatcher( sSearchPat, nSearchFlags, nIcuErr ); aRegexMatcher.reset( sSource ); int64_t nStartPos = 0; while( aRegexMatcher.find(nStartPos, nIcuErr) && @@ -386,7 +583,7 @@ namespace if ( bHelpText ) return lcl_EscapeTags(rText.replaceAll("\\","\\\\")); else - return lcl_EscapeText(rText,"\n\t\r","\\n\\t\\r"); + return lcl_EscapeTextBlanks(rText); } } |