• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

biojppm / rapidyaml / 18058224053

27 Sep 2025 09:47AM UTC coverage: 97.35% (+0.04%) from 97.312%
18058224053

Pull #536

github

web-flow
Merge f0cd8f7b3 into 9a044c88b
Pull Request #536: Int handler

959 of 990 new or added lines in 15 files covered. (96.87%)

7 existing lines in 2 files now uncovered.

13007 of 13361 relevant lines covered (97.35%)

765169.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.5
/samples/quickstart-ints.cpp
1
// This file shows a quick example of parsing YAML to an int events
2
// buffer. Since this functionality is meant to implement in other
3
// programming languages, the code is kept very simple, and using only
4
// C-like idioms.
5

6
// ryml can be used as a single header, or as a simple library:
7
#if defined(RYML_SINGLE_HEADER) // using the single header directly in the executable
8
    #define RYML_SINGLE_HDR_DEFINE_NOW
9
    #include <ryml_all.hpp>
10
#elif defined(RYML_SINGLE_HEADER_LIB) // using the single header from a library
11
    #include <ryml_all.hpp>
12
#else
13
#include <c4/yml/parse_engine.def.hpp>
14
#endif
15
#include <c4/yml/extra/event_handler_ints.hpp>
16

17

18
// NOLINTBEGIN(hicpp-signed-bitwise)
19

