damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

str.h (8126B)


      1 /* Licensed under BSD-MIT - see LICENSE file for details */
      2 #ifndef CCAN_STR_TAL_H
      3 #define CCAN_STR_TAL_H
      4 #ifdef TAL_USE_TALLOC
      5 #include <ccan/tal/talloc/talloc.h>
      6 #else
      7 #include <ccan/tal/tal.h>
      8 #endif
      9 #include <string.h>
     10 #include <stdbool.h>
     11 
     12 /**
     13  * tal_strdup - duplicate a string
     14  * @ctx: NULL, or tal allocated object to be parent.
     15  * @p: the string to copy (can be take(), must not be NULL).
     16  *
     17  * The returned string will have tal_count() == strlen() + 1.
     18  */
     19 #define tal_strdup(ctx, p) tal_strdup_(ctx, p, TAL_LABEL(char, "[]"))
     20 char *tal_strdup_(const tal_t *ctx, const char *p TAKES, const char *label)
     21 	TAL_RETURN_PTR NON_NULL_ARGS(2);
     22 
     23 /**
     24  * tal_strndup - duplicate a limited amount of a string.
     25  * @ctx: NULL, or tal allocated object to be parent.
     26  * @p: the string to copy (can be take(), must not be NULL).
     27  * @n: the maximum length to copy.
     28  *
     29  * Always gives a nul-terminated string, with strlen() <= @n.
     30  * The returned string will have tal_count() == strlen() + 1.
     31  */
     32 #define tal_strndup(ctx, p, n) tal_strndup_(ctx, p, n, TAL_LABEL(char, "[]"))
     33 char *tal_strndup_(const tal_t *ctx, const char *p TAKES, size_t n,
     34 		   const char *label)
     35 	TAL_RETURN_PTR NON_NULL_ARGS(2);
     36 
     37 /**
     38  * tal_fmt - allocate a formatted string
     39  * @ctx: NULL, or tal allocated object to be parent.
     40  * @fmt: the printf-style format (can be take(), must not be NULL).
     41  *
     42  * The returned string will have tal_count() == strlen() + 1.
     43  */
     44 #define tal_fmt(ctx, ...)				 \
     45 	tal_fmt_(ctx, TAL_LABEL(char, "[]"), __VA_ARGS__)
     46 char *tal_fmt_(const tal_t *ctx, const char *label, const char *fmt TAKES,
     47 	       ...) PRINTF_FMT(3,4) TAL_RETURN_PTR NON_NULL_ARGS(3);
     48 
     49 /**
     50  * tal_vfmt - allocate a formatted string (va_list version)
     51  * @ctx: NULL, or tal allocated object to be parent.
     52  * @fmt: the printf-style format (can be take(), must not be NULL).
     53  * @va: the va_list containing the format args.
     54  *
     55  * The returned string will have tal_count() == strlen() + 1.
     56  */
     57 #define tal_vfmt(ctx, fmt, va)				\
     58 	tal_vfmt_(ctx, fmt, va, TAL_LABEL(char, "[]"))
     59 char *tal_vfmt_(const tal_t *ctx, const char *fmt TAKES, va_list ap,
     60 		const char *label)
     61 	PRINTF_FMT(2,0) TAL_RETURN_PTR NON_NULL_ARGS(2);
     62 
     63 /**
     64  * tal_append_fmt - append a formatted string to a talloc string.
     65  * @baseptr: a pointer to the tal string to be appended to.
     66  * @fmt: the printf-style format (can be take(), must not be NULL).
     67  *
     68  * Returns false on allocation failure.
     69  * Otherwise tal_count(*@baseptr) == strlen(*@baseptr) + 1.
     70  */
     71 bool tal_append_fmt(char **baseptr, const char *fmt TAKES, ...)
     72 	PRINTF_FMT(2,3) NON_NULL_ARGS(2);
     73 
     74 /**
     75  * tal_append_vfmt - append a formatted string to a talloc string (va_list)
     76  * @baseptr: a pointer to the tal string to be appended to.
     77  * @fmt: the printf-style format (can be take(), must not be NULL).
     78  * @va: the va_list containing the format args.
     79  *
     80  * Returns false on allocation failure.
     81  * Otherwise tal_count(*@baseptr) == strlen(*@baseptr) + 1.
     82  */
     83 bool tal_append_vfmt(char **baseptr, const char *fmt TAKES, va_list ap)
     84 	NON_NULL_ARGS(2);
     85 
     86 /**
     87  * tal_strcat - join two strings together
     88  * @ctx: NULL, or tal allocated object to be parent.
     89  * @s1: the first string (can be take(), must not be NULL).
     90  * @s2: the second string (can be take(), must not be NULL).
     91  *
     92  * The returned string will have tal_count() == strlen() + 1.
     93  */
     94 #define tal_strcat(ctx, s1, s2) tal_strcat_(ctx, s1, s2, TAL_LABEL(char, "[]"))
     95 char *tal_strcat_(const tal_t *ctx, const char *s1 TAKES, const char *s2 TAKES,
     96 		  const char *label) TAL_RETURN_PTR NON_NULL_ARGS(2,3);
     97 
     98 enum strsplit {
     99 	STR_EMPTY_OK,
    100 	STR_NO_EMPTY
    101 };
    102 
    103 /**
    104  * tal_strsplit - Split string into an array of substrings
    105  * @ctx: the context to tal from (often NULL).
    106  * @string: the string to split (can be take(), must not be NULL).
    107  * @delims: delimiters where lines should be split (can be take(), must not be NULL).
    108  * @flags: whether to include empty substrings.
    109  *
    110  * This function splits a single string into multiple strings.
    111  *
    112  * If @string is take(), the returned array will point into the
    113  * mangled @string.
    114  *
    115  * Multiple delimiters result in empty substrings.  By definition, no
    116  * delimiters will appear in the substrings.
    117  *
    118  * The final char * in the array will be NULL, and tal_count() will
    119  * return the number of elements plus 1 (for that NULL).
    120  *
    121  * Example:
    122  *	#include <ccan/tal/str/str.h>
    123  *	...
    124  *	static unsigned int count_long_lines(const char *string)
    125  *	{
    126  *		char **lines;
    127  *		unsigned int i, long_lines = 0;
    128  *
    129  *		// Can only fail on out-of-memory.
    130  *		lines = tal_strsplit(NULL, string, "\n", STR_NO_EMPTY);
    131  *		for (i = 0; lines[i] != NULL; i++)
    132  *			if (strlen(lines[i]) > 80)
    133  *				long_lines++;
    134  *		tal_free(lines);
    135  *		return long_lines;
    136  *	}
    137  */
    138 #define tal_strsplit(ctx, string, delims, flag)	\
    139 	tal_strsplit_(ctx, string, delims, flag, TAL_LABEL(char *, "[]"))
    140 char **tal_strsplit_(const tal_t *ctx,
    141 		     const char *string TAKES,
    142 		     const char *delims TAKES,
    143 		     enum strsplit flag,
    144 		     const char *label)
    145 	TAL_RETURN_PTR NON_NULL_ARGS(2,3);
    146 
    147 enum strjoin {
    148 	STR_TRAIL,
    149 	STR_NO_TRAIL
    150 };
    151 
    152 /**
    153  * tal_strjoin - Join an array of substrings into one long string
    154  * @ctx: the context to tal from (often NULL).
    155  * @strings: the NULL-terminated array of strings to join (can be take(), must not be NULL)
    156  * @delim: the delimiter to insert between the strings (can be take(), must not be NULL)
    157  * @flags: whether to add a delimieter to the end
    158  *
    159  * This function joins an array of strings into a single string.  The
    160  * return value is allocated using tal.  Each string in @strings is
    161  * followed by a copy of @delim.
    162  *
    163  * The returned string will have tal_count() == strlen() + 1.
    164  *
    165  * Example:
    166  *	// Append the string "--EOL" to each line.
    167  *	static char *append_to_all_lines(const char *string)
    168  *	{
    169  *		char **lines, *ret;
    170  *
    171  *		lines = tal_strsplit(NULL, string, "\n", STR_EMPTY_OK);
    172  *		ret = tal_strjoin(NULL, lines, "-- EOL\n", STR_TRAIL);
    173  *		tal_free(lines);
    174  *		return ret;
    175  *	}
    176  */
    177 #define tal_strjoin(ctx, strings, delim, flags)				\
    178 	tal_strjoin_(ctx, strings, delim, flags, TAL_LABEL(char, "[]"))
    179 char *tal_strjoin_(const void *ctx,
    180 		   char *strings[] TAKES,
    181 		   const char *delim TAKES,
    182 		   enum strjoin flags,
    183 		   const char *label)
    184 	TAL_RETURN_PTR NON_NULL_ARGS(2,3);
    185 
    186 /**
    187  * tal_strreg - match/extract from a string via (extended) regular expressions.
    188  * @ctx: the context to tal from (often NULL)
    189  * @string: the string to try to match (can be take(), must not be NULL)
    190  * @regex: the regular expression to match (can be take(), must not be NULL)
    191  * ...: pointers to strings to allocate for subexpressions.
    192  *
    193  * Returns true if we matched, in which case any parenthesized
    194  * expressions in @regex are allocated and placed in the char **
    195  * arguments following @regex.  NULL arguments mean the match is not
    196  * saved.  The order of the strings is the order
    197  * of opening braces in the expression: in the case of repeated
    198  * expressions (eg "([a-z])*") the last one is saved, in the case of
    199  * non-existent matches (eg "([a-z]*)?") the pointer is set to NULL.
    200  *
    201  * Allocation failures or malformed regular expressions return false.
    202  * The allocated strings will have tal_count() == strlen() + 1.
    203  *
    204  * See Also:
    205  *	regcomp(3), regex(3).
    206  *
    207  * Example:
    208  *	// Given "My name is Rusty" outputs "Hello Rusty!\n"
    209  *	// Given "my first name is Rusty Russell" outputs "Hello Rusty Russell!\n"
    210  *	// Given "My name isnt Rusty Russell" outputs "Hello there!\n"
    211  *	int main(int argc, char *argv[])
    212  *	{
    213  *		char *person, *input;
    214  *
    215  *		(void)argc;
    216  *		// Join args and trim trailing space.
    217  *		input = tal_strjoin(NULL, argv+1, " ", STR_NO_TRAIL);
    218  *		if (tal_strreg(NULL, input,
    219  *			       "[Mm]y (first )?name is ([A-Za-z ]+)",
    220  *			       NULL, &person))
    221  *			printf("Hello %s!\n", person);
    222  *		else
    223  *			printf("Hello there!\n");
    224  *		return 0;
    225  *	}
    226  */
    227 #define tal_strreg(ctx, string, ...)					\
    228 	tal_strreg_(ctx, string, TAL_LABEL(char, "[]"), __VA_ARGS__)
    229 bool tal_strreg_(const void *ctx, const char *string TAKES,
    230 		 const char *label, const char *regex TAKES, ...)
    231 	NON_NULL_ARGS(2,4);
    232 #endif /* CCAN_STR_TAL_H */