"Utstring" which is convenient for manipulating character strings in C language

It may be convenient to use "utstring" when it is troublesome to allocate memory dynamically for manipulating character strings in C language.

What is utstring

utstring is a library for dynamically manipulating strings in C language. The feature is that most of them are implemented by macros, and it is very easy to use because it can be used just by including utstring.h. You can get it from the following site. https://troydhanson.github.io/uthash/utstring.html

The following is also explained on the official website, so please refer to the official website for accurate information. Also, since utstring.h is just a block of macro definitions, detailed behavior can be confirmed from the source code.

How to use

Just download utstring.h and include it.

#include "utstring.h"

A structure called UT_string is defined as a string type, so use this instead of the usual char *. The structure definition of UT_string is as follows. d is a pointer to the area of the internal string, n is the size of the reserved area, and i is the index to the unused area.

typedef struct {
    char *d;  /* pointer to allocated buffer */
    size_t n; /* allocated capacity */
    size_t i; /* index of first unused byte */
} UT_string;

Various operations for manipulating strings using UT_string are provided under the name utstring_xxxx, which allows you to perform a series of operations that you can do with string.h. Specifically, the following operations are available.

--utstring_new (s): Create a new utstring --utstring_free (s): Free the area of s --utstring_init (s): Initialize s --utstring_done (s): Free the string area of s --utstring_renew (s): If s is NULL, allocate a new area. Clear if not NULL (equivalent to utstring_clear) --utstring_printf (s, fmt,…): Set s to string --utstring_bincpy (s, bin, len): Copy (add) the char * string to s --utstring_concat (dst, src): Concatenate utstrings together --utstring_clear (s): Clear s (make it an empty string) --utstring_len (s): Returns the string length of s --utstring_body (s): Returns the string of s as char * --utstring_find (s, pos, str, len): Search the substring in s backward from the position of pos --utstring_findR (s, pos, str, len): Searches the substring in s forward from the position of pos.

Difference between utstring_new (s) / utstring_free (s) and utstring_init (s) / utstring_done (s)

utstring_new (s) utstring_free (s) allocates / frees the memory area of s itself. utstring_init (s) / utstring_done (s) initializes and frees the area of the internal string. Note that utstring_clear (s) only sets a null character as an internal string and sets the size to 0, it does not release the memory.

Example of use

#include <stdio.h>
#include "utstring.h"

int main() {
	UT_string *s;
	
	utstring_new(s);	//Create New
	utstring_printf(s, "number=%d", 123);	//Store string
	printf("%s\n", utstring_body(s));	//Output number=123

	UT_string *t;
	char *str = "hoge";
	utstring_new(t);
	utstring_bincpy(t, str, strlen(str));	// char*Copy string from
	printf("%s\n", utstring_body(t));	//Output hoge
	
	utstring_concat(s, t);			//Connect t to s
	printf("%s\n", utstring_body(s));	//Output number=123hoge

	utstring_free(s);	//release
	utstring_free(t);
}

Recommended Posts