65 if (arg ==
"on" || arg ==
"true") {
67 }
else if (arg ==
"0" || arg ==
"off" || arg ==
"false") {
69 }
else if (atoi(arg.c_str())) {
76int16_t
parseToken(String arg,
const char **tokenList, int16_t defaultVal = -1) {
93 for (
const char **pToken = tokenList; *pToken; pToken++) {
94 if (!strcmp(arg.c_str(), *pToken)) {
95 return pToken - tokenList;
112 if (arg.length() == 0) {
115 for (
const char *pPtr = arg.c_str(); *pPtr; pPtr++) {
116 if (*pPtr < '0' || *pPtr >
'9') {
117 if (pPtr == arg.c_str() && *pPtr !=
'-') {
122 return atol(arg.c_str());
125long parseRangedLong(String arg,
long minVal,
long maxVal,
long minDefaultVal,
long maxDefaultVal) {
141 long val =
parseLong(arg, minDefaultVal);
142 if (val == minDefaultVal) {
144 return minDefaultVal;
145 }
else if (val < minVal) {
146 return minDefaultVal;
147 }
else if (val > maxVal) {
148 return maxDefaultVal;
168 if (arg ==
"on" || arg ==
"true") {
170 }
else if (arg ==
"off" || arg ==
"false") {
172 }
else if (arg ==
"pct") {
174 val = atoi(arg.c_str()) / 100.0;
175 }
else if (arg.endsWith(
"%")) {
176 arg.remove(arg.length() - 1);
177 val = atoi(arg.c_str()) / 100.0;
178 }
else if (arg.indexOf(
'.') == -1) {
179 val = atoi(arg.c_str()) / 100.0;
181 val = atof(arg.c_str());
183 return val < 0.0 ? 0.0 : val > 1.0 ? 1.0
190 for (uint16_t i = 0; i < txt.length(); i++) {
191 if (txt[i] == c) ++n;
196bool getNthHexbyte(String hex, uint8_t ind, uint8_t *pVal) {
197 if (hex.length() < (uint16_t)((ind + 1) * 2))
return false;
199 *pVal = (uint8_t)strtol(hex.substring(ind * 2, ind * 2 + 2).c_str(), 0, 16);
205bool getNthInt(String arg, uint8_t ind, uint8_t *pVal) {
207 uint8_t i = 0, n = 0, p1 = 0, p2 = 0;
208 String carg =
"," + arg +
",";
210 if (carg[i] ==
',') {
214 *pVal = strtol(carg.substring(p1, p2).c_str(), 0, 10);
218 }
else if (n == ind) {
224 if (i == carg.length())
return false;
228bool parseColor(String arg, uint8_t *pr, uint8_t *pg, uint8_t *pb, uint8_t *pw =
nullptr, uint8_t *pww =
nullptr) {
249 if (arg.startsWith(
"#")) {
250 hex = arg.substring(1);
251 }
else if (arg.startsWith(
"0x")) {
252 hex = arg.substring(2);
253 }
else if (arg.indexOf(
',') != -1) {
257 if (!getNthInt(arg, 0, pr))
return false;
259 if (!getNthInt(arg, 1, pg))
return false;
261 if (!getNthInt(arg, 2, pb))
return false;
264 if (!getNthInt(arg, 0, pw))
return false;
266 if (!getNthInt(arg, 1, pr))
return false;
268 if (!getNthInt(arg, 2, pg))
return false;
270 if (!getNthInt(arg, 3, pb))
return false;
273 if (!getNthInt(arg, 0, pww))
return false;
275 if (!getNthInt(arg, 1, pw))
return false;
277 if (!getNthInt(arg, 2, pr))
return false;
279 if (!getNthInt(arg, 3, pg))
return false;
281 if (!getNthInt(arg, 4, pb))
return false;
286 if (hex.length() == 6) {
288 if (!getNthHexbyte(hex, 0, pr))
return false;
290 if (!getNthHexbyte(hex, 1, pg))
return false;
292 if (!getNthHexbyte(hex, 2, pb))
return false;
293 }
else if (hex.length() == 8) {
295 if (!getNthHexbyte(hex, 0, pw))
return false;
297 if (!getNthHexbyte(hex, 1, pr))
return false;
299 if (!getNthHexbyte(hex, 2, pg))
return false;
301 if (!getNthHexbyte(hex, 3, pb))
return false;
302 }
else if (hex.length() == 10) {
304 if (!getNthHexbyte(hex, 0, pww))
return false;
306 if (!getNthHexbyte(hex, 1, pw))
return false;
308 if (!getNthHexbyte(hex, 2, pr))
return false;
310 if (!getNthHexbyte(hex, 3, pg))
return false;
312 if (!getNthHexbyte(hex, 4, pb))
return false;
343 for (
unsigned int i = 0; i < utf8string.length(); i++) {
344 unsigned char c = utf8string[i];
351bool isNumber(String
string,
bool integer =
false) {
359 return isNumber(
string.c_str(), integer);
362bool isNumber(
const char *value,
bool integer =
false) {
376 bool decimalpoint =
false;
378 if (*value < '0' || *value >
'9') {
379 if (integer || decimalpoint || *value !=
'.') {
409 for (
unsigned int i = 0; i < utf8string.length(); i++) {
411 if ((c & 0x80) == 0) {
415 if ((c & 0b11100000) == 0b11000000) {
416 if (i < utf8string.length() - 1) {
417 nc = utf8string[i + 1];
427 latin += (char)invalid_char;
433 latin += (char)invalid_char;
437 if ((c & 0b11110000) == 0b11100000) {
439 latin += (char)invalid_char;
442 if ((c & 0b11111000) == 0b11110000) {
444 latin += (char)invalid_char;
447 latin += (char)invalid_char;
467 for (
unsigned int i = 0; i < latin.length(); i++) {
473 utf8str += (char)0xc2;
476 utf8str += (char)0xc3;
494class HD44780Charset {
496 HD44780Charset(
char *pLookupTable =
nullptr) {
499 static String toHD_ASCII(String utf8string,
char invalid_char =
'_') {
519 for (
unsigned int i = 0; i < utf8string.length(); i++) {
522 if ((c & 0x80) == 0) {
524 if (strchr(
"gjpqy", c) !=
nullptr)
528 hdstr += (char)invalid_char;
532 if ((c & 0b11100000) == 0b11000000) {
533 if (i < utf8string.length() - 1) {
534 hdstr += (char)invalid_char;
538 hdstr += (char)invalid_char;
542 if ((c & 0b11110000) == 0b11100000) {
544 hdstr += (char)invalid_char;
547 if ((c & 0b11111000) == 0b11110000) {
549 hdstr += (char)invalid_char;
552 hdstr += (char)invalid_char;
562 static String toUtf8(String hdstr,
char invalid_char =
'_') {
573 for (
unsigned int i = 0; i < hdstr.length(); i++) {
578 char cf = (char)(c - 0x80);
579 if (strchr(
"gjpqy", cf) !=
nullptr)
582 utf8str += (char)invalid_char;
The muwerk namespace.
Definition: home_assistant.h:10
long parseRangedLong(String arg, long minVal, long maxVal, long minDefaultVal, long maxDefaultVal)
Definition: mupplet_core.h:125
int8_t parseBoolean(String arg)
Definition: mupplet_core.h:51
long parseLong(String arg, long defaultVal)
Definition: mupplet_core.h:101
String utf8ToLatin(String utf8string, char invalid_char='_')
Definition: mupplet_core.h:390
double parseUnitLevel(String arg)
Definition: mupplet_core.h:154
String latinToUtf8(String latin)
Definition: mupplet_core.h:457
int16_t parseToken(String arg, const char **tokenList, int16_t defaultVal=-1)
Definition: mupplet_core.h:76
uint16_t countChars(String txt, char c)
Definition: mupplet_core.h:187
bool isAscii(String utf8string)
mupplet-core string encoding utilities
Definition: mupplet_core.h:337
bool parseColor(String arg, uint8_t *pr, uint8_t *pg, uint8_t *pb, uint8_t *pw=nullptr, uint8_t *pww=nullptr)
Definition: mupplet_core.h:228
bool isNumber(String string, bool integer=false)
Definition: mupplet_core.h:351