rapidyaml  0.13.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 38 of file preprocess.cpp.

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

◆ 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