explode.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. *
  32. * Synopsis:
  33. * compile_search(string str)
  34. *
  35. * Description:
  36. * Performs calculations to enable efficient string searches for the
  37. * given string (builds the KMP table). The string must be non-empty.
  38. *
  39. *
  40. * Synopsis:
  41. * explode(string delimiter, string input [, string limit])
  42. * compile_search::explode(string input [, string limit])
  43. *
  44. * Description:
  45. * Splits the string 'input' into a list of components. The first component
  46. * is the part of 'input' until the first occurence of 'delimiter', if any.
  47. * If 'delimiter' was found, the remaining components are defined recursively
  48. * via the same procedure, starting with the part of 'input' after the first
  49. * substring.
  50. * 'delimiter' must be nonempty.
  51. * The compile_search variant uses an precompiled delimiter string for better
  52. * performance.
  53. *
  54. * Variables:
  55. * list (empty) - the components of 'input', determined based on 'delimiter'
  56. */
  57. #include <stdlib.h>
  58. #include <string.h>
  59. #include <limits.h>
  60. #include <misc/exparray.h>
  61. #include <misc/string_begins_with.h>
  62. #include <misc/substring.h>
  63. #include <misc/balloc.h>
  64. #include <ncd/module_common.h>
  65. #include <generated/blog_channel_ncd_explode.h>
  66. struct compile_search_instance {
  67. NCDModuleInst *i;
  68. MemRef str;
  69. size_t *table;
  70. };
  71. struct instance {
  72. NCDModuleInst *i;
  73. NCDValRef input;
  74. struct ExpArray arr;
  75. size_t num;
  76. };
  77. static void compile_search_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  78. {
  79. struct compile_search_instance *o = vo;
  80. o->i = i;
  81. NCDValRef str_arg;
  82. if (!NCDVal_ListRead(params->args, 1, &str_arg)) {
  83. ModuleLog(i, BLOG_ERROR, "wrong arity");
  84. goto fail0;
  85. }
  86. if (!NCDVal_IsString(str_arg)) {
  87. ModuleLog(i, BLOG_ERROR, "wrong type");
  88. goto fail0;
  89. }
  90. o->str = NCDVal_StringMemRef(str_arg);
  91. if (o->str.len == 0) {
  92. ModuleLog(i, BLOG_ERROR, "string must be nonempty");
  93. goto fail0;
  94. }
  95. o->table = BAllocArray(o->str.len, sizeof(o->table[0]));
  96. if (!o->table) {
  97. ModuleLog(i, BLOG_ERROR, "BAllocArray failed");
  98. goto fail0;
  99. }
  100. build_substring_backtrack_table(o->str, o->table);
  101. NCDModuleInst_Backend_Up(i);
  102. return;
  103. fail0:
  104. NCDModuleInst_Backend_DeadError(i);
  105. }
  106. static void compile_search_die (void *vo)
  107. {
  108. struct compile_search_instance *o = vo;
  109. BFree(o->table);
  110. NCDModuleInst_Backend_Dead(o->i);
  111. }
  112. static void func_new_common (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, struct compile_search_instance *compiled)
  113. {
  114. struct instance *o = vo;
  115. o->i = i;
  116. int arg_start;
  117. MemRef del;
  118. size_t const *table;
  119. if (compiled) {
  120. arg_start = 0;
  121. del = compiled->str;
  122. table = compiled->table;
  123. } else {
  124. NCDValRef delimiter_arg;
  125. if (!NCDVal_ListReadHead(params->args, 1, &delimiter_arg)) {
  126. ModuleLog(i, BLOG_ERROR, "missing delimiter argument");
  127. goto fail0;
  128. }
  129. if (!NCDVal_IsString(delimiter_arg)) {
  130. ModuleLog(i, BLOG_ERROR, "wrong delimiter type");
  131. goto fail0;
  132. }
  133. arg_start = 1;
  134. del = NCDVal_StringMemRef(delimiter_arg);
  135. if (del.len == 0) {
  136. ModuleLog(i, BLOG_ERROR, "delimiter must be nonempty");
  137. goto fail0;
  138. }
  139. table = BAllocArray(del.len, sizeof(table[0]));
  140. if (!table) {
  141. ModuleLog(i, BLOG_ERROR, "ExpArray_init failed");
  142. goto fail0;
  143. }
  144. build_substring_backtrack_table(del, (size_t *)table);
  145. }
  146. // read arguments
  147. NCDValRef input_arg;
  148. NCDValRef limit_arg = NCDVal_NewInvalid();
  149. if (!NCDVal_ListReadStart(params->args, arg_start, 1, &input_arg) && !NCDVal_ListReadStart(params->args, arg_start, 2, &input_arg, &limit_arg)) {
  150. ModuleLog(i, BLOG_ERROR, "wrong arity");
  151. goto fail1;
  152. }
  153. if (!NCDVal_IsString(input_arg)) {
  154. ModuleLog(i, BLOG_ERROR, "wrong type");
  155. goto fail1;
  156. }
  157. o->input = input_arg;
  158. size_t limit = SIZE_MAX;
  159. if (!NCDVal_IsInvalid(limit_arg)) {
  160. uintmax_t n;
  161. if (!ncd_read_uintmax(limit_arg, &n) || n == 0) {
  162. ModuleLog(i, BLOG_ERROR, "bad limit argument");
  163. goto fail1;
  164. }
  165. n--;
  166. limit = (n <= SIZE_MAX ? n : SIZE_MAX);
  167. }
  168. if (!ExpArray_init(&o->arr, sizeof(MemRef), 8)) {
  169. ModuleLog(i, BLOG_ERROR, "ExpArray_init failed");
  170. goto fail1;
  171. }
  172. o->num = 0;
  173. MemRef data = NCDVal_StringMemRef(input_arg);
  174. while (1) {
  175. size_t start;
  176. int is_end = 0;
  177. if (limit == 0 || !find_substring(data, del, table, &start)) {
  178. start = data.len;
  179. is_end = 1;
  180. }
  181. if (!ExpArray_resize(&o->arr, o->num + 1)) {
  182. ModuleLog(i, BLOG_ERROR, "ExpArray_init failed");
  183. goto fail2;
  184. }
  185. ((MemRef *)o->arr.v)[o->num] = MemRef_SubTo(data, start);
  186. o->num++;
  187. if (is_end) {
  188. break;
  189. }
  190. data = MemRef_SubFrom(data, start + del.len);
  191. limit--;
  192. }
  193. if (!compiled) {
  194. BFree((size_t *)table);
  195. }
  196. // signal up
  197. NCDModuleInst_Backend_Up(i);
  198. return;
  199. fail2:
  200. free(o->arr.v);
  201. fail1:
  202. if (!compiled) {
  203. BFree((size_t *)table);
  204. }
  205. fail0:
  206. NCDModuleInst_Backend_DeadError(i);
  207. }
  208. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  209. {
  210. return func_new_common(vo, i, params, NULL);
  211. }
  212. static void func_new_compiled (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  213. {
  214. struct compile_search_instance *compiled = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  215. return func_new_common(vo, i, params, compiled);
  216. }
  217. static void func_die (void *vo)
  218. {
  219. struct instance *o = vo;
  220. free(o->arr.v);
  221. NCDModuleInst_Backend_Dead(o->i);
  222. }
  223. static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  224. {
  225. struct instance *o = vo;
  226. if (name == NCD_STRING_EMPTY) {
  227. *out = NCDVal_NewList(mem, o->num);
  228. if (NCDVal_IsInvalid(*out)) {
  229. goto fail;
  230. }
  231. int is_external = NCDVal_IsExternalString(o->input);
  232. for (size_t j = 0; j < o->num; j++) {
  233. MemRef elem = ((MemRef *)o->arr.v)[j];
  234. NCDValRef str;
  235. if (is_external) {
  236. str = NCDVal_NewExternalString(mem, elem.ptr, elem.len, NCDVal_ExternalStringTarget(o->input));
  237. } else {
  238. str = NCDVal_NewStringBinMr(mem, elem);
  239. }
  240. if (NCDVal_IsInvalid(str)) {
  241. goto fail;
  242. }
  243. if (!NCDVal_ListAppend(*out, str)) {
  244. goto fail;
  245. }
  246. }
  247. return 1;
  248. }
  249. return 0;
  250. fail:
  251. *out = NCDVal_NewInvalid();
  252. return 1;
  253. }
  254. static struct NCDModule modules[] = {
  255. {
  256. .type = "compile_search",
  257. .func_new2 = compile_search_new,
  258. .func_die = compile_search_die,
  259. .alloc_size = sizeof(struct compile_search_instance)
  260. }, {
  261. .type = "explode",
  262. .func_new2 = func_new,
  263. .func_die = func_die,
  264. .func_getvar2 = func_getvar2,
  265. .alloc_size = sizeof(struct instance)
  266. }, {
  267. .type = "compile_search::explode",
  268. .func_new2 = func_new_compiled,
  269. .func_die = func_die,
  270. .func_getvar2 = func_getvar2,
  271. .alloc_size = sizeof(struct instance)
  272. }, {
  273. .type = NULL
  274. }
  275. };
  276. const struct NCDModuleGroup ncdmodule_explode = {
  277. .modules = modules
  278. };