Commit db511536 authored by Philipp Schafft's avatar Philipp Schafft 🦁

Feature: Support alternative quoting with %#H

parent ceab1c8c
...@@ -445,6 +445,7 @@ static void __vsnprintf(char *str, size_t size, const char *format, va_list ap) ...@@ -445,6 +445,7 @@ static void __vsnprintf(char *str, size_t size, const char *format, va_list ap)
int block_size = 0; int block_size = 0;
int block_len = 0; int block_len = 0;
int block_space = 0; int block_space = 0;
int block_alt = 0;
const char * arg; const char * arg;
char buf[80]; char buf[80];
...@@ -457,6 +458,7 @@ static void __vsnprintf(char *str, size_t size, const char *format, va_list ap) ...@@ -457,6 +458,7 @@ static void __vsnprintf(char *str, size_t size, const char *format, va_list ap)
block_size = 0; block_size = 0;
block_len = 0; block_len = 0;
block_space = 0; block_space = 0;
block_alt = 0;
} }
else else
{ {
...@@ -485,6 +487,9 @@ static void __vsnprintf(char *str, size_t size, const char *format, va_list ap) ...@@ -485,6 +487,9 @@ static void __vsnprintf(char *str, size_t size, const char *format, va_list ap)
case ' ': case ' ':
block_space = 1; block_space = 1;
break; break;
case '#':
block_alt = 1;
break;
case '1': case '1':
case '2': case '2':
case '3': case '3':
...@@ -544,15 +549,36 @@ static void __vsnprintf(char *str, size_t size, const char *format, va_list ap) ...@@ -544,15 +549,36 @@ static void __vsnprintf(char *str, size_t size, const char *format, va_list ap)
// TODO. // TODO.
if (!arg) if (!arg)
arg = va_arg(ap, const char *); arg = va_arg(ap, const char *);
if (!arg) if (*format != 'H') {
block_alt = 0;
}
if (!arg && !block_alt)
arg = "(null)"; arg = "(null)";
if (!block_len) if (!block_len) {
block_len = strlen(arg); if (arg) {
block_len = strlen(arg) + ((block_alt) ? 2 : 0);
} else {
block_len = 1;
}
}
// the if() is the outer structure so the inner for() // the if() is the outer structure so the inner for()
// is branch optimized. // is branch optimized.
if (*format == 'H') if (*format == 'H' && !arg)
{ {
if (size && block_len) {
*(str++) = '-';
size--;
block_len--;
}
}
else if (*format == 'H')
{
if (block_alt && size && block_len) {
*(str++) = '"';
size--;
block_len--;
}
for (; *arg && block_len && size; arg++, size--, block_len--) for (; *arg && block_len && size; arg++, size--, block_len--)
{ {
if ((*arg <= '"' || *arg == '`' || *arg == '\\') && !(block_space && *arg == ' ')) { if ((*arg <= '"' || *arg == '`' || *arg == '\\') && !(block_space && *arg == ' ')) {
...@@ -572,6 +598,11 @@ static void __vsnprintf(char *str, size_t size, const char *format, va_list ap) ...@@ -572,6 +598,11 @@ static void __vsnprintf(char *str, size_t size, const char *format, va_list ap)
*(str++) = *arg; *(str++) = *arg;
} }
} }
if (block_alt && size && block_len) {
*(str++) = '"';
size--;
block_len--;
}
} }
else else
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment