1 | /* |
---|---|
2 | This file is part of the KDE Baloo Project |
3 | SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org> |
4 | |
5 | SPDX-License-Identifier: LGPL-2.1-or-later |
6 | */ |
7 | |
8 | #include "positioncodec.h" |
9 | #include "positioninfo.h" |
10 | #include "coding.h" |
11 | |
12 | using namespace Baloo; |
13 | |
14 | QByteArray PositionCodec::encode(const QVector<PositionInfo>& list) |
15 | { |
16 | QByteArray data; |
17 | QByteArray temporaryStorage; |
18 | |
19 | for (const PositionInfo& pos : list) { |
20 | putFixed64(dst: &data, value: pos.docId); |
21 | putDifferentialVarInt32(temporaryStorage, dst: &data, values: pos.positions); |
22 | } |
23 | |
24 | return data; |
25 | } |
26 | |
27 | QVector<PositionInfo> PositionCodec::decode(const QByteArray& arr) |
28 | { |
29 | char* data = const_cast<char*>(arr.data()); |
30 | char* end = data + arr.size(); |
31 | |
32 | QVector<PositionInfo> vec; |
33 | while (data < end) { |
34 | PositionInfo info; |
35 | |
36 | info.docId = decodeFixed64(ptr: data); |
37 | data += sizeof(quint64); |
38 | data = getDifferentialVarInt32(input: data, limit: end, values: &info.positions); |
39 | if (!data) { |
40 | return QVector<PositionInfo>(); |
41 | } |
42 | |
43 | vec << info; |
44 | } |
45 | |
46 | return vec; |
47 | } |
48 |