OdbDesignLib
OdbDesign ODB++ Parsing Library
 
Loading...
Searching...
No Matches
ProtobufMapHelpers.h
1#pragma once
2
3#include <string>
4#include "../Text/Utf8Sanitizer.h"
5#include "Logger.h"
6
7namespace Odb::Lib::ProductModel
8{
9 // Copies entries into a protobuf map<string, TMessage> field, sanitizing each
10 // key with Odb::Lib::Text::ToUtf8. Distinct raw keys can sanitize to the same
11 // UTF-8 key — most commonly through the five undefined CP1252 slots (0x81,
12 // 0x8D, 0x8F, 0x90, 0x9D), which all map to U+FFFD, but also when a repaired
13 // key ("A\xC3" -> "AÃ") matches an already-valid UTF-8 key, or a key already
14 // containing U+FFFD matches one whose undefined byte mapped to U+FFFD. The
15 // find() check handles every collision uniformly: the first entry wins and
16 // the collision is logged, so no entry is ever silently overwritten.
17 template <typename TPbMap, typename TEntries>
18 void FillProtobufMapWithSanitizedKeys(TPbMap& pbMap, const TEntries& entries)
19 {
20 for (const auto& kv : entries)
21 {
22 auto sanitizedKey = Odb::Lib::Text::ToUtf8(kv.first);
23 if (pbMap.find(sanitizedKey) != pbMap.end())
24 {
25 logwarn("UTF-8 sanitization collision in protobuf map: key \"" + kv.first +
26 "\" sanitizes to a key that is already present; keeping the first entry");
27 continue;
28 }
29 pbMap[sanitizedKey] = *kv.second->to_protobuf();
30 }
31 }
32}
std::string ToUtf8(std::string_view input)
Converts input to valid UTF-8.