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(a: terms.first());
17 full.append(c: '\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(bv: prevTerm)) {
24 full.append(a: term.mid(index: prevTerm.size()));
25 full.append(c: static_cast<char>(1));
26 } else {
27 full.append(a: term);
28 full.append(c: '\0');
29 }
30 }
31
32 return full;
33}
34
35QVector<QByteArray> DocTermsCodec::decode(const QByteArray& full)
36{
37 Q_ASSERT(full.size());
38
39 QVector<QByteArray> list;
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}
67

source code of baloo/src/codecs/doctermscodec.cpp