Commit 1c323788 authored by Niels Möller's avatar Niels Möller
Browse files

(format_length_string): Deleted function.

(format_string): Deleted function.
(sexp_vformat): New %t specifier, formatting an optional display
type. Deleted %z specifier. Instead, introduced a new modifier "0"
that can be used with %s, %l and %t, which says that the data is
NUL-terminated.

Rev: src/nettle/sexp-format.c:1.5
Rev: src/nettle/sexp.h:1.11
parent a38808d6
......@@ -71,27 +71,6 @@ format_prefix(struct nettle_buffer *buffer,
return prefix_length + 1;
}
static unsigned
format_length_string(struct nettle_buffer *buffer,
unsigned length, const char *s)
{
unsigned done = format_prefix(buffer, length);
if (!done)
return 0;
if (buffer && !nettle_buffer_write(buffer, length, s))
return 0;
return done + length;
}
static unsigned
format_string(struct nettle_buffer *buffer,
const char *s)
{
return format_length_string(buffer, strlen(s), s);
}
unsigned
sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
{
......@@ -127,27 +106,77 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
break;
case '%':
{
int nul_flag = 0;
if (*format == '0')
{
format++;
nul_flag = 1;
}
switch (*format++)
{
default:
abort();
case 'z':
case 's':
{
const char *s;
unsigned length;
unsigned prefix_length;
if (nul_flag)
{
s = va_arg(args, const char *);
length = strlen(s);
}
else
{
const char *s = va_arg(args, const char *);
unsigned length = format_string(buffer, s);
length = va_arg(args, unsigned);
s = va_arg(args, const char *);
}
prefix_length = format_prefix(buffer, length);
if (!prefix_length)
return 0;
done += prefix_length;
if (!length)
if (buffer && !nettle_buffer_write(buffer, length, s))
return 0;
done += length;
break;
}
case 's':
case 't':
{
const char *s;
unsigned length;
unsigned prefix_length;
if (nul_flag)
{
unsigned length = va_arg(args, unsigned);
const char *s = va_arg(args, const char *);
unsigned prefix_length = format_prefix(buffer, length);
s = va_arg(args, const char *);
if (!s)
break;
length = strlen(s);
}
else
{
length = va_arg(args, unsigned);
s = va_arg(args, const char *);
if (!s)
break;
}
if (buffer && !NETTLE_BUFFER_PUTC(buffer, '['))
return 0;
done++;
prefix_length = format_prefix(buffer, length);
if (!prefix_length)
return 0;
......@@ -159,12 +188,28 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
done += length;
if (buffer && !NETTLE_BUFFER_PUTC(buffer, ']'))
return 0;
done++;
break;
}
case 'l':
{
unsigned length = va_arg(args, unsigned);
const char *s = va_arg(args, const char *);
const char *s;
unsigned length;
if (nul_flag)
{
s = va_arg(args, const char *);
length = strlen(s);
}
else
{
length = va_arg(args, unsigned);
s = va_arg(args, const char *);
}
if (buffer && !nettle_buffer_write(buffer, length, s))
return 0;
......@@ -255,6 +300,7 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
}
}
}
}
}
unsigned
......
......@@ -137,10 +137,12 @@ struct nettle_buffer;
* Format strings can contained matched parentheses, and the following
* formatting specifiers:
*
* %z NUL-terminated string, const uint8_t *.
*
* %s String represented as unsigned length, const uint8_t *data.
*
* %t Optional display type, represented as
* unsigned display_length, const uint8_t *display,
* display == NULL means no display type.
*
* %i Non-negative small integer, uint32_t.
*
* %b Non-negative bignum, mpz_t.
......@@ -148,7 +150,15 @@ struct nettle_buffer;
* %l Literal string (no length added), typically a balanced
* subexpression. Represented as unsigned length, const uint8_t
* *data.
*/
*
* Modifiers:
*
* %0 For %s, %t and %l, says that there's no length argument,
* instead the string is NUL-terminated, and there's only one
* const uint8_t * argument.
*
* FIXME: Allow literals, like "(x%b)". Allow "%(" for unbalanced
* parenthesis. */
unsigned
sexp_format(struct nettle_buffer *buffer,
......
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