All the cool kids are using tal/str now...
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
stringmap \
strmap \
strset \
- str_talloc \
take \
tal \
tal/grab_file \
+++ /dev/null
-../../licenses/LGPL-2.1
\ No newline at end of file
+++ /dev/null
-#include <stdio.h>
-#include <string.h>
-#include "config.h"
-
-/**
- * str_talloc - string helper routines which use talloc
- *
- * This is a grab bag of fnctions for string operations, designed to enhance
- * the standard string.h; these are separated from the non-talloc-needing
- * string utilities in "str.h".
- *
- * Example:
- * #include <ccan/str_talloc/str_talloc.h>
- * #include <ccan/talloc/talloc.h>
- * #include <ccan/grab_file/grab_file.h>
- * #include <err.h>
- *
- * // Dumb demo program to double-linespace a file.
- * int main(int argc, char *argv[])
- * {
- * char *textfile;
- * char **lines;
- *
- * // Grab lines in file.
- * textfile = grab_file(NULL, argv[1], NULL);
- * if (!textfile)
- * err(1, "Failed reading %s", argv[1]);
- * lines = strsplit(textfile, textfile, "\n");
- *
- * // Join them back together with two linefeeds.
- * printf("%s", strjoin(textfile, lines, "\n\n"));
- *
- * // Free everything, just because we can.
- * talloc_free(textfile);
- * return 0;
- * }
- *
- * License: LGPL (v2.1 or any later version)
- * Author: Rusty Russell <rusty@rustcorp.com.au>
- */
-int main(int argc, char *argv[])
-{
- if (argc != 2)
- return 1;
-
- if (strcmp(argv[1], "depends") == 0) {
- printf("ccan/str\n");
- printf("ccan/talloc\n");
- return 0;
- }
-
- return 1;
-}
+++ /dev/null
-/* Licensed under LGPLv2.1+ - see LICENSE file for details */
-#include <unistd.h>
-#include <stdint.h>
-#include <string.h>
-#include <limits.h>
-#include <stdlib.h>
-#include "str_talloc.h"
-#include <sys/types.h>
-#include <regex.h>
-#include <stdarg.h>
-#include <unistd.h>
-#include <ccan/talloc/talloc.h>
-#include <ccan/str/str.h>
-
-char **strsplit(const void *ctx, const char *string, const char *delims)
-{
- char **lines = NULL;
- unsigned int max = 64, num = 0;
-
- lines = talloc_array(ctx, char *, max+1);
-
- while (*string != '\0') {
- unsigned int len = strcspn(string, delims);
- lines[num] = talloc_array(lines, char, len + 1);
- memcpy(lines[num], string, len);
- lines[num][len] = '\0';
- string += len;
- string += strspn(string, delims) ? 1 : 0;
- if (++num == max)
- lines = talloc_realloc(ctx, lines, char *, max*=2 + 1);
- }
- lines[num] = NULL;
-
- /* Shrink, so talloc_get_size works */
- return talloc_realloc(ctx, lines, char *, num+1);
-}
-
-char *strjoin(const void *ctx, char *strings[], const char *delim)
-{
- unsigned int i;
- char *ret = talloc_strdup(ctx, "");
- size_t totlen = 0, dlen = strlen(delim);
-
- for (i = 0; strings[i]; i++) {
- size_t len = strlen(strings[i]);
- ret = talloc_realloc(ctx, ret, char, totlen + len + dlen + 1);
- memcpy(ret + totlen, strings[i], len);
- totlen += len;
- memcpy(ret + totlen, delim, dlen);
- totlen += dlen;
- }
- ret[totlen] = '\0';
- return ret;
-}
-
-bool strreg(const void *ctx, const char *string, const char *regex, ...)
-{
- size_t nmatch = 1 + strcount(regex, "(");
- regmatch_t *matches = talloc_array(ctx, regmatch_t, nmatch);
- regex_t r;
- bool ret;
-
- if (!matches || regcomp(&r, regex, REG_EXTENDED) != 0)
- return false;
-
- if (regexec(&r, string, nmatch, matches, 0) == 0) {
- unsigned int i;
- va_list ap;
-
- ret = true;
- va_start(ap, regex);
- for (i = 1; i < nmatch; i++) {
- char **arg;
- arg = va_arg(ap, char **);
- if (arg) {
- /* eg. ([a-z])? can give "no match". */
- if (matches[i].rm_so == -1)
- *arg = NULL;
- else {
- *arg = talloc_strndup(ctx,
- string + matches[i].rm_so,
- matches[i].rm_eo
- - matches[i].rm_so);
- if (!*arg) {
- ret = false;
- break;
- }
- }
- }
- }
- va_end(ap);
- } else
- ret = false;
- talloc_free(matches);
- regfree(&r);
- return ret;
-}
+++ /dev/null
-/* Licensed under LGPLv2.1+ - see LICENSE file for details */
-#ifndef CCAN_STR_TALLOC_H
-#define CCAN_STR_TALLOC_H
-#include <string.h>
-#include <stdbool.h>
-
-/**
- * strsplit - Split string into an array of substrings
- * @ctx: the context to tallocate from (often NULL)
- * @string: the string to split
- * @delims: delimiters where lines should be split.
- *
- * This function splits a single string into multiple strings. The
- * original string is untouched: an array is allocated (using talloc)
- * pointing to copies of each substring. Multiple delimiters result
- * in empty substrings. By definition, no delimiters will appear in
- * the substrings.
- *
- * The final char * in the array will be NULL, talloc_array_length() of the
- * returned value is 1 greater than the number of valid elements in
- * the array.
- *
- * Example:
- * #include <ccan/talloc/talloc.h>
- * #include <ccan/str_talloc/str_talloc.h>
- * ...
- * static unsigned int count_long_lines(const char *string)
- * {
- * char **lines;
- * unsigned int i, long_lines = 0;
- *
- * // Can only fail on out-of-memory.
- * lines = strsplit(NULL, string, "\n");
- * for (i = 0; lines[i] != NULL; i++)
- * if (strlen(lines[i]) > 80)
- * long_lines++;
- * talloc_free(lines);
- * return long_lines;
- * }
- */
-char **strsplit(const void *ctx, const char *string, const char *delims);
-
-/**
- * strjoin - Join an array of substrings into one long string
- * @ctx: the context to tallocate from (often NULL)
- * @strings: the NULL-terminated array of strings to join
- * @delim: the delimiter to insert between the strings
- *
- * This function joins an array of strings into a single string. The
- * return value is allocated using talloc. Each string in @strings is
- * followed by a copy of @delim.
- *
- * Example:
- * // Append the string "--EOL" to each line.
- * static char *append_to_all_lines(const char *string)
- * {
- * char **lines, *ret;
- *
- * lines = strsplit(NULL, string, "\n");
- * ret = strjoin(NULL, lines, "-- EOL\n");
- * talloc_free(lines);
- * return ret;
- * }
- */
-char *strjoin(const void *ctx, char *strings[], const char *delim);
-
-/**
- * strreg - match and extract from a string via (extended) regular expressions.
- * @ctx: the context to tallocate from (often NULL)
- * @string: the string to try to match.
- * @regex: the regular expression to match.
- * ...: pointers to strings to allocate for subexpressions.
- *
- * Returns true if we matched, in which case any parenthesized
- * expressions in @regex are allocated and placed in the char **
- * arguments following @regex. NULL arguments mean the match is not
- * saved. The order of the strings is the order
- * of opening braces in the expression: in the case of repeated
- * expressions (eg "([a-z])*") the last one is saved, in the case of
- * non-existent matches (eg "([a-z]*)?") the pointer is set to NULL.
- *
- * Allocation failures or malformed regular expressions return false.
- *
- * See Also:
- * regcomp(3), regex(3).
- *
- * Example:
- * // Given 'My name is Rusty' outputs 'Hello Rusty!'
- * // Given 'my first name is Rusty Russell' outputs 'Hello Rusty Russell!'
- * // Given 'My name isnt Rusty Russell' outputs 'Hello there!'
- * int main(int argc, char *argv[])
- * {
- * char *person, *input;
- *
- * // Join args and trim trailing space.
- * input = strjoin(NULL, argv+1, " ");
- * if (strlen(input) != 0)
- * input[strlen(input)-1] = '\0';
- *
- * if (strreg(NULL, input, "[Mm]y (first )?name is ([A-Za-z ]+)",
- * NULL, &person))
- * printf("Hello %s!\n", person);
- * else
- * printf("Hello there!\n");
- * return 0;
- * }
- */
-bool strreg(const void *ctx, const char *string, const char *regex, ...);
-#endif /* CCAN_STR_TALLOC_H */
+++ /dev/null
-#include <ccan/str_talloc/str_talloc.h>
-#include <ccan/str_talloc/str_talloc.c>
-#include <ccan/tap/tap.h>
-
-int main(int argc, char *argv[])
-{
- void *ctx = talloc_init("toplevel");
- unsigned int top_blocks = talloc_total_blocks(ctx);
- char *a, *b;
- /* If it accesses this, it will crash. */
- char **invalid = (char **)1L;
-
- plan_tests(25);
- /* Simple matching. */
- ok1(strreg(ctx, "hello world!", "hello") == true);
- ok1(strreg(ctx, "hello world!", "hi") == false);
-
- /* No parentheses means we don't use any extra args. */
- ok1(strreg(ctx, "hello world!", "hello", invalid) == true);
- ok1(strreg(ctx, "hello world!", "hi", invalid) == false);
-
- ok1(strreg(ctx, "hello world!", "[a-z]+", invalid) == true);
- ok1(strreg(ctx, "hello world!", "([a-z]+)", &a, invalid) == true);
- /* Found string */
- ok1(streq(a, "hello"));
- /* Allocated off ctx */
- ok1(talloc_find_parent_byname(a, "toplevel") == ctx);
- talloc_free(a);
-
- ok1(strreg(ctx, "hello world!", "([a-z]*) ([a-z]+)",
- &a, &b, invalid) == true);
- ok1(streq(a, "hello"));
- ok1(streq(b, "world"));
- ok1(talloc_find_parent_byname(a, "toplevel") == ctx);
- ok1(talloc_find_parent_byname(b, "toplevel") == ctx);
- talloc_free(a);
- talloc_free(b);
-
- /* * after parentheses returns last match. */
- ok1(strreg(ctx, "hello world!", "([a-z])* ([a-z]+)",
- &a, &b, invalid) == true);
- ok1(streq(a, "o"));
- ok1(streq(b, "world"));
- talloc_free(a);
- talloc_free(b);
-
- /* Nested parentheses are ordered by open brace. */
- ok1(strreg(ctx, "hello world!", "(([a-z]*) world)",
- &a, &b, invalid) == true);
- ok1(streq(a, "hello world"));
- ok1(streq(b, "hello"));
- talloc_free(a);
- talloc_free(b);
-
- /* Nested parentheses are ordered by open brace. */
- ok1(strreg(ctx, "hello world!", "(([a-z]*) world)",
- &a, &b, invalid) == true);
- ok1(streq(a, "hello world"));
- ok1(streq(b, "hello"));
- talloc_free(a);
- talloc_free(b);
-
- /* NULL means we're not interested. */
- ok1(strreg(ctx, "hello world!", "((hello|goodbye) world)",
- &a, NULL, invalid) == true);
- ok1(streq(a, "hello world"));
- talloc_free(a);
-
- /* No leaks! */
- ok1(talloc_total_blocks(ctx) == top_blocks);
- talloc_free(ctx);
- talloc_disable_null_tracking();
-
- return exit_status();
-}
+++ /dev/null
-#include <ccan/str_talloc/str_talloc.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <ccan/str_talloc/str_talloc.c>
-#include <ccan/tap/tap.h>
-
-#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
-
-static const char *substrings[]
-= { "far", "bar", "baz", "b", "ba", "z", "ar", NULL };
-
-int main(int argc, char *argv[])
-{
- char **split, *str;
- void *ctx;
-
- plan_tests(16);
- split = strsplit(NULL, "hello world", " ");
- ok1(talloc_array_length(split) == 4);
- ok1(!strcmp(split[0], "hello"));
- ok1(!strcmp(split[1], ""));
- ok1(!strcmp(split[2], "world"));
- ok1(split[3] == NULL);
- talloc_free(split);
-
- split = strsplit(NULL, "hello world", "o ");
- ok1(talloc_array_length(split) == 6);
- ok1(!strcmp(split[0], "hell"));
- ok1(!strcmp(split[1], ""));
- ok1(!strcmp(split[2], ""));
- ok1(!strcmp(split[3], "w"));
- ok1(!strcmp(split[4], "rld"));
- ok1(split[5] == NULL);
-
- ctx = split;
- split = strsplit(ctx, "hello world", "o ");
- ok1(talloc_parent(split) == ctx);
- talloc_free(ctx);
-
- str = strjoin(NULL, (char **)substrings, ", ");
- ok1(!strcmp(str, "far, bar, baz, b, ba, z, ar, "));
- ctx = str;
- str = strjoin(ctx, (char **)substrings, "");
- ok1(!strcmp(str, "farbarbazbbazar"));
- ok1(talloc_parent(str) == ctx);
- talloc_free(ctx);
-
- return exit_status();
-}