diff options
author | August Sodora <augsod@gmail.com> | 2011-11-26 15:16:49 -0500 |
---|---|---|
committer | August Sodora <augsod@gmail.com> | 2011-11-26 15:16:49 -0500 |
commit | 734d838f8083e6939d577fa0d0b5ab96367aea5f (patch) | |
tree | 01c57ed859e5c22515b5af803127730c46ce1984 /svl | |
parent | f04882ba6a6d58ee45aa509359747f3c607a863b (diff) |
Simplification in lngmisc with accompanying tests
Diffstat (limited to 'svl')
-rw-r--r-- | svl/inc/svl/lngmisc.hxx | 1 | ||||
-rw-r--r-- | svl/qa/unit/test_lngmisc.cxx | 34 | ||||
-rw-r--r-- | svl/source/misc/lngmisc.cxx | 45 |
3 files changed, 47 insertions, 33 deletions
diff --git a/svl/inc/svl/lngmisc.hxx b/svl/inc/svl/lngmisc.hxx index a473ab69f910..6c71b45e2c0b 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 47f671a9dfe6..6411e208cd5e 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 3c32099db5ee..56c10661887c 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) |