svl/inc/svl/lngmisc.hxx | 1 svl/qa/unit/test_lngmisc.cxx | 34 ++++++- svl/source/misc/lngmisc.cxx | 185 ++++++++++++++++++------------------------- 3 files changed, 112 insertions(+), 108 deletions(-)
New commits: commit 82e5a0ddcbb9e367e60c772f4605df7fc9b63642 Author: August Sodora <aug...@gmail.com> Date: Sat Nov 26 15:21:12 2011 -0500 Remove unnecessary includes and cosmetics diff --git a/svl/source/misc/lngmisc.cxx b/svl/source/misc/lngmisc.cxx index 56c1066..4065290 100644 --- a/svl/source/misc/lngmisc.cxx +++ b/svl/source/misc/lngmisc.cxx @@ -28,125 +28,115 @@ // MARKER(update_precomp.py): autogen include statement, do not remove #include "precompiled_svl.hxx" + +#include "svl/lngmisc.hxx" + #include <comphelper/string.hxx> -#include <svl/lngmisc.hxx> -#include <tools/solar.h> -#include <tools/string.hxx> -#include <tools/debug.hxx> #include <rtl/ustrbuf.hxx> -#include <rtl/ustring.hxx> - -using ::rtl::OUString; -using ::rtl::OUStringBuffer; +#include <tools/debug.hxx> +#include <tools/string.hxx> namespace linguistic { + sal_Int32 GetNumControlChars(const rtl::OUString &rTxt) + { + sal_Int32 nCnt = 0; + for (sal_Int32 i = 0; i < rTxt.getLength(); ++i) + if (IsControlChar(rTxt[i])) + ++nCnt; + return nCnt; + } -/////////////////////////////////////////////////////////////////////////// - -sal_Int32 GetNumControlChars( const OUString &rTxt ) -{ - sal_Int32 nCnt = 0; - for (sal_Int32 i = 0; i < rTxt.getLength(); ++i) - if (IsControlChar(rTxt[i])) - ++nCnt; - return nCnt; -} - -bool RemoveHyphens( OUString &rTxt ) -{ - sal_Int32 n = rTxt.getLength(); - rTxt = comphelper::string::remove(rTxt, SVT_SOFT_HYPHEN); - rTxt = comphelper::string::remove(rTxt, SVT_HARD_HYPHEN); - return n != rTxt.getLength(); -} + bool RemoveHyphens(rtl::OUString &rTxt) + { + sal_Int32 n = rTxt.getLength(); + rTxt = comphelper::string::remove(rTxt, SVT_SOFT_HYPHEN); + rTxt = comphelper::string::remove(rTxt, SVT_HARD_HYPHEN); + return n != rTxt.getLength(); + } -bool RemoveControlChars( OUString &rTxt ) -{ - sal_Int32 nSize = rTxt.getLength() - GetNumControlChars(rTxt); - if(nSize == rTxt.getLength()) - return false; + bool RemoveControlChars(rtl::OUString &rTxt) + { + sal_Int32 nSize = rTxt.getLength() - GetNumControlChars(rTxt); + if(nSize == rTxt.getLength()) + return false; - OUStringBuffer aBuf(nSize); - aBuf.setLength(nSize); - for (sal_Int32 i = 0, j = 0; i < rTxt.getLength() && j < nSize; ++i) - if (!IsControlChar(rTxt[i])) - aBuf[j++] = rTxt[i]; + rtl::OUStringBuffer aBuf(nSize); + aBuf.setLength(nSize); + for (sal_Int32 i = 0, j = 0; i < rTxt.getLength() && j < nSize; ++i) + if (!IsControlChar(rTxt[i])) + aBuf[j++] = rTxt[i]; - rTxt = aBuf.makeStringAndClear(); - DBG_ASSERT(rTxt.getLength() == nSize, "GetNumControlChars returned a different number of control characters than were actually removed."); + rTxt = aBuf.makeStringAndClear(); + DBG_ASSERT(rTxt.getLength() == nSize, "GetNumControlChars returned a different number of control characters than were actually removed."); - return true; -} + return true; + } -// non breaking field character + // non breaking field character #define CH_TXTATR_INWORD ((sal_Char) 0x02) -bool ReplaceControlChars( rtl::OUString &rTxt, sal_Char /*aRplcChar*/ ) -{ - // the resulting string looks like this: - // 1. non breaking field characters get removed - // 2. remaining control characters will be replaced by ' ' - - bool bModified = false; - sal_Int32 nCtrlChars = GetNumControlChars( rTxt ); - if (nCtrlChars) + bool ReplaceControlChars( rtl::OUString &rTxt, sal_Char /*aRplcChar*/ ) { - sal_Int32 nLen = rTxt.getLength(); - OUStringBuffer aBuf( nLen ); - sal_Int32 nCnt = 0; - for (sal_Int32 i = 0; i < nLen; ++i) + // the resulting string looks like this: + // 1. non breaking field characters get removed + // 2. remaining control characters will be replaced by ' ' + + bool bModified = false; + sal_Int32 nCtrlChars = GetNumControlChars( rTxt ); + if (nCtrlChars) { - sal_Unicode cChar = rTxt[i]; - if (CH_TXTATR_INWORD != cChar) + sal_Int32 nLen = rTxt.getLength(); + rtl::OUStringBuffer aBuf( nLen ); + sal_Int32 nCnt = 0; + for (sal_Int32 i = 0; i < nLen; ++i) { - if (IsControlChar( cChar )) - cChar = ' '; - DBG_ASSERT( nCnt < nLen, "index out of range" ); - aBuf.setCharAt( nCnt++, cChar ); + sal_Unicode cChar = rTxt[i]; + if (CH_TXTATR_INWORD != cChar) + { + if (IsControlChar( cChar )) + cChar = ' '; + DBG_ASSERT( nCnt < nLen, "index out of range" ); + aBuf.setCharAt( nCnt++, cChar ); + } } + aBuf.setLength( nCnt ); + rTxt = aBuf.makeStringAndClear(); + bModified = true; } - aBuf.setLength( nCnt ); - rTxt = aBuf.makeStringAndClear(); - bModified = true; + return bModified; } - return bModified; -} - -String GetThesaurusReplaceText( const String &rText ) -{ - // The strings for synonyms returned by the thesaurus sometimes have some - // explanation text put in between '(' and ')' or a trailing '*'. - // These parts should not be put in the ReplaceEdit Text that may get - // inserted into the document. Thus we strip them from the text. - - String aText( rText ); - - xub_StrLen nPos = aText.Search( sal_Unicode('(') ); - while (STRING_NOTFOUND != nPos) + String GetThesaurusReplaceText(const String &rText) { - xub_StrLen nEnd = aText.Search( sal_Unicode(')'), nPos ); - if (STRING_NOTFOUND != nEnd) - aText.Erase( nPos, nEnd-nPos+1 ); - else - break; - nPos = aText.Search( sal_Unicode('(') ); - } + // The strings for synonyms returned by the thesaurus sometimes have some + // explanation text put in between '(' and ')' or a trailing '*'. + // These parts should not be put in the ReplaceEdit Text that may get + // inserted into the document. Thus we strip them from the text. - nPos = aText.Search( sal_Unicode('*') ); - if (STRING_NOTFOUND != nPos) - aText.Erase( nPos ); + String aText( rText ); - // remove any possible remaining ' ' that may confuse the thesaurus - // when it gets called with the text - aText = comphelper::string::strip(aText, ' '); + xub_StrLen nPos = aText.Search( sal_Unicode('(') ); + while (STRING_NOTFOUND != nPos) + { + xub_StrLen nEnd = aText.Search( sal_Unicode(')'), nPos ); + if (STRING_NOTFOUND != nEnd) + aText.Erase( nPos, nEnd-nPos+1 ); + else + break; + nPos = aText.Search( sal_Unicode('(') ); + } - return aText; -} + nPos = aText.Search( sal_Unicode('*') ); + if (STRING_NOTFOUND != nPos) + aText.Erase( nPos ); -/////////////////////////////////////////////////////////////////////////// + // remove any possible remaining ' ' that may confuse the thesaurus + // when it gets called with the text + aText = comphelper::string::strip(aText, ' '); + return aText; + } } // namespace linguistic /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ commit 734d838f8083e6939d577fa0d0b5ab96367aea5f Author: August Sodora <aug...@gmail.com> Date: Sat Nov 26 15:16:49 2011 -0500 Simplification in lngmisc with accompanying tests diff --git a/svl/inc/svl/lngmisc.hxx b/svl/inc/svl/lngmisc.hxx index a473ab6..6c71b45 100644 --- a/svl/inc/svl/lngmisc.hxx +++ b/svl/inc/svl/lngmisc.hxx @@ -54,6 +54,7 @@ inline bool IsHyphen( sal_Unicode cChar ) inline bool IsControlChar( sal_Unicode cChar ) { + // TODO: why doesn't this include 0x0F DEL? return cChar < static_cast<sal_Unicode>(' '); } diff --git a/svl/qa/unit/test_lngmisc.cxx b/svl/qa/unit/test_lngmisc.cxx index 47f671a..6411e20 100644 --- a/svl/qa/unit/test_lngmisc.cxx +++ b/svl/qa/unit/test_lngmisc.cxx @@ -16,14 +16,14 @@ namespace { private: void testRemoveHyphens(); - // void testRemoveControlChars(); + void testRemoveControlChars(); // void testReplaceControlChars(); // void testGetThesaurusReplaceText(); CPPUNIT_TEST_SUITE(LngMiscTest); CPPUNIT_TEST(testRemoveHyphens); - // CPPUNIT_TEST(testRemoveControlChars); + CPPUNIT_TEST(testRemoveControlChars); // CPPUNIT_TEST(testReplaceControlChars); // CPPUNIT_TEST(testGetThesaurusReplaceText); @@ -61,12 +61,38 @@ namespace CPPUNIT_ASSERT(str4.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("asdf"))); } - /* void LngMiscTest::testRemoveControlChars() { - CPPUNIT_ASSERT(true); + ::rtl::OUString str1(RTL_CONSTASCII_USTRINGPARAM("")); + ::rtl::OUString str2(RTL_CONSTASCII_USTRINGPARAM("asdf")); + ::rtl::OUString str3(RTL_CONSTASCII_USTRINGPARAM("asdf\nasdf")); + + ::rtl::OUStringBuffer str4Buf(33); + str4Buf.setLength(33); + for(int i = 0; i < 33; i++) + str4Buf[i] = static_cast<sal_Unicode>(i); + // TODO: is this a bug? shouldn't RemoveControlChars remove this? + // str4Buf[33] = static_cast<sal_Unicode>(0x7F); + ::rtl::OUString str4(str4Buf.makeStringAndClear()); + + bool bModified = linguistic::RemoveControlChars(str1); + CPPUNIT_ASSERT(!bModified); + CPPUNIT_ASSERT(str1.isEmpty()); + + bModified = linguistic::RemoveControlChars(str2); + CPPUNIT_ASSERT(!bModified); + CPPUNIT_ASSERT(str2.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("asdf"))); + + bModified = linguistic::RemoveControlChars(str3); + CPPUNIT_ASSERT(bModified); + CPPUNIT_ASSERT(str3.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("asdfasdf"))); + + bModified = linguistic::RemoveControlChars(str4); + CPPUNIT_ASSERT(bModified); + CPPUNIT_ASSERT(str4.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(" "))); } + /* void LngMiscTest::testReplaceControlChars() { CPPUNIT_ASSERT(true); diff --git a/svl/source/misc/lngmisc.cxx b/svl/source/misc/lngmisc.cxx index 3c32099..56c1066 100644 --- a/svl/source/misc/lngmisc.cxx +++ b/svl/source/misc/lngmisc.cxx @@ -47,12 +47,9 @@ namespace linguistic sal_Int32 GetNumControlChars( const OUString &rTxt ) { sal_Int32 nCnt = 0; - sal_Int32 nLen = rTxt.getLength(); - for (sal_Int32 i = 0; i < nLen; ++i) - { - if (IsControlChar( rTxt[i] )) + for (sal_Int32 i = 0; i < rTxt.getLength(); ++i) + if (IsControlChar(rTxt[i])) ++nCnt; - } return nCnt; } @@ -66,31 +63,21 @@ bool RemoveHyphens( OUString &rTxt ) bool RemoveControlChars( OUString &rTxt ) { - bool bModified = false; - sal_Int32 nCtrlChars = GetNumControlChars( rTxt ); - if (nCtrlChars) - { - sal_Int32 nLen = rTxt.getLength(); - sal_Int32 nSize = nLen - nCtrlChars; - OUStringBuffer aBuf( nSize ); - aBuf.setLength( nSize ); - sal_Int32 nCnt = 0; - for (sal_Int32 i = 0; i < nLen; ++i) - { - sal_Unicode cChar = rTxt[i]; - if (!IsControlChar( cChar )) - { - DBG_ASSERT( nCnt < nSize, "index out of range" ); - aBuf.setCharAt( nCnt++, cChar ); - } - } - DBG_ASSERT( nCnt == nSize, "wrong size" ); - rTxt = aBuf.makeStringAndClear(); - bModified = true; - } - return bModified; -} + sal_Int32 nSize = rTxt.getLength() - GetNumControlChars(rTxt); + if(nSize == rTxt.getLength()) + return false; + + OUStringBuffer aBuf(nSize); + aBuf.setLength(nSize); + for (sal_Int32 i = 0, j = 0; i < rTxt.getLength() && j < nSize; ++i) + if (!IsControlChar(rTxt[i])) + aBuf[j++] = rTxt[i]; + rTxt = aBuf.makeStringAndClear(); + DBG_ASSERT(rTxt.getLength() == nSize, "GetNumControlChars returned a different number of control characters than were actually removed."); + + return true; +} // non breaking field character #define CH_TXTATR_INWORD ((sal_Char) 0x02) _______________________________________________ Libreoffice-commits mailing list Libreoffice-commits@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/libreoffice-commits