Path: blob/master/dep/rapidyaml/src/c4/yml/preprocess.cpp
4262 views
#include "c4/yml/preprocess.hpp"1#include "c4/yml/detail/parser_dbg.hpp"23/** @file preprocess.hpp Functions for preprocessing YAML prior to parsing. */45namespace c4 {6namespace yml {78C4_SUPPRESS_WARNING_GCC_CLANG_WITH_PUSH("-Wold-style-cast")910//-----------------------------------------------------------------------------11//-----------------------------------------------------------------------------12//-----------------------------------------------------------------------------1314namespace {15C4_ALWAYS_INLINE bool _is_idchar(char c)16{17return (c >= 'a' && c <= 'z')18|| (c >= 'A' && c <= 'Z')19|| (c >= '0' && c <= '9')20|| (c == '_' || c == '-' || c == '~' || c == '$');21}2223typedef enum { kReadPending = 0, kKeyPending = 1, kValPending = 2 } _ppstate;24C4_ALWAYS_INLINE _ppstate _next(_ppstate s)25{26int n = (int)s + 1;27return (_ppstate)(n <= (int)kValPending ? n : 0);28}29} // empty namespace303132//-----------------------------------------------------------------------------3334size_t preprocess_rxmap(csubstr s, substr buf)35{36detail::_SubstrWriter writer(buf);37_ppstate state = kReadPending;38size_t last = 0;3940if(s.begins_with('{'))41{42RYML_CHECK(s.ends_with('}'));43s = s.offs(1, 1);44}4546writer.append('{');4748for(size_t i = 0; i < s.len; ++i)49{50const char curr = s[i];51const char next = i+1 < s.len ? s[i+1] : '\0';5253if(curr == '\'' || curr == '"')54{55csubstr ss = s.sub(i).pair_range_esc(curr, '\\');56i += static_cast<size_t>(ss.end() - (s.str + i));57state = _next(state);58}59else if(state == kReadPending && _is_idchar(curr))60{61state = _next(state);62}6364switch(state)65{66case kKeyPending:67{68if(curr == ':' && next == ' ')69{70state = _next(state);71}72else if(curr == ',' && next == ' ')73{74writer.append(s.range(last, i));75writer.append(": 1, ");76last = i + 2;77}78break;79}80case kValPending:81{82if(curr == '[' || curr == '{' || curr == '(')83{84csubstr ss = s.sub(i).pair_range_nested(curr, '\\');85i += static_cast<size_t>(ss.end() - (s.str + i));86state = _next(state);87}88else if(curr == ',' && next == ' ')89{90state = _next(state);91}92break;93}94default:95// nothing to do96break;97}98}99100writer.append(s.sub(last));101if(state == kKeyPending)102writer.append(": 1");103writer.append('}');104105return writer.pos;106}107108C4_SUPPRESS_WARNING_GCC_CLANG_POP109110} // namespace yml111} // namespace c4112113114