On 06/03/2010 07:35 PM, Denys Vlasenko wrote:
On Thu, 2010-06-03 at 15:36 +0200, Nikola Pajkovsky wrote:
> new functions are prefixcmp and suffixcmp
>
> Signed-off-by: Nikola Pajkovsky<npajkovs(a)redhat.com>
> ---
> inc/abrtlib.h | 3 +++
> lib/Utils/strbuf.c | 26 ++++++++++++++++++++------
> 2 files changed, 23 insertions(+), 6 deletions(-)
>
> diff --git a/inc/abrtlib.h b/inc/abrtlib.h
> index 4c4aa6b..cfeb62e 100644
> --- a/inc/abrtlib.h
> +++ b/inc/abrtlib.h
> @@ -98,6 +98,9 @@ extern int g_verbose;
>
> char* skip_whitespace(const char *s);
> char* skip_non_whitespace(const char *s);
> +extern int prefixcmp(const char *str, const char *prefix);
> +extern int suffixcmp(const char *str, const char *suffix);
extern keyword is not needed
> +
>
> unsigned xatou(const char *numstr);
> int xatoi(const char *numstr);
> diff --git a/lib/Utils/strbuf.c b/lib/Utils/strbuf.c
> index 0206233..0d14100 100644
> --- a/lib/Utils/strbuf.c
> +++ b/lib/Utils/strbuf.c
> @@ -25,6 +25,25 @@
> #include<stdarg.h>
> #include "xfuncs.h"
>
> +
> +int prefixcmp(const char *str, const char *prefix)
> +{
> + for (; ; str++, prefix++)
> + if (!*prefix)
> + return 0;
> + else if (*str != *prefix)
> + return (unsigned char)*prefix - (unsigned char)*str;
> +}
this can be done simpler:
return strncmp(str, prefix, strlen(prefix))
I leave it as it is, because your solution needs extra strlen.
> +int suffixcmp(const char *str, const char *suffix)
> +{
> + int len = strlen(str), suflen = strlen(suffix);
> + if (len< suflen)
> + return -1;
> + else
> + return strcmp(str + len - suflen, suffix);
> +}
CPU performs comparison as subtraction and compare result with 0.
Thus above code does that twice. Here's how this can be done once -
"if (len_minus_suflen< 0)" part is provided in CPU flags
"free of charge" by previous subtraction:
+ int len_minus_suflen = strlen(str) - strlen(suffix);
+ if (len_minus_suflen< 0)
+ return len_minus_suflen;
+ return strcmp(str + len_minus_suflen, suffix);
Fixed
> struct strbuf *strbuf_new()
> {
> struct strbuf *buf = xmalloc(sizeof(struct strbuf));
> @@ -66,12 +85,7 @@ static void strbuf_grow(struct strbuf *strbuf, int num)
> while (strbuf->len + num + 1> strbuf->alloc)
> strbuf->alloc *= 2; /* huge grow = infinite loop */
>
> - strbuf->buf = realloc(strbuf->buf, strbuf->alloc);
> - if (!strbuf->buf)
> - {
> - puts("Error while allocating memory for string buffer.");
> - exit(5);
> - }
> + strbuf->buf = xrealloc(strbuf->buf, strbuf->alloc);
> }
> }
Yes!
Thx