cpp

Coverage Report

Created: 2022-07-20 01:16

/home/uke/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
#ifdef LEAKY_BINDINGS
7
#include "mycpp/mylib_leaky.h"
8
using gc_heap::StackRoots;  // no-op
9
using mylib::AllocStr;
10
using mylib::CopyStr;
11
using mylib::OverAllocatedStr;
12
#else
13
#include "mycpp/gc_heap.h"
14
using gc_heap::AllocStr;
15
using gc_heap::CopyStr;
16
using gc_heap::OverAllocatedStr;
17
using gc_heap::StackRoots;
18
using gc_heap::Str;
19
#endif
20
21
namespace qsn {
22
23
33
inline bool IsUnprintableLow(Str* ch) {
24
33
  assert(len(ch) == 1);
25
0
  return ch->data_[0] < ' ';
26
33
}
27
28
0
inline bool IsUnprintableHigh(Str* ch) {
29
0
  assert(len(ch) == 1);
30
0
  return ch->data_[0] >= 0x7f;
31
0
}
32
33
79
inline bool IsPlainChar(Str* ch) {
34
79
  assert(len(ch) == 1);
35
0
  uint8_t c = ch->data_[0];
36
79
  switch (c) {
37
0
  case '.':
38
0
  case '-':
39
0
  case '_':
40
0
    return true;
41
79
  }
42
79
  return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') ||
43
79
         ('0' <= c && c <= '9');
44
79
}
45
46
10
inline Str* XEscape(Str* ch) {
47
10
  assert(len(ch) == 1);
48
0
  StackRoots _roots({&ch});
49
10
  Str* result = AllocStr(4);
50
10
  sprintf(result->data(), "\\x%02x", ch->data_[0] & 0xff);
51
10
  return result;
52
10
}
53
54
10
inline Str* UEscape(int codepoint) {
55
  // maximum length:
56
  // 3 for \u{
57
  // 6 for codepoint
58
  // 1 for }
59
10
  Str* result = OverAllocatedStr(10);
60
10
  int n = sprintf(result->data(), "\\u{%x}", codepoint);
61
10
  result->SetObjLenFromStrLen(n);  // truncate to what we wrote
62
10
  return result;
63
10
}
64
65
}  // namespace qsn
66
67
#endif  // QSN_H