Baloo

doctermscodec.cpp
1/*
2 SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5*/
6
7#include "doctermscodec.h"
8
9using namespace Baloo;
10
11QByteArray DocTermsCodec::encode(const QVector<QByteArray>& terms)
12{
13 Q_ASSERT(!terms.isEmpty());
14
15 QByteArray full;
16 full.append(terms.first());
17 full.append('\0');
18
19 for (int i = 1; i < terms.size(); i++) {
20 const QByteArray term = terms[i];
21 const QByteArray prevTerm = terms[i-1];
22
23 if (term.startsWith(prevTerm)) {
24 full.append(term.mid(prevTerm.size()));
25 full.append(static_cast<char>(1));
26 } else {
27 full.append(term);
28 full.append('\0');
29 }
30 }
31
32 return full;
33}
34
35QVector<QByteArray> DocTermsCodec::decode(const QByteArray& full)
36{
37 Q_ASSERT(full.size());
38
40
41 int prevWordBoundary = 0;
42 for (int i = 0; i < full.size(); i++) {
43 if (full[i] == 1) {
44 if (list.isEmpty()) {
45 // corrupted entry - no way to recover
46 return list;
47 }
48
49 QByteArray arr(full.constData() + prevWordBoundary, i - prevWordBoundary);
50
51 list << list.last() + arr;
52 prevWordBoundary = i + 1;
53 continue;
54 }
55
56 if (full[i] == '\0') {
57 QByteArray arr(full.constData() + prevWordBoundary, i - prevWordBoundary);
58
59 list << arr;
60 prevWordBoundary = i + 1;
61 continue;
62 }
63 }
64
65 return list;
66}
Implements storage for docIds without any associated data Instantiated for:
Definition coding.cpp:11
KIOCORE_EXPORT QStringList list(const QString &fileClass)
QByteArray & append(QByteArrayView data)
const char * constData() const const
QByteArray mid(qsizetype pos, qsizetype len) const const
qsizetype size() const const
bool startsWith(QByteArrayView bv) const const
T & first()
bool isEmpty() const const
T & last()
qsizetype size() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.