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

biojppm / rapidyaml / 18073302777

28 Sep 2025 10:57AM UTC coverage: 97.448% (+0.1%) from 97.312%
18073302777

Pull #536

github

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

976 of 1001 new or added lines in 16 files covered. (97.5%)

2 existing lines in 1 file now uncovered.

13021 of 13362 relevant lines covered (97.45%)

765405.06 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
    #ifndef RYML_SINGLE_HEADER_INTS
10
        #include <ryml_all.hpp>
11
    #else
12
        #include <ryml_ints.hpp>
13
    #endif
14
#elif defined(RYML_SINGLE_HEADER_LIB) // using the single header from a library
15
    #ifndef RYML_SINGLE_HEADER_INTS
16
        #include <ryml_all.hpp>
17
    #else
18
        #include <ryml_ints.hpp>
19
    #endif
20
#else
21
#include <c4/yml/parse_engine.def.hpp>
22
#endif
23

24
#ifndef RYML_SINGLE_HEADER_INTS
25
#include <c4/yml/extra/event_handler_ints.hpp>
26
#endif
27

28

29
// NOLINTBEGIN(hicpp-signed-bitwise)
30

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

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

82
    // buffer to where we will write the events
83
    constexpr const int events_size = 100;
8✔
84
    int events[events_size] = {};
8✔
85
    static_assert(events_size >= sizeof(expected_events)/sizeof(expected_events[0]), "buffer too small");
86
    // buffer for placing any scalars/tags that cannot be filtered
87
    // in-place
88
    char arena[100] = {};
8✔
89

90

91
    // ensure the estimation will succeed vs required size
92
    int estimated_size = c4::yml::extra::estimate_events_ints_size(yaml);
8✔
93
    if (estimated_size > events_size)
8✔
94
    {
NEW
95
        printf("the estimated size (%d) will not fit the events array (%d)\n",
×
96
               estimated_size, events_size);
NEW
97
        return 1;
×
98
    }
99

100
    // parse now. the parse should succeed (because the YAML above is
101
    // legit), but if there were would be a parse error, we would get
102
    // the default behavior which is abort on error, since we did not
103
    // set up the error callbacks
104
    c4::yml::extra::EventHandlerInts handler;
8✔
105
    c4::yml::ParseEngine<c4::yml::extra::EventHandlerInts> parser(&handler);
8✔
106
    handler.reset(yaml, arena, events, estimated_size); // note we pass the estimated size!
8✔
107
    parser.parse_in_place_ev("filename", yaml);
8✔
108

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

126
    // done!
127
    printf("success! YAML requires event size %d, estimated=%d (required_arena=%zu actual=%zu)\n",
8✔
128
           handler.required_size_events(), estimated_size,
129
           handler.required_size_arena(), c4::to_csubstr(arena).len);
130

131
    // ensure the result is as expected
132
    bool compare = true;
8✔
133

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

161
    return compare ? 0 : 1;
8✔
162
}
8✔
163

164
// 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