explode.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**
  2. * @file explode.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Synopsis:
  32. * explode(string delimiter, string input [, string limit])
  33. *
  34. * Description:
  35. * Splits the string 'input' into a list of components. The first component
  36. * is the part of 'input' until the first occurence of 'delimiter', if any.
  37. * If 'delimiter' was found, the remaining components are defined recursively
  38. * via the same procedure, starting with the part of 'input' after the first
  39. * substring.
  40. * 'delimiter' must be nonempty.
  41. *
  42. * Variables:
  43. * list (empty) - the components of 'input', determined based on 'delimiter'
  44. */
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <limits.h>
  48. #include <misc/exparray.h>
  49. #include <misc/string_begins_with.h>
  50. #include <misc/substring.h>
  51. #include <misc/balloc.h>
  52. #include <ncd/module_common.h>
  53. #include <generated/blog_channel_ncd_explode.h>
  54. struct instance {
  55. NCDModuleInst *i;
  56. struct ExpArray arr;
  57. size_t num;
  58. };
  59. struct substring {
  60. char *data;
  61. size_t len;
  62. };
  63. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  64. {
  65. struct instance *o = vo;
  66. o->i = i;
  67. // read arguments
  68. NCDValRef delimiter_arg;
  69. NCDValRef input_arg;
  70. NCDValRef limit_arg = NCDVal_NewInvalid();
  71. if (!NCDVal_ListRead(params->args, 2, &delimiter_arg, &input_arg) && !NCDVal_ListRead(params->args, 3, &delimiter_arg, &input_arg, &limit_arg)) {
  72. ModuleLog(i, BLOG_ERROR, "wrong arity");
  73. goto fail0;
  74. }
  75. if (!NCDVal_IsString(delimiter_arg) || !NCDVal_IsString(input_arg)) {
  76. ModuleLog(i, BLOG_ERROR, "wrong type");
  77. goto fail0;
  78. }
  79. size_t limit = SIZE_MAX;
  80. if (!NCDVal_IsInvalid(limit_arg)) {
  81. uintmax_t n;
  82. if (!ncd_read_uintmax(limit_arg, &n) || n == 0) {
  83. ModuleLog(i, BLOG_ERROR, "bad limit argument");
  84. goto fail0;
  85. }
  86. n--;
  87. limit = (n <= SIZE_MAX ? n : SIZE_MAX);
  88. }
  89. MemRef del = NCDVal_StringMemRef(delimiter_arg);
  90. if (del.len == 0) {
  91. ModuleLog(i, BLOG_ERROR, "delimiter must be nonempty");
  92. goto fail0;
  93. }
  94. size_t *table = BAllocArray(del.len, sizeof(table[0]));
  95. if (!table) {
  96. ModuleLog(i, BLOG_ERROR, "ExpArray_init failed");
  97. goto fail0;
  98. }
  99. build_substring_backtrack_table(del.ptr, del.len, table);
  100. if (!ExpArray_init(&o->arr, sizeof(struct substring), 8)) {
  101. ModuleLog(i, BLOG_ERROR, "ExpArray_init failed");
  102. goto fail1;
  103. }
  104. o->num = 0;
  105. const char *data = NCDVal_StringData(input_arg);
  106. size_t len = NCDVal_StringLength(input_arg);
  107. while (1) {
  108. size_t start;
  109. int is_end = 0;
  110. if (limit == 0 || !find_substring(data, len, del.ptr, del.len, table, &start)) {
  111. start = len;
  112. is_end = 1;
  113. }
  114. if (!ExpArray_resize(&o->arr, o->num + 1)) {
  115. ModuleLog(i, BLOG_ERROR, "ExpArray_init failed");
  116. goto fail2;
  117. }
  118. struct substring *elem = &((struct substring *)o->arr.v)[o->num];
  119. if (!(elem->data = BAlloc(start))) {
  120. ModuleLog(i, BLOG_ERROR, "BAlloc failed");
  121. goto fail2;
  122. }
  123. memcpy(elem->data, data, start);
  124. elem->len = start;
  125. o->num++;
  126. if (is_end) {
  127. break;
  128. }
  129. data += start + del.len;
  130. len -= start + del.len;
  131. limit--;
  132. }
  133. BFree(table);
  134. // signal up
  135. NCDModuleInst_Backend_Up(i);
  136. return;
  137. fail2:
  138. while (o->num-- > 0) {
  139. BFree(((struct substring *)o->arr.v)[o->num].data);
  140. }
  141. free(o->arr.v);
  142. fail1:
  143. BFree(table);
  144. fail0:
  145. NCDModuleInst_Backend_DeadError(i);
  146. }
  147. static void func_die (void *vo)
  148. {
  149. struct instance *o = vo;
  150. while (o->num-- > 0) {
  151. BFree(((struct substring *)o->arr.v)[o->num].data);
  152. }
  153. free(o->arr.v);
  154. NCDModuleInst_Backend_Dead(o->i);
  155. }
  156. static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  157. {
  158. struct instance *o = vo;
  159. if (name == NCD_STRING_EMPTY) {
  160. *out = NCDVal_NewList(mem, o->num);
  161. if (NCDVal_IsInvalid(*out)) {
  162. goto fail;
  163. }
  164. for (size_t j = 0; j < o->num; j++) {
  165. struct substring *elem = &((struct substring *)o->arr.v)[j];
  166. NCDValRef str = NCDVal_NewStringBin(mem, (uint8_t *)elem->data, elem->len);
  167. if (NCDVal_IsInvalid(str)) {
  168. goto fail;
  169. }
  170. if (!NCDVal_ListAppend(*out, str)) {
  171. goto fail;
  172. }
  173. }
  174. return 1;
  175. }
  176. return 0;
  177. fail:
  178. *out = NCDVal_NewInvalid();
  179. return 1;
  180. }
  181. static struct NCDModule modules[] = {
  182. {
  183. .type = "explode",
  184. .func_new2 = func_new,
  185. .func_die = func_die,
  186. .func_getvar2 = func_getvar2,
  187. .alloc_size = sizeof(struct instance)
  188. }, {
  189. .type = NULL
  190. }
  191. };
  192. const struct NCDModuleGroup ncdmodule_explode = {
  193. .modules = modules
  194. };