-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextSanitize.cpp
More file actions
68 lines (63 loc) · 1.8 KB
/
Copy pathtextSanitize.cpp
File metadata and controls
68 lines (63 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "textSanitize.h"
#include <cstdio>
namespace puretext
{
std::string sanitizeHostname(const std::string &raw)
{
std::string out;
for (size_t i = 0; i < raw.length() && out.length() < 32; i++)
{
char c = raw[i];
if (c >= 'A' && c <= 'Z') c = c - 'A' + 'a';
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-')
{
out += c;
}
}
while (!out.empty() && out.front() == '-') out.erase(out.begin());
while (!out.empty() && out.back() == '-') out.pop_back();
if (out.empty()) out = "esp32worldclock";
return out;
}
bool parseMac(const std::string &s, uint8_t out[6])
{
unsigned int v[6];
int n = sscanf(s.c_str(), "%x:%x:%x:%x:%x:%x",
&v[0], &v[1], &v[2], &v[3], &v[4], &v[5]);
if (n != 6)
{
n = sscanf(s.c_str(), "%x-%x-%x-%x-%x-%x",
&v[0], &v[1], &v[2], &v[3], &v[4], &v[5]);
}
if (n != 6)
return false;
for (int i = 0; i < 6; i++)
{
if (v[i] > 0xFF)
return false;
out[i] = (uint8_t)v[i];
}
return true;
}
// Trim ASCII whitespace from both ends (matches Arduino String::trim()).
static std::string trimmed(const std::string &s)
{
size_t b = 0, e = s.length();
while (b < e && (unsigned char)s[b] <= ' ') b++;
while (e > b && (unsigned char)s[e - 1] <= ' ') e--;
return s.substr(b, e - b);
}
std::string normalizeMac(const std::string &s)
{
std::string t = trimmed(s);
if (t.empty())
return "";
uint8_t m[6];
if (!parseMac(t, m))
return t; // keep the typo visible so the user can fix it
char buf[18];
snprintf(buf, sizeof(buf), "%02X:%02X:%02X:%02X:%02X:%02X",
m[0], m[1], m[2], m[3], m[4], m[5]);
return std::string(buf);
}
} // namespace puretext