rapidyaml  0.11.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 36 of file preprocess.cpp.

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

◆ 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().