summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Kaganski <mike.kaganski@collabora.com>2024-12-07 18:33:17 +0500
committerMike Kaganski <mike.kaganski@collabora.com>2024-12-07 16:11:57 +0100
commita9734683dfa8a36eafd6f32414408c9b8f4a221c (patch)
tree09e12842468d81653976467817fcc992a2e8b436
parent5528f9c8b8ea08ad0a347e9564e7ce98564bed30 (diff)
Simplify OTextInputStream a bit
Change-Id: I94c4432228c470ad31d15a765de87565b4ada9df Reviewed-on: https://gerrit.libreoffice.org/c/core/+/178039 Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com> Tested-by: Jenkins
-rw-r--r--io/source/TextInputStream/TextInputStream.cxx114
1 files changed, 43 insertions, 71 deletions
diff --git a/io/source/TextInputStream/TextInputStream.cxx b/io/source/TextInputStream/TextInputStream.cxx
index bcea56604e3c..94710fe6c8d5 100644
--- a/io/source/TextInputStream/TextInputStream.cxx
+++ b/io/source/TextInputStream/TextInputStream.cxx
@@ -169,28 +169,21 @@ OUString OTextInputStream::implReadString( const Sequence< sal_Unicode >& Delimi
return aRetStr;
// Only for bFindLineEnd
- sal_Unicode cLineEndChar1 = 0x0D;
- sal_Unicode cLineEndChar2 = 0x0A;
+ constexpr sal_Unicode cLineEndChar1 = '\r';
+ constexpr sal_Unicode cLineEndChar2 = '\n';
sal_Int32 nBufferReadPos = 0;
- sal_Int32 nCopyLen = 0;
- bool bFound = false;
- bool bFoundFirstLineEndChar = false;
+ sal_Int32 nCopyLen = -1;
sal_Unicode cFirstLineEndChar = 0;
- while( !bFound )
+ while (true)
{
// Still characters available?
if( nBufferReadPos == mnCharsInBuffer )
{
// Already reached EOF? Then we can't read any more
// Or no, so read new characters
- if( mbReachedEOF || !implReadNext() ) {
- if( bFoundFirstLineEndChar ) {
- bFound = true;
- nCopyLen = nBufferReadPos - 1;
- }
+ if( !implReadNext() )
break;
- }
}
// Now there should be characters available
@@ -199,41 +192,35 @@ OUString OTextInputStream::implReadString( const Sequence< sal_Unicode >& Delimi
if( bFindLineEnd )
{
- if( bFoundFirstLineEndChar )
+ if (cFirstLineEndChar != 0)
{
- bFound = true;
- nCopyLen = nBufferReadPos - 2;
- if( c == cLineEndChar1 || c == cLineEndChar2 )
+ assert(nCopyLen >= 0);
+ // This is a check if the next character after a line end char is its second half
+ // Same line end char -> new line break; non-line-end char -> new line start
+ if ((c == cFirstLineEndChar) || (c != cLineEndChar1 && c != cLineEndChar2))
{
- // Same line end char -> new line break
- if( c == cFirstLineEndChar )
- {
- nBufferReadPos--;
- }
- }
- else
- {
- // No second line end char
+ // Not a two-char line end
nBufferReadPos--;
}
+ break;
}
else if( c == cLineEndChar1 || c == cLineEndChar2 )
{
- bFoundFirstLineEndChar = true;
- cFirstLineEndChar = c;
+ nCopyLen = nBufferReadPos - 1; // we know what to copy
+ cFirstLineEndChar = c; // take one more loop, to check if it's a two-char line end
}
}
else if( comphelper::findValue(Delimiters, c) != -1 )
{
- bFound = true;
nCopyLen = nBufferReadPos;
if( bRemoveDelimiter )
nCopyLen--;
+ break;
}
}
// Nothing found? Return all
- if( !nCopyLen && !bFound && mbReachedEOF )
+ if (nCopyLen < 0)
nCopyLen = nBufferReadPos;
// Create string
@@ -251,35 +238,40 @@ OUString OTextInputStream::implReadString( const Sequence< sal_Unicode >& Delimi
sal_Int32 OTextInputStream::implReadNext()
{
- sal_Int32 nFreeBufferSize = mvBuffer.size() - mnCharsInBuffer;
- if( nFreeBufferSize < READ_BYTE_COUNT )
- mvBuffer.resize(mvBuffer.size() * 2);
- nFreeBufferSize = mvBuffer.size() - mnCharsInBuffer;
+ // Already reached EOF? Then we can't read any more
+ if (mbReachedEOF)
+ return 0;
try
{
- sal_Int32 nRead = mxStream->readSomeBytes( mSeqSource, READ_BYTE_COUNT );
- sal_Int32 nTotalRead = nRead;
- if( nRead == 0 )
+ if (mxStream->readSomeBytes(mSeqSource, READ_BYTE_COUNT) == 0)
+ {
mbReachedEOF = true;
+ return 0;
+ }
// Try to convert
- sal_uInt32 uiInfo;
- sal_Size nSrcCvtBytes = 0;
- sal_Size nTargetCount = 0;
+ sal_uInt32 uiInfo = mvBuffer.size() - mnCharsInBuffer < o3tl::make_unsigned(mSeqSource.getLength())
+ ? RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL
+ : 0;
+ const sal_Int32 nOldCharsInBuffer = mnCharsInBuffer;
sal_Size nSourceCount = 0;
while( true )
{
- const sal_Int8 *pbSource = mSeqSource.getConstArray();
+ if (uiInfo & RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL)
+ {
+ mvBuffer.resize(mvBuffer.size() * 2);
+ }
// All invalid characters are transformed to the unicode undefined char
- nTargetCount += rtl_convertTextToUnicode(
+ sal_Size nSrcCvtBytes = 0;
+ mnCharsInBuffer += rtl_convertTextToUnicode(
mConvText2Unicode,
mContextText2Unicode,
- reinterpret_cast<const char*>(&( pbSource[nSourceCount] )),
- nTotalRead - nSourceCount,
- mvBuffer.data() + mnCharsInBuffer + nTargetCount,
- nFreeBufferSize - nTargetCount,
+ reinterpret_cast<const char*>(mSeqSource.getConstArray() + nSourceCount),
+ mSeqSource.getLength() - nSourceCount,
+ mvBuffer.data() + mnCharsInBuffer,
+ mvBuffer.size() - mnCharsInBuffer,
RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_DEFAULT |
RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_DEFAULT |
RTL_TEXTTOUNICODE_FLAGS_INVALID_DEFAULT,
@@ -287,41 +279,21 @@ sal_Int32 OTextInputStream::implReadNext()
&nSrcCvtBytes );
nSourceCount += nSrcCvtBytes;
- bool bCont = false;
- if( uiInfo & RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL )
- {
- mvBuffer.resize(mvBuffer.size() * 2);
- bCont = true;
- }
-
if( uiInfo & RTL_TEXTTOUNICODE_INFO_SRCBUFFERTOOSMALL )
{
// read next byte
- static Sequence< sal_Int8 > aOneByteSeq( 1 );
- nRead = mxStream->readSomeBytes( aOneByteSeq, 1 );
- if( nRead == 0 )
+ Sequence<sal_Int8> aOneByteSeq(1);
+ if (mxStream->readSomeBytes(aOneByteSeq, 1) == 0)
{
mbReachedEOF = true;
- break;
+ return mnCharsInBuffer - nOldCharsInBuffer;
}
- sal_Int32 nOldLen = mSeqSource.getLength();
- nTotalRead++;
- if( nTotalRead > nOldLen )
- {
- mSeqSource.realloc( nTotalRead );
- }
- mSeqSource.getArray()[ nOldLen ] = aOneByteSeq.getConstArray()[ 0 ];
- bCont = true;
+ mSeqSource = comphelper::concatSequences(mSeqSource, aOneByteSeq);
}
-
- if( bCont )
- continue;
- break;
+ else if (!(uiInfo & RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL))
+ return mnCharsInBuffer - nOldCharsInBuffer; // finished
}
-
- mnCharsInBuffer += nTargetCount;
- return nTargetCount;
}
catch( NotConnectedException& )
{