explode.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. struct ExpArray arr;
  74. size_t num;
  75. };
  76. static void compile_search_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  77. {
  78. struct compile_search_instance *o = vo;
  79. o->i = i;
  80. NCDValRef str_arg;
  81. if (!NCDVal_ListRead(params->args, 1, &str_arg)) {
  82. ModuleLog(i, BLOG_ERROR, "wrong arity");
  83. goto fail0;
  84. }
  85. if (!NCDVal_IsString(str_arg)) {
  86. ModuleLog(i, BLOG_ERROR, "wrong type");
  87. goto fail0;
  88. }
  89. o->str = NCDVal_StringMemRef(str_arg);
  90. if (o->str.len == 0) {
  91. ModuleLog(i, BLOG_ERROR, "string must be nonempty");
  92. goto fail0;
  93. }
  94. o->table = BAllocArray(o->str.len, sizeof(o->table[0]));
  95. if (!o->table) {
  96. ModuleLog(i, BLOG_ERROR, "BAllocArray failed");
  97. goto fail0;
  98. }
  99. build_substring_backtrack_table(o->str, o->table);
  100. NCDModuleInst_Backend_Up(i);
  101. return;
  102. fail0:
  103. NCDModuleInst_Backend_DeadError(i);
  104. }
  105. static void compile_search_die (void *vo)
  106. {
  107. struct compile_search_instance *o = vo;
  108. BFree(o->table);
  109. NCDModuleInst_Backend_Dead(o->i);
  110. }
  111. static void func_new_common (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, struct compile_search_instance *compiled)
  112. {
  113. struct instance *o = vo;
  114. o->i = i;
  115. int arg_start;
  116. MemRef del;
  117. size_t const *table;
  118. if (compiled) {
  119. arg_start = 0;
  120. del = compiled->str;
  121. table = compiled->table;
  122. } else {
  123. NCDValRef delimiter_arg;
  124. if (!NCDVal_ListReadHead(params->args, 1, &delimiter_arg)) {
  125. ModuleLog(i, BLOG_ERROR, "missing delimiter argument");
  126. goto fail0;
  127. }
  128. if (!NCDVal_IsString(delimiter_arg)) {
  129. ModuleLog(i, BLOG_ERROR, "wrong delimiter type");
  130. goto fail0;
  131. }
  132. arg_start = 1;
  133. del = NCDVal_StringMemRef(delimiter_arg);
  134. if (del.len == 0) {
  135. ModuleLog(i, BLOG_ERROR, "delimiter must be nonempty");
  136. goto fail0;
  137. }
  138. table = BAllocArray(del.len, sizeof(table[0]));
  139. if (!table) {
  140. ModuleLog(i, BLOG_ERROR, "ExpArray_init failed");
  141. goto fail0;
  142. }
  143. build_substring_backtrack_table(del, (size_t *)table);
  144. }
  145. // read arguments
  146. NCDValRef input_arg;
  147. NCDValRef limit_arg = NCDVal_NewInvalid();
  148. if (!NCDVal_ListReadStart(params->args, arg_start, 1, &input_arg) && !NCDVal_ListReadStart(params->args, arg_start, 2, &input_arg, &limit_arg)) {
  149. ModuleLog(i, BLOG_ERROR, "wrong arity");
  150. goto fail1;
  151. }
  152. if (!NCDVal_IsString(input_arg)) {
  153. ModuleLog(i, BLOG_ERROR, "wrong type");
  154. goto fail1;
  155. }
  156. size_t limit = SIZE_MAX;
  157. if (!NCDVal_IsInvalid(limit_arg)) {
  158. uintmax_t n;
  159. if (!ncd_read_uintmax(limit_arg, &n) || n == 0) {
  160. ModuleLog(i, BLOG_ERROR, "bad limit argument");
  161. goto fail1;
  162. }
  163. n--;
  164. limit = (n <= SIZE_MAX ? n : SIZE_MAX);
  165. }
  166. if (!ExpArray_init(&o->arr, sizeof(MemRef), 8)) {
  167. ModuleLog(i, BLOG_ERROR, "ExpArray_init failed");
  168. goto fail1;
  169. }
  170. o->num = 0;
  171. MemRef data = NCDVal_StringMemRef(input_arg);
  172. while (1) {
  173. size_t start;
  174. int is_end = 0;
  175. if (limit == 0 || !find_substring(data, del, table, &start)) {
  176. start = data.len;
  177. is_end = 1;
  178. }
  179. if (!ExpArray_resize(&o->arr, o->num + 1)) {
  180. ModuleLog(i, BLOG_ERROR, "ExpArray_init failed");
  181. goto fail2;
  182. }
  183. ((MemRef *)o->arr.v)[o->num] = MemRef_SubTo(data, start);
  184. o->num++;
  185. if (is_end) {
  186. break;
  187. }
  188. data = MemRef_SubFrom(data, start + del.len);
  189. limit--;
  190. }
  191. if (!compiled) {
  192. BFree((size_t *)table);
  193. }
  194. // signal up
  195. NCDModuleInst_Backend_Up(i);
  196. return;
  197. fail2:
  198. free(o->arr.v);
  199. fail1:
  200. if (!compiled) {
  201. BFree((size_t *)table);
  202. }
  203. fail0:
  204. NCDModuleInst_Backend_DeadError(i);
  205. }
  206. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  207. {
  208. return func_new_common(vo, i, params, NULL);
  209. }
  210. static void func_new_compiled (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  211. {
  212. struct compile_search_instance *compiled = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  213. return func_new_common(vo, i, params, compiled);
  214. }
  215. static void func_die (void *vo)
  216. {
  217. struct instance *o = vo;
  218. free(o->arr.v);
  219. NCDModuleInst_Backend_Dead(o->i);
  220. }
  221. static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  222. {
  223. struct instance *o = vo;
  224. if (name == NCD_STRING_EMPTY) {
  225. *out = NCDVal_NewList(mem, o->num);
  226. if (NCDVal_IsInvalid(*out)) {
  227. goto fail;
  228. }
  229. for (size_t j = 0; j < o->num; j++) {
  230. MemRef elem = ((MemRef *)o->arr.v)[j];
  231. NCDValRef str = NCDVal_NewStringBinMr(mem, elem);
  232. if (NCDVal_IsInvalid(str)) {
  233. goto fail;
  234. }
  235. if (!NCDVal_ListAppend(*out, str)) {
  236. goto fail;
  237. }
  238. }
  239. return 1;
  240. }
  241. return 0;
  242. fail:
  243. *out = NCDVal_NewInvalid();
  244. return 1;
  245. }
  246. static struct NCDModule modules[] = {
  247. {
  248. .type = "compile_search",
  249. .func_new2 = compile_search_new,
  250. .func_die = compile_search_die,
  251. .alloc_size = sizeof(struct compile_search_instance)
  252. }, {
  253. .type = "explode",
  254. .func_new2 = func_new,
  255. .func_die = func_die,
  256. .func_getvar2 = func_getvar2,
  257. .alloc_size = sizeof(struct instance)
  258. }, {
  259. .type = "compile_search::explode",
  260. .func_new2 = func_new_compiled,
  261. .func_die = func_die,
  262. .func_getvar2 = func_getvar2,
  263. .alloc_size = sizeof(struct instance)
  264. }, {
  265. .type = NULL
  266. }
  267. };
  268. const struct NCDModuleGroup ncdmodule_explode = {
  269. .modules = modules
  270. };