forked from oils-for-unix/oils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqsn.h
56 lines (46 loc) · 1.11 KB
/
qsn.h
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
// cpp/qsn.h
#ifndef QSN_H
#define QSN_H
#include "mycpp/runtime.h"
namespace qsn {
inline bool IsUnprintableLow(Str* ch) {
assert(len(ch) == 1);
return ch->data_[0] < ' ';
}
inline bool IsUnprintableHigh(Str* ch) {
assert(len(ch) == 1);
// 255 should not be -1!
// log("ch->data_[0] %d", ch->data_[0]);
unsigned char c = static_cast<unsigned char>(ch->data_[0]);
return c >= 0x7f;
}
inline bool IsPlainChar(Str* ch) {
assert(len(ch) == 1);
uint8_t c = ch->data_[0];
switch (c) {
case '.':
case '-':
case '_':
return true;
}
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') ||
('0' <= c && c <= '9');
}
inline Str* XEscape(Str* ch) {
assert(len(ch) == 1);
Str* result = NewStr(4);
sprintf(result->data(), "\\x%02x", ch->data_[0] & 0xff);
return result;
}
inline Str* UEscape(int codepoint) {
// maximum length:
// 3 for \u{
// 6 for codepoint
// 1 for }
Str* result = OverAllocatedStr(10);
int n = sprintf(result->data(), "\\u{%x}", codepoint);
result->MaybeShrink(n); // truncate to what we wrote
return result;
}
} // namespace qsn
#endif // QSN_H