Skip to content

Commit

Permalink
Move decode_utf8 to source file (.cpp)
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardtfn committed Dec 5, 2024
1 parent 9ea9e68 commit 668655a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
26 changes: 26 additions & 0 deletions components/nspanel_ha_blueprint/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,30 @@ namespace nspanel_ha_blueprint {
return processed_time_format;
}

uint32_t decode_utf8(const char* bytes) {
if (!bytes || bytes[0] == '\0') {
return 0;
}
uint32_t code_point = 0;
unsigned char byte = static_cast<unsigned char>(bytes[0]);
if ((byte & 0x80) == 0x00) {
code_point = byte;
} else if ((byte & 0xE0) == 0xC0 && bytes[1] != '\0') {
code_point = ((byte & 0x1F) << 6) | (static_cast<unsigned char>(bytes[1]) & 0x3F);
} else if ((byte & 0xF0) == 0xE0 && bytes[1] != '\0' && bytes[2] != '\0') {
code_point = ((byte & 0x0F) << 12) |
((static_cast<unsigned char>(bytes[1]) & 0x3F) << 6) |
(static_cast<unsigned char>(bytes[2]) & 0x3F);
} else if ((byte & 0xF8) == 0xF0 && bytes[1] != '\0' && bytes[2] != '\0' && bytes[3] != '\0') {
code_point = ((byte & 0x07) << 18) |
((static_cast<unsigned char>(bytes[1]) & 0x3F) << 12) |
((static_cast<unsigned char>(bytes[2]) & 0x3F) << 6) |
(static_cast<unsigned char>(bytes[3]) & 0x3F);
} else {
code_point = 0;
}
return code_point;
}


} // namespace nspanel_ha_blueprint
25 changes: 1 addition & 24 deletions components/nspanel_ha_blueprint/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,7 @@ namespace nspanel_ha_blueprint {
std::string process_time_format(const std::string &time_format, int current_hour,
const std::string &meridiem_am, const std::string &meridiem_pm);

uint32_t decode_utf8(const char* bytes) {
if (!bytes || bytes[0] == '\0') {
return 0;
}
uint32_t code_point = 0;
unsigned char byte = static_cast<unsigned char>(bytes[0]);
if ((byte & 0x80) == 0x00) {
code_point = byte;
} else if ((byte & 0xE0) == 0xC0 && bytes[1] != '\0') {
code_point = ((byte & 0x1F) << 6) | (static_cast<unsigned char>(bytes[1]) & 0x3F);
} else if ((byte & 0xF0) == 0xE0 && bytes[1] != '\0' && bytes[2] != '\0') {
code_point = ((byte & 0x0F) << 12) |
((static_cast<unsigned char>(bytes[1]) & 0x3F) << 6) |
(static_cast<unsigned char>(bytes[2]) & 0x3F);
} else if ((byte & 0xF8) == 0xF0 && bytes[1] != '\0' && bytes[2] != '\0' && bytes[3] != '\0') {
code_point = ((byte & 0x07) << 18) |
((static_cast<unsigned char>(bytes[1]) & 0x3F) << 12) |
((static_cast<unsigned char>(bytes[2]) & 0x3F) << 6) |
(static_cast<unsigned char>(bytes[3]) & 0x3F);
} else {
code_point = 0;
}
return code_point;
}
uint32_t decode_utf8(const char* bytes);

inline std::string get_page_and_component(const std::string& page, const std::string& component) {
return page + "." + component;
Expand Down

0 comments on commit 668655a

Please sign in to comment.