hwpfilter/source/formula.cxx |   10 +++++-----
 hwpfilter/source/mapping.h   |   28 ++++++++++------------------
 2 files changed, 15 insertions(+), 23 deletions(-)

New commits:
commit 7b9e27da2033192c628b23e4e1686209e951dadb
Author:     Simon Chenery <simon_chen...@yahoo.com>
AuthorDate: Sun Feb 23 11:02:10 2025 +0100
Commit:     Hossein <hoss...@libreoffice.org>
CommitDate: Wed Mar 12 01:41:16 2025 +0100

    tdf#147021 avoid use of SAL_N_ELEMENTS macro
    
    Refactor getMathMLEntity() to use std::string_view
    and OUString for string handling, so that there is less
    string conversion when calling this function.
    
    Use std::find_if to find formula in array instead of a loop.
    
    Change-Id: I0ed98d33e62f6bc730ac750f4fd545d2f07145fc
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/182065
    Tested-by: Jenkins
    Reviewed-by: Hossein   <hoss...@libreoffice.org>

diff --git a/hwpfilter/source/formula.cxx b/hwpfilter/source/formula.cxx
index f6fd509d9a67..cc47e8be1037 100644
--- a/hwpfilter/source/formula.cxx
+++ b/hwpfilter/source/formula.cxx
@@ -216,7 +216,7 @@ void Formula::makeIdentifier(Node *res)
           indo;
 #else
           rstartEl(u"math:mi"_ustr, mxList);
-          
runistr(fromHcharStringToOUString(getMathMLEntity(tmp->value.get())));
+          runistr(getMathMLEntity(tmp->value.get()));
           rendEl(u"math:mi"_ustr);
 #endif
           break;
@@ -238,7 +238,7 @@ void Formula::makeIdentifier(Node *res)
           inds; fprintf(stderr,"<math:mo>%s</math:mo>
",tmp->value.get()); indo;
 #else
           rstartEl(u"math:mo"_ustr, mxList);
-          
runistr(fromHcharStringToOUString(getMathMLEntity(tmp->value.get())));
+          runistr(getMathMLEntity(tmp->value.get()));
           rendEl(u"math:mo"_ustr);
 #endif
           break;
@@ -399,7 +399,7 @@ void Formula::makeDecoration(Node *res)
      indo;
 #else
      rstartEl(u"math:mo"_ustr, mxList);
-     runistr(fromHcharStringToOUString(getMathMLEntity(tmp->value.get())));
+     runistr(getMathMLEntity(tmp->value.get()));
      rendEl(u"math:mo"_ustr);
 #endif
 
@@ -521,9 +521,9 @@ void Formula::makeFence(Node *res)
                 getMathMLEntity(tmp->next->next->value.get()).c_str());
 #else
      padd(u"open"_ustr, u"CDATA"_ustr,
-             OUString(reinterpret_cast<sal_Unicode const 
*>(getMathMLEntity(tmp->value.get()).c_str())));
+             getMathMLEntity(tmp->value.get()));
      padd(u"close"_ustr, u"CDATA"_ustr,
-             OUString(reinterpret_cast<sal_Unicode const 
*>(getMathMLEntity(tmp->next->next->value.get()).c_str())));
+             getMathMLEntity(tmp->next->next->value.get()));
      rstartEl(u"math:mfenced"_ustr, mxList);
      mxList->clear();
 #endif
diff --git a/hwpfilter/source/mapping.h b/hwpfilter/source/mapping.h
index 34d8687d9529..389ed6272bbd 100644
--- a/hwpfilter/source/mapping.h
+++ b/hwpfilter/source/mapping.h
@@ -21,19 +21,20 @@
 
 #include <string.h>
 #include <stdio.h>
+#include <algorithm>
 #include <sal/macros.h>
 
 #include "hwplib.h"
 
 
 struct FormulaEntry{
-     const char *tex;
+     std::string_view tex;
      hchar ucs;
 };
 
 // empty square
 #define DEFAULT_VALUE   0x25a1
-const struct FormulaEntry FormulaMapTab[] = {
+constexpr struct FormulaEntry FormulaMapTab[] = {
 /* Capital Greek */
 {"Alpha", 0x0391},
 {"Beta", 0x0392},
@@ -360,28 +361,19 @@ const struct FormulaEntry FormulaMapTab[] = {
 };
 
 #if OSL_DEBUG_LEVEL < 2
-hchar_string getMathMLEntity(const char *tex)
+OUString getMathMLEntity(std::string_view tex)
 {
-     static const size_t tabSize = SAL_N_ELEMENTS(FormulaMapTab);
-
-     hchar_string buf;
-     for (size_t i = 0 ; i < tabSize ; i++) {
-          if( !strcmp(tex, FormulaMapTab[i].tex ) ) {
-                buf.push_back(FormulaMapTab[i].ucs);
-                return buf;
-          }
+     auto it = std::find_if(std::begin(FormulaMapTab), std::end(FormulaMapTab),
+          [&tex](const auto& entry) { return entry.tex == tex; });
+     if ( it != std::end(FormulaMapTab) ) {
+          return OUString(it->ucs);
      }
 
-     size_t const len = strlen(tex);
-     for (size_t i = 0 ; i < len ; i++)
-     {
-         buf.push_back(tex[i]);
-     }
-     return buf;
+     return OUString::fromUtf8(tex);
 }
 
 #else
-::std::string getMathMLEntity(const char *tex)
+::std::string getMathMLEntity(std::string_view tex)
 {
      ::std::string buf;
      buf.append(tex);

Reply via email to