Going a bit farther than Eric's xmemdup0 function, I would like to propose
a module 'xstring' with an <xstring.h> header file that defines the following
functions:

  /* Return a substring of the given string, len bytes long starting at str.  */
  char * xsubstring (const char *str, size_t len);

  /* Return a string containing the bytes from the given memory region,
     checking that they are not NUL.  */
  char * xmemtostr (const void *ptr, size_t len);

  /* Concatenate an array of strings.  The last argument must be NULL.  */
  char *xstrconcat (const char *string1, ...);
  /* Concatenate an array of strings.  */
  char *xvstrconcat (const char *string[], size_t nstrings);

  /* Concatenate an array of strings, freeing all the arguments.  The last
     argument must be NULL.  */
  char *xstrconcat_free (char *string1, ...);
  /* Concatenate an array of strings, freeing all the arguments.  */
  char *xvstrconcat_free (char *string[], size_t nstrings);

  /* Return a freshly allocated string obtained by concatenating all the
     strings in the list, separated by the separator character, terminated
     by the terminator character.  The terminator character is not added if
     drop_redundant_terminator is true and the last string already ends with
     the terminator. */
  char *xstrjoin (char separator, char terminator, bool 
drop_redundant_terminator,
                  const char *string1, ...);
  char *xvstrjoin (char separator, char terminator, bool 
drop_redundant_terminator,
                   const char *string[], size_t nstrings);

  /* Duplicate a string. Moved here from xalloc.h. Keeps being declared in
     xalloc.h for a transition period, with a warning if it's used from there
     (a migration aid).  */
  char * xstrdup (const char *string);


Comments? Additions?

Bruno



Reply via email to