Skip to content

Commit 2dbb452

Browse files
committed
int events: do not use c4/bitmask.hpp to convert event to_chars()
1 parent e21755b commit 2dbb452

5 files changed

Lines changed: 62 additions & 69 deletions

File tree

changelog/current.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,4 @@
136136
-
137137
```
138138
- [PR#628](https://github.com/biojppm/rapidyaml/pull/628): Add serialization fuzzing. Relax `c4::atof()` / `c4::atod()`, disable redundant assertions that prevent returning false on bad strings.
139+
- [PR#629](https://github.com/biojppm/rapidyaml/pull/629): int events: do not use c4/bitmask.hpp in `to_chars(substr, ievt::DataType)`.

src_extra/c4/yml/extra/ints_to_testsuite.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616
#include "c4/yml/extra/ints_utils.hpp"
1717
#endif
1818

19-
#ifndef _C4_BITMASK_HPP_
20-
#include "c4/bitmask.hpp"
21-
#endif
22-
2319

2420
C4_SUPPRESS_WARNING_GCC_WITH_PUSH("-Wold-style-cast")
2521
C4_SUPPRESS_WARNING_CLANG_WITH_PUSH("-Wold-style-cast")

src_extra/c4/yml/extra/ints_utils.cpp

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,71 +12,69 @@
1212
#include "c4/yml/extra/ints_utils.hpp"
1313
#endif
1414

15-
#ifndef _C4_YML_ERROR_HPP_
16-
#include "c4/yml/error.hpp"
17-
#endif
18-
19-
#ifndef _C4_BITMASK_HPP_
20-
#include "c4/bitmask.hpp"
21-
#endif
22-
2315

2416
C4_SUPPRESS_WARNING_GCC_WITH_PUSH("-Wold-style-cast")
2517
C4_SUPPRESS_WARNING_CLANG_WITH_PUSH("-Wold-style-cast")
2618
// NOLINTBEGIN(hicpp-signed-bitwise,*avoid-c-style-cast)
2719

2820

2921
namespace c4 {
30-
template<>
31-
c4::EnumSymbols<yml::extra::ievt::EventFlags> esyms<yml::extra::ievt::EventFlags>()
32-
{
33-
static constexpr const EnumSymbols<yml::extra::ievt::EventFlags>::Sym syms[] = {
34-
{yml::extra::ievt::KEY_, "KEY_"},
35-
{yml::extra::ievt::VAL_, "VAL_"},
36-
{yml::extra::ievt::SCLR, "SCLR"},
37-
{yml::extra::ievt::BSEQ, "BSEQ"},
38-
{yml::extra::ievt::ESEQ, "ESEQ"},
39-
{yml::extra::ievt::BMAP, "BMAP"},
40-
{yml::extra::ievt::EMAP, "EMAP"},
41-
{yml::extra::ievt::ALIA, "ALIA"},
42-
{yml::extra::ievt::ANCH, "ANCH"},
43-
{yml::extra::ievt::TAG_, "TAG_"},
44-
{yml::extra::ievt::PLAI, "PLAI"},
45-
{yml::extra::ievt::SQUO, "SQUO"},
46-
{yml::extra::ievt::DQUO, "DQUO"},
47-
{yml::extra::ievt::LITL, "LITL"},
48-
{yml::extra::ievt::FOLD, "FOLD"},
49-
{yml::extra::ievt::FLOW, "FLOW"},
50-
{yml::extra::ievt::BLCK, "BLCK"},
51-
{yml::extra::ievt::BDOC, "BDOC"},
52-
{yml::extra::ievt::EDOC, "EDOC"},
53-
{yml::extra::ievt::BSTR, "BSTR"},
54-
{yml::extra::ievt::ESTR, "ESTR"},
55-
{yml::extra::ievt::EXPL, "EXPL"},
56-
{yml::extra::ievt::AREN, "AREN"},
57-
{yml::extra::ievt::PSTR, "PSTR"},
58-
{yml::extra::ievt::UNFILT, "UNFILT"},
59-
{yml::extra::ievt::YAML, "YAML"},
60-
{yml::extra::ievt::TAGH, "TAGH"},
61-
{yml::extra::ievt::TAGP, "TAGP"},
62-
};
63-
return EnumSymbols<yml::extra::ievt::EventFlags>(syms);
64-
}
6522
namespace yml {
6623
namespace extra {
6724
namespace ievt {
68-
size_t to_chars(substr buf, ievt::DataType flags)
69-
{
70-
flags &= ievt::MASK; // clear any other bits
71-
return c4::bm2str<ievt::EventFlags>(flags, buf.str, buf.len);
72-
}
73-
csubstr to_chars_sub(substr buf, ievt::DataType flags)
25+
26+
namespace {
27+
struct FlagSym { const char *str; EventFlags flags; };
28+
const FlagSym flag_syms_[] = {
29+
{"KEY_", KEY_},
30+
{"VAL_", VAL_},
31+
{"SCLR", SCLR},
32+
{"BSEQ", BSEQ},
33+
{"ESEQ", ESEQ},
34+
{"BMAP", BMAP},
35+
{"EMAP", EMAP},
36+
{"ALIA", ALIA},
37+
{"ANCH", ANCH},
38+
{"TAG_", TAG_},
39+
{"PLAI", PLAI},
40+
{"SQUO", SQUO},
41+
{"DQUO", DQUO},
42+
{"LITL", LITL},
43+
{"FOLD", FOLD},
44+
{"FLOW", FLOW},
45+
{"BLCK", BLCK},
46+
{"BDOC", BDOC},
47+
{"EDOC", EDOC},
48+
{"BSTR", BSTR},
49+
{"ESTR", ESTR},
50+
{"EXPL", EXPL},
51+
{"AREN", AREN},
52+
{"PSTR", PSTR},
53+
{"UNFILT", UNFILT},
54+
{"YAML", YAML},
55+
{"TAGH", TAGH},
56+
{"TAGP", TAGP},
57+
};
58+
} // namespace
59+
60+
size_t to_chars(substr buf, ievt::DataType flags) noexcept
7461
{
75-
size_t reqsize = ievt::to_chars(buf, flags);
76-
_RYML_CHECK_BASIC(reqsize > 0u);
77-
_RYML_CHECK_BASIC(reqsize < buf.len);
78-
return buf.first(reqsize - 1u);
62+
detail::_SubstrWriter writer(buf);
63+
for(const FlagSym sym : flag_syms_)
64+
{
65+
if(flags & sym.flags)
66+
{
67+
if(writer.pos)
68+
writer.append('|');
69+
writer.append(sym.str);
70+
flags &= ~sym.flags;
71+
}
72+
}
73+
if(!writer.pos)
74+
writer.append("NONE");
75+
return writer.pos;
7976
}
77+
8078
} // namespace ievt
8179
} // namespace extra
8280
} // namespace yml
@@ -100,7 +98,7 @@ void events_ints_print(csubstr parsed_yaml, csubstr arena, ievt::DataType const*
10098
evtpos += ((evts[evtpos] & ievt::WSTR) ? 3 : 1))
10199
{
102100
ievt::DataType evt = evts[evtpos];
103-
csubstr flags = ievt::to_chars_sub(buf, evt);
101+
csubstr flags = to_chars_sub(buf, evt);
104102
printf("[%d][%d] %.*s(0x%x)", evtnumber, evtpos, (int)flags.len, flags.str, evt);
105103
if (evt & ievt::WSTR)
106104
{

src_extra/c4/yml/extra/ints_utils.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ namespace extra {
1919

2020
namespace ievt {
2121
/** Convert bit mask of @ref ievt::EventFlags to text. */
22-
RYML_EXPORT size_t to_chars(substr buf, yml::extra::ievt::DataType flags);
23-
/** Convert bit mask of @ref ievt::EventFlags to text. */
24-
RYML_EXPORT csubstr to_chars_sub(substr buf, yml::extra::ievt::DataType flags);
22+
RYML_EXPORT size_t to_chars(substr buf, yml::extra::ievt::DataType flags) noexcept;
2523
} // namespace ievt
2624

2725

test/test_lib/test_events_ints_helpers.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ void test_events_ints(IntEventWithScalar const* expected, size_t expected_sz,
2828
EXPECT_EQ(actual_sz, num_ints_expected);
2929
status = (actual_sz == num_ints_expected);
3030

31-
char actualbuf[100];(void)actualbuf;
32-
char expectedbuf[100];(void)expectedbuf;
31+
char actualbuf[200];(void)actualbuf;
32+
char expectedbuf[200];(void)expectedbuf;
3333
for(size_t ia = 0, ie = 0; ie < expected_sz; ++ie)
3434
{
3535
EXPECT_LT(ia, actual_sz);
@@ -43,8 +43,8 @@ void test_events_ints(IntEventWithScalar const* expected, size_t expected_sz,
4343
status &= int(lhs == rhs); \
4444
EXPECT_EQ(lhs, rhs); \
4545
} while(0)
46-
csubstr sactual = ievt::to_chars_sub(actualbuf, actual[ia]);
47-
csubstr sexpect = ievt::to_chars_sub(expectedbuf, expected[ie].flags);
46+
csubstr sactual = to_chars_sub(actualbuf, actual[ia]);
47+
csubstr sexpect = to_chars_sub(expectedbuf, expected[ie].flags);
4848
_test_eq(actual[ia], expected[ie].flags, "", 0);
4949
_test_eq(sactual, sexpect, "", 0);
5050
if((expected[ie].flags & ievt::WSTR) && (actual[ia] & ievt::WSTR))
@@ -103,8 +103,8 @@ void test_events_ints_invariants(csubstr parsed_yaml,
103103
ievt::DataType const* evts,
104104
ievt::DataType evts_sz)
105105
{
106-
char bufpos[100];
107-
char bufprev[100];
106+
char bufpos[200];
107+
char bufprev[200];
108108
EXPECT_GT(evts_sz, 0);
109109
for(ievt::DataType evtpos = 0, evtnumber = 0;
110110
evtpos < evts_sz;
@@ -117,7 +117,7 @@ void test_events_ints_invariants(csubstr parsed_yaml,
117117
ievt::DataType next = {};
118118
SCOPED_TRACE(evtpos); // position in the array
119119
SCOPED_TRACE(evtnumber); // event number
120-
SCOPED_TRACE(ievt::to_chars_sub(bufpos, evt));
120+
SCOPED_TRACE(to_chars_sub(bufpos, evt));
121121
if(evtpos)
122122
prev = (evt & ievt::PSTR) ? evts[evtpos - 3] : evts[evtpos - 1];
123123
if(nextpos < evts_sz)
@@ -146,7 +146,7 @@ void test_events_ints_invariants(csubstr parsed_yaml,
146146
{
147147
EXPECT_GT(evtnumber, 0);
148148
EXPECT_GE(evtpos, 3);
149-
SCOPED_TRACE(ievt::to_chars_sub(bufprev, prev));
149+
SCOPED_TRACE(to_chars_sub(bufprev, prev));
150150
EXPECT_NE(prev & ievt::WSTR, 0);
151151
}
152152
constexpr const ievt::DataType style = ievt::PLAI|ievt::SQUO|ievt::DQUO|ievt::LITL|ievt::FOLD;

0 commit comments

Comments
 (0)