cpp

Coverage Report

Created: 2023-03-07 20:24

/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
54
inline bool IsUnprintableLow(Str* ch) {
11
54
  assert(len(ch) == 1);
12
0
  return ch->data_[0] < ' ';
13
54
}
14
15
20
inline bool IsUnprintableHigh(Str* ch) {
16
20
  assert(len(ch) == 1);
17
  // 255 should not be -1!
18
  // log("ch->data_[0] %d", ch->data_[0]);
19
0
  unsigned char c = static_cast<unsigned char>(ch->data_[0]);
20
20
  return c >= 0x7f;
21
20
}
22
23
112
inline bool IsPlainChar(Str* ch) {
24
112
  assert(len(ch) == 1);
25
0
  uint8_t c = ch->data_[0];
26
112
  switch (c) {
27
0
  case '.':
28
11
  case '-':
29
11
  case '_':
30
11
    return true;
31
112
  }
32
101
  return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') ||
33
101
         ('0' <= c && c <= '9');
34
112
}
35
36
10
inline Str* XEscape(Str* ch) {
37
10
  assert(len(ch) == 1);
38
0
  Str* result = NewStr(4);
39
10
  sprintf(result->data(), "\\x%02x", ch->data_[0] & 0xff);
40
10
  return result;
41
10
}
42
43
10
inline Str* UEscape(int codepoint) {
44
  // maximum length:
45
  // 3 for \u{
46
  // 6 for codepoint
47
  // 1 for }
48
10
  Str* result = OverAllocatedStr(10);
49
10
  int n = sprintf(result->data(), "\\u{%x}", codepoint);
50
10
  result->MaybeShrink(n);  // truncate to what we wrote
51
10
  return result;
52
10
}
53
54
}  // namespace qsn
55
56
#endif  // QSN_H