cpp

Coverage Report

Created: 2023-09-13 01:07

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