utility 2026.2.0
A comprehensive C++ utilities library tailored for the development of modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
code_points.cpp
1/*
2** ETIB PROJECT, 2026
3** utility
4** File description:
5** code_points
6*/
7
8#include <charconv>
9#include <sstream>
10#include <utility/graphic/text/code_points.hpp>
11
12namespace utility::graphic
13{
14 CodePoints::CodePoints(const std::string &content)
15 {
16 parse(content);
17 }
18
19 void CodePoints::parse(const std::string &content)
20 {
21 std::istringstream stream(content);
22 std::string line;
23 while (std::getline(stream, line)) {
24 const size_t spacePos = line.find(' ');
25 if (spacePos == std::string::npos) {
26 continue;
27 }
28 // Skip a line whose code is not a 32-bit hexadecimal number, as a
29 // line without a space is skipped: std::stoul threw out of the
30 // constructor on it, and truncated codes above 0xFFFFFFFF.
31 const char *first = line.data() + spacePos + 1;
32 const char *last = line.data() + line.size();
33 while (first != last && (*first == ' ' || *first == '\t')) {
34 ++first;
35 }
36 if (last - first > 2 && first[0] == '0'
37 && (first[1] == 'x' || first[1] == 'X')) {
38 first += 2;
39 }
40 uint32_t code = 0;
41 if (std::from_chars(first, last, code, 16).ec != std::errc()) {
42 continue;
43 }
44 _codes[line.substr(0, spacePos)] = code;
45 }
46 }
47
48 uint32_t CodePoints::getCode(const std::string &name) const
49 {
50 auto it = _codes.find(name);
51 if (it != _codes.end()) {
52 return it->second;
53 }
54 return 0;
55 }
56
57 std::string CodePoints::toUtf8(uint32_t codePoint)
58 {
59 std::string result;
60 if (codePoint <= 0x7F) {
61 result.push_back(static_cast<char>(codePoint));
62 } else if (codePoint <= 0x7FF) {
63 result.push_back(
64 static_cast<char>(0xC0 | ((codePoint >> 6) & 0x1F)));
65 result.push_back(static_cast<char>(0x80 | (codePoint & 0x3F)));
66 } else if (codePoint <= 0xFFFF) {
67 result.push_back(
68 static_cast<char>(0xE0 | ((codePoint >> 12) & 0x0F)));
69 result.push_back(
70 static_cast<char>(0x80 | ((codePoint >> 6) & 0x3F)));
71 result.push_back(static_cast<char>(0x80 | (codePoint & 0x3F)));
72 } else {
73 result.push_back(
74 static_cast<char>(0xF0 | ((codePoint >> 18) & 0x07)));
75 result.push_back(
76 static_cast<char>(0x80 | ((codePoint >> 12) & 0x3F)));
77 result.push_back(
78 static_cast<char>(0x80 | ((codePoint >> 6) & 0x3F)));
79 result.push_back(static_cast<char>(0x80 | (codePoint & 0x3F)));
80 }
81 return result;
82 }
83} // namespace utility::graphic
void parse(const std::string &content)
Populates the mapping from a .codepoints formatted string.
std::map< std::string, uint32_t > _codes
Internal name-to-code mapping.
uint32_t getCode(const std::string &name) const
Retrieves the Unicode code point for a given glyph name.
static std::string toUtf8(uint32_t codePoint)
Converts a single Unicode code point to a UTF-8 encoded string.
CodePoints()=default
Constructs an empty CodePoints object.