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   <hossein@libreoffice.org>
This commit is contained in:
Simon Chenery
2025-03-12 01:41:16 +01:00
committed by Hossein
parent 0176752f6d
commit 7b9e27da20
2 changed files with 15 additions and 23 deletions
+5 -5
View File
@@ -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>\n",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
+10 -18
View File
@@ -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);