-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrtutils.cpp
More file actions
44 lines (39 loc) · 1.21 KB
/
crtutils.cpp
File metadata and controls
44 lines (39 loc) · 1.21 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
#include "crtutils.hpp"
#include <ctime>
#include <random>
#include <regex>
bool isInvalidID(const QStringView &id) {
for (const auto it : id.toString().toStdString()) {
if (!isalnum(it) && it != '_')
return true;
}
return false;
}
namespace EmailIdentifierDetail {
static const std::regex emailre("^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
}
bool isInvalidEmail(const QStringView &email) {
return !std::regex_match(email.toString().toStdString(), EmailIdentifierDetail::emailre);
}
QString randomID(uint8_t len) {
std::random_device rd;
QString o;
std::mt19937_64 r(time(nullptr) + rd());
for (uint8_t i{}; i < len; i++) {
uint8_t _r = r() % 63;
if (_r < 26)
o += (QChar)('a' + _r); // 0 ~ 25 : 'a' ~ 'z'
else if (_r < 52)
o += (QChar)('A' + _r - 26); // 26 ~ 51 : 'A' ~ 'Z'
else if (_r < 62)
o += (QChar)('0' + _r - 52); // 52 ~ 61 : '0' ~ '9'
else
o += '_'; // 62 : '_'
}
return o;
}
QString char32ToQString(const char32_t &c) {
char32_t alignedChar;
std::memcpy(&alignedChar, &c, sizeof(char32_t));
return QString::fromUcs4(&alignedChar, 1);
}