summaryrefslogtreecommitdiff
path: root/sc
diff options
context:
space:
mode:
authorAndreas Heinisch <andreas.heinisch@yahoo.de>2021-08-20 09:53:37 +0200
committerEike Rathke <erack@redhat.com>2021-08-27 11:29:35 +0200
commitc4709b6192340a4d5a82bf156a7342aba6aa99a1 (patch)
tree287d0c6594083648092deac4b7a708a7e4520b56 /sc
parent72590c55ad6c699b34dc404542abc3f60bc32a09 (diff)
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 <erack@redhat.com>
Diffstat (limited to 'sc')
-rw-r--r--sc/source/core/data/column2.cxx57
1 files changed, 53 insertions, 4 deletions
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());
}
}