KOSMIndoorMap

stringpool.cpp
1 /*
2  SPDX-FileCopyrightText: 2020-2021 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "stringpool.h"
8 
9 #include <algorithm>
10 #include <cstring>
11 
12 OSM::StringKeyRegistryBase::StringKeyRegistryBase() = default;
13 OSM::StringKeyRegistryBase::StringKeyRegistryBase(OSM::StringKeyRegistryBase&&) = default;
14 OSM::StringKeyRegistryBase& OSM::StringKeyRegistryBase::operator=(OSM::StringKeyRegistryBase&&) = default;
15 
16 OSM::StringKeyRegistryBase::~StringKeyRegistryBase()
17 {
18  std::for_each(m_pool.begin(), m_pool.end(), free);
19 }
20 
21 const char* OSM::StringKeyRegistryBase::makeKeyInternal(const char *name, std::size_t len, OSM::StringMemory memOpt)
22 {
23  const auto it = std::lower_bound(m_registry.begin(), m_registry.end(), name, [len](const char *lhs, const char *rhs) {
24  return std::strncmp(lhs, rhs, len) < 0;
25  });
26  if (it == m_registry.end() || std::strncmp((*it), name, len) != 0 || std::strlen(*it) != len) {
27  if (memOpt == OSM::StringMemory::Transient) {
28  auto s = strndup(name, len);
29  m_pool.push_back(s);
30  name = s;
31  }
32  m_registry.insert(it, name);
33  return name;
34  }
35  return (*it);
36 }
37 
38 const char* OSM::StringKeyRegistryBase::keyInternal(const char *name) const
39 {
40  const auto it = std::lower_bound(m_registry.begin(), m_registry.end(), name, [](const char *lhs, const char *rhs) {
41  return std::strcmp(lhs, rhs) < 0;
42  });
43  if (it == m_registry.end() || std::strcmp((*it), name) != 0) {
44  return {};
45  }
46  return (*it);
47 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 25 2021 23:04:00 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.