diff options
author | Caolán McNamara <caolanm@redhat.com> | 2012-01-30 11:31:05 +0000 |
---|---|---|
committer | Caolán McNamara <caolanm@redhat.com> | 2012-01-30 13:18:38 +0000 |
commit | bbc94edb9a91b27910d43610db9994df10dd99e1 (patch) | |
tree | 99f81b7bc96dd93ae6c6e6a4f7a57cfda72cdfa4 /tools | |
parent | 47e1e8494dde85414d28efafa99b33a62b59116d (diff) |
refactor ConvertLineEnd
Diffstat (limited to 'tools')
-rw-r--r-- | tools/Package_inc.mk | 1 | ||||
-rw-r--r-- | tools/inc/tools/lineend.hxx | 53 | ||||
-rw-r--r-- | tools/inc/tools/string.hxx | 24 | ||||
-rw-r--r-- | tools/source/stream/stream.cxx | 92 | ||||
-rw-r--r-- | tools/source/string/strimp.cxx | 95 |
5 files changed, 147 insertions, 118 deletions
diff --git a/tools/Package_inc.mk b/tools/Package_inc.mk index 91642c41344f..b4765f95cd6c 100644 --- a/tools/Package_inc.mk +++ b/tools/Package_inc.mk @@ -54,6 +54,7 @@ $(eval $(call gb_Package_add_file,tools_inc,inc/tools/inetmime.hxx,tools/inetmim $(eval $(call gb_Package_add_file,tools_inc,inc/tools/inetmsg.hxx,tools/inetmsg.hxx)) $(eval $(call gb_Package_add_file,tools_inc,inc/tools/inetstrm.hxx,tools/inetstrm.hxx)) $(eval $(call gb_Package_add_file,tools_inc,inc/tools/line.hxx,tools/line.hxx)) +$(eval $(call gb_Package_add_file,tools_inc,inc/tools/lineend.hxx,tools/lineend.hxx)) $(eval $(call gb_Package_add_file,tools_inc,inc/tools/link.hxx,tools/link.hxx)) $(eval $(call gb_Package_add_file,tools_inc,inc/tools/list.hxx,tools/list.hxx)) $(eval $(call gb_Package_add_file,tools_inc,inc/tools/mapunit.hxx,tools/mapunit.hxx)) diff --git a/tools/inc/tools/lineend.hxx b/tools/inc/tools/lineend.hxx new file mode 100644 index 000000000000..b9ee807ffa30 --- /dev/null +++ b/tools/inc/tools/lineend.hxx @@ -0,0 +1,53 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * Version: MPL 1.1 / GPLv3+ / LGPLv3+ + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License or as specified alternatively below. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * Major Contributor(s): + * Copyright (C) 2012 Red Hat, Inc., Caolán McNamara <caolanm@redhat.com> + * (initial developer) + * + * All Rights Reserved. + * + * For minor contributions see the git repository. + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 3 or later (the "GPLv3+"), or + * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"), + * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable + * instead of those above. + */ + +#ifndef _LINEEND_HXX +#define _LINEEND_HXX + +#include <rtl/string.hxx> +#include <rtl/ustring.hxx> +#include "tools/toolsdllapi.h" + +enum LineEnd { LINEEND_CR, LINEEND_LF, LINEEND_CRLF }; + +inline LineEnd GetSystemLineEnd() +{ +#if defined WNT + return LINEEND_CRLF; +#else + return LINEEND_LF; +#endif +} + +TOOLS_DLLPUBLIC rtl::OString convertLineEnd(const rtl::OString &rIn, LineEnd eLineEnd); +TOOLS_DLLPUBLIC rtl::OUString convertLineEnd(const rtl::OUString &rIn, LineEnd eLineEnd); + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/tools/inc/tools/string.hxx b/tools/inc/tools/string.hxx index 8f3be409d9b9..22aecff2038e 100644 --- a/tools/inc/tools/string.hxx +++ b/tools/inc/tools/string.hxx @@ -36,6 +36,7 @@ #include <rtl/string.hxx> #include <rtl/ustring.hxx> #include "tools/toolsdllapi.h" +#include "tools/lineend.hxx" /******************************************************************************* * @@ -80,21 +81,6 @@ typedef rtl_TextEncoding CharSet; #endif // ENUM_CHARSET_DECLARED -// ----------- -// - LineEnd - -// ----------- - -enum LineEnd { LINEEND_CR, LINEEND_LF, LINEEND_CRLF }; - -inline LineEnd GetSystemLineEnd() -{ -#if defined UNX - return LINEEND_LF; -#else - return LINEEND_CRLF; -#endif -} - // ----------------------------------------------------------------------- // ---------------- @@ -227,10 +213,6 @@ public: ByteString& Erase( xub_StrLen nIndex = 0, xub_StrLen nCount = STRING_LEN ); ByteString Copy( xub_StrLen nIndex = 0, xub_StrLen nCount = STRING_LEN ) const; - ByteString& ConvertLineEnd( LineEnd eLineEnd ); - ByteString& ConvertLineEnd() - { return ConvertLineEnd( GetSystemLineEnd() ); } - ByteString& ToLowerAscii(); ByteString& ToUpperAscii(); @@ -480,10 +462,6 @@ public: UniString& EraseTrailingChars( sal_Unicode c = ' ' ); UniString& Reverse(); - UniString& ConvertLineEnd( LineEnd eLineEnd ); - UniString& ConvertLineEnd() - { return ConvertLineEnd( GetSystemLineEnd() ); } - UniString& ToLowerAscii(); UniString& ToUpperAscii(); diff --git a/tools/source/stream/stream.cxx b/tools/source/stream/stream.cxx index fd5a579a8bde..902f17876d3a 100644 --- a/tools/source/stream/stream.cxx +++ b/tools/source/stream/stream.cxx @@ -2388,4 +2388,96 @@ rtl::OUString read_uInt16s_ToOUString(SvStream& rStrm, sal_Size nLen) return pStr ? rtl::OUString(pStr, SAL_NO_ACQUIRE) : rtl::OUString(); } +namespace +{ + template <typename T, typename O> T tmpl_convertLineEnd(const T &rIn, LineEnd eLineEnd) + { + // Zeilenumbrueche ermitteln und neue Laenge berechnen + bool bConvert = false; // Muss konvertiert werden + sal_Int32 nStrLen = rIn.getLength(); + sal_Int32 nLineEndLen = (eLineEnd == LINEEND_CRLF) ? 2 : 1; + sal_Int32 nLen = 0; // Ziel-Laenge + sal_Int32 i = 0; // Source-Zaehler + + while (i < nStrLen) + { + // Bei \r oder \n gibt es neuen Zeilenumbruch + if ( (rIn[i] == _CR) || (rIn[i] == _LF) ) + { + nLen = nLen + nLineEndLen; + + // Wenn schon gesetzt, dann brauchen wir keine aufwendige Abfrage + if ( !bConvert ) + { + // Muessen wir Konvertieren + if ( ((eLineEnd != LINEEND_LF) && (rIn[i] == _LF)) || + ((eLineEnd == LINEEND_CRLF) && (rIn[i+1] != _LF)) || + ((eLineEnd == LINEEND_LF) && + ((rIn[i] == _CR) || (rIn[i+1] == _CR))) || + ((eLineEnd == LINEEND_CR) && + ((rIn[i] == _LF) || (rIn[i+1] == _LF))) ) + bConvert = true; + } + + // \r\n oder \n\r, dann Zeichen ueberspringen + if ( ((rIn[i+1] == _CR) || (rIn[i+1] == _LF)) && + (rIn[i] != rIn[i+1]) ) + ++i; + } + else + ++nLen; + ++i; + } + + if (!bConvert) + return rIn; + + // Zeilenumbrueche konvertieren + // Neuen String anlegen + O aNewData(nLen); + i = 0; + while (i < nStrLen) + { + // Bei \r oder \n gibt es neuen Zeilenumbruch + if ( (rIn[i] == _CR) || (rIn[i] == _LF) ) + { + if ( eLineEnd == LINEEND_CRLF ) + { + aNewData.append(_CR); + aNewData.append(_LF); + } + else + { + if ( eLineEnd == LINEEND_CR ) + aNewData.append(_CR); + else + aNewData.append(_LF); + } + + if ( ((rIn[i+1] == _CR) || (rIn[i+1] == _LF)) && + (rIn[i] != rIn[i+1]) ) + ++i; + } + else + { + aNewData.append(rIn[i]); + } + + ++i; + } + + return aNewData.makeStringAndClear(); + } +} + +rtl::OString convertLineEnd(const rtl::OString &rIn, LineEnd eLineEnd) +{ + return tmpl_convertLineEnd<rtl::OString, rtl::OStringBuffer>(rIn, eLineEnd); +} + +rtl::OUString convertLineEnd(const rtl::OUString &rIn, LineEnd eLineEnd) +{ + return tmpl_convertLineEnd<rtl::OUString, rtl::OUStringBuffer>(rIn, eLineEnd); +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/tools/source/string/strimp.cxx b/tools/source/string/strimp.cxx index 41966383d3dc..32913e0d2331 100644 --- a/tools/source/string/strimp.cxx +++ b/tools/source/string/strimp.cxx @@ -808,101 +808,6 @@ STRING& STRING::ToUpperAscii() // ----------------------------------------------------------------------- -STRING& STRING::ConvertLineEnd( LineEnd eLineEnd ) -{ - DBG_CHKTHIS( STRING, DBGCHECKSTRING ); - - // Zeilenumbrueche ermitteln und neue Laenge berechnen - sal_Bool bConvert = sal_False; // Muss konvertiert werden - const STRCODE* pStr = mpData->maStr; // damit es schneller geht - xub_StrLen nLineEndLen = (eLineEnd == LINEEND_CRLF) ? 2 : 1; - xub_StrLen nLen = 0; // Ziel-Laenge - xub_StrLen i = 0; // Source-Zaehler - - while ( i < mpData->mnLen ) - { - // Bei \r oder \n gibt es neuen Zeilenumbruch - if ( (pStr[i] == _CR) || (pStr[i] == _LF) ) - { - nLen = nLen + nLineEndLen; - - // Wenn schon gesetzt, dann brauchen wir keine aufwendige Abfrage - if ( !bConvert ) - { - // Muessen wir Konvertieren - if ( ((eLineEnd != LINEEND_LF) && (pStr[i] == _LF)) || - ((eLineEnd == LINEEND_CRLF) && (pStr[i+1] != _LF)) || - ((eLineEnd == LINEEND_LF) && - ((pStr[i] == _CR) || (pStr[i+1] == _CR))) || - ((eLineEnd == LINEEND_CR) && - ((pStr[i] == _LF) || (pStr[i+1] == _LF))) ) - bConvert = sal_True; - } - - // \r\n oder \n\r, dann Zeichen ueberspringen - if ( ((pStr[i+1] == _CR) || (pStr[i+1] == _LF)) && - (pStr[i] != pStr[i+1]) ) - ++i; - } - else - ++nLen; - ++i; - - // Wenn String zu lang, dann konvertieren wir nicht - if ( nLen >= STRING_MAXLEN ) - return *this; - } - - // Zeilenumbrueche konvertieren - if ( bConvert ) - { - // Neuen String anlegen - STRINGDATA* pNewData = ImplAllocData( nLen ); - xub_StrLen j = 0; - i = 0; - while ( i < mpData->mnLen ) - { - // Bei \r oder \n gibt es neuen Zeilenumbruch - if ( (pStr[i] == _CR) || (pStr[i] == _LF) ) - { - if ( eLineEnd == LINEEND_CRLF ) - { - pNewData->maStr[j] = _CR; - pNewData->maStr[j+1] = _LF; - j += 2; - } - else - { - if ( eLineEnd == LINEEND_CR ) - pNewData->maStr[j] = _CR; - else - pNewData->maStr[j] = _LF; - ++j; - } - - if ( ((pStr[i+1] == _CR) || (pStr[i+1] == _LF)) && - (pStr[i] != pStr[i+1]) ) - ++i; - } - else - { - pNewData->maStr[j] = mpData->maStr[i]; - ++j; - } - - ++i; - } - - // Alte Daten loeschen und Neue zuweisen - STRING_RELEASE((STRING_TYPE *)mpData); - mpData = pNewData; - } - - return *this; -} - -// ----------------------------------------------------------------------- - StringCompare STRING::CompareTo( const STRING& rStr, xub_StrLen nLen ) const { DBG_CHKTHIS( STRING, DBGCHECKSTRING ); |