Skip to content

Commit b677500

Browse files
committed
Add helper to dbgprint maybe null strings
1 parent fa8ee9d commit b677500

3 files changed

Lines changed: 27 additions & 8 deletions

File tree

src/c4/yml/detail/dbgprint.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,24 @@ inline C4_NO_INLINE void __c4presc(const char *s, size_t len, bool keep_newlines
118118
__c4presc(csubstr(s, len), keep_newlines);
119119
}
120120

121+
struct _maybe_null_str
122+
{
123+
csubstr s;
124+
_maybe_null_str(csubstr s_) noexcept : s(s_) {}
125+
};
126+
// LCOV_EXCL_START
127+
inline C4_NO_INLINE size_t to_chars(substr buf, _maybe_null_str const& v)
128+
{
129+
return c4::format(buf, v.s.str ? v.s : csubstr("(out of size)"));
130+
}
131+
// LCOV_EXCL_STOP
132+
template<class SinkPfn>
133+
C4_NO_INLINE size_t dump(SinkPfn &&sinkfn, substr /*buf*/, _maybe_null_str const& v)
134+
{
135+
std::forward<SinkPfn>(sinkfn)(v.s.str ? v.s : csubstr("(out of size)"));
136+
return 0; // no space needed in the buffer
137+
}
138+
121139
/** print string as [{s.len}]~~~{s}~~~ */
122140
struct _prs
123141
{
@@ -205,6 +223,7 @@ C4_NO_INLINE size_t dump(SinkPfn &&sinkfn, substr buf, _prs const& v)
205223
}
206224
return sz; // we require this space in the buffer
207225
}
226+
208227
} // namespace yml
209228
} // namespace c4
210229

