sc/source/core/data/column2.cxx | 57 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 4 deletions(-)
New commits: commit c4709b6192340a4d5a82bf156a7342aba6aa99a1 Author: Andreas Heinisch <andreas.heini...@yahoo.de> AuthorDate: Fri Aug 20 09:53:37 2021 +0200 Commit: Eike Rathke <er...@redhat.com> CommitDate: Fri Aug 27 11:29:35 2021 +0200 tdf#59820 - Search for the longest substring in a multiline string In order to determine the optimal column width of imported ASCII files in the simple text mode, search for the longest substring in a multiline string which is determined by a line break. Change-Id: I2c606dd97549455009b9f8fad2b8ec4386b7b7db Reviewed-on: https://gerrit.libreoffice.org/c/core/+/120772 Tested-by: Jenkins Reviewed-by: Eike Rathke <er...@redhat.com> diff --git a/sc/source/core/data/column2.cxx b/sc/source/core/data/column2.cxx index e3f8284d5061..93e16239dfca 100644 --- a/sc/source/core/data/column2.cxx +++ b/sc/source/core/data/column2.cxx @@ -614,6 +614,45 @@ class MaxStrLenFinder OUString maMaxLenStr; sal_Int32 mnMaxLen; + // tdf#59820 - search for the longest substring in a multiline string + void checkLineBreak(const OUString& aStrVal) + { + sal_Int32 nFromIndex = 0; + sal_Int32 nToIndex = aStrVal.indexOf('\n', nFromIndex); + // if there is no line break, just take the length of the entire string + if (nToIndex == -1) + { + mnMaxLen = aStrVal.getLength(); + maMaxLenStr = aStrVal; + } + else + { + sal_Int32 nMaxLen = 0; + // search for the longest substring in the multiline string + while (nToIndex != -1) + { + if (nMaxLen < nToIndex - nFromIndex) + { + nMaxLen = nToIndex - nFromIndex; + } + nFromIndex = nToIndex + 1; + nToIndex = aStrVal.indexOf('\n', nFromIndex); + } + // take into consideration the last part of multiline string + nToIndex = aStrVal.getLength() - nFromIndex; + if (nMaxLen < nToIndex) + { + nMaxLen = nToIndex; + } + // assign new maximum including its substring + if (mnMaxLen < nMaxLen) + { + mnMaxLen = nMaxLen; + maMaxLenStr = aStrVal.subView(nFromIndex); + } + } + } + void checkLength(const ScRefCellValue& rCell) { const Color* pColor; @@ -623,8 +662,19 @@ class MaxStrLenFinder if (aValStr.getLength() > mnMaxLen) { - mnMaxLen = aValStr.getLength(); - maMaxLenStr = aValStr; + switch (rCell.meType) + { + case CELLTYPE_NONE: + case CELLTYPE_VALUE: + mnMaxLen = aValStr.getLength(); + maMaxLenStr = aValStr; + break; + case CELLTYPE_EDIT: + case CELLTYPE_STRING: + case CELLTYPE_FORMULA: + default: + checkLineBreak(aValStr); + } } } @@ -642,8 +692,7 @@ public: { if (rSS.getLength() > mnMaxLen) { - mnMaxLen = rSS.getLength(); - maMaxLenStr = rSS.getString(); + checkLineBreak(rSS.getString()); } }