File indexing completed on 2024-12-29 04:50:10

0001 /*
0002     SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "alphaid.h"
0008 
0009 using namespace KItinerary;
0010 
0011 uint32_t KnowledgeDb::Internal::alphaIdFromString(QStringView s, int size)
0012 {
0013     uint32_t res = {};
0014     if (size != s.size()) {
0015         return res;
0016     }
0017 
0018     for (int i = 0; i < size; ++i) {
0019         const auto c = s[i];
0020         if (c.row() != 0 || (c.cell() < 'A' || c.cell() > 'Z')) {
0021             return {};
0022         }
0023         res |= (c.cell() - '@') << (5 * (size - i - 1));
0024     }
0025 
0026     return res;
0027 }
0028 
0029 QString KnowledgeDb::Internal::alphaIdToString(uint32_t id, int size)
0030 {
0031     if (id == 0) {
0032         return {};
0033     }
0034 
0035     QString s;
0036     s.reserve(size);
0037     for (int i = 0; i < size; ++i) {
0038         const auto shift = 5 * (size - i - 1);
0039         const auto mask = 31;
0040         const auto c = (id & (mask << shift)) >> shift;
0041         s.push_back(QLatin1Char(c + '@'));
0042     }
0043 
0044     return s;
0045 }