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

(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, ...@@ -71,6 +71,20 @@ format_prefix(struct nettle_buffer *buffer,
return prefix_length + 1; 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 unsigned
sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args) 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) ...@@ -81,8 +95,18 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
switch (*format++) switch (*format++)
{ {
default: 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': case '\0':
assert(!nesting); assert(!nesting);
...@@ -123,7 +147,7 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args) ...@@ -123,7 +147,7 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
{ {
const char *s; const char *s;
unsigned length; unsigned length;
unsigned prefix_length; unsigned output_length;
if (nul_flag) if (nul_flag)
{ {
...@@ -136,25 +160,18 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args) ...@@ -136,25 +160,18 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
s = va_arg(args, const char *); s = va_arg(args, const char *);
} }
prefix_length = format_prefix(buffer, length); output_length = format_string(buffer, length, s);
if (!output_length)
if (!prefix_length)
return 0; return 0;
done += prefix_length; done += output_length;
if (buffer && !nettle_buffer_write(buffer, length, s))
return 0;
done += length;
break; break;
} }
case 't': case 't':
{ {
const char *s; const char *s;
unsigned length; unsigned length;
unsigned prefix_length; unsigned output_length;
if (nul_flag) if (nul_flag)
{ {
...@@ -176,18 +193,13 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args) ...@@ -176,18 +193,13 @@ sexp_vformat(struct nettle_buffer *buffer, const char *format, va_list args)
return 0; return 0;
done++; done++;
prefix_length = format_prefix(buffer, length); output_length = format_string(buffer, length, s);
if (!prefix_length) if (!output_length)
return 0; return 0;
done += prefix_length; done += output_length;
if (buffer && !nettle_buffer_write(buffer, length, s))
return 0;
done += length;
if (buffer && !NETTLE_BUFFER_PUTC(buffer, ']')) if (buffer && !NETTLE_BUFFER_PUTC(buffer, ']'))
return 0; return 0;
done++; done++;
......
...@@ -134,7 +134,8 @@ struct nettle_buffer; ...@@ -134,7 +134,8 @@ struct nettle_buffer;
/* Returns the number of output characters, or 0 on out of memory. If /* Returns the number of output characters, or 0 on out of memory. If
* buffer == NULL, just compute length. * 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: * formatting specifiers:
* *
* %s String represented as unsigned length, const uint8_t *data. * %s String represented as unsigned length, const uint8_t *data.
...@@ -157,8 +158,7 @@ struct nettle_buffer; ...@@ -157,8 +158,7 @@ struct nettle_buffer;
* instead the string is NUL-terminated, and there's only one * instead the string is NUL-terminated, and there's only one
* const uint8_t * argument. * const uint8_t * argument.
* *
* FIXME: Allow literals, like "(x%b)". Allow "%(" for unbalanced * FIXME: Allow "%(" for unbalanced parenthesis. */
* parenthesis. */
unsigned unsigned
sexp_format(struct nettle_buffer *buffer, sexp_format(struct nettle_buffer *buffer,
...@@ -179,4 +179,10 @@ unsigned ...@@ -179,4 +179,10 @@ unsigned
sexp_transport_vformat(struct nettle_buffer *buffer, sexp_transport_vformat(struct nettle_buffer *buffer,
const char *format, va_list args); 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 */ #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