Skip to content

Commit 707bc2d

Browse files
committed
wip dbg
1 parent 36a7758 commit 707bc2d

3 files changed

Lines changed: 71 additions & 7 deletions

File tree

src/c4/yml/detail/dbgprint.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#else
1818
# define _c4dbgt(fmt, ...) do { \
1919
if(_dbg_enabled()) { \
20-
this->_dbg ("{}:{}: " fmt , __FILE__, __LINE__, __VA_ARGS__); \
20+
this->_dbg("{}:{}: " fmt , __FILE__, __LINE__, __VA_ARGS__); \
2121
} \
2222
} while(0)
2323
# define _c4dbgpf(fmt, ...) _dbg_printf("{}:{}: " fmt "\n", __FILE__, __LINE__, __VA_ARGS__)

src/c4/yml/escape_scalar.hpp

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ namespace yml {
4747
* ```
4848
*/
4949
template<class Fn>
50-
void escape_scalar_fn(Fn &&fn, csubstr scalar, bool keep_newlines=false)
50+
C4_NO_INLINE void escape_scalar_fn(Fn &&fn, csubstr scalar, bool keep_newlines=false)
5151
{
5252
size_t prev = 0; // the last position that was flushed
5353
size_t skip = 0; // how much to add to prev
@@ -161,10 +161,72 @@ void escape_scalar_fn(Fn &&fn, csubstr scalar, bool keep_newlines=false)
161161

162162

163163
C4_SUPPRESS_WARNING_GCC_WITH_PUSH("-Wattributes")
164+
165+
/** Adjust a position in a scalar, increasing it to account for any
166+
* escaped characters.
167+
*
168+
* @note This is a utility/debugging function, so it is provided in
169+
* this optional header. For this reason, we inline it to obey to the
170+
* One Definition Rule. But then we set the noinline attribute to
171+
* ensure they are not inlined in calling code. */
172+
inline C4_NO_INLINE size_t adjust_pos_with_escapes(csubstr scalar, size_t pos, bool keep_newlines=false)
173+
{
174+
// cast to u8 to avoid having to deal with negative
175+
// signed chars (which are present in some platforms)
176+
uint8_t const* C4_RESTRICT s = reinterpret_cast<uint8_t const*>(scalar.str); // NOLINT(*-reinterpret-cast)
177+
pos = pos < scalar.len ? pos : scalar.len;
178+
const size_t newbump = keep_newlines ? 2 : 1;
179+
for(size_t i = 0; i < pos; ++i)
180+
{
181+
switch(s[i])
182+
{
183+
case UINT8_C(0x5c): // '\\'
184+
case UINT8_C(0x09): // \t
185+
case UINT8_C(0x0d): // \r
186+
case UINT8_C(0x00): // \0
187+
case UINT8_C(0x0c): // \f (form feed)
188+
case UINT8_C(0x08): // \b (backspace)
189+
case UINT8_C(0x07): // \a (bell)
190+
case UINT8_C(0x0b): // \v (vertical tab)
191+
case UINT8_C(0x1b): // \e (escape)
192+
++pos;
193+
break;
194+
case UINT8_C(0x0a): // \n
195+
pos += newbump;
196+
break;
197+
case UINT8_C(0xc2): // AKA -0x3e
198+
if(i+1 < scalar.len)
199+
{
200+
if(s[i+1] == UINT8_C(0xa0) // AKA -0x60 -> \_
201+
||
202+
s[i+1] == UINT8_C(0x85)) // AKA -0x7b -> \N
203+
++pos;
204+
}
205+
break;
206+
case UINT8_C(0xe2): // AKA -0x1e
207+
if(i+2 < scalar.len)
208+
{
209+
if(s[i+1] == UINT8_C(0x80)) // AKA -0x80
210+
{
211+
if(s[i+2] == UINT8_C(0xa8) // AKA -0x58 -> \L
212+
||
213+
s[i+2] == UINT8_C(0xa9)) // AKA -0x57 -> \P
214+
++pos;
215+
}
216+
}
217+
break;
218+
default:
219+
break;
220+
}
221+
}
222+
return pos;
223+
}
224+
225+
164226
/** Escape a scalar to an existing buffer, using @ref escape_scalar_fn
165227
*
166-
* @note This is a utility/debugging function, so it is provided in this
167-
* (optional) header. For this reason, we inline it to obey to the
228+
* @note This is a utility/debugging function, so it is provided in
229+
* this optional header. For this reason, we inline it to obey to the
168230
* One Definition Rule. But then we set the noinline attribute to
169231
* ensure they are not inlined in calling code. */
170232
inline C4_NO_INLINE size_t escape_scalar(substr buffer, csubstr scalar, bool keep_newlines=false)

src/c4/yml/parse_engine.def.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,14 +454,16 @@ C4_NO_INLINE void ParseEngine<EventHandler>::_fmt_msg(DumpFn &&dumpfn) const
454454
_dbg_dump(std::forward<DumpFn>(dumpfn), "{}:{}: ", st->pos.line, st->pos.col);
455455
csubstr maybe_full_content = (contents.len < 80u ? contents : contents.first(80u));
456456
csubstr maybe_ellipsis = (contents.len < 80u ? csubstr{} : csubstr("..."));
457-
_dbg_dump(std::forward<DumpFn>(dumpfn), "{}{} (size={})\n", maybe_full_content, maybe_ellipsis, contents.len);
457+
_dbg_dump(std::forward<DumpFn>(dumpfn), "{}{} (size={})\n", escaped_scalar(maybe_full_content, /*escape*/true), maybe_ellipsis, contents.len);
458458
// highlight the remaining portion of the previous line
459459
size_t firstcol = (size_t)(lc.rem.begin() - lc.full.begin());
460460
size_t lastcol = firstcol + lc.rem.len;
461-
for(size_t i = 0; i < offs + firstcol; ++i)
461+
size_t firstcol_adj = adjust_pos_with_escapes(lc.full, firstcol);
462+
size_t len = adjust_pos_with_escapes(lc.rem, lc.rem.len);
463+
for(size_t i = 0; i < offs + firstcol_adj; ++i)
462464
std::forward<DumpFn>(dumpfn)(" ");
463465
std::forward<DumpFn>(dumpfn)("^");
464-
for(size_t i = 1, e = (lc.rem.len < 80u ? lc.rem.len : 80u); i < e; ++i)
466+
for(size_t i = 1, e = (len < 80u ? len : 80u); i < e; ++i)
465467
std::forward<DumpFn>(dumpfn)("~");
466468
_dbg_dump(std::forward<DumpFn>(dumpfn), "{} (cols {}-{})\n", maybe_ellipsis, firstcol+1, lastcol+1);
467469
}

0 commit comments

Comments
 (0)