Implement putstr and putstrn in ui_file

In my tour of the ui_file subsystem, I found that fputstr and fputstrn
can be simplified.  The _filtered forms are never used (and IMO
unlikely to ever be used) and so can be removed.  And, the interface
can be simplified by removing a callback function and moving the
implementation directly to ui_file.

A new self-test is included.  Previously, I think nothing was testing
this code.

Regression tested on x86-64 Fedora 34.
This commit is contained in:
Tom Tromey 2021-12-31 10:40:02 -07:00
parent 28a4e64dd1
commit d53fd721a1
10 changed files with 158 additions and 139 deletions

View file

@ -47,13 +47,15 @@ ui_file::printf (const char *format, ...)
void
ui_file::putstr (const char *str, int quoter)
{
fputstr_unfiltered (str, quoter, this);
while (*str)
printchar (*str++, quoter, false);
}
void
ui_file::putstrn (const char *str, int n, int quoter)
ui_file::putstrn (const char *str, int n, int quoter, bool async_safe)
{
fputstrn_unfiltered (str, n, quoter, fputc_unfiltered, this);
for (int i = 0; i < n; i++)
printchar (str[i], quoter, async_safe);
}
int
@ -68,6 +70,67 @@ ui_file::vprintf (const char *format, va_list args)
vfprintf_unfiltered (this, format, args);
}
/* See ui-file.h. */
void
ui_file::printchar (int c, int quoter, bool async_safe)
{
char buf[4];
int out = 0;
c &= 0xFF; /* Avoid sign bit follies */
if (c < 0x20 /* Low control chars */
|| (c >= 0x7F && c < 0xA0) /* DEL, High controls */
|| (sevenbit_strings && c >= 0x80))
{ /* high order bit set */
buf[out++] = '\\';
switch (c)
{
case '\n':
buf[out++] = 'n';
break;
case '\b':
buf[out++] = 'b';
break;
case '\t':
buf[out++] = 't';
break;
case '\f':
buf[out++] = 'f';
break;
case '\r':
buf[out++] = 'r';
break;
case '\033':
buf[out++] = 'e';
break;
case '\007':
buf[out++] = 'a';
break;
default:
{
buf[out++] = '0' + ((c >> 6) & 0x7);
buf[out++] = '0' + ((c >> 3) & 0x7);
buf[out++] = '0' + ((c >> 0) & 0x7);
break;
}
}
}
else
{
if (quoter != 0 && (c == '\\' || c == quoter))
buf[out++] = '\\';
buf[out++] = c;
}
if (async_safe)
this->write_async_safe (buf, out);
else
this->write (buf, out);
}
void