-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringDict.cpp
More file actions
50 lines (45 loc) · 1.26 KB
/
StringDict.cpp
File metadata and controls
50 lines (45 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "StringDict.h"
#include <MapCollector.hpp>
#include "Arduino.h"
namespace SA
{
int32_t FindIndexOfValue(const std::vector<std::string>& values, const char* value)
{
for (int index = 0; index < values.size(); ++index)
{
if (values[index] == value)
{
return index;
}
}
return -1;
}
StringDict::StringDict(std::string_view str, std::string_view keyCommonPart)
{
{
auto func = [&](std::string key, std::string value)
{
const size_t idx = keyCommonPart.empty() ? 0 : key.find(keyCommonPart.begin(), 0, keyCommonPart.length());
if (idx != std::string::npos)
{
const std::string_view keyStr(key.begin() + idx + keyCommonPart.length(), key.end());
const std::size_t keyHash = std::hash<std::string_view>{}(keyStr);
int32_t index = FindIndexOfValue(m_values, value.c_str());
if (index == -1)
{
index = m_values.size();
m_values.emplace_back(std::move(value));
}
m_keys.push_back({static_cast<int32_t>(keyHash), index});
}
return false;
};
MapCollector parser(func);
for (char c : str)
{
parser.parse(c);
}
}
m_values.shrink_to_fit();
}
}