diff options
-rw-r--r-- | svl/qa/unit/test_lngmisc.cxx | 36 | ||||
-rw-r--r-- | svl/source/misc/lngmisc.cxx | 40 |
2 files changed, 51 insertions, 25 deletions
diff --git a/svl/qa/unit/test_lngmisc.cxx b/svl/qa/unit/test_lngmisc.cxx index 6411e208cd5e..d982f449be1a 100644 --- a/svl/qa/unit/test_lngmisc.cxx +++ b/svl/qa/unit/test_lngmisc.cxx @@ -17,14 +17,14 @@ namespace private: void testRemoveHyphens(); void testRemoveControlChars(); - // void testReplaceControlChars(); + void testReplaceControlChars(); // void testGetThesaurusReplaceText(); CPPUNIT_TEST_SUITE(LngMiscTest); CPPUNIT_TEST(testRemoveHyphens); CPPUNIT_TEST(testRemoveControlChars); - // CPPUNIT_TEST(testReplaceControlChars); + CPPUNIT_TEST(testReplaceControlChars); // CPPUNIT_TEST(testGetThesaurusReplaceText); CPPUNIT_TEST_SUITE_END(); @@ -92,12 +92,40 @@ namespace CPPUNIT_ASSERT(str4.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(" "))); } - /* void LngMiscTest::testReplaceControlChars() { - 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::ReplaceControlChars(str1); + CPPUNIT_ASSERT(!bModified); + CPPUNIT_ASSERT(str1.isEmpty()); + + bModified = linguistic::ReplaceControlChars(str2); + CPPUNIT_ASSERT(!bModified); + CPPUNIT_ASSERT(str2.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("asdf"))); + + bModified = linguistic::ReplaceControlChars(str3); + CPPUNIT_ASSERT(bModified); + CPPUNIT_ASSERT(str3.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("asdf asdf"))); + + bModified = linguistic::ReplaceControlChars(str4); + CPPUNIT_ASSERT(bModified); + CPPUNIT_ASSERT(str4.getLength() == 32); + for(int i = 0; i < 32; i++) + CPPUNIT_ASSERT(str4[i] == ' '); } + /* void LngMiscTest::testGetThesaurusReplaceText() { CPPUNIT_ASSERT(true); diff --git a/svl/source/misc/lngmisc.cxx b/svl/source/misc/lngmisc.cxx index 5821db685e07..49a44334de62 100644 --- a/svl/source/misc/lngmisc.cxx +++ b/svl/source/misc/lngmisc.cxx @@ -82,29 +82,27 @@ namespace linguistic // 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) + if (GetNumControlChars(rTxt) == 0) + return false; + + sal_Int32 n = rTxt.getLength(); + + rtl::OUStringBuffer aBuf(n); + aBuf.setLength(n); + + sal_Int32 j = 0; + for (sal_Int32 i = 0; i < n && j < n; ++i) { - sal_Int32 nLen = rTxt.getLength(); - rtl::OUStringBuffer aBuf( nLen ); - sal_Int32 nCnt = 0; - for (sal_Int32 i = 0; i < nLen; ++i) - { - 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; + if (CH_TXTATR_INWORD == rTxt[i]) + continue; + + aBuf[j++] = IsControlChar(rTxt[i]) ? ' ' : rTxt[i]; } - return bModified; + + aBuf.setLength(j); + rTxt = aBuf.makeStringAndClear(); + + return true; } String GetThesaurusReplaceText(const String &rText) |