diff options
author | Alin Andrei Abahnencei <alinandrei2004@gmail.com> | 2024-11-27 10:39:54 +0200 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2024-12-07 14:26:16 +0100 |
commit | 734f80b66947210861ab2beb216e0dc327959e18 (patch) | |
tree | 9609ebff03340e1737c07e4cb9ee01eae679f92f | |
parent | b664c08a6d1096d279437c883e55735a0c431ac8 (diff) |
tdf#162716 Always strip line-ending characters
Signed-off-by: Alin Andrei Abahnencei <alinandrei2004@gmail.com>
Change-Id: I3dfc6908e18f7bad54468620cf657a0a4afb8046
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/177393
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Tested-by: Jenkins
-rw-r--r-- | io/qa/textinputstream.cxx | 80 | ||||
-rw-r--r-- | io/source/TextInputStream/TextInputStream.cxx | 12 |
2 files changed, 69 insertions, 23 deletions
diff --git a/io/qa/textinputstream.cxx b/io/qa/textinputstream.cxx index ddfbd3afd134..e53a35ba3801 100644 --- a/io/qa/textinputstream.cxx +++ b/io/qa/textinputstream.cxx @@ -32,7 +32,11 @@ namespace { class Input: public cppu::WeakImplHelper<css::io::XInputStream> { public: - Input(): open_(true), index_(0) {} + Input(char* inputData, sal_Int32 inputSize): + open_(true), + index_(0), + size(inputSize), + data(inputData) {} private: virtual ~Input() override {} @@ -47,14 +51,14 @@ private: assert(nMaxBytesToRead >= 0); osl::MutexGuard g(mutex_); checkClosed(); - assert(index_ >= 0 && index_ <= SIZE); + assert(index_ >= 0 && index_ <= size); sal_Int32 n = std::min<sal_Int32>( - std::min<sal_Int32>(nMaxBytesToRead, 2), SIZE - index_); - assert(n >= 0 && n <= SIZE - index_); + std::min<sal_Int32>(nMaxBytesToRead, 2), size - index_); + assert(n >= 0 && n <= size - index_); aData.realloc(n); std::memcpy(aData.getArray(), data + index_, n); index_ += n; - assert(index_ >= 0 && index_ <= SIZE); + assert(index_ >= 0 && index_ <= size); return n; } @@ -63,17 +67,17 @@ private: assert(nBytesToSkip >= 0); osl::MutexGuard g(mutex_); checkClosed(); - assert(index_ >= 0 && index_ <= SIZE); - index_ += std::min<sal_Int32>(nBytesToSkip, SIZE - index_); - assert(index_ >= 0 && index_ <= SIZE); + assert(index_ >= 0 && index_ <= size); + index_ += std::min<sal_Int32>(nBytesToSkip, size - index_); + assert(index_ >= 0 && index_ <= size); } sal_Int32 SAL_CALL available() override { osl::MutexGuard g(mutex_); checkClosed(); - assert(index_ >= 0 && index_ <= SIZE); - return SIZE - index_; + assert(index_ >= 0 && index_ <= size); + return size - index_; } void SAL_CALL closeInput() override @@ -90,33 +94,73 @@ private: } } - static sal_Int32 const SIZE = 9; - static char const data[SIZE]; - osl::Mutex mutex_; bool open_; sal_Int32 index_; -}; -char const Input::data[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' }; + sal_Int32 size; + char* data; + +}; class Test: public test::BootstrapFixtureBase { private: CPPUNIT_TEST_SUITE(Test); CPPUNIT_TEST(testReadLine); + CPPUNIT_TEST(testReadLineEndChars); CPPUNIT_TEST_SUITE_END(); void testReadLine(); + void testReadLineEndChars(); + + OUString readFirstLine(char data1[], int size); }; -void Test::testReadLine() { +OUString Test::readFirstLine(char *inputData, int inputSize) { css::uno::Reference<css::io::XTextInputStream2> s( css::io::TextInputStream::create(getComponentContext())); - s->setInputStream(new Input); - OUString l(s->readLine()); + s->setInputStream(new Input(inputData, inputSize)); + return s->readLine(); +} + +void Test::testReadLine() { + char inputData[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'}; + OUString l(readFirstLine(inputData, sizeof(inputData))); CPPUNIT_ASSERT_EQUAL(u"123456789"_ustr, l); } +void Test::testReadLineEndChars() { + std::vector<char> inputData = {'a', 'b', 'c', '\r'}; + OUString l(readFirstLine(inputData.data(), inputData.size())); + CPPUNIT_ASSERT_EQUAL(u"abc"_ustr, l); + + inputData = {'a', 'b', 'c', '\n'}; + l = readFirstLine(inputData.data(), inputData.size()); + CPPUNIT_ASSERT_EQUAL(u"abc"_ustr, l); + + inputData = {'a', 'b', 'c', '\r', '\n'}; + l = readFirstLine(inputData.data(), inputData.size()); + CPPUNIT_ASSERT_EQUAL(u"abc"_ustr, l); + + inputData = {'a', 'b', 'c', '\r', 'd', 'e', 'f'}; + l = readFirstLine(inputData.data(), inputData.size()); + CPPUNIT_ASSERT_EQUAL(u"abc"_ustr, l); + + inputData = {'a', 'b', 'c', '\n', 'd', 'e', 'f'}; + l = readFirstLine(inputData.data(), inputData.size()); + CPPUNIT_ASSERT_EQUAL(u"abc"_ustr, l); + + css::uno::Reference<css::io::XTextInputStream2> s( + css::io::TextInputStream::create(getComponentContext())); + inputData = {'a', 'b', 'c', '\r', '\n', 'd', 'e', 'f'}; + s->setInputStream(new Input(inputData.data(), inputData.size())); + l = s->readLine(); + CPPUNIT_ASSERT_EQUAL(u"abc"_ustr, l); + l = s->readLine(); + CPPUNIT_ASSERT_EQUAL(u"def"_ustr, l); +} + + CPPUNIT_TEST_SUITE_REGISTRATION(Test); } diff --git a/io/source/TextInputStream/TextInputStream.cxx b/io/source/TextInputStream/TextInputStream.cxx index 4dd0bd50dc25..bcea56604e3c 100644 --- a/io/source/TextInputStream/TextInputStream.cxx +++ b/io/source/TextInputStream/TextInputStream.cxx @@ -183,12 +183,14 @@ OUString OTextInputStream::implReadString( const Sequence< sal_Unicode >& Delimi if( nBufferReadPos == mnCharsInBuffer ) { // Already reached EOF? Then we can't read any more - if( mbReachedEOF ) - break; - - // No, so read new characters - if( !implReadNext() ) + // Or no, so read new characters + if( mbReachedEOF || !implReadNext() ) { + if( bFoundFirstLineEndChar ) { + bFound = true; + nCopyLen = nBufferReadPos - 1; + } break; + } } // Now there should be characters available |