ESPHome
2026.11.0b6
Loading...
Searching...
No Matches
esphome
components
unicode
unicode.cpp
Go to the documentation of this file.
1
#include "
unicode.h
"
2
3
namespace
esphome::unicode
{
4
5
// Reads count continuation bytes into code_point, stopping at the first byte that is not one so a
6
// sequence cut short by the terminator is never read past
7
static
bool
read_continuation(
const
uint8_t *¤t, uint8_t
count
,
uint32_t
&code_point) {
8
for
(uint8_t i = 0; i <
count
; i++) {
9
const
uint8_t c = *current;
10
if
((c & 0xC0) != 0x80) {
11
return
false
;
12
}
13
current++;
14
code_point = (code_point << 6) | (c & 0x3F);
15
}
16
return
true
;
17
}
18
19
uint32_t
extract_unicode_codepoint
(
const
char
*utf8_str,
size_t
*
length
) {
20
const
auto
*start =
reinterpret_cast<
const
uint8_t *
>
(utf8_str);
21
const
uint8_t *current = start;
22
const
uint8_t c1 = *current++;
23
uint8_t
count
;
24
uint32_t
code_point;
25
uint32_t
lowest;
// the smallest code point this sequence length may encode; shorter forms are overlong
26
if
(c1 == 0) {
27
*
length
= 0;
28
return
0;
29
}
30
if
(c1 < 0x80) {
31
count
= 0;
32
code_point = c1;
33
lowest = 0;
34
}
else
if
((c1 & 0xE0) == 0xC0) {
35
count
= 1;
36
code_point = c1 & 0x1F;
37
lowest = 0x80;
38
}
else
if
((c1 & 0xF0) == 0xE0) {
39
count
= 2;
40
code_point = c1 & 0x0F;
41
lowest = 0x800;
42
}
else
if
((c1 & 0xF8) == 0xF0) {
43
count
= 3;
44
code_point = c1 & 0x07;
45
lowest = 0x10000;
46
}
else
{
47
*
length
= 0;
// a continuation byte or an invalid lead byte
48
return
0;
49
}
50
if
(!read_continuation(current,
count
, code_point) || code_point < lowest || code_point > 0x10FFFF ||
51
(code_point >= 0xD800 && code_point <= 0xDFFF)) {
52
*
length
= 0;
53
return
0;
54
}
55
*
length
= current - start;
56
return
code_point;
57
}
58
59
}
// namespace esphome::unicode
count
unsigned count
Definition
gif.cpp:51
esphome::unicode
Definition
unicode.cpp:3
esphome::unicode::extract_unicode_codepoint
uint32_t extract_unicode_codepoint(const char *utf8_str, size_t *length)
Attempt to extract a 32 bit Unicode codepoint from a UTF-8 string.
Definition
unicode.cpp:19
uint32_t
static void uint32_t
Definition
crash_handler.cpp:153
length
uint16_t length
Definition
tt21100.cpp:0
unicode.h
Generated by
1.12.0