include/tools/globname.hxx | 40 +-------- tools/source/ref/globname.cxx | 177 +++++++++++------------------------------- 2 files changed, 55 insertions(+), 162 deletions(-)
New commits: commit 4e0ba699ab3ba0294acd2589507b50fab82c98f5 Author: Mike Kaganski <mike.kagan...@collabora.com> AuthorDate: Tue Feb 8 18:21:22 2022 +0300 Commit: Mike Kaganski <mike.kagan...@collabora.com> CommitDate: Wed Feb 9 06:11:39 2022 +0100 Simplify SvGlobalName Its data only takes 16 bytes, the same as std::string_view on 64-bit platforms, which is considered trivial. No need to use cow_wrapper that would itself take 8 bytes, and have the performance penalty. Also reuse the conversion to sequence from comphelper. Change-Id: I3e3177ea759bf22d099aaa5402559196c5934ee0 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129679 Tested-by: Jenkins Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com> diff --git a/include/tools/globname.hxx b/include/tools/globname.hxx index 88a5e492d923..8ff2a8b28220 100644 --- a/include/tools/globname.hxx +++ b/include/tools/globname.hxx @@ -21,7 +21,6 @@ #include <tools/toolsdllapi.h> #include <com/sun/star/uno/Sequence.hxx> -#include <o3tl/cow_wrapper.hxx> struct SAL_WARN_UNUSED SvGUID { @@ -31,39 +30,13 @@ struct SAL_WARN_UNUSED SvGUID sal_uInt8 Data4[8]; }; -struct SAL_WARN_UNUSED ImpSvGlobalName -{ - struct SvGUID szData = {}; - - ImpSvGlobalName(const SvGUID &rData) - : szData(rData) - { - } - ImpSvGlobalName(sal_uInt32 n1, sal_uInt16 n2, sal_uInt16 n3, - sal_uInt8 b8, sal_uInt8 b9, sal_uInt8 b10, sal_uInt8 b11, - sal_uInt8 b12, sal_uInt8 b13, sal_uInt8 b14, sal_uInt8 b15); - ImpSvGlobalName( const ImpSvGlobalName & rObj ); - ImpSvGlobalName() = default; - - bool operator == ( const ImpSvGlobalName & rObj ) const; -}; - class SvStream; class SAL_WARN_UNUSED TOOLS_DLLPUBLIC SvGlobalName { - ::o3tl::cow_wrapper< ImpSvGlobalName > pImp; - public: - SvGlobalName(); - SvGlobalName( const SvGlobalName & rObj ) : - pImp( rObj.pImp ) - { - } - SvGlobalName( SvGlobalName && rObj ) noexcept : - pImp( std::move(rObj.pImp) ) - { - } + SvGlobalName() = default; + SvGlobalName(const SvGlobalName& rObj) = default; SvGlobalName( sal_uInt32 n1, sal_uInt16 n2, sal_uInt16 n3, sal_uInt8 b8, sal_uInt8 b9, sal_uInt8 b10, sal_uInt8 b11, @@ -74,9 +47,7 @@ public: SvGlobalName( const SvGUID & rId ); - SvGlobalName & operator = ( const SvGlobalName & rObj ); - SvGlobalName & operator = ( SvGlobalName && rObj ) noexcept; - ~SvGlobalName(); + SvGlobalName & operator = ( const SvGlobalName & rObj ) = default; TOOLS_DLLPUBLIC friend SvStream & operator >> ( SvStream &, SvGlobalName & ); TOOLS_DLLPUBLIC friend SvStream & WriteSvGlobalName( SvStream &, const SvGlobalName & ); @@ -91,11 +62,14 @@ public: bool MakeId( const OUString & rId ); OUString GetHexName() const; - const SvGUID& GetCLSID() const { return pImp->szData; } + const SvGUID& GetCLSID() const { return m_aData; } // platform independent representation of a "GlobalName" // maybe transported remotely css::uno::Sequence < sal_Int8 > GetByteSequence() const; + +private: + SvGUID m_aData = {}; }; #endif diff --git a/tools/source/ref/globname.cxx b/tools/source/ref/globname.cxx index fda6fafab53c..87bb46d50c2b 100644 --- a/tools/source/ref/globname.cxx +++ b/tools/source/ref/globname.cxx @@ -20,134 +20,81 @@ #include <stdio.h> #include <string.h> +#include <comphelper/mimeconfighelper.hxx> #include <rtl/strbuf.hxx> #include <rtl/character.hxx> #include <tools/stream.hxx> #include <tools/globname.hxx> -// ImpSvGlobalName ------------------------------------------------------------ -ImpSvGlobalName::ImpSvGlobalName( const ImpSvGlobalName & rObj ) - : szData(rObj.szData) -{ -} - -ImpSvGlobalName::ImpSvGlobalName(sal_uInt32 n1, sal_uInt16 n2, sal_uInt16 n3, - sal_uInt8 b8, sal_uInt8 b9, sal_uInt8 b10, sal_uInt8 b11, - sal_uInt8 b12, sal_uInt8 b13, sal_uInt8 b14, sal_uInt8 b15) -{ - szData.Data1 = n1; - szData.Data2 = n2; - szData.Data3 = n3; - szData.Data4[0] = b8; - szData.Data4[1] = b9; - szData.Data4[2] = b10; - szData.Data4[3] = b11; - szData.Data4[4] = b12; - szData.Data4[5] = b13; - szData.Data4[6] = b14; - szData.Data4[7] = b15; -} - -bool ImpSvGlobalName::operator == ( const ImpSvGlobalName & rObj ) const -{ - return !memcmp( &szData, &rObj.szData, sizeof( szData ) ); -} - // SvGlobalName ---------------------------------------------------------------- -SvGlobalName::SvGlobalName() -{ -} - SvGlobalName::SvGlobalName( const SvGUID & rId ) : - pImp( ImpSvGlobalName( rId ) ) + m_aData( rId ) { } SvGlobalName::SvGlobalName( sal_uInt32 n1, sal_uInt16 n2, sal_uInt16 n3, sal_uInt8 b8, sal_uInt8 b9, sal_uInt8 b10, sal_uInt8 b11, sal_uInt8 b12, sal_uInt8 b13, sal_uInt8 b14, sal_uInt8 b15 ) : - pImp( ImpSvGlobalName(n1, n2, n3, b8, b9, b10, b11, b12, b13, b14, b15) ) + m_aData{ n1, n2, n3, { b8, b9, b10, b11, b12, b13, b14, b15 } } { } SvGlobalName::SvGlobalName( const css::uno::Sequence < sal_Int8 >& aSeq ) { // create SvGlobalName from a platform independent representation - SvGUID aResult = {}; if ( aSeq.getLength() == 16 ) { - aResult.Data1 = ( ( ( ( ( static_cast<sal_uInt8>(aSeq[0]) << 8 ) + static_cast<sal_uInt8>(aSeq[1]) ) << 8 ) + static_cast<sal_uInt8>(aSeq[2]) ) << 8 ) + static_cast<sal_uInt8>(aSeq[3]); - aResult.Data2 = ( static_cast<sal_uInt8>(aSeq[4]) << 8 ) + static_cast<sal_uInt8>(aSeq[5]); - aResult.Data3 = ( static_cast<sal_uInt8>(aSeq[6]) << 8 ) + static_cast<sal_uInt8>(aSeq[7]); + m_aData.Data1 = ( ( ( ( ( static_cast<sal_uInt8>(aSeq[0]) << 8 ) + static_cast<sal_uInt8>(aSeq[1]) ) << 8 ) + static_cast<sal_uInt8>(aSeq[2]) ) << 8 ) + static_cast<sal_uInt8>(aSeq[3]); + m_aData.Data2 = ( static_cast<sal_uInt8>(aSeq[4]) << 8 ) + static_cast<sal_uInt8>(aSeq[5]); + m_aData.Data3 = ( static_cast<sal_uInt8>(aSeq[6]) << 8 ) + static_cast<sal_uInt8>(aSeq[7]); for( int nInd = 0; nInd < 8; nInd++ ) - aResult.Data4[nInd] = static_cast<sal_uInt8>(aSeq[nInd+8]); + m_aData.Data4[nInd] = static_cast<sal_uInt8>(aSeq[nInd+8]); } - - pImp = ::o3tl::cow_wrapper< ImpSvGlobalName >(aResult); -} - -SvGlobalName::~SvGlobalName() -{ -} - -SvGlobalName & SvGlobalName::operator = ( const SvGlobalName & rObj ) -{ - pImp = rObj.pImp; - - return *this; -} - -SvGlobalName & SvGlobalName::operator = ( SvGlobalName && rObj ) noexcept -{ - pImp = std::move(rObj.pImp); - return *this; } SvStream& WriteSvGlobalName( SvStream& rOStr, const SvGlobalName & rObj ) { - rOStr.WriteUInt32( rObj.pImp->szData.Data1 ); - rOStr.WriteUInt16( rObj.pImp->szData.Data2 ); - rOStr.WriteUInt16( rObj.pImp->szData.Data3 ); - rOStr.WriteBytes( &rObj.pImp->szData.Data4, 8 ); + rOStr.WriteUInt32( rObj.m_aData.Data1 ); + rOStr.WriteUInt16( rObj.m_aData.Data2 ); + rOStr.WriteUInt16( rObj.m_aData.Data3 ); + rOStr.WriteBytes( &rObj.m_aData.Data4, 8 ); return rOStr; } SvStream& operator >> ( SvStream& rStr, SvGlobalName & rObj ) { - // the non-const dereferencing operator - // ensures pImp is unique - rStr.ReadUInt32( rObj.pImp->szData.Data1 ); - rStr.ReadUInt16( rObj.pImp->szData.Data2 ); - rStr.ReadUInt16( rObj.pImp->szData.Data3 ); - rStr.ReadBytes( &rObj.pImp->szData.Data4, 8 ); + rStr.ReadUInt32( rObj.m_aData.Data1 ); + rStr.ReadUInt16( rObj.m_aData.Data2 ); + rStr.ReadUInt16( rObj.m_aData.Data3 ); + rStr.ReadBytes( &rObj.m_aData.Data4, 8 ); return rStr; } bool SvGlobalName::operator < ( const SvGlobalName & rObj ) const { - if( pImp->szData.Data3 < rObj.pImp->szData.Data3 ) + if( m_aData.Data3 < rObj.m_aData.Data3 ) return true; - else if( pImp->szData.Data3 > rObj.pImp->szData.Data3 ) + else if( m_aData.Data3 > rObj.m_aData.Data3 ) return false; - if( pImp->szData.Data2 < rObj.pImp->szData.Data2 ) + if( m_aData.Data2 < rObj.m_aData.Data2 ) return true; - else if( pImp->szData.Data2 > rObj.pImp->szData.Data2 ) + else if( m_aData.Data2 > rObj.m_aData.Data2 ) return false; - return pImp->szData.Data1 < rObj.pImp->szData.Data1; + return m_aData.Data1 < rObj.m_aData.Data1; } bool SvGlobalName::operator == ( const SvGlobalName & rObj ) const { - return pImp == rObj.pImp; + return memcmp(&m_aData, &rObj.m_aData, sizeof(m_aData)) == 0; } void SvGlobalName::MakeFromMemory( void const * pData ) { - memcpy( &pImp->szData, pData, sizeof( pImp->szData ) ); + memcpy( &m_aData, pData, sizeof( m_aData ) ); } bool SvGlobalName::MakeId( const OUString & rIdStr ) @@ -157,57 +104,48 @@ bool SvGlobalName::MakeId( const OUString & rIdStr ) && '-' == pStr[ 8 ] && '-' == pStr[ 13 ] && '-' == pStr[ 18 ] && '-' == pStr[ 23 ] ) { - sal_uInt32 nFirst = 0; - int i = 0; - for( i = 0; i < 8; i++ ) + SvGUID aGuid = {}; + auto asciiHexDigitToNumber = [](sal_Unicode c) -> sal_uInt8 + { + if (rtl::isAsciiDigit(c)) + return c - '0'; + else + return rtl::toAsciiUpperCase(c) - 'A' + 10; + }; + for( int i = 0; i < 8; i++ ) { if( rtl::isAsciiHexDigit( *pStr ) ) - if( rtl::isAsciiDigit( *pStr ) ) - nFirst = nFirst * 16 + (*pStr - '0'); - else - nFirst = nFirst * 16 + (rtl::toAsciiUpperCase( *pStr ) - 'A' + 10 ); + aGuid.Data1 = aGuid.Data1 * 16 + asciiHexDigitToNumber( *pStr ); else return false; pStr++; } - sal_uInt16 nSec = 0; pStr++; - for( i = 0; i < 4; i++ ) + for( int i = 0; i < 4; i++ ) { if( rtl::isAsciiHexDigit( *pStr ) ) - if( rtl::isAsciiDigit( *pStr ) ) - nSec = nSec * 16 + (*pStr - '0'); - else - nSec = nSec * 16 + static_cast<sal_uInt16>(rtl::toAsciiUpperCase( *pStr ) - 'A' + 10 ); + aGuid.Data2 = aGuid.Data2 * 16 + asciiHexDigitToNumber( *pStr ); else return false; pStr++; } - sal_uInt16 nThird = 0; pStr++; - for( i = 0; i < 4; i++ ) + for( int i = 0; i < 4; i++ ) { if( rtl::isAsciiHexDigit( *pStr ) ) - if( rtl::isAsciiDigit( *pStr ) ) - nThird = nThird * 16 + (*pStr - '0'); - else - nThird = nThird * 16 + static_cast<sal_uInt16>(rtl::toAsciiUpperCase( *pStr ) - 'A' + 10 ); + aGuid.Data3 = aGuid.Data3 * 16 + asciiHexDigitToNumber( *pStr ); else return false; pStr++; } - sal_Int8 szRemain[ 8 ] = {}; pStr++; - for( i = 0; i < 16; i++ ) + for( int i = 0; i < 16; i++ ) { if( rtl::isAsciiHexDigit( *pStr ) ) - if( rtl::isAsciiDigit( *pStr ) ) - szRemain[i/2] = szRemain[i/2] * 16 + (*pStr - '0'); - else - szRemain[i/2] = szRemain[i/2] * 16 + static_cast<sal_Int8>(rtl::toAsciiUpperCase( *pStr ) - 'A' + 10 ); + aGuid.Data4[i/2] = aGuid.Data4[i/2] * 16 + asciiHexDigitToNumber( *pStr ); else return false; pStr++; @@ -215,10 +153,7 @@ bool SvGlobalName::MakeId( const OUString & rIdStr ) pStr++; } - memcpy(&pImp->szData.Data1, &nFirst, sizeof(nFirst)); - memcpy(&pImp->szData.Data2, &nSec, sizeof(nSec)); - memcpy(&pImp->szData.Data3, &nThird, sizeof(nThird)); - memcpy(&pImp->szData.Data4, szRemain, 8); + m_aData = aGuid; return true; } return false; @@ -229,24 +164,24 @@ OUString SvGlobalName::GetHexName() const OStringBuffer aHexBuffer(36); char buf[ 10 ]; - sprintf( buf, "%8.8" SAL_PRIXUINT32, pImp->szData.Data1 ); + sprintf( buf, "%8.8" SAL_PRIXUINT32, m_aData.Data1 ); aHexBuffer.append(buf); aHexBuffer.append('-'); - sprintf( buf, "%4.4X", pImp->szData.Data2 ); + sprintf( buf, "%4.4X", m_aData.Data2 ); aHexBuffer.append(buf); aHexBuffer.append('-'); - sprintf( buf, "%4.4X", pImp->szData.Data3 ); + sprintf( buf, "%4.4X", m_aData.Data3 ); aHexBuffer.append(buf); aHexBuffer.append('-'); for( int i = 0; i < 2; i++ ) { - sprintf( buf, "%2.2x", pImp->szData.Data4[ i ] ); + sprintf( buf, "%2.2x", m_aData.Data4[ i ] ); aHexBuffer.append(buf); } aHexBuffer.append('-'); for( int i = 2; i < 8; i++ ) { - sprintf( buf, "%2.2x", pImp->szData.Data4[ i ] ); + sprintf( buf, "%2.2x", m_aData.Data4[ i ] ); aHexBuffer.append(buf); } return OStringToOUString(aHexBuffer.makeStringAndClear(), RTL_TEXTENCODING_ASCII_US); @@ -256,26 +191,10 @@ css::uno::Sequence < sal_Int8 > SvGlobalName::GetByteSequence() const { // platform independent representation of a "GlobalName" // maybe transported remotely - css::uno::Sequence< sal_Int8 > aResult{ - /* [ 0] */ static_cast<sal_Int8>(pImp->szData.Data1 >> 24), - /* [ 1] */ static_cast<sal_Int8>((pImp->szData.Data1 << 8 ) >> 24), - /* [ 2] */ static_cast<sal_Int8>((pImp->szData.Data1 << 16 ) >> 24), - /* [ 3] */ static_cast<sal_Int8>((pImp->szData.Data1 << 24 ) >> 24), - /* [ 4] */ static_cast<sal_Int8>(pImp->szData.Data2 >> 8), - /* [ 5] */ static_cast<sal_Int8>((pImp->szData.Data2 << 8 ) >> 8), - /* [ 6] */ static_cast<sal_Int8>(pImp->szData.Data3 >> 8), - /* [ 7] */ static_cast<sal_Int8>((pImp->szData.Data3 << 8 ) >> 8), - /* [ 8] */ static_cast<sal_Int8>(pImp->szData.Data4[ 0 ]), - /* [ 9] */ static_cast<sal_Int8>(pImp->szData.Data4[ 1 ]), - /* [10] */ static_cast<sal_Int8>(pImp->szData.Data4[ 2 ]), - /* [11] */ static_cast<sal_Int8>(pImp->szData.Data4[ 3 ]), - /* [12] */ static_cast<sal_Int8>(pImp->szData.Data4[ 4 ]), - /* [13] */ static_cast<sal_Int8>(pImp->szData.Data4[ 5 ]), - /* [14] */ static_cast<sal_Int8>(pImp->szData.Data4[ 6 ]), - /* [15] */ static_cast<sal_Int8>(pImp->szData.Data4[ 7 ]) - }; - - return aResult; + return comphelper::MimeConfigurationHelper::GetSequenceClassID( + m_aData.Data1, m_aData.Data2, m_aData.Data3, + m_aData.Data4[0], m_aData.Data4[1], m_aData.Data4[2], m_aData.Data4[3], + m_aData.Data4[4], m_aData.Data4[5], m_aData.Data4[6], m_aData.Data4[7]); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */