editeng/source/items/numitem.cxx |   48 ++++++++-------------------------------
 1 file changed, 11 insertions(+), 37 deletions(-)

New commits:
commit 3cc41346f5529026d7d38f2863ae3d84948e6ab7
Author:     Hossein <hoss...@libreoffice.org>
AuthorDate: Tue Mar 15 09:07:59 2022 +0100
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Tue Mar 15 13:50:38 2022 +0100

    Better CreateRomanString() to create Roman numbers
    
    The previous implementation of CreateRomanString() was complex and
    was using various tricks and pointers to create Roman representation
    of the integers.
    
    The new implementation is simpler, and easier to understand.
    
    Change-Id: Ibda4f8cfab02fd360e718736c957b3e04110c57a
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/131573
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/editeng/source/items/numitem.cxx b/editeng/source/items/numitem.cxx
index 7d6fa5fe23ae..c4220c78a4b5 100644
--- a/editeng/source/items/numitem.cxx
+++ b/editeng/source/items/numitem.cxx
@@ -539,48 +539,22 @@ Size SvxNumberFormat::GetGraphicSizeMM100(const Graphic* 
pGraphic)
 
 OUString SvxNumberFormat::CreateRomanString( sal_Int32 nNo, bool bUpper )
 {
-    nNo %= 4000;            // more can not be displayed
-//      i, ii, iii, iv, v, vi, vii, vii, viii, ix
-//                          (Dummy),1000,500,100,50,10,5,1
-    const char *cRomanArr = bUpper
-                        ? "MDCLXVI--"   // +2 Dummy entries!
-                        : "mdclxvi--";  // +2 Dummy entries!
-
     OUStringBuffer sRet;
-    sal_uInt16 nMask = 1000;
-    while( nMask )
-    {
-        sal_uInt8 nNumber = sal_uInt8(nNo / nMask);
-        sal_uInt8 nDiff = 1;
-        nNo %= nMask;
 
-        if( 5 < nNumber )
-        {
-            if( nNumber < 9 )
-                sRet.append(*(cRomanArr-1));
-            ++nDiff;
-            nNumber -= 5;
-        }
-        switch( nNumber )
+    constexpr char romans[][13] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", 
"X", "IX", "V", "IV", "I"};
+    constexpr sal_Int32 values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 
9, 5, 4, 1};
+
+    for (size_t i = 0; i < std::size(romans); ++i)
+    {
+        while(nNo - values[i] >= 0)
         {
-        case 3:     { sRet.append(*cRomanArr); [[fallthrough]]; }
-        case 2:     { sRet.append(*cRomanArr); [[fallthrough]]; }
-        case 1:     { sRet.append(*cRomanArr); }
-                    break;
-
-        case 4:     {
-                        sRet.append(*cRomanArr);
-                        sRet.append(*(cRomanArr-nDiff));
-                    }
-                    break;
-        case 5:     { sRet.append(*(cRomanArr-nDiff)); }
-                    break;
+            sRet.appendAscii(romans[i]);
+            nNo -= values[i];
         }
-
-        nMask /= 10;            // for the next decade
-        cRomanArr += 2;
     }
-    return sRet.makeStringAndClear();
+
+    return bUpper ? sRet.makeStringAndClear()
+                  : sRet.makeStringAndClear().toAsciiLowerCase();
 }
 
 void SvxNumberFormat::SetListFormat(const OUString& rPrefix, const OUString& 
rSuffix, int nLevel)

Reply via email to