Skip to content

Commit

Permalink
Fix first letter not capitalized in Polish
Browse files Browse the repository at this point in the history
  • Loading branch information
pswid committed Jan 18, 2023
1 parent 4f05dda commit 7e174a1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
35 changes: 35 additions & 0 deletions src/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,41 @@ std::string Helpers::toUpper(std::string const & s) {
return lc;
}

// capitalizes one UTF-8 character in char array
// works with Latin1 (1 byte) and Polish (2 bytes) characters
// TODO add special characters that occur in other supported languages
void Helpers::CharToUpperUTF8(char * c) {
switch (*c) {
case 0xC3:
if (*(c + 1) == 0xB3) //ó (0xC3,0xB3) -> Ó (0xC3,0x93)
*(c + 1) = 0x93;
break;
case 0xC4:
switch (*(c + 1)) {
case 0x85: //ą (0xC4,0x85) -> Ą (0xC4,0x84)
case 0x87: //ć (0xC4,0x87) -> Ć (0xC4,0x86)
case 0x99: //ę (0xC4,0x99) -> Ę (0xC4,0x98)
*(c + 1) = *(c + 1) - 1;
break;
}
break;
case 0xC5:
switch (*(c + 1)) {
case 0x82: //ł (0xC5,0x82) -> Ł (0xC5,0x81)
case 0x84: //ń (0xC5,0x84) -> Ń (0xC5,0x83)
case 0x9B: //ś (0xC5,0x9B) -> Ś (0xC5,0x9A)
case 0xBA: //ź (0xC5,0xBA) -> Ź (0xC5,0xB9)
case 0xBC: //ż (0xC5,0xBC) -> Ż (0xC5,0xBB)
*(c + 1) = *(c + 1) - 1;
break;
}
break;
default:
*c = toupper(*c); //works on Latin1 letters
break;
}
}

// replace char in char string
void Helpers::replace_char(char * str, char find, char replace) {
if (str == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions src/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Helpers {
static std::string toLower(std::string const & s);
static std::string toUpper(std::string const & s);
static std::string toLower(const char * s);
static void CharToUpperUTF8(char * c);

static void replace_char(char * str, char find, char replace);

Expand Down
4 changes: 2 additions & 2 deletions src/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ void Mqtt::publish_ha_sensor_config(DeviceValue & dv, const std::string & model,

if (create_device_config) {
auto cap_name = strdup(device_type_name);
cap_name[0] = toupper(cap_name[0]); // capitalize
Helpers::CharToUpperUTF8(cap_name); // capitalize first letter
dev_json["name"] = std::string("EMS-ESP ") + cap_name;
dev_json["mf"] = brand;
dev_json["mdl"] = model;
Expand Down Expand Up @@ -1132,7 +1132,7 @@ void Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
// friendly name = <tag> <name>
char ha_name[70];
char * F_name = strdup(fullname);
F_name[0] = toupper(F_name[0]); // capitalize first letter
Helpers::CharToUpperUTF8(F_name); // capitalize first letter
if (EMSdevice::tag_to_string(tag).empty()) {
snprintf(ha_name, sizeof(ha_name), "%s", F_name); // no tag
} else {
Expand Down

0 comments on commit 7e174a1

Please sign in to comment.