Commit f8d92255 authored by Pontus Freyhult's avatar Pontus Freyhult
Browse files

Added lsh_string_colonize to insert colons in a lsh_string.

Rev: src/format.c:1.45
Rev: src/format.h:1.36
parent d537b654
......@@ -631,3 +631,38 @@ lsh_string_prefixp(const struct lsh_string *prefix,
return ( (prefix->length <= s->length)
&& !memcmp(prefix->data, s->data, prefix->length));
}
struct lsh_string*
lsh_string_colonize(struct lsh_string *s, int every, int freeflag)
{
UINT32 i = 0;
UINT32 j = 0;
struct lsh_string *packet;
UINT32 length;
int colons;
/* No of colonds depens on length, 0..every => 0,
* every..2*every => 1 */
colons = s->length ? (s->length - 1) / every : 0;
length = s->length + colons;
packet = lsh_string_alloc(length);
for (; i<s->length; i++)
{
assert( j < length ); /* Extra sanity check */
if (i && !(i%every)) /* Every nth position except at the beginning */
packet->data[j++] = ':';
assert( j < length ); /* Extra sanity check */
packet->data[j++] = s->data[i];
}
if (freeflag) /* Throw away the source string? */
lsh_string_free( s );
return packet;
}
......@@ -106,4 +106,7 @@ int lsh_string_eq_l(const struct lsh_string *a, UINT32 length, const UINT8 *b);
int lsh_string_prefixp(const struct lsh_string *prefix,
const struct lsh_string *s);
struct lsh_string*
lsh_string_colonize(struct lsh_string *s, int every, int freeflag);
#endif /* LSH_FORMAT_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