-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpers.cpp
More file actions
88 lines (67 loc) · 2.93 KB
/
Helpers.cpp
File metadata and controls
88 lines (67 loc) · 2.93 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "Helpers.h"
/**
* Borrowed from spacehuhn
* https://github.com/spacehuhn/esp8266_deauther/blob/master/esp8266_deauther/functions.h
*/
bool getBit(uint8_t b, uint8_t n) {
return (b >> n) % 2 != 0;
}
uint8_t utf8(uint8_t c) {
if (!getBit(c, 7)) return 1;
if (getBit(c, 7) && getBit(c, 6) && !getBit(c, 5)) return 2;
if (getBit(c, 7) && getBit(c, 6) && getBit(c, 5) && !getBit(c, 4)) return 3;
if (getBit(c, 7) && getBit(c, 6) && getBit(c, 5) && getBit(c, 4) && !getBit(c, 3)) return 4;
return 0;
}
bool utf8Part(uint8_t c) {
return getBit(c, 7) && !getBit(c, 6);
}
String fixUtf8(String str) {
int size = str.length();
String result = String();
char c;
uint8_t len;
bool ok;
for (int i = 0; i < size; i++) {
c = str.charAt(i); // get character
len = utf8(c); // get utf8 char len
if (len <= 1) {
result += c; // when 1 byte char, add it :)
}
else if (i + len > size) { // when char bigger than remaining string, end loop
i = size + 1;
}
else {
ok = true;
for (int j = 1; j < len && ok; j++) {
ok = utf8Part(str.charAt(i + j)); // if following char is compliant or not
}
if (ok) result += c; // everything is ok, add char and continue
else { // utf8 char is broken
for (int j = 1; j < len; j++) { // go through the next bytes
c = str.charAt(i + j);
if (utf8(c) == 1) result += c; // when byte is ascii, add it :)
}
i += len - 1; // skip utf8 char because we already managed it
}
}
}
return result;
}
String replaceUtf8(String str, String r) {
str = fixUtf8(str); // fix it in case a utf char is broken
int size = str.length();
String result = String();
char c;
uint8_t len;
for (int i = 0; i < size; i++) {
c = str.charAt(i); // get character
len = utf8(c); // get utf8 char len
if (len <= 1) result += c; // when 1 byte char, add it :)
else {
result += r;
i += len - 1; // skip other chars
}
}
return result;
}