Re: [PATCH 1/3] Added generic string handling code.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 2007-05-22 at 15:40 +0200, Petr Baudis wrote:
> On Sun, May 20, 2007 at 04:24:29AM CEST, Timo Sirainen wrote:
> > diff --git a/str.c b/str.c
> > new file mode 100644
> > index 0000000..d46e7f4
> > --- /dev/null
> > +++ b/str.c
> > @@ -0,0 +1,40 @@
> > +#include "str.h"
> > +
> > +void _str_append(struct string *str, const char *cstr)
> 
> _ is reserved namespace.

I remember __ is, but was _ too? A lot of programs are using that. :)

> > +{
> > +	unsigned int avail = str->size - str->len;
> > +	unsigned int len = strlen(cstr);
> > +
> > +	if (len >= avail) {
> > +		len = avail - 1;
> > +		str->overflowed = 1;
> > +	}
> > +	memcpy(str->buf + str->len, cstr, len);
> > +	str->len += len;
> > +	str->buf[str->len] = '\0';
> 
> You can copy len + 1 and avoid this assignment.

Not if the string overflowed.

> > +}
> > +
> > +void _str_printfa(struct string *str, const char *fmt, ...)
> 
> printfA?

"append". I think I got it originally from glib:

/* These aliases are included for compatibility. */
#define g_string_sprintf g_string_printf
#define g_string_sprintfa g_string_append_printf

If there's a chance that this string handling code would get used, I
could write another patch with a bit clearer names and support for
dynamically growing strings too. Something like:

STATIC_STRING(name, 1234);
sstr_append(name, "hello"); // or static_str_append()?

struct string *dyn = str_new(1024); // initial length
str_append(dyn, "hello");

Attachment: signature.asc
Description: This is a digitally signed message part


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux