regex_internal.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /* Extended regular expression matching and search library.
  2. Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifndef _REGEX_INTERNAL_H
  17. #define _REGEX_INTERNAL_H 1
  18. #include <assert.h>
  19. #include <ctype.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #if defined HAVE_LANGINFO_H || defined HAVE_LANGINFO_CODESET
  23. # include <langinfo.h>
  24. #endif
  25. #if defined HAVE_LOCALE_H
  26. # include <locale.h>
  27. #endif
  28. #if defined HAVE_WCHAR_H
  29. # include <wchar.h>
  30. #endif
  31. #if defined HAVE_WCTYPE_H
  32. # include <wctype.h>
  33. #endif
  34. #if defined HAVE_STDBOOL_H
  35. # include <stdbool.h>
  36. #endif
  37. #if defined HAVE_STDINT_H
  38. # include <stdint.h>
  39. #endif
  40. #ifdef __UCLIBC_HAS_THREADS__
  41. #include <bits/libc-lock.h>
  42. #else
  43. #define __libc_lock_define(CLASS, NAME)
  44. #define __libc_lock_init(NAME) do { } while (0)
  45. #define __libc_lock_lock(NAME) do { } while (0)
  46. #define __libc_lock_unlock(NAME) do { } while (0)
  47. #endif
  48. #undef gettext
  49. #undef gettext_noop
  50. #define gettext(msgid) (msgid)
  51. #define gettext_noop(String) String
  52. #if (defined MB_CUR_MAX && HAVE_LOCALE_H && HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_WCRTOMB && HAVE_MBRTOWC && HAVE_WCSCOLL)
  53. # define RE_ENABLE_I18N
  54. #endif
  55. #if __GNUC__ >= 3
  56. /* uclibc: lean towards smaller size a bit:
  57. * OFF: # define BE(expr, val) __builtin_expect (expr, val) */
  58. # define BE(expr, val) (expr)
  59. #else
  60. # define BE(expr, val) (expr)
  61. # define inline
  62. #endif
  63. /* Number of single byte character. */
  64. #define SBC_MAX 256
  65. #define COLL_ELEM_LEN_MAX 8
  66. /* The character which represents newline. */
  67. #define NEWLINE_CHAR '\n'
  68. #define WIDE_NEWLINE_CHAR L'\n'
  69. #ifdef __GNUC__
  70. # define __attribute(arg) __attribute__ (arg)
  71. #else
  72. # define __attribute(arg)
  73. #endif
  74. /* An integer used to represent a set of bits. It must be unsigned,
  75. and must be at least as wide as unsigned int. */
  76. typedef unsigned long int bitset_word_t;
  77. /* All bits set in a bitset_word_t. */
  78. #define BITSET_WORD_MAX ULONG_MAX
  79. /* Number of bits in a bitset_word_t. */
  80. #define BITSET_WORD_BITS (sizeof (bitset_word_t) * CHAR_BIT)
  81. /* Number of bitset_word_t in a bit_set. */
  82. #define BITSET_WORDS (SBC_MAX / BITSET_WORD_BITS)
  83. typedef bitset_word_t bitset_t[BITSET_WORDS];
  84. typedef bitset_word_t *re_bitset_ptr_t;
  85. typedef const bitset_word_t *re_const_bitset_ptr_t;
  86. #define bitset_set(set,i) \
  87. (set[i / BITSET_WORD_BITS] |= (bitset_word_t) 1 << i % BITSET_WORD_BITS)
  88. #define bitset_clear(set,i) \
  89. (set[i / BITSET_WORD_BITS] &= ~((bitset_word_t) 1 << i % BITSET_WORD_BITS))
  90. #define bitset_contain(set,i) \
  91. (set[i / BITSET_WORD_BITS] & ((bitset_word_t) 1 << i % BITSET_WORD_BITS))
  92. #define bitset_empty(set) memset (set, '\0', sizeof (bitset_t))
  93. #define bitset_set_all(set) memset (set, '\xff', sizeof (bitset_t))
  94. #define bitset_copy(dest,src) memcpy (dest, src, sizeof (bitset_t))
  95. #define PREV_WORD_CONSTRAINT 0x0001
  96. #define PREV_NOTWORD_CONSTRAINT 0x0002
  97. #define NEXT_WORD_CONSTRAINT 0x0004
  98. #define NEXT_NOTWORD_CONSTRAINT 0x0008
  99. #define PREV_NEWLINE_CONSTRAINT 0x0010
  100. #define NEXT_NEWLINE_CONSTRAINT 0x0020
  101. #define PREV_BEGBUF_CONSTRAINT 0x0040
  102. #define NEXT_ENDBUF_CONSTRAINT 0x0080
  103. #define WORD_DELIM_CONSTRAINT 0x0100
  104. #define NOT_WORD_DELIM_CONSTRAINT 0x0200
  105. typedef enum
  106. {
  107. INSIDE_WORD = PREV_WORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
  108. WORD_FIRST = PREV_NOTWORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
  109. WORD_LAST = PREV_WORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
  110. INSIDE_NOTWORD = PREV_NOTWORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
  111. LINE_FIRST = PREV_NEWLINE_CONSTRAINT,
  112. LINE_LAST = NEXT_NEWLINE_CONSTRAINT,
  113. BUF_FIRST = PREV_BEGBUF_CONSTRAINT,
  114. BUF_LAST = NEXT_ENDBUF_CONSTRAINT,
  115. WORD_DELIM = WORD_DELIM_CONSTRAINT,
  116. NOT_WORD_DELIM = NOT_WORD_DELIM_CONSTRAINT
  117. } re_context_type;
  118. typedef struct
  119. {
  120. int alloc;
  121. int nelem;
  122. int *elems;
  123. } re_node_set;
  124. typedef enum
  125. {
  126. NON_TYPE = 0,
  127. /* Node type, These are used by token, node, tree. */
  128. CHARACTER = 1,
  129. END_OF_RE = 2,
  130. SIMPLE_BRACKET = 3,
  131. OP_BACK_REF = 4,
  132. OP_PERIOD = 5,
  133. #ifdef RE_ENABLE_I18N
  134. COMPLEX_BRACKET = 6,
  135. OP_UTF8_PERIOD = 7,
  136. #endif /* RE_ENABLE_I18N */
  137. /* We define EPSILON_BIT as a macro so that OP_OPEN_SUBEXP is used
  138. when the debugger shows values of this enum type. */
  139. #define EPSILON_BIT 8
  140. OP_OPEN_SUBEXP = EPSILON_BIT | 0,
  141. OP_CLOSE_SUBEXP = EPSILON_BIT | 1,
  142. OP_ALT = EPSILON_BIT | 2,
  143. OP_DUP_ASTERISK = EPSILON_BIT | 3,
  144. ANCHOR = EPSILON_BIT | 4,
  145. /* Tree type, these are used only by tree. */
  146. CONCAT = 16,
  147. SUBEXP = 17,
  148. /* Token type, these are used only by token. */
  149. OP_DUP_PLUS = 18,
  150. OP_DUP_QUESTION,
  151. OP_OPEN_BRACKET,
  152. OP_CLOSE_BRACKET,
  153. OP_CHARSET_RANGE,
  154. OP_OPEN_DUP_NUM,
  155. OP_CLOSE_DUP_NUM,
  156. OP_NON_MATCH_LIST,
  157. OP_OPEN_COLL_ELEM,
  158. OP_CLOSE_COLL_ELEM,
  159. OP_OPEN_EQUIV_CLASS,
  160. OP_CLOSE_EQUIV_CLASS,
  161. OP_OPEN_CHAR_CLASS,
  162. OP_CLOSE_CHAR_CLASS,
  163. OP_WORD,
  164. OP_NOTWORD,
  165. OP_SPACE,
  166. OP_NOTSPACE,
  167. BACK_SLASH
  168. } re_token_type_t;
  169. #ifdef RE_ENABLE_I18N
  170. typedef struct
  171. {
  172. /* Multibyte characters. */
  173. wchar_t *mbchars;
  174. /* Collating symbols. */
  175. # if 0
  176. int32_t *coll_syms;
  177. # endif
  178. /* Equivalence classes. */
  179. # if 0
  180. int32_t *equiv_classes;
  181. # endif
  182. /* Range expressions. */
  183. # if 0
  184. uint32_t *range_starts;
  185. uint32_t *range_ends;
  186. # else
  187. wchar_t *range_starts;
  188. wchar_t *range_ends;
  189. # endif
  190. /* Character classes. */
  191. wctype_t *char_classes;
  192. /* If this character set is the non-matching list. */
  193. unsigned int non_match : 1;
  194. /* # of multibyte characters. */
  195. int nmbchars;
  196. /* # of collating symbols. */
  197. int ncoll_syms;
  198. /* # of equivalence classes. */
  199. int nequiv_classes;
  200. /* # of range expressions. */
  201. int nranges;
  202. /* # of character classes. */
  203. int nchar_classes;
  204. } re_charset_t;
  205. #endif /* RE_ENABLE_I18N */
  206. typedef struct
  207. {
  208. union
  209. {
  210. unsigned char c; /* for CHARACTER */
  211. re_bitset_ptr_t sbcset; /* for SIMPLE_BRACKET */
  212. #ifdef RE_ENABLE_I18N
  213. re_charset_t *mbcset; /* for COMPLEX_BRACKET */
  214. #endif /* RE_ENABLE_I18N */
  215. int idx; /* for BACK_REF */
  216. re_context_type ctx_type; /* for ANCHOR */
  217. } opr;
  218. #if __GNUC__ >= 2
  219. re_token_type_t type : 8;
  220. #else
  221. re_token_type_t type;
  222. #endif
  223. unsigned int constraint : 10; /* context constraint */
  224. unsigned int duplicated : 1;
  225. unsigned int opt_subexp : 1;
  226. #ifdef RE_ENABLE_I18N
  227. unsigned int accept_mb : 1;
  228. /* These 2 bits can be moved into the union if needed (e.g. if running out
  229. of bits; move opr.c to opr.c.c and move the flags to opr.c.flags). */
  230. unsigned int mb_partial : 1;
  231. #endif
  232. unsigned int word_char : 1;
  233. } re_token_t;
  234. #define IS_EPSILON_NODE(type) ((type) & EPSILON_BIT)
  235. struct re_string_t
  236. {
  237. /* Indicate the raw buffer which is the original string passed as an
  238. argument of regexec(), re_search(), etc.. */
  239. const unsigned char *raw_mbs;
  240. /* Store the multibyte string. In case of "case insensitive mode" like
  241. REG_ICASE, upper cases of the string are stored, otherwise MBS points
  242. the same address that RAW_MBS points. */
  243. unsigned char *mbs;
  244. #ifdef RE_ENABLE_I18N
  245. /* Store the wide character string which is corresponding to MBS. */
  246. wint_t *wcs;
  247. int *offsets;
  248. mbstate_t cur_state;
  249. #endif
  250. /* Index in RAW_MBS. Each character mbs[i] corresponds to
  251. raw_mbs[raw_mbs_idx + i]. */
  252. int raw_mbs_idx;
  253. /* The length of the valid characters in the buffers. */
  254. int valid_len;
  255. /* The corresponding number of bytes in raw_mbs array. */
  256. int valid_raw_len;
  257. /* The length of the buffers MBS and WCS. */
  258. int bufs_len;
  259. /* The index in MBS, which is updated by re_string_fetch_byte. */
  260. int cur_idx;
  261. /* length of RAW_MBS array. */
  262. int raw_len;
  263. /* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN. */
  264. int len;
  265. /* End of the buffer may be shorter than its length in the cases such
  266. as re_match_2, re_search_2. Then, we use STOP for end of the buffer
  267. instead of LEN. */
  268. int raw_stop;
  269. /* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS. */
  270. int stop;
  271. /* The context of mbs[0]. We store the context independently, since
  272. the context of mbs[0] may be different from raw_mbs[0], which is
  273. the beginning of the input string. */
  274. unsigned int tip_context;
  275. /* The translation passed as a part of an argument of re_compile_pattern. */
  276. __RE_TRANSLATE_TYPE trans;
  277. /* Copy of re_dfa_t's word_char. */
  278. re_const_bitset_ptr_t word_char;
  279. /* 1 if REG_ICASE. */
  280. unsigned char icase;
  281. unsigned char is_utf8;
  282. unsigned char map_notascii;
  283. unsigned char mbs_allocated;
  284. unsigned char offsets_needed;
  285. unsigned char newline_anchor;
  286. unsigned char word_ops_used;
  287. int mb_cur_max;
  288. };
  289. typedef struct re_string_t re_string_t;
  290. struct re_dfa_t;
  291. typedef struct re_dfa_t re_dfa_t;
  292. static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr,
  293. int new_buf_len)
  294. internal_function;
  295. #ifdef RE_ENABLE_I18N
  296. static void build_wcs_buffer (re_string_t *pstr) internal_function;
  297. static reg_errcode_t build_wcs_upper_buffer (re_string_t *pstr) internal_function;
  298. #endif /* RE_ENABLE_I18N */
  299. static void build_upper_buffer (re_string_t *pstr) internal_function;
  300. static void re_string_translate_buffer (re_string_t *pstr) internal_function;
  301. static unsigned int re_string_context_at (const re_string_t *input, int idx,
  302. int eflags)
  303. internal_function __attribute ((pure));
  304. #define re_string_peek_byte(pstr, offset) \
  305. ((pstr)->mbs[(pstr)->cur_idx + offset])
  306. #define re_string_fetch_byte(pstr) \
  307. ((pstr)->mbs[(pstr)->cur_idx++])
  308. #define re_string_first_byte(pstr, idx) \
  309. ((idx) == (pstr)->valid_len || (pstr)->wcs[idx] != WEOF)
  310. #define re_string_is_single_byte_char(pstr, idx) \
  311. ((pstr)->wcs[idx] != WEOF && ((pstr)->valid_len == (idx) + 1 \
  312. || (pstr)->wcs[(idx) + 1] != WEOF))
  313. #define re_string_eoi(pstr) ((pstr)->stop <= (pstr)->cur_idx)
  314. #define re_string_cur_idx(pstr) ((pstr)->cur_idx)
  315. #define re_string_get_buffer(pstr) ((pstr)->mbs)
  316. #define re_string_length(pstr) ((pstr)->len)
  317. #define re_string_byte_at(pstr,idx) ((pstr)->mbs[idx])
  318. #define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
  319. #define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
  320. #include <alloca.h>
  321. #if 1
  322. # ifdef HAVE_ALLOCA
  323. /* The OS usually guarantees only one guard page at the bottom of the stack,
  324. and a page size can be as small as 4096 bytes. So we cannot safely
  325. allocate anything larger than 4096 bytes. Also care for the possibility
  326. of a few compiler-allocated temporary stack slots. */
  327. # define __libc_use_alloca(n) ((n) < 4032)
  328. # else
  329. /* alloca is implemented with malloc, so just use malloc. */
  330. # define __libc_use_alloca(n) 0
  331. # endif
  332. #endif
  333. #define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
  334. #define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t)))
  335. #define re_free(p) free (p)
  336. struct bin_tree_t
  337. {
  338. struct bin_tree_t *parent;
  339. struct bin_tree_t *left;
  340. struct bin_tree_t *right;
  341. struct bin_tree_t *first;
  342. struct bin_tree_t *next;
  343. re_token_t token;
  344. /* `node_idx' is the index in dfa->nodes, if `type' == 0.
  345. Otherwise `type' indicate the type of this node. */
  346. int node_idx;
  347. };
  348. typedef struct bin_tree_t bin_tree_t;
  349. #define BIN_TREE_STORAGE_SIZE \
  350. ((1024 - sizeof (void *)) / sizeof (bin_tree_t))
  351. struct bin_tree_storage_t
  352. {
  353. struct bin_tree_storage_t *next;
  354. bin_tree_t data[BIN_TREE_STORAGE_SIZE];
  355. };
  356. typedef struct bin_tree_storage_t bin_tree_storage_t;
  357. #define CONTEXT_WORD 1
  358. #define CONTEXT_NEWLINE (CONTEXT_WORD << 1)
  359. #define CONTEXT_BEGBUF (CONTEXT_NEWLINE << 1)
  360. #define CONTEXT_ENDBUF (CONTEXT_BEGBUF << 1)
  361. #define IS_WORD_CONTEXT(c) ((c) & CONTEXT_WORD)
  362. #define IS_NEWLINE_CONTEXT(c) ((c) & CONTEXT_NEWLINE)
  363. #define IS_BEGBUF_CONTEXT(c) ((c) & CONTEXT_BEGBUF)
  364. #define IS_ENDBUF_CONTEXT(c) ((c) & CONTEXT_ENDBUF)
  365. #define IS_ORDINARY_CONTEXT(c) ((c) == 0)
  366. #define IS_WORD_CHAR(ch) (isalnum (ch) || (ch) == '_')
  367. #define IS_NEWLINE(ch) ((ch) == NEWLINE_CHAR)
  368. #define IS_WIDE_WORD_CHAR(ch) (iswalnum (ch) || (ch) == L'_')
  369. #define IS_WIDE_NEWLINE(ch) ((ch) == WIDE_NEWLINE_CHAR)
  370. #define NOT_SATISFY_PREV_CONSTRAINT(constraint,context) \
  371. ((((constraint) & PREV_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
  372. || ((constraint & PREV_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
  373. || ((constraint & PREV_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context))\
  374. || ((constraint & PREV_BEGBUF_CONSTRAINT) && !IS_BEGBUF_CONTEXT (context)))
  375. #define NOT_SATISFY_NEXT_CONSTRAINT(constraint,context) \
  376. ((((constraint) & NEXT_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
  377. || (((constraint) & NEXT_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
  378. || (((constraint) & NEXT_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context)) \
  379. || (((constraint) & NEXT_ENDBUF_CONSTRAINT) && !IS_ENDBUF_CONTEXT (context)))
  380. struct re_dfastate_t
  381. {
  382. unsigned int hash;
  383. re_node_set nodes;
  384. re_node_set non_eps_nodes;
  385. re_node_set inveclosure;
  386. re_node_set *entrance_nodes;
  387. struct re_dfastate_t **trtable, **word_trtable;
  388. unsigned int context : 4;
  389. unsigned int halt : 1;
  390. /* If this state can accept `multi byte'.
  391. Note that we refer to multibyte characters, and multi character
  392. collating elements as `multi byte'. */
  393. unsigned int accept_mb : 1;
  394. /* If this state has backreference node(s). */
  395. unsigned int has_backref : 1;
  396. unsigned int has_constraint : 1;
  397. };
  398. typedef struct re_dfastate_t re_dfastate_t;
  399. struct re_state_table_entry
  400. {
  401. int num;
  402. int alloc;
  403. re_dfastate_t **array;
  404. };
  405. /* Array type used in re_sub_match_last_t and re_sub_match_top_t. */
  406. typedef struct
  407. {
  408. int next_idx;
  409. int alloc;
  410. re_dfastate_t **array;
  411. } state_array_t;
  412. /* Store information about the node NODE whose type is OP_CLOSE_SUBEXP. */
  413. typedef struct
  414. {
  415. int node;
  416. int str_idx; /* The position NODE match at. */
  417. state_array_t path;
  418. } re_sub_match_last_t;
  419. /* Store information about the node NODE whose type is OP_OPEN_SUBEXP.
  420. And information about the node, whose type is OP_CLOSE_SUBEXP,
  421. corresponding to NODE is stored in LASTS. */
  422. typedef struct
  423. {
  424. int str_idx;
  425. int node;
  426. state_array_t *path;
  427. int alasts; /* Allocation size of LASTS. */
  428. int nlasts; /* The number of LASTS. */
  429. re_sub_match_last_t **lasts;
  430. } re_sub_match_top_t;
  431. struct re_backref_cache_entry
  432. {
  433. int node;
  434. int str_idx;
  435. int subexp_from;
  436. int subexp_to;
  437. char more;
  438. char unused;
  439. unsigned short int eps_reachable_subexps_map;
  440. };
  441. typedef struct
  442. {
  443. /* The string object corresponding to the input string. */
  444. re_string_t input;
  445. const re_dfa_t *dfa;
  446. /* EFLAGS of the argument of regexec. */
  447. int eflags;
  448. /* Where the matching ends. */
  449. int match_last;
  450. int last_node;
  451. /* The state log used by the matcher. */
  452. re_dfastate_t **state_log;
  453. int state_log_top;
  454. /* Back reference cache. */
  455. int nbkref_ents;
  456. int abkref_ents;
  457. struct re_backref_cache_entry *bkref_ents;
  458. int max_mb_elem_len;
  459. int nsub_tops;
  460. int asub_tops;
  461. re_sub_match_top_t **sub_tops;
  462. } re_match_context_t;
  463. typedef struct
  464. {
  465. re_dfastate_t **sifted_states;
  466. re_dfastate_t **limited_states;
  467. int last_node;
  468. int last_str_idx;
  469. re_node_set limits;
  470. } re_sift_context_t;
  471. struct re_fail_stack_ent_t
  472. {
  473. int idx;
  474. int node;
  475. regmatch_t *regs;
  476. re_node_set eps_via_nodes;
  477. };
  478. struct re_fail_stack_t
  479. {
  480. int num;
  481. int alloc;
  482. struct re_fail_stack_ent_t *stack;
  483. };
  484. struct re_dfa_t
  485. {
  486. re_token_t *nodes;
  487. size_t nodes_alloc;
  488. size_t nodes_len;
  489. int *nexts;
  490. int *org_indices;
  491. re_node_set *edests;
  492. re_node_set *eclosures;
  493. re_node_set *inveclosures;
  494. struct re_state_table_entry *state_table;
  495. re_dfastate_t *init_state;
  496. re_dfastate_t *init_state_word;
  497. re_dfastate_t *init_state_nl;
  498. re_dfastate_t *init_state_begbuf;
  499. bin_tree_t *str_tree;
  500. bin_tree_storage_t *str_tree_storage;
  501. re_bitset_ptr_t sb_char;
  502. int str_tree_storage_idx;
  503. /* number of subexpressions `re_nsub' is in regex_t. */
  504. unsigned int state_hash_mask;
  505. int init_node;
  506. int nbackref; /* The number of backreference in this dfa. */
  507. /* Bitmap expressing which backreference is used. */
  508. bitset_word_t used_bkref_map;
  509. bitset_word_t completed_bkref_map;
  510. unsigned int has_plural_match : 1;
  511. /* If this dfa has "multibyte node", which is a backreference or
  512. a node which can accept multibyte character or multi character
  513. collating element. */
  514. unsigned int has_mb_node : 1;
  515. unsigned int is_utf8 : 1;
  516. unsigned int map_notascii : 1;
  517. unsigned int word_ops_used : 1;
  518. int mb_cur_max;
  519. bitset_t word_char;
  520. reg_syntax_t syntax;
  521. int *subexp_map;
  522. #ifdef DEBUG
  523. char* re_str;
  524. #endif
  525. __libc_lock_define (, lock)
  526. };
  527. #define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
  528. #define re_node_set_remove(set,id) \
  529. (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
  530. #define re_node_set_empty(p) ((p)->nelem = 0)
  531. #define re_node_set_free(set) re_free ((set)->elems)
  532. typedef enum
  533. {
  534. SB_CHAR,
  535. MB_CHAR,
  536. EQUIV_CLASS,
  537. COLL_SYM,
  538. CHAR_CLASS
  539. } bracket_elem_type;
  540. typedef struct
  541. {
  542. bracket_elem_type type;
  543. union
  544. {
  545. unsigned char ch;
  546. unsigned char *name;
  547. #ifdef __UCLIBC_HAS_WCHAR__
  548. wchar_t wch;
  549. #endif
  550. } opr;
  551. } bracket_elem_t;
  552. /* Inline functions for bitset operation. */
  553. static __inline__ void
  554. bitset_not (bitset_t set)
  555. {
  556. int bitset_i;
  557. for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
  558. set[bitset_i] = ~set[bitset_i];
  559. }
  560. static __inline__ void
  561. bitset_merge (bitset_t dest, const bitset_t src)
  562. {
  563. int bitset_i;
  564. for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
  565. dest[bitset_i] |= src[bitset_i];
  566. }
  567. static __inline__ void
  568. bitset_mask (bitset_t dest, const bitset_t src)
  569. {
  570. int bitset_i;
  571. for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
  572. dest[bitset_i] &= src[bitset_i];
  573. }
  574. #ifdef RE_ENABLE_I18N
  575. /* Inline functions for re_string. */
  576. static __inline__ int
  577. internal_function __attribute ((pure))
  578. re_string_char_size_at (const re_string_t *pstr, int idx)
  579. {
  580. int byte_idx;
  581. if (pstr->mb_cur_max == 1)
  582. return 1;
  583. for (byte_idx = 1; idx + byte_idx < pstr->valid_len; ++byte_idx)
  584. if (pstr->wcs[idx + byte_idx] != WEOF)
  585. break;
  586. return byte_idx;
  587. }
  588. static __inline__ wint_t
  589. internal_function __attribute ((pure))
  590. re_string_wchar_at (const re_string_t *pstr, int idx)
  591. {
  592. if (pstr->mb_cur_max == 1)
  593. return (wint_t) pstr->mbs[idx];
  594. return (wint_t) pstr->wcs[idx];
  595. }
  596. static int
  597. internal_function __attribute ((pure))
  598. re_string_elem_size_at (const re_string_t *pstr, int idx)
  599. {
  600. # if 0
  601. const unsigned char *p, *extra;
  602. const int32_t *table, *indirect;
  603. int32_t tmp;
  604. # include <locale/weight.h>
  605. uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
  606. if (nrules != 0)
  607. {
  608. table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
  609. extra = (const unsigned char *)
  610. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
  611. indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
  612. _NL_COLLATE_INDIRECTMB);
  613. p = pstr->mbs + idx;
  614. tmp = findidx (&p);
  615. return p - pstr->mbs - idx;
  616. }
  617. # endif
  618. return 1;
  619. }
  620. #endif /* RE_ENABLE_I18N */
  621. #endif /* _REGEX_INTERNAL_H */