forked from nickbnf/glogg
-
-
Notifications
You must be signed in to change notification settings - Fork 282
Expand file tree
/
Copy pathlogdata.cpp
More file actions
677 lines (548 loc) · 23.1 KB
/
logdata.cpp
File metadata and controls
677 lines (548 loc) · 23.1 KB
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/*
* Copyright (C) 2009, 2010, 2013, 2014, 2015 Nicolas Bonnefon and other contributors
*
* This file is part of glogg.
*
* glogg is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* glogg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with glogg. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Copyright (C) 2016 -- 2019 Anton Filimonov and other contributors
*
* This file is part of klogg.
*
* klogg is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* klogg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with klogg. If not, see <http://www.gnu.org/licenses/>.
*/
// This file implements LogData, the content of a log file.
#include <algorithm>
#include <limits>
#include <numeric>
#include <qregularexpression.h>
#include <qtextcodec.h>
#include <string_view>
#include <utility>
#include <vector>
#include <QFileInfo>
#include <simdutf.h>
#include "configuration.h"
#include "containers.h"
#include "linetypes.h"
#include "log.h"
#include "logfiltereddata.h"
#include "logdata.h"
LogData::LogData()
: AbstractLogData()
, indexing_data_( std::make_shared<IndexingData>() )
, operationQueue_( [ this ] { attached_file_->attachReader(); } )
, codec_( QTextCodec::codecForName( "ISO-8859-1" ) )
{
// Initialise the file watcher
connect( &FileWatcher::getFileWatcher(), &FileWatcher::fileChanged, this,
&LogData::fileChangedOnDisk, Qt::QueuedConnection );
auto worker = std::make_unique<LogDataWorker>( indexing_data_ );
// Forward the update signal
connect( worker.get(), &LogDataWorker::indexingProgressed, this, &LogData::loadingProgressed );
connect( worker.get(), &LogDataWorker::indexingFinished, this, &LogData::indexingFinished,
Qt::QueuedConnection );
connect( worker.get(), &LogDataWorker::checkFileChangesFinished, this,
&LogData::checkFileChangesFinished, Qt::QueuedConnection );
operationQueue_.setWorker( std::move( worker ) );
const auto& config = Configuration::get();
keepFileClosed_ = config.keepFileClosed();
if ( keepFileClosed_ ) {
LOG_INFO << "Keep file closed option is set";
}
const auto defaultEncodingMib = config.defaultEncodingMib();
if ( defaultEncodingMib >= 0 ) {
codec_.setCodec( QTextCodec::codecForMib( defaultEncodingMib ) );
}
}
LogData::~LogData()
{
LOG_DEBUG << "Destroying log data";
operationQueue_.shutdown();
}
void LogData::setPrefilter( const QString& prefilterPattern )
{
IndexingData::MutateAccessor scopedAccessor{ indexing_data_.get() };
prefilterPattern_ = prefilterPattern;
}
void LogData::attachFile( const QString& fileName )
{
LOG_DEBUG << "LogData::attachFile " << fileName.toStdString();
if ( attached_file_ ) {
// We cannot reattach
throw CantReattachErr();
}
indexingFileName_ = fileName;
attached_file_.reset( new FileHolder( keepFileClosed_ ) );
attached_file_->open( indexingFileName_ );
operationQueue_.enqueueOperation<AttachOperation>( fileName );
}
void LogData::interruptLoading()
{
operationQueue_.interrupt();
}
qint64 LogData::getFileSize() const
{
return IndexingData::ConstAccessor{ indexing_data_.get() }.getIndexedSize();
}
QDateTime LogData::getLastModifiedDate() const
{
return lastModifiedDate_;
}
// Return an initialised LogFilteredData. The search is not started.
std::unique_ptr<LogFilteredData> LogData::getNewFilteredData() const
{
return std::make_unique<LogFilteredData>( this );
}
void LogData::reload( QTextCodec* forcedEncoding )
{
operationQueue_.interrupt();
// Re-open the file, useful in case the file has been moved
attached_file_->reOpenFile();
operationQueue_.enqueueOperation<FullReindexOperation>( forcedEncoding );
}
void LogData::fileChangedOnDisk( const QString& filename )
{
LOG_INFO << "signalFileChanged " << filename << ", indexed file " << indexingFileName_;
QFileInfo info( indexingFileName_ );
const auto currentFileId = FileId::getFileId( indexingFileName_ );
const auto attachedFileId = attached_file_->getFileId();
const auto indexedHash = IndexingData::ConstAccessor{ indexing_data_.get() }.getHash();
LOG_INFO << "current indexed fileSize=" << indexedHash.size;
LOG_INFO << "current indexed hash=" << indexedHash.fullDigest;
LOG_INFO << "info file_->size()=" << info.size();
LOG_INFO << "attached_file_->size()=" << attached_file_->size();
LOG_INFO << "attached_file_id_ index " << attachedFileId.fileIndex;
LOG_INFO << "currentFileId index " << currentFileId.fileIndex;
// In absence of any clearer information, we use the following size comparison
// to determine whether we are following the same file or not (i.e. the file
// has been moved and the inode we are following is now under a new name, if for
// instance log has been rotated). We want to follow the name so we have to reopen
// the file to ensure we are reading the right one.
// This is a crude heuristic but necessary for notification services that do not
// give details (e.g. kqueues)
const bool isFileIdChanged = attachedFileId != currentFileId;
if ( !isFileIdChanged && filename != indexingFileName_ ) {
LOG_INFO << "ignore other file update";
return;
}
if ( isFileIdChanged || ( info.size() != attached_file_->size() )
|| ( !attached_file_->isOpen() ) ) {
LOG_INFO << "Inconsistent size, or file index, the file might have changed, re-opening";
attached_file_->reOpenFile();
}
operationQueue_.enqueueOperation<CheckDataChangesOperation>();
}
void LogData::indexingFinished( LoadingStatus status )
{
attached_file_->detachReader();
LOG_INFO << "indexingFinished for: " << indexingFileName_
<< ( status == LoadingStatus::Successful ) << ", found "
<< IndexingData::ConstAccessor{ indexing_data_.get() }.getNbLines() << " lines.";
if ( status == LoadingStatus::Successful ) {
FileWatcher::getFileWatcher().addFile( indexingFileName_ );
// Update the modified date/time if the file exists
lastModifiedDate_ = QDateTime();
QFileInfo fileInfo( indexingFileName_ );
if ( fileInfo.exists() )
lastModifiedDate_ = fileInfo.lastModified();
}
fileChangedOnDisk_ = MonitoredFileStatus::Unchanged;
LOG_DEBUG << "Sending indexingFinished.";
Q_EMIT loadingFinished( status );
operationQueue_.finishOperationAndStartNext();
}
void LogData::checkFileChangesFinished( MonitoredFileStatus status )
{
attached_file_->detachReader();
LOG_INFO << "File " << indexingFileName_ << " status " << static_cast<uint8_t>( status );
if ( fileChangedOnDisk_ != MonitoredFileStatus::Truncated ) {
switch ( status ) {
case MonitoredFileStatus::Truncated:
fileChangedOnDisk_ = MonitoredFileStatus::Truncated;
operationQueue_.enqueueOperation<FullReindexOperation>();
break;
case MonitoredFileStatus::DataAdded:
fileChangedOnDisk_ = MonitoredFileStatus::DataAdded;
operationQueue_.enqueueOperation<PartialReindexOperation>();
break;
case MonitoredFileStatus::Unchanged:
fileChangedOnDisk_ = MonitoredFileStatus::Unchanged;
break;
}
}
else {
operationQueue_.enqueueOperation<FullReindexOperation>();
}
if ( status != MonitoredFileStatus::Unchanged
|| fileChangedOnDisk_ == MonitoredFileStatus::Truncated ) {
Q_EMIT fileChanged( fileChangedOnDisk_ );
}
operationQueue_.finishOperationAndStartNext();
}
//
// Implementation of virtual functions
//
LinesCount LogData::doGetNbLine() const
{
return IndexingData::ConstAccessor{ indexing_data_.get() }.getNbLines();
}
LineLength LogData::doGetMaxLength() const
{
return IndexingData::ConstAccessor{ indexing_data_.get() }.getMaxLength();
}
LineLength LogData::doGetLineLength( LineNumber line ) const
{
if ( line >= IndexingData::ConstAccessor{ indexing_data_.get() }.getNbLines() ) {
return 0_length; /* exception? */
}
return LineLength{ doGetExpandedLineString( line ).size() };
}
void LogData::doSetDisplayEncoding( const char* encoding )
{
LOG_DEBUG << "AbstractLogData::setDisplayEncoding: " << encoding;
codec_.setCodec( QTextCodec::codecForName( encoding ) );
auto needReload = false;
auto useGuessedCodec = false;
{
IndexingData::ConstAccessor scopedAccessor{ indexing_data_.get() };
const QTextCodec* currentIndexCodec = scopedAccessor.getForcedEncoding();
if ( !currentIndexCodec ) {
currentIndexCodec = scopedAccessor.getEncodingGuess();
}
if ( currentIndexCodec && codec_.mibEnum() != currentIndexCodec->mibEnum() ) {
if ( codec_.encodingParameters() != EncodingParameters( currentIndexCodec ) ) {
needReload = true;
useGuessedCodec = codec_.mibEnum() == scopedAccessor.getEncodingGuess()->mibEnum();
}
}
}
if ( needReload ) {
reload( useGuessedCodec ? nullptr : codec_.codec() );
}
}
QTextCodec* LogData::doGetDisplayEncoding() const
{
return codec_.codec();
}
OneLineLog LogData::doGetOneLineLog( LineNumber line ) const
{
auto lines = doGetOneLineLogs( line, 1_lcount );
return lines.empty() ? OneLineLog{} : std::move( lines.front() );
}
klogg::vector<OneLineLog> LogData::doGetOneLineLogs( LineNumber firstLine, LinesCount number ) const
{
LOG_DEBUG << "firstLine:" << firstLine << " nb:" << number;
if ( number.get() == 0 ) {
return {};
}
klogg::vector<OneLineLog> processedLines;
std::shared_ptr<TextDecoder> dec{ std::make_shared<TextDecoder>( codec_.makeDecoder() ) };
std::shared_ptr<QRegularExpression> reg{ std::make_shared<QRegularExpression>(
prefilterPattern_, QRegularExpression::CaseInsensitiveOption ) };
try {
const auto rawLines = getLinesRaw( firstLine, number );
processedLines = rawLines.splitLines(
[ &dec, ® ]( const char* log, OneLineLog::Length len ) -> OneLineLog {
return { log, len, dec, reg };
} );
} catch ( const std::bad_alloc& e ) {
LOG_ERROR << "not enough memory " << e.what();
const char* replaceLog = "KLOGG WARNING: not enough memory";
processedLines.emplace_back(
replaceLog, type_safe::narrow_cast<OneLineLog::Length>( ::strlen( replaceLog ) ), dec,
reg );
}
while ( processedLines.size() < number.get() ) {
const char* replaceLog = "KLOGG WARNING: failed to read some lines before this one";
processedLines.emplace_back(
replaceLog, type_safe::narrow_cast<OneLineLog::Length>( ::strlen( replaceLog ) ), dec,
reg );
}
return processedLines;
}
QString LogData::doGetLineString( LineNumber line ) const
{
const auto lines = doGetLines( line, 1_lcount );
return lines.empty() ? QString{} : lines.front();
}
QString LogData::doGetExpandedLineString( LineNumber line ) const
{
return untabify( doGetLineString( line ) );
}
// Note this function is also called from the LogFilteredDataWorker thread, so
// data must be protected because they are changed in the main thread (by
// indexingFinished).
klogg::vector<QString> LogData::doGetLines( LineNumber first_line, LinesCount number ) const
{
return getLinesFromFile( first_line, number, []( QString&& lineData ) {
if ( lineData.endsWith( QChar::CarriageReturn ) ) {
lineData.chop( 1 );
}
return std::move( lineData );
} );
}
klogg::vector<QString> LogData::doGetExpandedLines( LineNumber first_line, LinesCount number ) const
{
return getLinesFromFile( first_line, number, []( QString&& lineData ) {
return untabify( std::move( lineData ) );
} );
}
LineNumber LogData::doGetLineNumber( LineNumber index ) const
{
return index;
}
LogData::RawLines LogData::getLinesRaw( LineNumber firstLine, LinesCount number ) const
{
RawLines rawLines;
rawLines.startLine = firstLine;
try {
rawLines.endOfLines.reserve( number.get() );
klogg::vector<LineNumber> lineNumbers{ static_cast<size_t>( number.get() ) };
std::iota( lineNumbers.begin(), lineNumbers.end(), firstLine );
IndexingData::ConstAccessor scopedAccessor{ indexing_data_.get() };
rawLines.prefilterPattern
= !prefilterPattern_.isEmpty() ? QRegularExpression(
prefilterPattern_, QRegularExpression::CaseInsensitiveOption )
: QRegularExpression{};
if ( lineNumbers.back() >= scopedAccessor.getNbLines() ) {
LOG_WARNING << "Lines out of bound asked for";
return {}; /* exception? */
}
ScopedFileHolder<FileHolder> fileHolder( attached_file_.get() );
const auto firstByte
= ( firstLine == 0_lnum )
? 0
: scopedAccessor.getEndOfLineOffset( firstLine - 1_lcount ).get();
const auto lastByte = scopedAccessor.getEndOfLineOffset( lineNumbers.back() ).get();
std::transform( lineNumbers.begin(), lineNumbers.end(),
std::back_inserter( rawLines.endOfLines ),
[ &scopedAccessor, firstByte ]( const LineNumber& line ) {
return scopedAccessor.getEndOfLineOffset( line ).get() - firstByte;
} );
const auto bytesToRead = lastByte - firstByte;
LOG_DEBUG << "will try to read:" << bytesToRead << " bytes";
rawLines.buffer.resize( static_cast<std::size_t>( bytesToRead ) );
fileHolder.getFile()->seek( firstByte );
const auto bytesRead = fileHolder.getFile()->read( rawLines.buffer.data(), bytesToRead );
if ( bytesRead != bytesToRead ) {
LOG_DEBUG << "failed to read " << bytesToRead << " bytes, got " << bytesRead;
}
LOG_DEBUG << "done reading lines:" << rawLines.buffer.size();
rawLines.textDecoder = codec_.makeDecoder();
return rawLines;
} catch ( const std::bad_alloc& ) {
LOG_ERROR << "not enough memory";
rawLines.endOfLines.clear();
rawLines.buffer.clear();
return rawLines;
}
}
klogg::vector<QString> LogData::getLinesFromFile( LineNumber firstLine, LinesCount number,
QString ( *processLine )( QString&& ) ) const
{
LOG_DEBUG << "firstLine:" << firstLine << " nb:" << number;
if ( number.get() == 0 ) {
return klogg::vector<QString>();
}
klogg::vector<QString> processedLines;
try {
const auto rawLines = getLinesRaw( firstLine, number );
auto decodedLines = rawLines.decodeLines();
processedLines.reserve( decodedLines.size() );
for ( auto&& line : decodedLines ) {
processedLines.push_back( processLine( std::move( line ) ) );
}
} catch ( const std::bad_alloc& e ) {
LOG_ERROR << "not enough memory " << e.what();
processedLines.emplace_back( "KLOGG WARNING: not enough memory" );
}
processedLines.reserve( number.get() - processedLines.size() );
while ( processedLines.size() < number.get() ) {
processedLines.emplace_back( "KLOGG WARNING: failed to read some lines before this one" );
}
return processedLines;
}
QTextCodec* LogData::getDetectedEncoding() const
{
return IndexingData::ConstAccessor{ indexing_data_.get() }.getEncodingGuess();
}
void LogData::doAttachReader() const
{
attached_file_->attachReader();
}
void LogData::doDetachReader() const
{
attached_file_->detachReader();
}
klogg::vector<QString> LogData::RawLines::decodeLines() const
{
if ( this->endOfLines.empty() ) {
return klogg::vector<QString>();
}
klogg::vector<QString> decodedLines;
decodedLines.reserve( this->endOfLines.size() );
try {
qint64 lineStart = 0;
size_t currentLineIndex = 0;
const auto lineFeedWidth = textDecoder.encodingParams.lineFeedWidth;
for ( const auto& lineEnd : this->endOfLines ) {
const auto length = lineEnd - lineStart - lineFeedWidth;
LOG_DEBUG << "line " << this->startLine.get() + currentLineIndex << ", length "
<< length;
constexpr auto maxlength = std::numeric_limits<int>::max() / 2;
if ( length >= maxlength ) {
decodedLines.emplace_back( "KLOGG WARNING: this line is too long" );
break;
}
if ( lineStart + length > klogg::ssize( buffer ) ) {
decodedLines.emplace_back( "KLOGG WARNING: file read failed" );
LOG_WARNING << "not enough data in buffer";
break;
}
auto decodedLine = textDecoder.decoder->toUnicode(
buffer.data() + lineStart, type_safe::narrow_cast<int>( length ) );
if ( !prefilterPattern.pattern().isEmpty() ) {
decodedLine.remove( prefilterPattern );
}
decodedLines.push_back( std::move( decodedLine ) );
lineStart = lineEnd;
}
} catch ( const std::bad_alloc& ) {
LOG_ERROR << "not enough memory";
decodedLines.emplace_back( "KLOGG WARNING: not enough memory" );
}
decodedLines.reserve( this->endOfLines.size() - decodedLines.size() );
while ( decodedLines.size() < this->endOfLines.size() ) {
decodedLines.emplace_back( "KLOGG WARNING: failed to decode some lines before this one" );
}
return decodedLines;
}
klogg::vector<OneLineLog>
LogData::RawLines::splitLines( OneLineLogConstructor makeOneLineLog ) const
{
if ( this->endOfLines.empty() ) {
return {};
}
klogg::vector<OneLineLog> decodedLines;
decodedLines.reserve( this->endOfLines.size() );
try {
qint64 lineStart = 0;
// size_t currentLineIndex = 0;
const auto lineFeedWidth = textDecoder.encodingParams.lineFeedWidth;
for ( const auto& lineEnd : this->endOfLines ) {
const auto length = lineEnd - lineStart - lineFeedWidth;
constexpr auto maxlength = std::numeric_limits<int>::max() / 2;
if ( length >= maxlength ) {
const char* log = "KLOGG WARNING: this line is too long";
decodedLines.emplace_back( makeOneLineLog(
log, type_safe::narrow_cast<OneLineLog::Length>( ::strlen( log ) ) ) );
break;
}
if ( lineStart + length > klogg::ssize( buffer ) ) {
const char* log = "KLOGG WARNING: file read failed";
decodedLines.emplace_back( makeOneLineLog(
log, type_safe::narrow_cast<OneLineLog::Length>( ::strlen( log ) ) ) );
LOG_WARNING << "not enough data in buffer";
break;
}
decodedLines.emplace_back( makeOneLineLog(
buffer.data() + lineStart, type_safe::narrow_cast<OneLineLog::Length>( length ) ) );
lineStart = lineEnd;
}
} catch ( const std::bad_alloc& ) {
LOG_ERROR << "not enough memory";
const char* log = "KLOGG WARNING: not enough memory";
decodedLines.emplace_back(
makeOneLineLog( log, type_safe::narrow_cast<OneLineLog::Length>( ::strlen( log ) ) ) );
}
while ( decodedLines.size() < this->endOfLines.size() ) {
const char* log = "KLOGG WARNING: failed to decode some lines before this one";
decodedLines.emplace_back(
makeOneLineLog( log, type_safe::narrow_cast<OneLineLog::Length>( ::strlen( log ) ) ) );
}
return decodedLines;
}
klogg::vector<std::string_view> LogData::RawLines::buildUtf8View() const
{
klogg::vector<std::string_view> lines;
if ( this->endOfLines.empty() || textDecoder.decoder == nullptr ) {
return lines;
}
try {
// const auto optimizeForNotLatinEncodings
// = Configuration::get().optimizeForNotLatinEncodings();
lines.reserve( endOfLines.size() );
std::string_view wholeString;
if ( prefilterPattern.pattern().isEmpty() && textDecoder.encodingParams.isUtf8Compatible ) {
wholeString = std::string_view( buffer.data(), buffer.size() );
}
else {
QString utf16Data;
if ( prefilterPattern.pattern().isEmpty() && textDecoder.encodingParams.isUtf16LE ) {
utf16Data = QString::fromRawData( reinterpret_cast<const QChar*>( buffer.data() ),
klogg::isize( buffer ) / 2 );
}
else {
utf16Data = textDecoder.decoder->toUnicode( buffer.data(), klogg::isize( buffer ) );
}
if ( !prefilterPattern.pattern().isEmpty() ) {
utf16Data.remove( prefilterPattern );
}
size_t resultSize = 0;
// if ( !optimizeForNotLatinEncodings ) {
// utf8Data_ = utf16Data.toUtf8();
// resultSize = static_cast<size_t>( utf8Data_.size() );
// }
// else {
utf8Data_.resize( buffer.size() * 2 );
resultSize = simdutf::convert_utf16_to_utf8(
reinterpret_cast<const char16_t*>( utf16Data.utf16() ),
static_cast<size_t>( utf16Data.size() ), utf8Data_.data() );
// }
wholeString = { utf8Data_.data(), resultSize };
}
auto nextLineFeed = wholeString.find( '\n' );
while ( nextLineFeed != std::string_view::npos ) {
lines.push_back( wholeString.substr( 0, nextLineFeed ) );
wholeString.remove_prefix( nextLineFeed + 1 );
nextLineFeed = wholeString.find( '\n' );
}
if ( !wholeString.empty() ) {
lines.push_back( wholeString );
}
} catch ( const std::exception& e ) {
LOG_ERROR << "failed to transform lines to utf8 " << e.what();
const auto lastLineOffset = utf8Data_.size();
// utf8Data_.append( "KLOGG WARNING: not enough memory, try decrease search buffer" );
lines.reserve( this->endOfLines.size() - lines.size() );
while ( lines.size() < this->endOfLines.size() ) {
lines.emplace_back( utf8Data_.data() + lastLineOffset,
utf8Data_.size() - lastLineOffset );
}
}
return lines;
}