str_talloc.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Licensed under LGPLv2.1+ - see LICENSE file for details */
  2. #ifndef CCAN_STR_TALLOC_H
  3. #define CCAN_STR_TALLOC_H
  4. #include <string.h>
  5. #include <stdbool.h>
  6. /**
  7. * strsplit - Split string into an array of substrings
  8. * @ctx: the context to tallocate from (often NULL)
  9. * @string: the string to split
  10. * @delims: delimiters where lines should be split.
  11. *
  12. * This function splits a single string into multiple strings. The
  13. * original string is untouched: an array is allocated (using talloc)
  14. * pointing to copies of each substring. Multiple delimiters result
  15. * in empty substrings. By definition, no delimiters will appear in
  16. * the substrings.
  17. *
  18. * The final char * in the array will be NULL, talloc_array_length() of the
  19. * returned value is 1 greater than the number of valid elements in
  20. * the array.
  21. *
  22. * Example:
  23. * #include <ccan/talloc/talloc.h>
  24. * #include <ccan/str_talloc/str_talloc.h>
  25. * ...
  26. * static unsigned int count_long_lines(const char *string)
  27. * {
  28. * char **lines;
  29. * unsigned int i, long_lines = 0;
  30. *
  31. * // Can only fail on out-of-memory.
  32. * lines = strsplit(NULL, string, "\n");
  33. * for (i = 0; lines[i] != NULL; i++)
  34. * if (strlen(lines[i]) > 80)
  35. * long_lines++;
  36. * talloc_free(lines);
  37. * return long_lines;
  38. * }
  39. */
  40. char **strsplit(const void *ctx, const char *string, const char *delims);
  41. /**
  42. * strjoin - Join an array of substrings into one long string
  43. * @ctx: the context to tallocate from (often NULL)
  44. * @strings: the NULL-terminated array of strings to join
  45. * @delim: the delimiter to insert between the strings
  46. *
  47. * This function joins an array of strings into a single string. The
  48. * return value is allocated using talloc. Each string in @strings is
  49. * followed by a copy of @delim.
  50. *
  51. * Example:
  52. * // Append the string "--EOL" to each line.
  53. * static char *append_to_all_lines(const char *string)
  54. * {
  55. * char **lines, *ret;
  56. *
  57. * lines = strsplit(NULL, string, "\n");
  58. * ret = strjoin(NULL, lines, "-- EOL\n");
  59. * talloc_free(lines);
  60. * return ret;
  61. * }
  62. */
  63. char *strjoin(const void *ctx, char *strings[], const char *delim);
  64. /**
  65. * strreg - match and extract from a string via (extended) regular expressions.
  66. * @ctx: the context to tallocate from (often NULL)
  67. * @string: the string to try to match.
  68. * @regex: the regular expression to match.
  69. * ...: pointers to strings to allocate for subexpressions.
  70. *
  71. * Returns true if we matched, in which case any parenthesized
  72. * expressions in @regex are allocated and placed in the char **
  73. * arguments following @regex. NULL arguments mean the match is not
  74. * saved. The order of the strings is the order
  75. * of opening braces in the expression: in the case of repeated
  76. * expressions (eg "([a-z])*") the last one is saved, in the case of
  77. * non-existent matches (eg "([a-z]*)?") the pointer is set to NULL.
  78. *
  79. * Allocation failures or malformed regular expressions return false.
  80. *
  81. * See Also:
  82. * regcomp(3), regex(3).
  83. *
  84. * Example:
  85. * // Given 'My name is Rusty' outputs 'Hello Rusty!'
  86. * // Given 'my first name is Rusty Russell' outputs 'Hello Rusty Russell!'
  87. * // Given 'My name isnt Rusty Russell' outputs 'Hello there!'
  88. * int main(int argc, char *argv[])
  89. * {
  90. * char *person, *input;
  91. *
  92. * // Join args and trim trailing space.
  93. * input = strjoin(NULL, argv+1, " ");
  94. * if (strlen(input) != 0)
  95. * input[strlen(input)-1] = '\0';
  96. *
  97. * if (strreg(NULL, input, "[Mm]y (first )?name is ([A-Za-z ]+)",
  98. * NULL, &person))
  99. * printf("Hello %s!\n", person);
  100. * else
  101. * printf("Hello there!\n");
  102. * return 0;
  103. * }
  104. */
  105. bool strreg(const void *ctx, const char *string, const char *regex, ...);
  106. #endif /* CCAN_STR_TALLOC_H */