Baloo

doctermscodec.cpp
1 /*
2  SPDX-FileCopyrightText: 2015 Vishesh Handa <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6 
7 #include "doctermscodec.h"
8 
9 using namespace Baloo;
10 
11 QByteArray 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 
35 QVector<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 }
bool isEmpty() const const
QByteArray & append(char ch)
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
T & first()
Implements storage for docIds without any associated data Instantiated for:
Definition: coding.cpp:11
QByteArray mid(int pos, int len) const const
bool startsWith(const QByteArray &ba) const const
bool isEmpty() const const
T & last()
const char * constData() const const
int size() const const
int size() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Nov 29 2023 03:56:26 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.