sot/source/base/exchange.cxx | 94 ++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 59 deletions(-)
New commits: commit 215c4397420108cdd2598f98afb2e72cc66744a6 Author: Mike Kaganski <[email protected]> AuthorDate: Sat Sep 20 09:37:23 2025 +0200 Commit: Mike Kaganski <[email protected]> CommitDate: Sat Sep 20 11:28:57 2025 +0200 Deduplicate the code Before commit ba1cd4964259221549f3671f89a701e67d7deb71 (Make SotExchange::GetFormat() accept a MIME type with additional parameters, 2021-01-14), SotExchange::GetFormatIdFromMimeType and SotExchange::GetFormat did exactly the same thing; it's unclear why weren't they deduplicated from start. The said commit introduced some functional difference. Maybe it makes sense to unify their functionality again; but since there was some discussion in the respective git review (see https://gerrit.libreoffice.org/c/core/+/109254), this change keeps the difference. Change-Id: I062b21b25c47d27592c4b05bd0f6cbc629040efc Reviewed-on: https://gerrit.libreoffice.org/c/core/+/191214 Reviewed-by: Mike Kaganski <[email protected]> Tested-by: Jenkins diff --git a/sot/source/base/exchange.cxx b/sot/source/base/exchange.cxx index 9c8741ee7ff4..00dcac19b7fa 100644 --- a/sot/source/base/exchange.cxx +++ b/sot/source/base/exchange.cxx @@ -27,6 +27,7 @@ #include <comphelper/classids.hxx> #include <com/sun/star/datatransfer/DataFlavor.hpp> #include <comphelper/documentconstants.hxx> +#include <o3tl/string_view.hxx> #include <memory> #include <vector> @@ -208,15 +209,45 @@ const DataFlavorRepresentation* FormatArray_Impl() }; typedef std::vector<css::datatransfer::DataFlavor> tDataFlavorList; -} -static tDataFlavorList& InitFormats_Impl() +tDataFlavorList& InitFormats_Impl() { static tools::DeleteOnDeinit<tDataFlavorList> gImplData; return *gImplData.get(); } +SotClipboardFormatId GetFormatIdFromMimeType_impl(std::u16string_view rMimeType, + bool allowMoreParams) +{ + const DataFlavorRepresentation *pFormatArray_Impl = FormatArray_Impl(); + for( SotClipboardFormatId i = SotClipboardFormatId::STRING; i <= SotClipboardFormatId::FILE_LIST; ++i ) + if( rMimeType == pFormatArray_Impl[ static_cast<int>(i) ].pMimeType ) + return i; + + // BM: the chart format 105 ("StarChartDocument 5.0") was written + // only into 5.1 chart documents - in 5.0 and 5.2 it was 42 ("StarChart 5.0") + // The registry only contains the entry for the 42 format id. + for( SotClipboardFormatId i = SotClipboardFormatId::RTF; i <= SotClipboardFormatId::USER_END; ++i ) + if (std::u16string_view rest; + o3tl::starts_with(rMimeType, pFormatArray_Impl[static_cast<int>(i)].pMimeType, &rest) + && (rest.empty() || (allowMoreParams && rest.starts_with(';')))) + return ( (i == SotClipboardFormatId::STARCHARTDOCUMENT_50) + ? SotClipboardFormatId::STARCHART_50 + : i ); + + // then in the dynamic list + tDataFlavorList& rL = InitFormats_Impl(); + for( tDataFlavorList::size_type i = 0; i < rL.size(); i++ ) + { + if( rMimeType == rL[ i ].MimeType ) + return static_cast<SotClipboardFormatId>(i + static_cast<int>(SotClipboardFormatId::USER_END) + 1); + } + + return SotClipboardFormatId::NONE; +} +} + /************************************************************************* |* |* SotExchange::RegisterFormatName() @@ -386,31 +417,7 @@ OUString SotExchange::GetFormatMimeType( SotClipboardFormatId nFormat ) SotClipboardFormatId SotExchange::GetFormatIdFromMimeType( std::u16string_view rMimeType ) { - const DataFlavorRepresentation *pFormatArray_Impl = FormatArray_Impl(); - for( SotClipboardFormatId i = SotClipboardFormatId::STRING; i <= SotClipboardFormatId::FILE_LIST; ++i ) - if( rMimeType == pFormatArray_Impl[ static_cast<int>(i) ].pMimeType ) - return i; - - // BM: the chart format 105 ("StarChartDocument 5.0") was written - // only into 5.1 chart documents - in 5.0 and 5.2 it was 42 ("StarChart 5.0") - // The registry only contains the entry for the 42 format id. - for( SotClipboardFormatId i = SotClipboardFormatId::RTF; i <= SotClipboardFormatId::USER_END; ++i ) - if( rMimeType == pFormatArray_Impl[ static_cast<int>(i) ].pMimeType ) - return ( (i == SotClipboardFormatId::STARCHARTDOCUMENT_50) - ? SotClipboardFormatId::STARCHART_50 - : i ); - - // then in the dynamic list - tDataFlavorList& rL = InitFormats_Impl(); - - for( tDataFlavorList::size_type i = 0; i < rL.size(); i++ ) - { - auto const& rFlavor = rL[ i ]; - if( rMimeType == rFlavor.MimeType ) - return static_cast<SotClipboardFormatId>(i + static_cast<int>(SotClipboardFormatId::USER_END) + 1); - } - - return SotClipboardFormatId::NONE; + return GetFormatIdFromMimeType_impl(rMimeType, false); } /************************************************************************* @@ -421,36 +428,7 @@ SotClipboardFormatId SotExchange::GetFormatIdFromMimeType( std::u16string_view r *************************************************************************/ SotClipboardFormatId SotExchange::GetFormat( const DataFlavor& rFlavor ) { - // test the default first - name - const OUString& rMimeType = rFlavor.MimeType; - - const DataFlavorRepresentation *pFormatArray_Impl = FormatArray_Impl(); - for( SotClipboardFormatId i = SotClipboardFormatId::STRING; i <= SotClipboardFormatId::FILE_LIST; ++i ) - if( rMimeType.equals( pFormatArray_Impl[ static_cast<int>(i) ].pMimeType ) ) - return i; - - // BM: the chart format 105 ("StarChartDocument 5.0") was written - // only into 5.1 chart documents - in 5.0 and 5.2 it was 42 ("StarChart 5.0") - // The registry only contains the entry for the 42 format id. - for( SotClipboardFormatId i = SotClipboardFormatId::RTF; i <= SotClipboardFormatId::USER_END; ++i ) - { - if (std::u16string_view rest; - rMimeType.startsWith(pFormatArray_Impl[static_cast<int>(i)].pMimeType, &rest) - && (rest.empty() || rest.starts_with(';'))) - return ( (i == SotClipboardFormatId::STARCHARTDOCUMENT_50) - ? SotClipboardFormatId::STARCHART_50 - : i ); - } - - // then in the dynamic list - tDataFlavorList& rL = InitFormats_Impl(); - for( tDataFlavorList::size_type i = 0; i < rL.size(); i++ ) - { - if( rMimeType == rL[ i ].MimeType ) - return static_cast<SotClipboardFormatId>(i + static_cast<int>(SotClipboardFormatId::USER_END) + 1); - } - - return SotClipboardFormatId::NONE; + return GetFormatIdFromMimeType_impl(rFlavor.MimeType, true); } /************************************************************************* commit d11111f342ad0cf74852f378286d647b2c9eb2f5 Author: Mike Kaganski <[email protected]> AuthorDate: Sat Sep 20 09:10:23 2025 +0200 Commit: Mike Kaganski <[email protected]> CommitDate: Sat Sep 20 11:28:50 2025 +0200 Simplify the check introduced in ba1cd4964259221549f3671f89a701e67d7deb71 Change-Id: Ie1c7acb9865f50fd0fd7784f6c045d6584f509ea Reviewed-on: https://gerrit.libreoffice.org/c/core/+/191213 Reviewed-by: Mike Kaganski <[email protected]> Tested-by: Jenkins diff --git a/sot/source/base/exchange.cxx b/sot/source/base/exchange.cxx index 3e9855162e22..9c8741ee7ff4 100644 --- a/sot/source/base/exchange.cxx +++ b/sot/source/base/exchange.cxx @@ -434,11 +434,9 @@ SotClipboardFormatId SotExchange::GetFormat( const DataFlavor& rFlavor ) // The registry only contains the entry for the 42 format id. for( SotClipboardFormatId i = SotClipboardFormatId::RTF; i <= SotClipboardFormatId::USER_END; ++i ) { - const OUString& pFormatMimeType = pFormatArray_Impl[ static_cast<int>(i) ].pMimeType; - const sal_Int32 nFormatMimeTypeLen = pFormatMimeType.getLength(); - if( rMimeType.match( pFormatMimeType ) && - ( rMimeType.getLength() == nFormatMimeTypeLen || - rMimeType[ nFormatMimeTypeLen ] == ';' ) ) + if (std::u16string_view rest; + rMimeType.startsWith(pFormatArray_Impl[static_cast<int>(i)].pMimeType, &rest) + && (rest.empty() || rest.starts_with(';'))) return ( (i == SotClipboardFormatId::STARCHARTDOCUMENT_50) ? SotClipboardFormatId::STARCHART_50 : i );
