KOSMIndoorMap

stringpool.cpp
1/*
2 SPDX-FileCopyrightText: 2020-2021 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "stringpool.h"
8
9#include <algorithm>
10
11OSM::StringKeyRegistryBase::StringKeyRegistryBase() = default;
12OSM::StringKeyRegistryBase::StringKeyRegistryBase(OSM::StringKeyRegistryBase&&) noexcept = default;
13OSM::StringKeyRegistryBase& OSM::StringKeyRegistryBase::operator=(OSM::StringKeyRegistryBase&&) noexcept = default;
14
15OSM::StringKeyRegistryBase::~StringKeyRegistryBase()
16{
17 std::for_each(m_pool.begin(), m_pool.end(), free);
18}
19
20const char* OSM::StringKeyRegistryBase::makeKeyInternal(const char *name, std::size_t len, OSM::StringMemory memOpt)
21{
22 const auto it = std::lower_bound(m_registry.begin(), m_registry.end(), name, [len](const char *lhs, const char *rhs) {
23 return std::strncmp(lhs, rhs, len) < 0;
24 });
25 if (it == m_registry.end() || std::strncmp((*it), name, len) != 0 || std::strlen(*it) != len) {
26 if (memOpt == OSM::StringMemory::Transient) {
27#ifndef _MSC_VER
28 auto s = strndup(name, len);
29#else
30 auto s = static_cast<char*>(malloc(len + 1));
31 std::strncpy(s, name, len);
32 s[len] = '\0';
33#endif
34 m_pool.push_back(s);
35 name = s;
36 }
37 m_registry.insert(it, name);
38 return name;
39 }
40 return (*it);
41}
42
43const char* OSM::StringKeyRegistryBase::keyInternal(const char *name) const
44{
45 const auto it = std::lower_bound(m_registry.begin(), m_registry.end(), name, [](const char *lhs, const char *rhs) {
46 return std::strcmp(lhs, rhs) < 0;
47 });
48 if (it == m_registry.end() || std::strcmp((*it), name) != 0) {
49 return {};
50 }
51 return (*it);
52}
Low-level types and functions to work with raw OSM data as efficiently as possible.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:20:03 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.