diff options
author | Noel Grandin <noelgrandin@gmail.com> | 2022-08-01 18:58:55 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2022-08-01 20:53:28 +0200 |
commit | 75a862b887fb0b7ff633a396ee7f7f34c2c82964 (patch) | |
tree | 50361f282bfcd20ff04f97b59806b95029a9a405 | |
parent | f8d764b138fddde719a04683f9bd3720c21e0656 (diff) |
use more string_view when dealing with attributes
which means we don't need to call strlen on them, since we already
know how long they are.
Change-Id: Iefc76f38a23250e87a65c27df3634d562464a760
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/137679
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
-rw-r--r-- | include/oox/helper/attributelist.hxx | 2 | ||||
-rw-r--r-- | include/sax/fastattribs.hxx | 3 | ||||
-rw-r--r-- | oox/source/helper/attributelist.cxx | 17 | ||||
-rw-r--r-- | sax/source/tools/fastattribs.cxx | 11 | ||||
-rw-r--r-- | sc/source/filter/inc/addressconverter.hxx | 6 | ||||
-rw-r--r-- | sc/source/filter/oox/addressconverter.cxx | 9 | ||||
-rw-r--r-- | sc/source/filter/oox/sheetdatacontext.cxx | 4 | ||||
-rw-r--r-- | sw/source/filter/ww8/docxattributeoutput.cxx | 14 | ||||
-rw-r--r-- | writerfilter/source/ooxml/OOXMLFactory.cxx | 12 | ||||
-rw-r--r-- | writerfilter/source/ooxml/OOXMLPropertySet.cxx | 41 | ||||
-rw-r--r-- | writerfilter/source/ooxml/OOXMLPropertySet.hxx | 12 |
11 files changed, 62 insertions, 69 deletions
diff --git a/include/oox/helper/attributelist.hxx b/include/oox/helper/attributelist.hxx index d58305bffa54..65e7f4ee2cf3 100644 --- a/include/oox/helper/attributelist.hxx +++ b/include/oox/helper/attributelist.hxx @@ -145,7 +145,7 @@ public: passed default string if the attribute is missing. */ OUString getXString( sal_Int32 nAttrToken, const OUString& rDefault ) const; - const char* getChar( sal_Int32 nAttrToken ) const; + std::string_view getView( sal_Int32 nAttrToken ) const; /** Returns the double value of the specified attribute, or the passed diff --git a/include/sax/fastattribs.hxx b/include/sax/fastattribs.hxx index 24133a4f82ca..ef7e97884735 100644 --- a/include/sax/fastattribs.hxx +++ b/include/sax/fastattribs.hxx @@ -103,9 +103,8 @@ public: // performance sensitive shortcuts to avoid allocation ... bool getAsInteger( sal_Int32 nToken, sal_Int32 &rInt) const; bool getAsDouble( sal_Int32 nToken, double &rDouble) const; - bool getAsChar( sal_Int32 nToken, const char*& rPos ) const; + bool getAsView( sal_Int32 nToken, std::string_view& rPos ) const; sal_Int32 getAsIntegerByIndex( sal_Int32 nTokenIndex ) const; - const char* getAsCharByIndex( sal_Int32 nTokenIndex ) const; std::string_view getAsViewByIndex( sal_Int32 nTokenIndex ) const; OUString getValueByIndex( sal_Int32 nTokenIndex ) const; diff --git a/oox/source/helper/attributelist.cxx b/oox/source/helper/attributelist.cxx index 043f0689fd40..0215588b2b97 100644 --- a/oox/source/helper/attributelist.cxx +++ b/oox/source/helper/attributelist.cxx @@ -227,15 +227,15 @@ std::optional< sal_Int32 > AttributeList::getIntegerHex( sal_Int32 nAttrToken ) std::optional< bool > AttributeList::getBool( sal_Int32 nAttrToken ) const { - const char *pAttr; + std::string_view pAttr; // catch the common cases as quickly as possible first - bool bHasAttr = getAttribList()->getAsChar( nAttrToken, pAttr ); + bool bHasAttr = getAttribList()->getAsView( nAttrToken, pAttr ); if( !bHasAttr ) return std::optional< bool >(); - if( !strcmp( pAttr, "false" ) ) + if( pAttr == "false" ) return std::optional< bool >( false ); - if( !strcmp( pAttr, "true" ) ) + if( pAttr == "true" ) return std::optional< bool >( true ); // now for all the crazy stuff @@ -299,13 +299,10 @@ OUString AttributeList::getXString( sal_Int32 nAttrToken, const OUString& rDefau return getXString( nAttrToken ).value_or( rDefault ); } -const char* AttributeList::getChar( sal_Int32 nAttrToken ) const +std::string_view AttributeList::getView( sal_Int32 nAttrToken ) const { - const char* p = nullptr; - bool bValid = getAttribList()->getAsChar(nAttrToken, p); - if (!bValid) - p = nullptr; - + std::string_view p; + getAttribList()->getAsView(nAttrToken, p); return p; } diff --git a/sax/source/tools/fastattribs.cxx b/sax/source/tools/fastattribs.cxx index 3522f38f3ef5..d020e18de18c 100644 --- a/sax/source/tools/fastattribs.cxx +++ b/sax/source/tools/fastattribs.cxx @@ -242,7 +242,7 @@ bool FastAttributeList::getAsDouble( sal_Int32 nToken, double &rDouble) const return false; } -bool FastAttributeList::getAsChar( sal_Int32 nToken, const char*& rPos ) const +bool FastAttributeList::getAsView( sal_Int32 nToken, std::string_view& rPos ) const { for (size_t i = 0, n = maAttributeTokens.size(); i < n; ++i) { @@ -250,19 +250,14 @@ bool FastAttributeList::getAsChar( sal_Int32 nToken, const char*& rPos ) const continue; sal_Int32 nOffset = maAttributeValues[i]; - rPos = mpChunk + nOffset; + size_t nValueLen = maAttributeValues[i + 1] - maAttributeValues[i] - 1; + rPos = { mpChunk + nOffset, nValueLen }; return true; } return false; } -const char* FastAttributeList::getAsCharByIndex( sal_Int32 nTokenIndex ) const -{ - sal_Int32 nOffset = maAttributeValues[nTokenIndex]; - return mpChunk + nOffset; -} - std::string_view FastAttributeList::getAsViewByIndex( sal_Int32 nTokenIndex ) const { sal_Int32 nOffset = maAttributeValues[nTokenIndex]; diff --git a/sc/source/filter/inc/addressconverter.hxx b/sc/source/filter/inc/addressconverter.hxx index af1bc340c47b..e014167839d0 100644 --- a/sc/source/filter/inc/addressconverter.hxx +++ b/sc/source/filter/inc/addressconverter.hxx @@ -123,7 +123,7 @@ public: sal_Int32 nLength = SAL_MAX_INT32 ); static bool parseOoxAddress2d( - sal_Int32& ornColumn, sal_Int32& ornRow, const char* pStr ); + sal_Int32& ornColumn, sal_Int32& ornRow, std::string_view pStr ); /** Tries to parse the passed string for a 2d cell range in A1 notation. @@ -214,7 +214,7 @@ public: sal_Int16 nSheet ); static bool convertToCellAddressUnchecked( - ScAddress& orAddress, const char* pStr, sal_Int16 nSheet ); + ScAddress& orAddress, std::string_view pStr, sal_Int16 nSheet ); /** Tries to convert the passed string to a single cell address. @@ -233,7 +233,7 @@ public: bool convertToCellAddress( ScAddress& rAddress, - const char* pStr, sal_Int16 nSheet, bool bTrackOverflow ); + std::string_view pStr, sal_Int16 nSheet, bool bTrackOverflow ); /** Returns a valid cell address by moving it into allowed dimensions. diff --git a/sc/source/filter/oox/addressconverter.cxx b/sc/source/filter/oox/addressconverter.cxx index b3fc5b16722d..b13a4a5cd896 100644 --- a/sc/source/filter/oox/addressconverter.cxx +++ b/sc/source/filter/oox/addressconverter.cxx @@ -138,13 +138,14 @@ bool AddressConverter::parseOoxAddress2d( return (ornColumn >= 0) && (ornRow >= 0); } -bool AddressConverter::parseOoxAddress2d( sal_Int32& ornColumn, sal_Int32& ornRow, const char* pStr ) +bool AddressConverter::parseOoxAddress2d( sal_Int32& ornColumn, sal_Int32& ornRow, std::string_view aStr ) { ornColumn = ornRow = 0; enum { STATE_COL, STATE_ROW } eState = STATE_COL; - while (*pStr) + const char* pStr = aStr.data(); + while (pStr != aStr.data() + aStr.size()) { char cChar = *pStr; switch( eState ) @@ -267,7 +268,7 @@ bool AddressConverter::convertToCellAddressUnchecked( ScAddress& orAddress, } bool AddressConverter::convertToCellAddressUnchecked( - ScAddress& orAddress, const char* pStr, sal_Int16 nSheet ) + ScAddress& orAddress, std::string_view pStr, sal_Int16 nSheet ) { orAddress.SetTab(nSheet); sal_Int32 nCol = 0; @@ -289,7 +290,7 @@ bool AddressConverter::convertToCellAddress( ScAddress& orAddress, bool AddressConverter::convertToCellAddress( ScAddress& rAddress, - const char* pStr, sal_Int16 nSheet, bool bTrackOverflow ) + std::string_view pStr, sal_Int16 nSheet, bool bTrackOverflow ) { if (!convertToCellAddressUnchecked(rAddress, pStr, nSheet)) return false; diff --git a/sc/source/filter/oox/sheetdatacontext.cxx b/sc/source/filter/oox/sheetdatacontext.cxx index d6c4c7a266ba..c1a270afb41a 100644 --- a/sc/source/filter/oox/sheetdatacontext.cxx +++ b/sc/source/filter/oox/sheetdatacontext.cxx @@ -312,9 +312,9 @@ void SheetDataContext::importRow( const AttributeList& rAttribs ) bool SheetDataContext::importCell( const AttributeList& rAttribs ) { bool bValid = true; - const char* p = rAttribs.getChar(XML_r); + std::string_view p = rAttribs.getView(XML_r); - if (!p) + if (p.empty()) { ++mnCol; ScAddress aAddress( mnCol, mnRow, mnSheet ); diff --git a/sw/source/filter/ww8/docxattributeoutput.cxx b/sw/source/filter/ww8/docxattributeoutput.cxx index b870cfabf8f1..38c0bcd2b224 100644 --- a/sw/source/filter/ww8/docxattributeoutput.cxx +++ b/sw/source/filter/ww8/docxattributeoutput.cxx @@ -3213,13 +3213,13 @@ void DocxAttributeOutput::WriteCollectedRunProperties() if (m_nCharTransparence != 0 && m_pColorAttrList && m_aTextEffectsGrabBag.empty()) { - const char* pVal = nullptr; - m_pColorAttrList->getAsChar(FSNS(XML_w, XML_val), pVal); - if (pVal != nullptr && std::string_view("auto") != pVal) + std::string_view pVal; + m_pColorAttrList->getAsView(FSNS(XML_w, XML_val), pVal); + if (!pVal.empty() && pVal != "auto") { m_pSerializer->startElementNS(XML_w14, XML_textFill); m_pSerializer->startElementNS(XML_w14, XML_solidFill); - m_pSerializer->startElementNS(XML_w14, XML_srgbClr, FSNS(XML_w14, XML_val), pVal); + m_pSerializer->startElementNS(XML_w14, XML_srgbClr, FSNS(XML_w14, XML_val), pVal.data()); sal_Int32 nTransparence = m_nCharTransparence * oox::drawingml::MAX_PERCENT / 255.0; m_pSerializer->singleElementNS(XML_w14, XML_alpha, FSNS(XML_w14, XML_val), OString::number(nTransparence)); m_pSerializer->endElementNS(XML_w14, XML_srgbClr); @@ -8075,10 +8075,10 @@ void DocxAttributeOutput::CharColor( const SvxColorItem& rColor ) const Color aColor( rColor.GetValue() ); OString aColorString = msfilter::util::ConvertColor( aColor ); - const char* pExistingValue(nullptr); - if (m_pColorAttrList.is() && m_pColorAttrList->getAsChar(FSNS(XML_w, XML_val), pExistingValue)) + std::string_view pExistingValue; + if (m_pColorAttrList.is() && m_pColorAttrList->getAsView(FSNS(XML_w, XML_val), pExistingValue)) { - assert(aColorString.equalsL(pExistingValue, rtl_str_getLength(pExistingValue))); + assert(aColorString.equalsL(pExistingValue.data(), pExistingValue.size())); return; } diff --git a/writerfilter/source/ooxml/OOXMLFactory.cxx b/writerfilter/source/ooxml/OOXMLFactory.cxx index efedbe56d285..b11cf07c2900 100644 --- a/writerfilter/source/ooxml/OOXMLFactory.cxx +++ b/writerfilter/source/ooxml/OOXMLFactory.cxx @@ -59,7 +59,7 @@ void OOXMLFactory::attributes(OOXMLFastContextHandler * pHandler, switch (pAttr->m_nResource) { case ResourceType::Boolean: - xValue = OOXMLBooleanValue::Create(rAttribs.getAsCharByIndex(nAttrIndex)); + xValue = OOXMLBooleanValue::Create(rAttribs.getAsViewByIndex(nAttrIndex)); break; case ResourceType::String: xValue = new OOXMLStringValue(rAttribs.getValueByIndex(nAttrIndex)); @@ -68,14 +68,14 @@ void OOXMLFactory::attributes(OOXMLFastContextHandler * pHandler, xValue = OOXMLIntegerValue::Create(rAttribs.getAsIntegerByIndex(nAttrIndex)); break; case ResourceType::Hex: - xValue = new OOXMLHexValue(rAttribs.getAsCharByIndex(nAttrIndex)); + xValue = new OOXMLHexValue(rAttribs.getAsViewByIndex(nAttrIndex)); break; case ResourceType::HexColor: - xValue = new OOXMLHexColorValue(rAttribs.getAsCharByIndex(nAttrIndex)); + xValue = new OOXMLHexColorValue(rAttribs.getAsViewByIndex(nAttrIndex)); break; case ResourceType::TwipsMeasure_asSigned: case ResourceType::TwipsMeasure_asZero: - xValue = new OOXMLTwipsMeasureValue(rAttribs.getAsCharByIndex(nAttrIndex)); + xValue = new OOXMLTwipsMeasureValue(rAttribs.getAsViewByIndex(nAttrIndex)); if (xValue->getInt() < 0) { if (pAttr->m_nResource == ResourceType::TwipsMeasure_asZero) @@ -83,10 +83,10 @@ void OOXMLFactory::attributes(OOXMLFastContextHandler * pHandler, } break; case ResourceType::HpsMeasure: - xValue = new OOXMLHpsMeasureValue(rAttribs.getAsCharByIndex(nAttrIndex)); + xValue = new OOXMLHpsMeasureValue(rAttribs.getAsViewByIndex(nAttrIndex)); break; case ResourceType::MeasurementOrPercent: - xValue = new OOXMLMeasurementOrPercentValue(rAttribs.getAsCharByIndex(nAttrIndex)); + xValue = new OOXMLMeasurementOrPercentValue(rAttribs.getAsViewByIndex(nAttrIndex)); break; case ResourceType::List: if (sal_uInt32 nValue; diff --git a/writerfilter/source/ooxml/OOXMLPropertySet.cxx b/writerfilter/source/ooxml/OOXMLPropertySet.cxx index 7a7d9a7d9150..230a58f34ecf 100644 --- a/writerfilter/source/ooxml/OOXMLPropertySet.cxx +++ b/writerfilter/source/ooxml/OOXMLPropertySet.cxx @@ -24,6 +24,7 @@ #include <com/sun/star/drawing/XShape.hpp> #include <sax/tools/converter.hxx> #include <tools/color.hxx> +#include <o3tl/string_view.hxx> #include <utility> namespace writerfilter::ooxml @@ -202,13 +203,13 @@ OOXMLValue * OOXMLBinaryValue::clone() const class OOXMLBooleanValue */ -static bool GetBooleanValue(const char *pValue) +static bool GetBooleanValue(std::string_view pValue) { - return !strcmp(pValue, "true") - || !strcmp(pValue, "True") - || !strcmp(pValue, "1") - || !strcmp(pValue, "on") - || !strcmp(pValue, "On"); + return pValue == "true" + || pValue == "True" + || pValue == "1" + || pValue == "on" + || pValue == "On"; } OOXMLValue::Pointer_t const & OOXMLBooleanValue::Create(bool bValue) @@ -219,7 +220,7 @@ OOXMLValue::Pointer_t const & OOXMLBooleanValue::Create(bool bValue) return bValue ? True : False; } -OOXMLValue::Pointer_t const & OOXMLBooleanValue::Create(const char *pValue) +OOXMLValue::Pointer_t const & OOXMLBooleanValue::Create(std::string_view pValue) { return Create (GetBooleanValue(pValue)); } @@ -540,8 +541,8 @@ OOXMLHexValue::OOXMLHexValue(sal_uInt32 nValue) { } -OOXMLHexValue::OOXMLHexValue(const char * pValue) -: mnValue(rtl_str_toUInt32(pValue, 16)) +OOXMLHexValue::OOXMLHexValue(std::string_view pValue) +: mnValue(o3tl::toUInt32(pValue, 16)) { } @@ -572,23 +573,23 @@ string OOXMLHexValue::toString() const /* class OOXMLHexColorValue */ -OOXMLHexColorValue::OOXMLHexColorValue(const char * pValue) +OOXMLHexColorValue::OOXMLHexColorValue(std::string_view pValue) : OOXMLHexValue(sal_uInt32(COL_AUTO)) { - if (!strcmp(pValue, "auto")) + if (pValue == "auto") return; - mnValue = rtl_str_toUInt32(pValue, 16); + mnValue = o3tl::toUInt32(pValue, 16); // Convert hash-encoded values (like #FF0080) - const sal_Int32 nLen = strlen(pValue); + const sal_Int32 nLen = pValue.size(); if ( !mnValue && nLen > 1 && pValue[0] == '#' ) { sal_Int32 nColor(COL_AUTO); // Word appears to require strict 6 digit length, else it ignores it if ( nLen == 7 ) { - const OUString sHashColor(pValue, nLen, RTL_TEXTENCODING_ASCII_US); + const OUString sHashColor(pValue.data(), nLen, RTL_TEXTENCODING_ASCII_US); sax::Converter::convertColor( nColor, sHashColor ); } mnValue = nColor; @@ -597,11 +598,11 @@ OOXMLHexColorValue::OOXMLHexColorValue(const char * pValue) // OOXMLUniversalMeasureValue // ECMA-376 5th ed. Part 1 , 22.9.2.15 -OOXMLUniversalMeasureValue::OOXMLUniversalMeasureValue(const char * pValue, sal_uInt32 npPt) +OOXMLUniversalMeasureValue::OOXMLUniversalMeasureValue(std::string_view pValue, sal_uInt32 npPt) { - double val = rtl_str_toDouble(pValue); // will ignore the trailing unit + double val = o3tl::toDouble(pValue); // will ignore the trailing unit - int nLen = strlen(pValue); + int nLen = pValue.size(); if (nLen > 2 && pValue[nLen-2] == 'p' && pValue[nLen-1] == 't') @@ -656,11 +657,11 @@ string OOXMLUniversalMeasureValue::toString() const // OOXMLMeasurementOrPercentValue // ECMA-376 5th ed. Part 1 , 17.18.107; 17.18.11 -OOXMLMeasurementOrPercentValue::OOXMLMeasurementOrPercentValue(const char * pValue) +OOXMLMeasurementOrPercentValue::OOXMLMeasurementOrPercentValue(std::string_view pValue) { - double val = rtl_str_toDouble(pValue); // will ignore the trailing unit + double val = o3tl::toDouble(pValue); // will ignore the trailing unit - int nLen = strlen(pValue); + int nLen = pValue.size(); if (nLen > 1 && pValue[nLen - 1] == '%') { diff --git a/writerfilter/source/ooxml/OOXMLPropertySet.hxx b/writerfilter/source/ooxml/OOXMLPropertySet.hxx index d13fd8c5bb61..3393050afa23 100644 --- a/writerfilter/source/ooxml/OOXMLPropertySet.hxx +++ b/writerfilter/source/ooxml/OOXMLPropertySet.hxx @@ -101,7 +101,7 @@ class OOXMLBooleanValue final : public OOXMLValue public: static OOXMLValue::Pointer_t const& Create(bool bValue); - static OOXMLValue::Pointer_t const& Create(const char* pValue); + static OOXMLValue::Pointer_t const& Create(std::string_view pValue); virtual ~OOXMLBooleanValue() override; @@ -262,7 +262,7 @@ protected: public: explicit OOXMLHexValue(sal_uInt32 nValue); - explicit OOXMLHexValue(const char* pValue); + explicit OOXMLHexValue(std::string_view pValue); virtual ~OOXMLHexValue() override; OOXMLHexValue(OOXMLHexValue const&) = default; @@ -280,7 +280,7 @@ public: class OOXMLHexColorValue final : public OOXMLHexValue { public: - explicit OOXMLHexColorValue(const char* pValue); + explicit OOXMLHexColorValue(std::string_view pValue); }; class OOXMLUniversalMeasureValue : public OOXMLValue @@ -289,7 +289,7 @@ private: int mnValue; public: - OOXMLUniversalMeasureValue(const char* pValue, sal_uInt32 npPt); + OOXMLUniversalMeasureValue(std::string_view pValue, sal_uInt32 npPt); virtual ~OOXMLUniversalMeasureValue() override; OOXMLUniversalMeasureValue(OOXMLUniversalMeasureValue const&) = default; @@ -307,7 +307,7 @@ public: template <sal_uInt32 npPt> class OOXMLNthPtMeasureValue final : public OOXMLUniversalMeasureValue { public: - explicit OOXMLNthPtMeasureValue(const char* pValue) + explicit OOXMLNthPtMeasureValue(std::string_view pValue) : OOXMLUniversalMeasureValue(pValue, npPt) { } @@ -325,7 +325,7 @@ class OOXMLMeasurementOrPercentValue final : public OOXMLValue int mnValue; public: - explicit OOXMLMeasurementOrPercentValue(const char* pValue); + explicit OOXMLMeasurementOrPercentValue(std::string_view pValue); virtual int getInt() const override; virtual OOXMLValue* clone() const override { return new OOXMLMeasurementOrPercentValue(*this); } |