src/c4/yml/parse_engine.def.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4327,7 +4327,7 @@ void ParseEngine<EventHandler>::_add_annotation(Annotation *C4_RESTRICT dst, csu
43274327
template<class EventHandler>
43284328
void ParseEngine<EventHandler>::_add_annotation(Annotation *C4_RESTRICT dst, csubstr str, size_t indentation, size_t line)
43294329
{
4330-
_c4dbgpf("store annotation[{}]: '{}' indentation={} line={}", dst->num_entries, str.str ? str : csubstr("(arena full)"), indentation, line);
4330+
_c4dbgpf("store annotation[{}]: '{}' indentation={} line={}", dst->num_entries, _maybe_null_str(str), indentation, line);
43314331
_RYML_ASSERT_PARSE_(m_evt_handler->m_stack.m_callbacks, dst->num_entries < C4_COUNTOF(dst->annotations), m_evt_handler->m_curr->pos); // NOLINT(bugprone-sizeof-expression)
43324332
if(C4_UNLIKELY(dst->num_entries && dst->annotations[0].line == line))
43334333
{
@@ -4343,7 +4343,7 @@ void ParseEngine<EventHandler>::_add_annotation(Annotation *C4_RESTRICT dst, csu
43434343
template<class EventHandler>
43444344
void ParseEngine<EventHandler>::_add_annotation(Annotation *C4_RESTRICT dst, csubstr str, size_t indentation, size_t line, csubstr orig)
43454345
{
4346-
_c4dbgpf("store annotation[{}]: '{}'->'{}' indentation={} line={}", dst->num_entries, orig, str.str ? str : csubstr("(arena full)"), indentation, line);
4346+
_c4dbgpf("store annotation[{}]: '{}'->'{}' indentation={} line={}", dst->num_entries, orig, _maybe_null_str(str), indentation, line);
43474347
_RYML_ASSERT_PARSE_(m_evt_handler->m_stack.m_callbacks, dst->num_entries < C4_COUNTOF(dst->annotations), m_evt_handler->m_curr->pos); // NOLINT(bugprone-sizeof-expression)
43484348
if(C4_UNLIKELY(dst->num_entries && dst->annotations[0].line == line))
43494349
{
@@ -4677,7 +4677,7 @@ csubstr ParseEngine<EventHandler>::_resolve_tag(csubstr tag)
46774677
_c4assert(!ttag.str || ttag.is_sub(m_evt_handler->arena()));
46784678
}
46794679
}
4680-
_c4dbgpf("resolved tag: {} --> [{}]~~~{}~~~", _prs(tag), ttag.len, ttag.str ? ttag : csubstr("(out of size)"));
4680+
_c4dbgpf("resolved tag: {} --> [{}]~~~{}~~~", _prs(tag), ttag.len, _maybe_null_str(ttag));
46814681
_c4assert(ttag.len > 0);
46824682
// cache the hard-earned result!
46834683
m_evt_handler->tag_cache().add(tag, ttag, m_evt_handler->m_curr_doc, ret.pos);
@@ -8261,7 +8261,7 @@ uint32_t ParseEngine<EventHandler>::_get_annotations_same_line(csubstr token_sou
82618261
{
82628262
_c4dbgpf("there are {} pending annotations: {} anchors + {} tags", total, m_pending_anchors.num_entries, m_pending_tags.num_entries);
82638263
auto valid_if_same_line = [this](EntryPtr entry){
8264-
_c4dbgpf("pending: {} indent={} line={} vs currline={}", entry->str, entry->indentation, entry->line, m_evt_handler->m_curr->pos.line);
8264+
_c4dbgpf("pending: {} indent={} line={} vs currline={}", _maybe_null_str(entry->str), entry->indentation, entry->line, m_evt_handler->m_curr->pos.line);
82658265
return (entry->line == m_evt_handler->m_curr->pos.line) ? entry : nullptr;
82668266
};
82678267
// now select annotations only on the same line
@@ -8290,14 +8290,14 @@ uint32_t ParseEngine<EventHandler>::_get_annotations_same_line(csubstr token_sou
82908290
// assign to first
82918291
first = get_first_on_same_line(nullptr);
82928292
_c4assert(first);
8293-
_c4dbgpf("first annotation: {} indent={} line={}", first->str, first->indentation, first->line);
8293+
_c4dbgpf("first annotation: {} indent={} line={}", _maybe_null_str(first->str), first->indentation, first->line);
82948294
if(total > 1)
82958295
{
82968296
_c4assert(total == 2);
82978297
// assign to second
82988298
second = get_first_on_same_line(first);
82998299
_c4assert(second);
8300-
_c4dbgpf("second annotation: {} indent={} line={}", second->str, second->indentation, second->line);
8300+
_c4dbgpf("second annotation: {} indent={} line={}", _maybe_null_str(second->str), second->indentation, second->line);
83018301
}
83028302
auto extract_string = [&](EntryPtr e){
83038303
// tags can be null when the arena ran out of space
@@ -8307,7 +8307,7 @@ uint32_t ParseEngine<EventHandler>::_get_annotations_same_line(csubstr token_sou
83078307
_c4assert(tag.str);
83088308
_c4assert(tag.len);
83098309
_c4assert(tag.is_sub(token_soup));
8310-
_c4dbgpf("tag: {} -> {}", e->str.str ? e->str : csubstr("(out of size)"), tag);
8310+
_c4dbgpf("tag: {} -> {}", _maybe_null_str(e->str), tag);
83118311
return tag;
83128312
}
83138313
csubstr anchor = e->str;

src/c4/yml/tag.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ void TagCache::add(csubstr tag, csubstr resolved, id_type doc_id, const_iterator
603603
ptr[pos].tag = tag;
604604
ptr[pos].resolved = resolved;
605605
ptr[pos].doc_id = doc_id;
606-
_c4dbgpf("tagcache: add entry @pos={}: docid={} {}->{}", pos, doc_id, tag, (resolved.str ? resolved : csubstr("(out of size)")));
606+
_c4dbgpf("tagcache: add entry @pos={}: docid={} {}->{}", pos, doc_id, tag, _maybe_null_str(resolved));
607607
}
608608

609609
} // namespace yml

0 commit comments

Comments
 (0)