Commit e01516ec authored by Niels Möller's avatar Niels Möller

(format_string): New function.

(sexp_vformat): Implemented support for literals in the format
string.

Rev: src/nettle/sexp-format.c:1.6
Rev: src/nettle/sexp.h:1.12
parent 790e9669
......@@ -71,6 +71,20 @@ format_prefix(struct nettle_buffer *buffer,
return prefix_length + 1;
}
static unsigned
format_string(struct nettle_buffer *buffer,
unsigned length, const uint8_t *s)
{
unsigned prefix_length = format_prefix(buffer, length);
if (!prefix_length)
return 0;
if (buffer && !nettle_buffer_write(buffer, length, s))
return 0;
return prefix_length + length;
}
unsigned
sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
{
......@@ -81,8 +95,18 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
switch (*format++)
{
default:
abort();
{
const char *start = format - 1;
unsigned length = 1 + strcspn(format, "()%");
unsigned output_length = format_string(buffer, length, start);
if (!output_length)
return 0;
done += output_length;
format = start + length;
break;
}
case '\0':
assert(!nesting);
......@@ -123,7 +147,7 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
{
const char *s;
unsigned length;
unsigned prefix_length;
unsigned output_length;
if (nul_flag)
{
......@@ -136,25 +160,18 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
s = va_arg(args, const char *);
}
prefix_length = format_prefix(buffer, length);
if (!prefix_length)
output_length = format_string(buffer, length, s);
if (!output_length)
return 0;
done += prefix_length;
if (buffer && !nettle_buffer_write(buffer, length, s))
return 0;
done += length;
done += output_length;
break;
}
case 't':
{
const char *s;
unsigned length;
unsigned prefix_length;
unsigned output_length;
if (nul_flag)
{
......@@ -176,18 +193,13 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
return 0;
done++;
prefix_length = format_prefix(buffer, length);
output_length = format_string(buffer, length, s);
if (!prefix_length)
if (!output_length)
return 0;
done += prefix_length;
if (buffer && !nettle_buffer_write(buffer, length, s))
return 0;
done += length;
done += output_length;
if (buffer && !NETTLE_BUFFER_PUTC(buffer, ']'))
return 0;
done++;
......
......@@ -134,7 +134,8 @@ struct nettle_buffer;
/* Returns the number of output characters, or 0 on out of memory. If
* buffer == NULL, just compute length.
*
* Format strings can contained matched parentheses, and the following
* Format strings can contained matched parentheses, tokens ("foo" in
* the format string is formatted as "3:foo"), and the following
* formatting specifiers:
*
* %s String represented as unsigned length, const uint8_t *data.
......@@ -157,8 +158,7 @@ struct nettle_buffer;
* 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. */
* FIXME: Allow "%(" for unbalanced parenthesis. */
unsigned
sexp_format(struct nettle_buffer *buffer,
......@@ -179,4 +179,10 @@ unsigned
sexp_transport_vformat(struct nettle_buffer *buffer,
const char *format, va_list args);
/* Classification for advanced syntax. */
extern const char
sexp_token_chars[0x80];
#define TOKEN_CHAR(c) ((c) < 0x80 && sexp_token_chars[(c)])
#endif /* NETTLE_SEXP_H_INCLUDED */
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