20
int main(int, const char *[])
8✔
21
{
22
    using namespace c4::yml::extra::ievt;
23
    auto PSTR_ = c4::yml::extra::ievt::PSTR; // PSTR does not work in windows
8✔
24
    // YAML code to be parsed in place
25
    char yaml[] = "do: a deer, a female deer\n"
8✔
26
                  "re: a drop of golden sun\n"
27
                  "mi: a name I call myself\n"
28
                  "fa: a long long way to run\n";
29
    // these are the event values we expect
30
    const int expected_events[] = {
8✔
31
        BSTR,
32
        BDOC,
33
        VAL_|BMAP|BLCK,
34
        //
35
        KEY_|SCLR|PLAI,        0,  2,  // "do"
36
        VAL_|SCLR|PLAI|PSTR_,  4, 21,  // "a deer, a female deer"
8✔
37
        //
38
        KEY_|SCLR|PLAI|PSTR_, 26,  2,  // "re"
8✔
39
        VAL_|SCLR|PLAI|PSTR_, 30, 20,  // "a drop of golden sun"
8✔
40
        //
41
        KEY_|SCLR|PLAI|PSTR_, 51,  2,  // "mi"
8✔
42
        VAL_|SCLR|PLAI|PSTR_, 55, 20,  // "a name I call myself"
8✔
43
        //
44
        KEY_|SCLR|PLAI|PSTR_, 76,  2,  // "fa"
8✔
45
        VAL_|SCLR|PLAI|PSTR_, 80, 22,  // "a long long way to run"
8✔
46
        //
47
        EMAP|PSTR_,
8✔
48
        EDOC,
49
        ESTR,
50
    };
8✔
51

52
    /* the output should be this:
53
     *
54
     * success! YAML requires event size 30, estimated=49
55
     * pos=0        event[0]:        0x1
56
     * pos=1        event[1]:        0x4
57
     * pos=2        event[2]:        0x110010
58
     * pos=3        event[3]:        0x80500         str=(0,2)        'do'
59
     * pos=6        event[4]:        0x900500        str=(4,21)        'a deer, a female deer'
60
     * pos=9        event[5]:        0x880500        str=(26,2)        're'
61
     * pos=12        event[6]:        0x900500        str=(30,20)        'a drop of golden sun'
62
     * pos=15        event[7]:        0x880500        str=(51,2)        'mi'
63
     * pos=18        event[8]:        0x900500        str=(55,20)        'a name I call myself'
64
     * pos=21        event[9]:        0x880500        str=(76,2)        'fa'
65
     * pos=24        event[10]:        0x900500        str=(80,22)        'a long long way to run'
66
     * pos=27        event[11]:        0x800020
67
     * pos=28        event[12]:        0x8
68
     * pos=29        event[13]:        0x2
69
     */
70

71
    // buffer to where we will write the events
72
    constexpr const int events_size = 100;
8✔
73
    int events[events_size] = {};
8✔
74
    static_assert(events_size >= sizeof(expected_events)/sizeof(expected_events[0]), "buffer too small");
75
    // buffer for placing any scalars/tags that cannot be filtered
76
    // in-place
77
    char arena[100] = {};
8✔
78

79

80
    // ensure the estimation will succeed vs required size
81
    int estimated_size = c4::yml::extra::estimate_events_ints_size(yaml);
8✔
82
    if (estimated_size > events_size)
8✔
83
    {
NEW
84
        printf("the estimated size (%d) will not fit the events array (%d)\n",
×
85
               estimated_size, events_size);
NEW
86
        return 1;
×
87
    }
88

89
    // parse now. the parse should succeed (because the YAML above is
90
    // legit), but if there were would be a parse error, we would get
91
    // the default behavior which is abort on error, since we did not
92
    // set up the error callbacks
93
    c4::yml::extra::EventHandlerInts handler;
8✔
94
    c4::yml::ParseEngine<c4::yml::extra::EventHandlerInts> parser(&handler);
8✔
95
    handler.reset(yaml, arena, events, estimated_size); // note we pass the estimated size!
8✔
96
    parser.parse_in_place_ev("filename", yaml);
8✔
97

98
    // the YAML was successfully parsed, but it may happen that it
99
    // requires more events than may fit in the buffer. so we need to
100
    // check that it actually fits (this is mandatory):
101
    if(!handler.fits_buffers())
8✔
102
    {
NEW
103
        printf("error: buffers too small:"
×
104
               "   required_evt=%d actual_evt=%d\n"
105
               "   required_arena=%zu actual_arena=%zu\n",
106
               handler.required_size_events(), estimated_size,
107
               handler.required_size_arena(), c4::to_csubstr(arena).len);
108
        // WATCHOUT: if you want to retry the parse, you need to set
109
        // up the source buffer again, because it is invalidated from
110
        // being parsed in place. refer to the doxygen documentation
111
        // for more details.
NEW
112
        return 1;
×
113
    }
114

115
    // done!
116
    printf("success! YAML requires event size %d, estimated=%d (required_arena=%zu actual=%zu)\n",
8✔
117
           handler.required_size_events(), estimated_size,
118
           handler.required_size_arena(), c4::to_csubstr(arena).len);
119

120
    // ensure the result is as expected
121
    bool compare = true;
8✔
122

123
    // example iterating through the events array: compare and print
124
    // the result
125
    for (int pos = 0, evt = 0; pos < handler.required_size_events(); ++pos, ++evt)
120✔
126
    {
127
        bool status = (events[pos] == expected_events[pos]);
112✔
128
        printf("pos=%d\tevent[%d]:\t0x%x", pos, evt, events[pos]);
112✔
129
        if(events[pos] & WSTR) // the event has a string following it
112✔
130
        {
131
            int offset = events[pos + 1];
64✔
132
            int length = events[pos + 2];
64✔
133
            bool in_arena = (events[pos] & AREN);
64✔
134
            // WATCHOUT! the string is NOT ZERO TERMINATED!
135
            const char *ptr = in_arena ? arena : yaml;
64✔
136
            const char *str = ptr + offset;
64✔
137
            printf("\tstr=(%d,%d)\t'%.*s'", offset, length, length, str);
64✔
138
            status = status && (offset == expected_events[pos + 1]);
64✔
139
            status = status && (length == expected_events[pos + 2]);
64✔
140
            pos += 2; // advance the two ints from the string
64✔
141
        }
142
        if(!status)
112✔
143
        {
NEW
144
            printf("  ... fail!");
×
NEW
145
            compare = false;
×
146
        }
147
        printf("\n");
112✔
148
    }
149

150
    return compare ? 0 : 1;
8✔
151
}
8✔
152

153
// NOLINTEND(hicpp-signed-bitwise)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc