rapidyaml  0.8.0
parse and emit YAML, and do it fast
test_suite_event_handler.cpp
Go to the documentation of this file.
1 #ifndef RYML_SINGLE_HEADER
2 #include <c4/yml/node.hpp>
3 #include <c4/yml/std/string.hpp>
5 #endif
7 
8 
9 namespace c4 {
10 namespace yml {
11 
12 // instantiate the template
13 template class ParseEngine<EventHandlerYamlStd>;
14 
15 void append_escaped(extra::string *es, csubstr val)
16 {
17  #define _c4flush_use_instead(i, repl, skip) \
18  do { \
19  es->append(val.range(prev, i)); \
20  es->append(repl); \
21  prev = i + skip; \
22  } \
23  while(0)
24  uint8_t const* C4_RESTRICT s = reinterpret_cast<uint8_t const*>(val.str);
25  size_t prev = 0;
26  for(size_t i = 0; i < val.len; ++i)
27  {
28  switch(s[i])
29  {
30  case UINT8_C(0x0a): // \n
31  _c4flush_use_instead(i, "\\n", 1); break;
32  case UINT8_C(0x5c): // '\\'
33  _c4flush_use_instead(i, "\\\\", 1); break;
34  case UINT8_C(0x09): // \t
35  _c4flush_use_instead(i, "\\t", 1); break;
36  case UINT8_C(0x0d): // \r
37  _c4flush_use_instead(i, "\\r", 1); break;
38  case UINT8_C(0x00): // \0
39  _c4flush_use_instead(i, "\\0", 1); break;
40  case UINT8_C(0x0c): // \f (form feed)
41  _c4flush_use_instead(i, "\\f", 1); break;
42  case UINT8_C(0x08): // \b (backspace)
43  _c4flush_use_instead(i, "\\b", 1); break;
44  case UINT8_C(0x07): // \a (bell)
45  _c4flush_use_instead(i, "\\a", 1); break;
46  case UINT8_C(0x0b): // \v (vertical tab)
47  _c4flush_use_instead(i, "\\v", 1); break;
48  case UINT8_C(0x1b): // \e (escape)
49  _c4flush_use_instead(i, "\\e", 1); break;
50  case UINT8_C(0xc2):
51  if(i+1 < val.len)
52  {
53  const uint8_t np1 = s[i+1];
54  if(np1 == UINT8_C(0xa0))
55  _c4flush_use_instead(i, "\\_", 2);
56  else if(np1 == UINT8_C(0x85))
57  _c4flush_use_instead(i, "\\N", 2);
58  }
59  break;
60  case UINT8_C(0xe2):
61  if(i+2 < val.len)
62  {
63  if(s[i+1] == UINT8_C(0x80))
64  {
65  if(s[i+2] == UINT8_C(0xa8))
66  _c4flush_use_instead(i, "\\L", 3);
67  else if(s[i+2] == UINT8_C(0xa9))
68  _c4flush_use_instead(i, "\\P", 3);
69  }
70  }
71  break;
72  }
73  }
74  // flush the rest
75  es->append(val.sub(prev));
76  #undef _c4flush_use_instead
77 }
78 
79 } // namespace yml
80 } // namespace c4
void append_escaped(extra::string *s, csubstr val)
Definition: common.cpp:12
Node classes.
substring conversions for/from std::string
#define _c4flush_use_instead(i, repl, skip)