regex_test_lib.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * This file is part of GNUnet
  3. * Copyright (C) 2012 GNUnet e.V.
  4. *
  5. * GNUnet is free software: you can redistribute it and/or modify it
  6. * under the terms of the GNU Affero General Public License as published
  7. * by the Free Software Foundation, either version 3 of the License,
  8. * or (at your option) any later version.
  9. *
  10. * GNUnet is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. SPDX-License-Identifier: AGPL3.0-or-later
  18. */
  19. /**
  20. * @file src/regex/regex_test_lib.h
  21. * @brief library to read regexes representing IP networks from a file.
  22. * and simplifying the into one big regex, in order to run
  23. * tests (regex performance, regex profiler).
  24. * @author Bertlomiej Polot
  25. */
  26. #ifndef REGEX_INTERNAL_TEST_LIB_H
  27. #define REGEX_INTERNAL_TEST_LIB_H
  28. #include "regex_internal_lib.h"
  29. #ifdef __cplusplus
  30. extern "C"
  31. {
  32. #if 0 /* keep Emacsens' auto-indent happy */
  33. }
  34. #endif
  35. #endif
  36. /**
  37. * Combine an array of regexes into a single prefix-shared regex.
  38. * Returns a prefix-combine regex that matches the same strings as
  39. * any of the original regexes.
  40. *
  41. * WARNING: only useful for reading specific regexes for specific applications,
  42. * namely the gnunet-regex-profiler / gnunet-regex-daemon.
  43. * This function DOES NOT support arbitrary regex combining.
  44. *
  45. * @param regexes A NULL-terminated array of regexes.
  46. * @param alphabet_size Size of the alphabet the regex uses.
  47. *
  48. * @return A string with a single regex that matches any of the original regexes
  49. */
  50. char *
  51. REGEX_TEST_combine (char * const regexes[], unsigned int alphabet_size);
  52. /**
  53. * Read a set of regexes from a file, one per line and return them in an array
  54. * suitable for REGEX_TEST_combine.
  55. * The array must be free'd using REGEX_TEST_free_from_file.
  56. *
  57. * @param filename Name of the file containing the regexes.
  58. *
  59. * @return A newly allocated, NULL terminated array of regexes.
  60. */
  61. char **
  62. REGEX_TEST_read_from_file (const char *filename);
  63. /**
  64. * Free all memory reserved for a set of regexes created by read_from_file.
  65. *
  66. * @param regexes NULL-terminated array of regexes.
  67. */
  68. void
  69. REGEX_TEST_free_from_file (char **regexes);
  70. /**
  71. * Generate a (pseudo) random regular expression of length 'rx_length', as well
  72. * as a (optional) string that will be matched by the generated regex. The
  73. * returned regex needs to be freed.
  74. *
  75. * @param rx_length length of the random regex.
  76. * @param matching_str (optional) pointer to a string that will contain a string
  77. * that will be matched by the generated regex, if
  78. * 'matching_str' pointer was not NULL.
  79. *
  80. * @return NULL if 'rx_length' is 0, a random regex of length 'rx_length', which
  81. * needs to be freed, otherwise.
  82. */
  83. char *
  84. REGEX_TEST_generate_random_regex (size_t rx_length, char *matching_str);
  85. /**
  86. * Generate a random string of maximum length 'max_len' that only contains literals allowed
  87. * in a regular expression. The string might be 0 chars long but is garantueed
  88. * to be shorter or equal to 'max_len'.
  89. *
  90. * @param max_len maximum length of the string that should be generated.
  91. *
  92. * @return random string that needs to be freed.
  93. */
  94. char *
  95. REGEX_TEST_generate_random_string (size_t max_len);
  96. /**
  97. * Options for graph creation function
  98. * REGEX_TEST_automaton_save_graph.
  99. */
  100. enum REGEX_TEST_GraphSavingOptions
  101. {
  102. /**
  103. * Default. Do nothing special.
  104. */
  105. REGEX_TEST_GRAPH_DEFAULT = 0,
  106. /**
  107. * The generated graph will include extra information such as the NFA states
  108. * that were used to generate the DFA state.
  109. */
  110. REGEX_TEST_GRAPH_VERBOSE = 1,
  111. /**
  112. * Enable graph coloring. Will color each SCC in a different color.
  113. */
  114. REGEX_TEST_GRAPH_COLORING = 2
  115. };
  116. /**
  117. * Save the given automaton as a GraphViz dot file.
  118. *
  119. * @param a the automaton to be saved.
  120. * @param filename where to save the file.
  121. * @param options options for graph generation that include coloring or verbose
  122. * mode
  123. */
  124. void
  125. REGEX_TEST_automaton_save_graph (struct REGEX_INTERNAL_Automaton *a,
  126. const char *filename,
  127. enum REGEX_TEST_GraphSavingOptions options);
  128. #if 0 /* keep Emacsens' auto-indent happy */
  129. {
  130. #endif
  131. #ifdef __cplusplus
  132. }
  133. #endif
  134. /* end of regex_internal_lib.h */
  135. #endif