debugcon.cpp, textbuf.cpp: More uses for std::string_view

This commit is contained in:
AJR 2020-12-11 22:03:42 -05:00
parent 94421d749c
commit 80cf54eaa2
4 changed files with 67 additions and 71 deletions

View File

@ -571,63 +571,61 @@ std::string debugger_console::cmderr_to_string(CMDERR error)
***************************************************************************/
/*-------------------------------------------------
print_core - write preformatted text
to the debug console
-------------------------------------------------*/
//-------------------------------------------------
// print_core - write preformatted text
// to the debug console
//-------------------------------------------------
void debugger_console::print_core(const char *text)
void debugger_console::print_core(std::string_view text)
{
// FIXME: this invokes strlen() twice; compute it once and pass it to text_buffer_print
text_buffer_print(*m_console_textbuf, text);
if (m_logfile)
m_logfile->write(text, strlen(text));
m_logfile->write(text.data(), text.length());
}
/*-------------------------------------------------
print_core_wrap - write preformatted text
to the debug console, with wrapping
-------------------------------------------------*/
//-------------------------------------------------
// print_core_wrap - write preformatted text
// to the debug console, with wrapping
//-------------------------------------------------
void debugger_console::print_core_wrap(const char *text, int wrapcol)
void debugger_console::print_core_wrap(std::string_view text, int wrapcol)
{
// FIXME: this invokes strlen() twice; compute it once and pass it to text_buffer_print
// FIXME: also look into honoring wrapcol for the logfile
// FIXME: look into honoring wrapcol for the logfile
text_buffer_print_wrap(*m_console_textbuf, text, wrapcol);
if (m_logfile)
m_logfile->write(text, strlen(text));
m_logfile->write(text.data(), text.length());
}
/*-------------------------------------------------
vprintf - vprintfs the given arguments using
the format to the debug console
-------------------------------------------------*/
//-------------------------------------------------
// vprintf - vprintfs the given arguments using
// the format to the debug console
//-------------------------------------------------
void debugger_console::vprintf(util::format_argument_pack<std::ostream> const &args)
{
print_core(util::string_format(args).c_str());
print_core(util::string_format(args));
/* force an update of any console views */
// force an update of any console views
m_machine.debug_view().update_all(DVT_CONSOLE);
}
void debugger_console::vprintf(util::format_argument_pack<std::ostream> &&args)
{
print_core(util::string_format(std::move(args)).c_str());
print_core(util::string_format(std::move(args)));
// force an update of any console views
m_machine.debug_view().update_all(DVT_CONSOLE);
}
/*-------------------------------------------------
vprintf_wrap - vprintfs the given arguments
using the format to the debug console
-------------------------------------------------*/
//-------------------------------------------------
// vprintf_wrap - vprintfs the given arguments
// using the format to the debug console
//-------------------------------------------------
void debugger_console::vprintf_wrap(int wrapcol, util::format_argument_pack<std::ostream> const &args)
{
print_core_wrap(util::string_format(args).c_str(), wrapcol);
print_core_wrap(util::string_format(args), wrapcol);
// force an update of any console views
m_machine.debug_view().update_all(DVT_CONSOLE);
@ -635,17 +633,17 @@ void debugger_console::vprintf_wrap(int wrapcol, util::format_argument_pack<std:
void debugger_console::vprintf_wrap(int wrapcol, util::format_argument_pack<std::ostream> &&args)
{
print_core_wrap(util::string_format(std::move(args)).c_str(), wrapcol);
print_core_wrap(util::string_format(std::move(args)), wrapcol);
// force an update of any console views
m_machine.debug_view().update_all(DVT_CONSOLE);
}
/*-------------------------------------------------
errorlog_write_line - writes a line to the
errorlog ring buffer
-------------------------------------------------*/
//-------------------------------------------------
// errorlog_write_line - writes a line to the
// errorlog ring buffer
//-------------------------------------------------
void debugger_console::errorlog_write_line(const char *line)
{

View File

@ -30,7 +30,7 @@ constexpr u32 CMDFLAG_NONE = 0x0000;
constexpr u32 CMDFLAG_KEEP_QUOTES = 0x0001;
constexpr u32 CMDFLAG_CUSTOM_HELP = 0x0002;
/* parameter separator macros */
// parameter separator macros
#define CMDPARAM_SEPARATOR "\0"
#define CMDPARAM_TERMINATOR "\0\0"
@ -94,11 +94,11 @@ public:
void vprintf_wrap(int wrapcol, util::format_argument_pack<std::ostream> &&args);
text_buffer &get_console_textbuf() const { return *m_console_textbuf; }
/* errorlog management */
// errorlog management
void errorlog_write_line(const char *line);
text_buffer &get_errorlog_textbuf() const { return *m_errorlog_textbuf; }
/* convenience templates */
// convenience templates
template <typename Format, typename... Params>
inline void printf(Format &&fmt, Params &&...args)
{
@ -126,8 +126,8 @@ private:
CMDERR internal_execute_command(bool execute, int params, char **param);
CMDERR internal_parse_command(const std::string &original_command, bool execute);
void print_core(const char *text); // core text output
void print_core_wrap(const char *text, int wrapcol); // core text output
void print_core(std::string_view text); // core text output
void print_core_wrap(std::string_view text, int wrapcol); // core text output
struct debug_command
{

View File

@ -139,32 +139,31 @@ void text_buffer_clear(text_buffer &text)
***************************************************************************/
/*-------------------------------------------------
text_buffer_print - print data to the text
buffer
-------------------------------------------------*/
//-------------------------------------------------
// text_buffer_print - print data to the text
// buffer
//-------------------------------------------------
void text_buffer_print(text_buffer &text, const char *data)
void text_buffer_print(text_buffer &text, std::string_view data)
{
text_buffer_print_wrap(text, data, MAX_LINE_LENGTH);
}
/*-------------------------------------------------
text_buffer_print_wrap - print data to the
text buffer with word wrapping to a given
column
-------------------------------------------------*/
//-------------------------------------------------
// text_buffer_print_wrap - print data to the
// text buffer with word wrapping to a given
// column
//-------------------------------------------------
void text_buffer_print_wrap(text_buffer &text, const char *data, int wrapcol)
void text_buffer_print_wrap(text_buffer &text, std::string_view data, int wrapcol)
{
s32 const stopcol = (wrapcol < MAX_LINE_LENGTH) ? wrapcol : MAX_LINE_LENGTH;
s32 needed_space;
/* we need to ensure there is enough space for this string plus enough for the max line length */
needed_space = s32(strlen(data)) + MAX_LINE_LENGTH;
// we need to ensure there is enough space for this string plus enough for the max line length
s32 const needed_space = s32(data.length()) + MAX_LINE_LENGTH;
/* make space in the buffer if we need to */
// make space in the buffer if we need to
while (text.buffer_space() < needed_space && text.linestart != text.lineend)
{
text.linestartseq++;
@ -173,56 +172,55 @@ void text_buffer_print_wrap(text_buffer &text, const char *data, int wrapcol)
text.bufstart = text.lineoffs[text.linestart];
}
/* now add the data */
for ( ; *data; data++)
// now add the data
for (int ch : data)
{
int ch = *data;
int linelen;
/* a CR resets our position */
// a CR resets our position
if (ch == '\r')
text.bufend = text.lineoffs[text.lineend];
/* non-CR data is just characters */
// non-CR data is just characters
else if (ch != '\n')
text.buffer[text.bufend++] = ch;
/* an explicit newline or line-too-long condition inserts a newline */
// an explicit newline or line-too-long condition inserts a newline */
linelen = text.bufend - text.lineoffs[text.lineend];
if (ch == '\n' || linelen >= stopcol)
{
int overflow = 0;
/* if we're wrapping, back off until we hit a space */
// if we're wrapping, back off until we hit a space
if (linelen >= wrapcol)
{
/* scan backwards, removing characters along the way */
// scan backwards, removing characters along the way
overflow = 1;
while (overflow < linelen && text.buffer[text.bufend - overflow] != ' ')
overflow++;
/* if we found a space, take it; otherwise, reset and pretend we didn't try */
// if we found a space, take it; otherwise, reset and pretend we didn't try
if (overflow < linelen)
linelen -= overflow;
else
overflow = 0;
}
/* did we beat the max width */
// did we beat the max width
if (linelen > text.maxwidth)
text.maxwidth = linelen;
/* append a terminator */
// append a terminator
if (overflow == 0)
text.buffer[text.bufend++] = 0;
else
text.buffer[text.bufend - overflow] = 0;
/* determine what the next line will be */
// determine what the next line will be
if (++text.lineend >= text.linesize)
text.lineend = 0;
/* if we're out of lines, consume the next one */
// if we're out of lines, consume the next one
if (text.lineend == text.linestart)
{
text.linestartseq++;
@ -231,16 +229,16 @@ void text_buffer_print_wrap(text_buffer &text, const char *data, int wrapcol)
text.bufstart = text.lineoffs[text.linestart];
}
/* if we don't have enough room in the buffer for a max line, wrap to the start */
// if we don't have enough room in the buffer for a max line, wrap to the start
if (text.bufend + MAX_LINE_LENGTH + 1 >= text.bufsize)
text.bufend = 0;
/* create a new empty line */
// create a new empty line
text.lineoffs[text.lineend] = text.bufend - (overflow ? (overflow - 1) : 0);
}
}
/* nullptr terminate what we have on this line */
// null terminate what we have on this line
text.buffer[text.bufend] = 0;
}
@ -321,7 +319,7 @@ std::string_view text_buffer_lines::text_buffer_line_iterator::operator*() const
if (difference < 0)
difference += m_buffer.bufsize;
return std::string_view{ line, size_t(difference) };
return std::string_view{ line, std::string_view::size_type(difference) };
}
/*---------------------------------------------------------------------

View File

@ -77,10 +77,10 @@ text_buffer_ptr text_buffer_alloc(u32 bytes, u32 lines);
void text_buffer_clear(text_buffer &text);
// "print" data to a text buffer
void text_buffer_print(text_buffer &text, const char *data);
void text_buffer_print(text_buffer &text, std::string_view data);
// "print" data to a text buffer with word wrapping to a given column
void text_buffer_print_wrap(text_buffer &text, const char *data, int wrapcol);
void text_buffer_print_wrap(text_buffer &text, std::string_view data, int wrapcol);
// get the maximum width of lines seen so far
u32 text_buffer_max_width(const text_buffer &text);