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

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