From 35871de09d3e6ca556409bfe43c0989ad2c5afd9 Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Thu, 26 Feb 2026 11:51:26 +0500 Subject: [PATCH] Set encoding in StartReadingUnicodeText Simplifies some code. It doesn't make sense to set endianness, but avoid setting encoding. Change-Id: I3a09fea7381b6c113075b9800b06b307cdaa6d45 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/200668 Tested-by: Jenkins Reviewed-by: Mike Kaganski --- basctl/source/basicide/baside2.cxx | 2 -- tools/source/stream/stream.cxx | 31 ++++++++++++------------------ 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/basctl/source/basicide/baside2.cxx b/basctl/source/basicide/baside2.cxx index fae3c01bbc29..8699f9e16203 100644 --- a/basctl/source/basicide/baside2.cxx +++ b/basctl/source/basicide/baside2.cxx @@ -443,8 +443,6 @@ void ModulWindow::LoadBasic() GetEditEngine()->SetUpdateMode( false ); // tdf#139196 - import macros using either default or utf-8 text encoding pStream->StartReadingUnicodeText(RTL_TEXTENCODING_UTF8); - if (pStream->Tell() == 3) - pStream->SetStreamEncoding(RTL_TEXTENCODING_UTF8); GetEditView()->Read( *pStream ); GetEditEngine()->SetUpdateMode( true ); GetEditorWindow().PaintImmediately(); diff --git a/tools/source/stream/stream.cxx b/tools/source/stream/stream.cxx index b8a6530a8590..1a65357f8cf7 100644 --- a/tools/source/stream/stream.cxx +++ b/tools/source/stream/stream.cxx @@ -702,7 +702,6 @@ void SvStream::StartReadingUnicodeText(rtl_TextEncoding eReadBomEncoding) return; // nothing to read const sal_uInt64 nOldPos = Tell(); - bool bGetBack = true; unsigned char nFlag(0); ReadUChar( nFlag ); switch ( nFlag ) @@ -714,8 +713,9 @@ void SvStream::StartReadingUnicodeText(rtl_TextEncoding eReadBomEncoding) ReadUChar(nFlag); if (nFlag == 0xff) { + SetStreamEncoding(RTL_TEXTENCODING_UNICODE); SetEndian(SvStreamEndian::BIG); - bGetBack = false; + return; } } break; @@ -726,8 +726,9 @@ void SvStream::StartReadingUnicodeText(rtl_TextEncoding eReadBomEncoding) ReadUChar(nFlag); if (nFlag == 0xfe) { + SetStreamEncoding(RTL_TEXTENCODING_UNICODE); SetEndian(SvStreamEndian::LITTLE); - bGetBack = false; + return; } } break; @@ -740,15 +741,17 @@ void SvStream::StartReadingUnicodeText(rtl_TextEncoding eReadBomEncoding) { ReadUChar(nFlag); if (nFlag == 0xbf) - bGetBack = false; // it is UTF-8 + { + SetStreamEncoding(RTL_TEXTENCODING_UTF8); + return; + } } } break; default: ; // nothing } - if (bGetBack) - Seek(nOldPos); // no BOM, pure data + Seek(nOldPos); // no BOM, pure data } void SvStream::DetectEncoding(size_t maxBytes) @@ -790,20 +793,10 @@ void SvStream::DetectEncoding(size_t maxBytes) StartReadingUnicodeText(RTL_TEXTENCODING_DONTKNOW); if (!good()) return; + if (GetStreamEncoding() != RTL_TEXTENCODING_DONTKNOW) + return; // BOM detected, encoding already set - const sal_uInt64 nBomSize = Tell() - nOrigPos; - if (nBomSize == 2) - { - SetStreamEncoding(RTL_TEXTENCODING_UCS2); - return; - } - if (nBomSize == 3) - { - SetStreamEncoding(RTL_TEXTENCODING_UTF8); - return; - } - - assert(nBomSize == 0); // we are at nOrigPos + assert(Tell() - nOrigPos == 0); // we are at nOrigPos auto bytes = std::make_unique(maxBytes); size_t nRead = ReadBytes(bytes.get(), maxBytes); Seek(nOrigPos);