Simplify FastAttributeList

Change-Id: Id89edb25e35527e8603c32e44fb2940721aeda58
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145562
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
This commit is contained in:
Mike Kaganski
2023-01-16 13:29:19 +03:00
parent f41d424915
commit 15fe7346ad
2 changed files with 23 additions and 45 deletions

View File

@@ -25,6 +25,7 @@
#include <com/sun/star/util/XCloneable.hpp> #include <com/sun/star/util/XCloneable.hpp>
#include <cppuhelper/implbase.hxx> #include <cppuhelper/implbase.hxx>
#include <o3tl/string_view.hxx>
#include <rtl/math.h> #include <rtl/math.h>
#include <sax/saxdllapi.h> #include <sax/saxdllapi.h>
@@ -105,9 +106,18 @@ public:
bool getAsInteger( sal_Int32 nToken, sal_Int32 &rInt) const; bool getAsInteger( sal_Int32 nToken, sal_Int32 &rInt) const;
bool getAsDouble( sal_Int32 nToken, double &rDouble) const; bool getAsDouble( sal_Int32 nToken, double &rDouble) const;
bool getAsView( sal_Int32 nToken, std::string_view& rPos ) const; bool getAsView( sal_Int32 nToken, std::string_view& rPos ) const;
sal_Int32 getAsIntegerByIndex( sal_Int32 nTokenIndex ) const; sal_Int32 getAsIntegerByIndex( sal_Int32 nTokenIndex ) const
std::string_view getAsViewByIndex( sal_Int32 nTokenIndex ) const; {
OUString getValueByIndex( sal_Int32 nTokenIndex ) const; return o3tl::toInt32(getAsViewByIndex(nTokenIndex));
}
std::string_view getAsViewByIndex( sal_Int32 nTokenIndex ) const
{
return std::string_view(getFastAttributeValue(nTokenIndex), AttributeValueLength(nTokenIndex));
}
OUString getValueByIndex( sal_Int32 nTokenIndex ) const
{
return OStringToOUString(getAsViewByIndex(nTokenIndex), RTL_TEXTENCODING_UTF8);
}
// XFastAttributeList // XFastAttributeList
virtual sal_Bool SAL_CALL hasAttribute( ::sal_Int32 Token ) override; virtual sal_Bool SAL_CALL hasAttribute( ::sal_Int32 Token ) override;
@@ -168,14 +178,12 @@ public:
sal_Int32 toInt32() const sal_Int32 toInt32() const
{ {
assert(mnIdx < mrList.maAttributeTokens.size()); assert(mnIdx < mrList.maAttributeTokens.size());
return rtl_str_toInt32(mrList.getFastAttributeValue(mnIdx), 10); return mrList.getAsIntegerByIndex(mnIdx);
} }
double toDouble() const double toDouble() const
{ {
assert(mnIdx < mrList.maAttributeTokens.size()); assert(mnIdx < mrList.maAttributeTokens.size());
const char* pStr = mrList.getFastAttributeValue(mnIdx); return o3tl::toDouble(mrList.getAsViewByIndex(mnIdx));
sal_Int32 nLen = mrList.AttributeValueLength(mnIdx);
return rtl_math_stringToDouble(pStr, pStr + nLen, '.', 0, nullptr, nullptr);
} }
bool toBoolean() const bool toBoolean() const
{ {
@@ -185,9 +193,7 @@ public:
OUString toString() const OUString toString() const
{ {
assert(mnIdx < mrList.maAttributeTokens.size()); assert(mnIdx < mrList.maAttributeTokens.size());
return OUString(mrList.getFastAttributeValue(mnIdx), return mrList.getValueByIndex(mnIdx);
mrList.AttributeValueLength(mnIdx),
RTL_TEXTENCODING_UTF8);
} }
const char* toCString() const const char* toCString() const
{ {
@@ -202,7 +208,7 @@ public:
std::string_view toView() const std::string_view toView() const
{ {
assert(mnIdx < mrList.maAttributeTokens.size()); assert(mnIdx < mrList.maAttributeTokens.size());
return std::string_view(mrList.getFastAttributeValue(mnIdx), mrList.AttributeValueLength(mnIdx)); return mrList.getAsViewByIndex(mnIdx);
} }
bool isString(const char *str) const bool isString(const char *str) const
{ {

View File

@@ -210,33 +210,19 @@ bool FastAttributeList::getAsInteger( sal_Int32 nToken, sal_Int32 &rInt) const
for (size_t i = 0, n = maAttributeTokens.size(); i < n; ++i) for (size_t i = 0, n = maAttributeTokens.size(); i < n; ++i)
if (maAttributeTokens[i] == nToken) if (maAttributeTokens[i] == nToken)
{ {
sal_Int64 nVal = rtl_str_toInt64_WithLength( getFastAttributeValue(i), 10, AttributeValueLength(i) ); rInt = getAsIntegerByIndex(i);
if (nVal < SAL_MIN_INT32 || nVal > SAL_MAX_INT32) {
nVal = 0;
}
rInt = nVal;
return true; return true;
} }
return false; return false;
} }
sal_Int32 FastAttributeList::getAsIntegerByIndex( sal_Int32 nTokenIndex ) const
{
sal_Int64 n = rtl_str_toInt64_WithLength( getFastAttributeValue(nTokenIndex), 10, AttributeValueLength(nTokenIndex) );
if (n < SAL_MIN_INT32 || n > SAL_MAX_INT32) {
n = 0;
}
return n;
}
bool FastAttributeList::getAsDouble( sal_Int32 nToken, double &rDouble) const bool FastAttributeList::getAsDouble( sal_Int32 nToken, double &rDouble) const
{ {
rDouble = 0.0; rDouble = 0.0;
for (size_t i = 0, n = maAttributeTokens.size(); i < n; ++i) for (size_t i = 0, n = maAttributeTokens.size(); i < n; ++i)
if (maAttributeTokens[i] == nToken) if (maAttributeTokens[i] == nToken)
{ {
auto const p = getFastAttributeValue(i); rDouble = o3tl::toDouble(getAsViewByIndex(i));
rDouble = rtl_math_stringToDouble( p, p + AttributeValueLength(i), '.', 0, nullptr, nullptr );
return true; return true;
} }
return false; return false;
@@ -249,41 +235,27 @@ bool FastAttributeList::getAsView( sal_Int32 nToken, std::string_view& rPos ) co
if (maAttributeTokens[i] != nToken) if (maAttributeTokens[i] != nToken)
continue; continue;
sal_Int32 nOffset = maAttributeValues[i]; rPos = getAsViewByIndex(i);
size_t nValueLen = maAttributeValues[i + 1] - maAttributeValues[i] - 1;
rPos = { mpChunk + nOffset, nValueLen };
return true; return true;
} }
return false; return false;
} }
std::string_view FastAttributeList::getAsViewByIndex( sal_Int32 nTokenIndex ) const
{
sal_Int32 nOffset = maAttributeValues[nTokenIndex];
size_t nValueLen = maAttributeValues[nTokenIndex + 1] - maAttributeValues[nTokenIndex] - 1;
return { mpChunk + nOffset, nValueLen };
}
OUString FastAttributeList::getValue( ::sal_Int32 Token ) OUString FastAttributeList::getValue( ::sal_Int32 Token )
{ {
for (size_t i = 0, n = maAttributeTokens.size(); i < n; ++i) for (size_t i = 0, n = maAttributeTokens.size(); i < n; ++i)
if (maAttributeTokens[i] == Token) if (maAttributeTokens[i] == Token)
return OUString( getFastAttributeValue(i), AttributeValueLength(i), RTL_TEXTENCODING_UTF8 ); return getValueByIndex(i);
throw SAXException("FastAttributeList::getValue: unknown token " + OUString::number(Token), nullptr, Any()); throw SAXException("FastAttributeList::getValue: unknown token " + OUString::number(Token), nullptr, Any());
} }
OUString FastAttributeList::getValueByIndex( ::sal_Int32 nTokenIndex ) const
{
return OUString( getFastAttributeValue(nTokenIndex), AttributeValueLength(nTokenIndex), RTL_TEXTENCODING_UTF8 );
}
OUString FastAttributeList::getOptionalValue( ::sal_Int32 Token ) OUString FastAttributeList::getOptionalValue( ::sal_Int32 Token )
{ {
for (size_t i = 0, n = maAttributeTokens.size(); i < n; ++i) for (size_t i = 0, n = maAttributeTokens.size(); i < n; ++i)
if (maAttributeTokens[i] == Token) if (maAttributeTokens[i] == Token)
return OUString( getFastAttributeValue(i), AttributeValueLength(i), RTL_TEXTENCODING_UTF8 ); return getValueByIndex(i);
return OUString(); return OUString();
} }
@@ -305,7 +277,7 @@ Sequence< FastAttribute > FastAttributeList::getFastAttributes( )
for (size_t i = 0, n = maAttributeTokens.size(); i < n; ++i) for (size_t i = 0, n = maAttributeTokens.size(); i < n; ++i)
{ {
pAttr->Token = maAttributeTokens[i]; pAttr->Token = maAttributeTokens[i];
pAttr->Value = OUString( getFastAttributeValue(i), AttributeValueLength(i), RTL_TEXTENCODING_UTF8 ); pAttr->Value = getValueByIndex(i);
pAttr++; pAttr++;
} }
return aSeq; return aSeq;