rapidyaml  0.9.0
parse and emit YAML, and do it fast
preprocess_rxmap

Convert flow-type relaxed maps (with implicit bools) into strict YAML flow map: More...

Functions

size_t c4::yml::preprocess_rxmap (csubstr rxmap, substr buf)
 Write into a given output buffer. More...
 
template<class CharContainer >
substr c4::yml::preprocess_rxmap (csubstr rxmap, CharContainer *out)
 
template<class CharContainer >
CharContainer c4::yml::preprocess_rxmap (csubstr rxmap)
 

Detailed Description

Convert flow-type relaxed maps (with implicit bools) into strict YAML flow map:

{a, b, c, d: [e, f], g: {a, b}}
# is converted into this:
{a: 1, b: 1, c: 1, d: [e, f], g: {a, b}}
Note
this is NOT recursive - conversion happens only in the top-level map
Parameters
rxmapA relaxed map
bufoutput buffer
outoutput container

Function Documentation

◆ preprocess_rxmap() [1/3]

size_t c4::yml::preprocess_rxmap ( csubstr  rxmap,
substr  buf 
)

Write into a given output buffer.

This function is safe to call with empty or small buffers; it won't write beyond the end of the buffer.

Returns
the number of characters required for output

Definition at line 35 of file preprocess.cpp.

36 {
37  detail::_SubstrWriter writer(buf);
38  _ppstate state = kReadPending;
39  size_t last = 0;
40 
41  if(s.begins_with('{'))
42  {
43  RYML_CHECK(s.ends_with('}'));
44  s = s.offs(1, 1);
45  }
46 
47  writer.append('{');
48 
49  for(size_t i = 0; i < s.len; ++i)
50  {
51  const char curr = s[i];
52  const char next = i+1 < s.len ? s[i+1] : '\0';
53 
54  if(curr == '\'' || curr == '"')
55  {
56  csubstr ss = s.sub(i).pair_range_esc(curr, '\\');
57  i += static_cast<size_t>(ss.end() - (s.str + i));
58  state = _next(state);
59  }
60  else if(state == kReadPending && _is_idchar(curr))
61  {
62  state = _next(state);
63  }
64 
65  switch(state)
66  {
67  case kKeyPending:
68  {
69  if(curr == ':' && next == ' ')
70  {
71  state = _next(state);
72  }
73  else if(curr == ',' && next == ' ')
74  {
75  writer.append(s.range(last, i));
76  writer.append(": 1, ");
77  last = i + 2;
78  }
79  break;
80  }
81  case kValPending:
82  {
83  if(curr == '[' || curr == '{' || curr == '(')
84  {
85  csubstr ss = s.sub(i).pair_range_nested(curr, '\\');
86  i += static_cast<size_t>(ss.end() - (s.str + i));
87  state = _next(state);
88  }
89  else if(curr == ',' && next == ' ')
90  {
91  state = _next(state);
92  }
93  break;
94  }
95  default:
96  // nothing to do
97  break;
98  }
99  }
100 
101  writer.append(s.sub(last));
102  if(state == kKeyPending)
103  writer.append(": 1");
104  writer.append('}');
105 
106  return writer.pos;
107 }

Referenced by c4::yml::preprocess_rxmap().

◆ preprocess_rxmap() [2/3]

template<class CharContainer >
substr c4::yml::preprocess_rxmap ( csubstr  rxmap,
CharContainer *  out 
)

Definition at line 75 of file preprocess.hpp.

76 {
77  return detail::preprocess_into_container<preprocess_rxmap>(rxmap, out);
78 }

◆ preprocess_rxmap() [3/3]

template<class CharContainer >
CharContainer c4::yml::preprocess_rxmap ( csubstr  rxmap)

Definition at line 84 of file preprocess.hpp.

85 {
86  CharContainer out;
87  preprocess_rxmap(rxmap, &out);
88  return out;
89 }
CharContainer preprocess_rxmap(csubstr rxmap)
Definition: preprocess.hpp:84

References c4::yml::preprocess_rxmap().