1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <algorithm>
#include <cassert>
#include <o3tl/safeint.hxx>
#include <osl/diagnose.h>
#include <tools/solar.h>
#include <xlstyle.hxx>
#include <xestyle.hxx>
#include <xestream.hxx>
#include <xestring.hxx>
#include <oox/token/tokens.hxx>
using namespace ::oox;
namespace {
// compare vectors
/** Compares two vectors.
@return A negative value, if rLeft<rRight; or a positive value, if rLeft>rRight;
or 0, if rLeft==rRight. */
template< typename Type >
int lclCompareVectors( const ::std::vector< Type >& rLeft, const ::std::vector< Type >& rRight )
{
int nResult = 0;
// 1st: compare all elements of the vectors
auto [aItL, aItR] = std::mismatch(rLeft.begin(), rLeft.end(), rRight.begin(), rRight.end());
if ((aItL != rLeft.end()) && (aItR != rRight.end()))
nResult = static_cast< int >( *aItL ) - static_cast< int >( *aItR );
else
// 2nd: compare the vector sizes. Shorter vector is less
nResult = static_cast< int >( rLeft.size() ) - static_cast< int >( rRight.size() );
return nResult;
}
// hashing helpers
/** Base class for value hashers.
@descr These function objects are used to hash any value to a sal_uInt32 value. */
template< typename Type >
struct XclHasher {};
template< typename Type >
struct XclDirectHasher : public XclHasher< Type >
{
sal_uInt32 operator()( Type nVal ) const { return nVal; }
};
struct XclFormatRunHasher : public XclHasher< const XclFormatRun& >
{
sal_uInt32 operator()( const XclFormatRun& rRun ) const
{ return (rRun.mnChar << 8) ^ rRun.mnFontIdx; }
};
/** Calculates a hash value from a vector.
@descr Uses the passed hasher function object to calculate hash values from
all vector elements. */
template< typename Type, typename ValueHasher >
sal_uInt16 lclHashVector( const ::std::vector< Type >& rVec, const ValueHasher& rHasher )
{
sal_uInt32 nHash = rVec.size();
for( const auto& rItem : rVec )
nHash = (nHash * 31) + rHasher( rItem );
return static_cast< sal_uInt16 >( nHash ^ (nHash >> 16) );
}
/** Calculates a hash value from a vector. Uses XclDirectHasher to hash the vector elements. */
template< typename Type >
sal_uInt16 lclHashVector( const ::std::vector< Type >& rVec )
{
return lclHashVector( rVec, XclDirectHasher< Type >() );
}
} // namespace
// constructors ---------------------------------------------------------------
XclExpString::XclExpString( XclStrFlags nFlags, sal_uInt16 nMaxLen )
{
Init( 0, nFlags, nMaxLen, true );
}
XclExpString::XclExpString( const OUString& rString, XclStrFlags nFlags, sal_uInt16 nMaxLen )
{
Assign( rString, nFlags, nMaxLen );
}
// assign ---------------------------------------------------------------------
void XclExpString::Assign( const OUString& rString, XclStrFlags nFlags, sal_uInt16 nMaxLen )
{
Build( rString.getStr(), rString.getLength(), nFlags, nMaxLen );
}
void XclExpString::Assign( sal_Unicode cChar )
{
Build( &cChar, 1, XclStrFlags::NONE, EXC_STR_MAXLEN );
}
void XclExpString::AssignByte(
std::u16string_view rString, rtl_TextEncoding eTextEnc, XclStrFlags nFlags,
sal_uInt16 nMaxLen )
{
// length may differ from length of rString
OString aByteStr(OUStringToOString(rString, eTextEnc));
Build(aByteStr.getStr(), aByteStr.getLength(), nFlags, nMaxLen);
}
// append ---------------------------------------------------------------------
void XclExpString::Append( std::u16string_view rString )
{
BuildAppend( rString );
}
void XclExpString::AppendByte( std::u16string_view rString, rtl_TextEncoding eTextEnc )
{
if (!rString.empty())
{
// length may differ from length of rString
OString aByteStr(OUStringToOString(rString, eTextEnc));
BuildAppend(aByteStr);
}
}
void XclExpString::AppendByte( sal_Unicode cChar, rtl_TextEncoding eTextEnc )
{
if( !cChar )
{
char cByteChar = 0;
BuildAppend( std::string_view(&cByteChar, 1) );
}
else
{
OString aByteStr( &cChar, 1, eTextEnc ); // length may be >1
BuildAppend( aByteStr );
}
}
// formatting runs ------------------------------------------------------------
void XclExpString::AppendFormat( sal_uInt16 nChar, sal_uInt16 nFontIdx, bool bDropDuplicate )
{
OSL_ENSURE( maFormats.empty() || (maFormats.back().mnChar < nChar), "XclExpString::AppendFormat - invalid char index" );
size_t nMaxSize = static_cast< size_t >( mbIsBiff8 ? EXC_STR_MAXLEN : EXC_STR_MAXLEN_8BIT );
if( maFormats.empty() || ((maFormats.size() < nMaxSize) && (!bDropDuplicate || (maFormats.back().mnFontIdx != nFontIdx))) )
maFormats.emplace_back( nChar, nFontIdx );
}
void XclExpString::AppendTrailingFormat( sal_uInt16 nFontIdx )
{
AppendFormat( mnLen, nFontIdx, false );
}
void XclExpString::LimitFormatCount( sal_uInt16 nMaxCount )
{
if( maFormats.size() > nMaxCount )
maFormats.erase( maFormats.begin() + nMaxCount, maFormats.end() );
}
sal_uInt16 XclExpString::GetLeadingFont()
{
sal_uInt16 nFontIdx = EXC_FONT_NOTFOUND;
if( !maFormats.empty() && (maFormats.front().mnChar == 0) )
{
nFontIdx = maFormats.front().mnFontIdx;
}
return nFontIdx;
}
sal_uInt16 XclExpString::RemoveLeadingFont()
{
sal_uInt16 nFontIdx = GetLeadingFont();
if( nFontIdx != EXC_FONT_NOTFOUND )
{
maFormats.erase( maFormats.begin() );
}
return nFontIdx;
}
bool XclExpString::IsEqual( const XclExpString& rCmp ) const
{
return
(mnLen == rCmp.mnLen) &&
(mbIsBiff8 == rCmp.mbIsBiff8) &&
(mbIsUnicode == rCmp.mbIsUnicode) &&
(mbWrapped == rCmp.mbWrapped) &&
(
( mbIsBiff8 && (maUniBuffer == rCmp.maUniBuffer)) ||
(!mbIsBiff8 && (maCharBuffer == rCmp.maCharBuffer))
) &&
(maFormats == rCmp.maFormats);
}
bool XclExpString::IsLessThan( const XclExpString& rCmp ) const
{
int nResult = mbIsBiff8 ?
lclCompareVectors( maUniBuffer, rCmp.maUniBuffer ) :
lclCompareVectors( maCharBuffer, rCmp.maCharBuffer );
return (nResult != 0) ? (nResult < 0) : (maFormats < rCmp.maFormats);
}
// get data -------------------------------------------------------------------
sal_uInt16 XclExpString::GetFormatsCount() const
{
return static_cast< sal_uInt16 >( maFormats.size() );
}
sal_uInt8 XclExpString::GetFlagField() const
{
return (mbIsUnicode ? EXC_STRF_16BIT : 0) | (IsWriteFormats() ? EXC_STRF_RICH : 0);
}
sal_uInt16 XclExpString::GetHeaderSize() const
{
return
(mb8BitLen ? 1 : 2) + // length field
(IsWriteFlags() ? 1 : 0) + // flag field
(IsWriteFormats() ? 2 : 0); // richtext formatting count
}
std::size_t XclExpString::GetBufferSize() const
{
return static_cast<std::size_t>(mnLen) * (mbIsUnicode ? 2 : 1);
}
std::size_t XclExpString::GetSize() const
{
return
GetHeaderSize() + // header
GetBufferSize() + // character buffer
(IsWriteFormats() ? (4 * GetFormatsCount()) : 0); // richtext formatting
}
sal_uInt16 XclExpString::GetChar( sal_uInt16 nCharIdx ) const
{
OSL_ENSURE( nCharIdx < Len(), "XclExpString::GetChar - invalid character index" );
return static_cast< sal_uInt16 >( mbIsBiff8 ? maUniBuffer[ nCharIdx ] : maCharBuffer[ nCharIdx ] );
}
sal_uInt16 XclExpString::GetHash() const
{
return
(mbIsBiff8 ? lclHashVector( maUniBuffer ) : lclHashVector( maCharBuffer )) ^
lclHashVector( maFormats, XclFormatRunHasher() );
}
// streaming ------------------------------------------------------------------
void XclExpString::WriteLenField( XclExpStream& rStrm ) const
{
if( mb8BitLen )
rStrm << static_cast< sal_uInt8 >( mnLen );
else
rStrm << mnLen;
}
void XclExpString::WriteFlagField( XclExpStream& rStrm ) const
{
if( mbIsBiff8 )
{
PrepareWrite( rStrm, 1 );
rStrm << GetFlagField();
rStrm.SetSliceSize( 0 );
}
}
void XclExpString::WriteHeader( XclExpStream& rStrm ) const
{
OSL_ENSURE( !mb8BitLen || (mnLen < 256), "XclExpString::WriteHeader - string too long" );
PrepareWrite( rStrm, GetHeaderSize() );
// length
WriteLenField( rStrm );
// flag field
if( IsWriteFlags() )
rStrm << GetFlagField();
// format run count
if( IsWriteFormats() )
rStrm << GetFormatsCount();
rStrm.SetSliceSize( 0 );
}
void XclExpString::WriteBuffer( XclExpStream& rStrm ) const
{
if( mbIsBiff8 )
rStrm.WriteUnicodeBuffer( maUniBuffer, GetFlagField() );
else
rStrm.WriteCharBuffer( maCharBuffer );
}
void XclExpString::WriteFormats( XclExpStream& rStrm, bool bWriteSize ) const
{
if( !IsRich() )
return;
if( mbIsBiff8 )
{
if( bWriteSize )
rStrm << GetFormatsCount();
rStrm.SetSliceSize( 4 );
for( const auto& rFormat : maFormats )
rStrm << rFormat.mnChar << rFormat.mnFontIdx;
}
else
{
if( bWriteSize )
rStrm << static_cast< sal_uInt8 >( GetFormatsCount() );
rStrm.SetSliceSize( 2 );
for( const auto& rFormat : maFormats )
rStrm << static_cast< sal_uInt8 >( rFormat.mnChar ) << static_cast< sal_uInt8 >( rFormat.mnFontIdx );
}
rStrm.SetSliceSize( 0 );
}
void XclExpString::Write( XclExpStream& rStrm ) const
{
if (!mbSkipHeader)
WriteHeader( rStrm );
WriteBuffer( rStrm );
if( IsWriteFormats() ) // only in BIFF8 included in string
WriteFormats( rStrm );
}
void XclExpString::WriteHeaderToMem( sal_uInt8* pnMem ) const
{
assert(pnMem);
OSL_ENSURE( !mb8BitLen || (mnLen < 256), "XclExpString::WriteHeaderToMem - string too long" );
OSL_ENSURE( !IsWriteFormats(), "XclExpString::WriteHeaderToMem - formatted strings not supported" );
// length
if( mb8BitLen )
{
*pnMem = static_cast< sal_uInt8 >( mnLen );
++pnMem;
}
else
{
ShortToSVBT16( mnLen, pnMem );
pnMem += 2;
}
// flag field
if( IsWriteFlags() )
*pnMem = GetFlagField();
}
void XclExpString::WriteBufferToMem( sal_uInt8* pnMem ) const
{
assert(pnMem);
if( IsEmpty() )
return;
if( mbIsBiff8 )
{
for( const sal_uInt16 nChar : maUniBuffer )
{
*pnMem = static_cast< sal_uInt8 >( nChar );
++pnMem;
if( mbIsUnicode )
{
*pnMem = static_cast< sal_uInt8 >( nChar >> 8 );
++pnMem;
}
}
}
else
memcpy( pnMem, maCharBuffer.data(), mnLen );
}
void XclExpString::WriteToMem( sal_uInt8* pnMem ) const
{
WriteHeaderToMem( pnMem );
WriteBufferToMem( pnMem + GetHeaderSize() );
}
static sal_uInt16 lcl_WriteRun( XclExpXmlStream& rStrm, const ScfUInt16Vec& rBuffer, sal_uInt16 nStart, sal_Int32 nLength, const XclExpFont* pFont )
{
if( nLength == 0 )
return nStart;
sax_fastparser::FSHelperPtr& rWorksheet = rStrm.GetCurrentStream();
rWorksheet->startElement(XML_r);
if( pFont )
{
const XclFontData& rFontData = pFont->GetFontData();
rWorksheet->startElement(XML_rPr);
XclXmlUtils::WriteFontData( rWorksheet, rFontData, XML_rFont );
rWorksheet->endElement( XML_rPr );
}
rWorksheet->startElement(XML_t, FSNS(XML_xml, XML_space), "preserve");
rWorksheet->writeEscaped( XclXmlUtils::ToOUString( rBuffer, nStart, nLength ) );
rWorksheet->endElement( XML_t );
rWorksheet->endElement( XML_r );
return nStart + nLength;
}
void XclExpString::WriteXml( XclExpXmlStream& rStrm ) const
{
sax_fastparser::FSHelperPtr rWorksheet = rStrm.GetCurrentStream();
if( !IsWriteFormats() )
{
rWorksheet->startElement(XML_t, FSNS(XML_xml, XML_space), "preserve");
rWorksheet->writeEscaped( XclXmlUtils::ToOUString( *this ) );
rWorksheet->endElement( XML_t );
}
else
{
XclExpFontBuffer& rFonts = rStrm.GetRoot().GetFontBuffer();
sal_uInt16 nStart = 0;
const XclExpFont* pFont = nullptr;
for ( const auto& rFormat : maFormats )
{
nStart = lcl_WriteRun( rStrm, GetUnicodeBuffer(),
nStart, rFormat.mnChar-nStart, pFont );
pFont = rFonts.GetFont( rFormat.mnFontIdx );
}
lcl_WriteRun( rStrm, GetUnicodeBuffer(),
nStart, GetUnicodeBuffer().size() - nStart, pFont );
}
}
bool XclExpString::IsWriteFlags() const
{
return mbIsBiff8 && (!IsEmpty() || !mbSmartFlags);
}
bool XclExpString::IsWriteFormats() const
{
return mbIsBiff8 && !mbSkipFormats && IsRich();
}
void XclExpString::SetStrLen( sal_Int32 nNewLen )
{
sal_uInt16 nAllowedLen = (mb8BitLen && (mnMaxLen > 255)) ? 255 : mnMaxLen;
mnLen = limit_cast< sal_uInt16 >( nNewLen, 0, nAllowedLen );
}
void XclExpString::CharsToBuffer( const sal_Unicode* pcSource, sal_Int32 nBegin, sal_Int32 nLen )
{
OSL_ENSURE( maUniBuffer.size() >= o3tl::make_unsigned( nBegin + nLen ),
"XclExpString::CharsToBuffer - char buffer invalid" );
ScfUInt16Vec::iterator aBeg = maUniBuffer.begin() + nBegin;
ScfUInt16Vec::iterator aEnd = aBeg + nLen;
const sal_Unicode* pcSrcChar = pcSource;
for( ScfUInt16Vec::iterator aIt = aBeg; aIt != aEnd; ++aIt, ++pcSrcChar )
{
*aIt = static_cast< sal_uInt16 >( *pcSrcChar );
if( *aIt & 0xFF00 )
mbIsUnicode = true;
}
if( !mbWrapped )
mbWrapped = ::std::find( aBeg, aEnd, EXC_LF ) != aEnd;
}
void XclExpString::CharsToBuffer( const char* pcSource, sal_Int32 nBegin, sal_Int32 nLen )
{
OSL_ENSURE( maCharBuffer.size() >= o3tl::make_unsigned( nBegin + nLen ),
"XclExpString::CharsToBuffer - char buffer invalid" );
ScfUInt8Vec::iterator aBeg = maCharBuffer.begin() + nBegin;
ScfUInt8Vec::iterator aEnd = aBeg + nLen;
const char* pcSrcChar = pcSource;
for( ScfUInt8Vec::iterator aIt = aBeg; aIt != aEnd; ++aIt, ++pcSrcChar )
*aIt = static_cast< sal_uInt8 >( *pcSrcChar );
mbIsUnicode = false;
if( !mbWrapped )
mbWrapped = ::std::find( aBeg, aEnd, EXC_LF_C ) != aEnd;
}
void XclExpString::Init( sal_Int32 nCurrLen, XclStrFlags nFlags, sal_uInt16 nMaxLen, bool bBiff8 )
{
mbIsBiff8 = bBiff8;
mbIsUnicode = bBiff8 && ( nFlags & XclStrFlags::ForceUnicode );
mb8BitLen = bool( nFlags & XclStrFlags::EightBitLength );
mbSmartFlags = bBiff8 && ( nFlags & XclStrFlags::SmartFlags );
mbSkipFormats = bool( nFlags & XclStrFlags::SeparateFormats );
mbWrapped = false;
mbSkipHeader = bool( nFlags & XclStrFlags::NoHeader );
mnMaxLen = nMaxLen;
SetStrLen( nCurrLen );
maFormats.clear();
if( mbIsBiff8 )
{
maCharBuffer.clear();
maUniBuffer.resize( mnLen );
}
else
{
maUniBuffer.clear();
maCharBuffer.resize( mnLen );
}
}
void XclExpString::Build( const sal_Unicode* pcSource, sal_Int32 nCurrLen, XclStrFlags nFlags, sal_uInt16 nMaxLen )
{
Init( nCurrLen, nFlags, nMaxLen, true );
CharsToBuffer( pcSource, 0, mnLen );
}
void XclExpString::Build( const char* pcSource, sal_Int32 nCurrLen, XclStrFlags nFlags, sal_uInt16 nMaxLen )
{
Init( nCurrLen, nFlags, nMaxLen, false );
CharsToBuffer( pcSource, 0, mnLen );
}
void XclExpString::InitAppend( sal_Int32 nAddLen )
{
SetStrLen( static_cast< sal_Int32 >( mnLen ) + nAddLen );
if( mbIsBiff8 )
maUniBuffer.resize( mnLen );
else
maCharBuffer.resize( mnLen );
}
void XclExpString::BuildAppend( std::u16string_view rSource )
{
OSL_ENSURE( mbIsBiff8, "XclExpString::BuildAppend - must not be called at byte strings" );
if( mbIsBiff8 )
{
sal_uInt16 nOldLen = mnLen;
InitAppend( rSource.size() );
CharsToBuffer( rSource.data(), nOldLen, mnLen - nOldLen );
}
}
void XclExpString::BuildAppend( std::string_view rSource )
{
OSL_ENSURE( !mbIsBiff8, "XclExpString::BuildAppend - must not be called at unicode strings" );
if( !mbIsBiff8 )
{
sal_uInt16 nOldLen = mnLen;
InitAppend( rSource.size() );
CharsToBuffer( rSource.data(), nOldLen, mnLen - nOldLen );
}
}
void XclExpString::PrepareWrite( XclExpStream& rStrm, sal_uInt16 nBytes ) const
{
rStrm.SetSliceSize( nBytes + (mbIsUnicode ? 2 : 1) );
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|