[Top][All Lists]

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

Re: string types

From: Tim Rühsen
Subject: Re: string types
Date: Sun, 29 Dec 2019 22:24:51 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.3.1

On 27.12.19 11:51, Bruno Haible wrote:
> Aga wrote:
>> I do not know if
>> you can (or if it is possible, how it can be done), extract with a way a 
>> specific
>> a functionality from gnulib, with the absolute necessary code and only that.
> gnulib-tool does this. With its --avoid option, the developer can even 
> customize
> their notion of "absolutely necessary".
>> In a myriad of codebases a string type is implemented at least as:
>>   size_t mem_size;
>>   size_t num_bytes;
>>   char *bytes;
> This is actually a string-buffer type. A string type does not need two size_t
> members. Long-term experience has shown that using different types for string
> and string-buffer is a win, because
>   - a string can be put in a read-only virtual memory area, thus enforcing
>     immutability (-> reducing multithread problems),
>   - providing primitives for string allocation reduces the amount of buffer
>     overflow bugs that otherwise occur in this area. [1]
> [1] https://lists.gnu.org/archive/html/bug-gnulib/2019-09/msg00031.html

Just FYI,

here is a string concatenation function without ellipsis, analogue to
writev() and struct iovec - just a suggestion. Instead of 'struct
strvec' a new string_t type would be handy.

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct strvec {
  char *strv_base;
  size_t strv_len;

__attribute__ ((nonnull (1)))
char *concat_stringv(const struct strvec *strv)
  const struct strvec *str;
  size_t len = 0;
  char *buf;

  for (str = strv; str->strv_base; str++)
    len += str->strv_len;

  if (!(buf = malloc(len + 1)))
    return buf;

  len = 0;
  for (str = strv; str->strv_base; len += str->strv_len, str++)
    memcpy(buf + len, str->strv_base, str->strv_len);

  buf[len] = 0;

  return buf;

void main(void)
  char *s = concat_stringv((struct strvec []) {
    { "a", 1 },
    { "b", 1 },
    { NULL }



In GNU Wget2 we already have type similar to string_t. Just used in
cases where we need pointer + len of URLs inside const HTML/XML/CSS data.

typedef struct {
        const char
                *p; //!< pointer to memory region
                len; //!< length of memory region
} wget_string;

So maybe we need a string_t and a const_string_t type !? (to avoid
casting from const char *)

Regards, Tim

Attachment: signature.asc
Description: OpenPGP digital signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]