cpp

Coverage Report

Created: 2022-11-10 11:34

/home/andy/git/oilshell/oil/cpp/qsn.h
Line
Count
Source (jump to first uncovered line)
1
// cpp/qsn.h
2
3
#ifndef QSN_H
4
#define QSN_H
5
6
#include "mycpp/runtime.h"
7
8
namespace qsn {
9
10
33
inline bool IsUnprintableLow(Str* ch) {
11
33
  assert(len(ch) == 1);
12
0
  return ch->data_[0] < ' ';
13
33
}
14
15
0
inline bool IsUnprintableHigh(Str* ch) {
16
0
  assert(len(ch) == 1);
17
0
  return ch->data_[0] >= 0x7f;
18
0
}
19
20
79
inline bool IsPlainChar(Str* ch) {
21
79
  assert(len(ch) == 1);
22
0
  uint8_t c = ch->data_[0];
23
79
  switch (c) {
24
0
  case '.':
25
0
  case '-':
26
0
  case '_':
27
0
    return true;
28
79
  }
29
79
  return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') ||
30
79
         ('0' <= c && c <= '9');
31
79
}
32
33
10
inline Str* XEscape(Str* ch) {
34
10
  assert(len(ch) == 1);
35
0
  StackRoots _roots({&ch});
36
10
  Str* result = NewStr(4);
37
10
  sprintf(result->data(), "\\x%02x", ch->data_[0] & 0xff);
38
10
  return result;
39
10
}
40
41
10
inline Str* UEscape(int codepoint) {
42
  // maximum length:
43
  // 3 for \u{
44
  // 6 for codepoint
45
  // 1 for }
46
10
  Str* result = OverAllocatedStr(10);
47
10
  int n = sprintf(result->data(), "\\u{%x}", codepoint);
48
10
  result->SetObjLenFromStrLen(n);  // truncate to what we wrote
49
10
  return result;
50
10
}
51
52
}  // namespace qsn
53
54
#endif  // QSN_H