svl/inc/svl/lngmisc.hxx      |    2 -
 svl/qa/unit/test_lngmisc.cxx |   36 ++++++++++++++++++++++++++++----
 svl/source/misc/lngmisc.cxx  |   48 ++++++++++++++++++++-----------------------
 3 files changed, 56 insertions(+), 30 deletions(-)

New commits:
commit 83345ff8d8b709d6558238cd8e4f08323f3c97bc
Author: August Sodora <aug...@gmail.com>
Date:   Sat Nov 26 18:13:06 2011 -0500

    Simplification and accompanying tests

diff --git a/svl/qa/unit/test_lngmisc.cxx b/svl/qa/unit/test_lngmisc.cxx
index 6411e20..d982f44 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 5821db6..49a4433 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)
commit e280ff03f3a2fdcd452123f206007bac804d6799
Author: August Sodora <aug...@gmail.com>
Date:   Sat Nov 26 15:26:46 2011 -0500

    Remove unused parameter

diff --git a/svl/inc/svl/lngmisc.hxx b/svl/inc/svl/lngmisc.hxx
index 6c71b45..4a94bc7 100644
--- a/svl/inc/svl/lngmisc.hxx
+++ b/svl/inc/svl/lngmisc.hxx
@@ -62,7 +62,7 @@ SVL_DLLPRIVATE sal_Int32 GetNumControlChars( const 
rtl::OUString &rTxt );
 SVL_DLLPUBLIC bool  RemoveHyphens( rtl::OUString &rTxt );
 SVL_DLLPUBLIC bool  RemoveControlChars( rtl::OUString &rTxt );
 
-SVL_DLLPUBLIC bool  ReplaceControlChars( rtl::OUString &rTxt, sal_Char 
aRplcChar = ' ' );
+SVL_DLLPUBLIC bool ReplaceControlChars(rtl::OUString &rTxt);
 
 SVL_DLLPUBLIC String GetThesaurusReplaceText( const String &rText );
 
diff --git a/svl/source/misc/lngmisc.cxx b/svl/source/misc/lngmisc.cxx
index 55028e8..5821db6 100644
--- a/svl/source/misc/lngmisc.cxx
+++ b/svl/source/misc/lngmisc.cxx
@@ -73,7 +73,7 @@ namespace linguistic
         return true;
     }
 
-    bool ReplaceControlChars( rtl::OUString &rTxt, sal_Char /*aRplcChar*/ )
+    bool ReplaceControlChars(rtl::OUString &rTxt)
     {
         // non breaking field character
         static const sal_Char CH_TXTATR_INWORD = static_cast<sal_Char>(0x02);
commit 919abbfe9b1461e4accbdebe4a2475379d2d5731
Author: August Sodora <aug...@gmail.com>
Date:   Sat Nov 26 15:24:38 2011 -0500

    Avoid use of the preprocessor

diff --git a/svl/source/misc/lngmisc.cxx b/svl/source/misc/lngmisc.cxx
index 4065290..55028e8 100644
--- a/svl/source/misc/lngmisc.cxx
+++ b/svl/source/misc/lngmisc.cxx
@@ -73,11 +73,11 @@ namespace linguistic
         return true;
     }
 
-    // non breaking field character
-#define CH_TXTATR_INWORD    ((sal_Char) 0x02)
-
     bool ReplaceControlChars( rtl::OUString &rTxt, sal_Char /*aRplcChar*/ )
     {
+        // non breaking field character
+        static const sal_Char CH_TXTATR_INWORD = static_cast<sal_Char>(0x02);
+
         // the resulting string looks like this:
         // 1. non breaking field characters get removed
         // 2. remaining control characters will be replaced by ' '
_______________________________________________
Libreoffice-commits mailing list
Libreoffice-commits@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to