regex_internal.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  1. /* Extended regular expression matching and search library.
  2. Copyright (C) 2002, 2003, 2004, 2005, 2006 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. static void re_string_construct_common (const char *str, int len,
  17. re_string_t *pstr,
  18. __RE_TRANSLATE_TYPE trans, int icase,
  19. const re_dfa_t *dfa) internal_function;
  20. static re_dfastate_t *create_ci_newstate (const re_dfa_t *dfa,
  21. const re_node_set *nodes,
  22. unsigned int hash) internal_function;
  23. static re_dfastate_t *create_cd_newstate (const re_dfa_t *dfa,
  24. const re_node_set *nodes,
  25. unsigned int context,
  26. unsigned int hash) internal_function;
  27. /* Functions for string operation. */
  28. /* This function allocate the buffers. It is necessary to call
  29. re_string_reconstruct before using the object. */
  30. static reg_errcode_t
  31. internal_function
  32. re_string_allocate (re_string_t *pstr, const char *str, int len, int init_len,
  33. __RE_TRANSLATE_TYPE trans, int icase, const re_dfa_t *dfa)
  34. {
  35. reg_errcode_t ret;
  36. int init_buf_len;
  37. /* Ensure at least one character fits into the buffers. */
  38. if (init_len < dfa->mb_cur_max)
  39. init_len = dfa->mb_cur_max;
  40. init_buf_len = (len + 1 < init_len) ? len + 1: init_len;
  41. re_string_construct_common (str, len, pstr, trans, icase, dfa);
  42. ret = re_string_realloc_buffers (pstr, init_buf_len);
  43. if (BE (ret != REG_NOERROR, 0))
  44. return ret;
  45. pstr->word_char = dfa->word_char;
  46. pstr->word_ops_used = dfa->word_ops_used;
  47. pstr->mbs = pstr->mbs_allocated ? pstr->mbs : (unsigned char *) str;
  48. pstr->valid_len = (pstr->mbs_allocated || dfa->mb_cur_max > 1) ? 0 : len;
  49. pstr->valid_raw_len = pstr->valid_len;
  50. return REG_NOERROR;
  51. }
  52. /* This function allocate the buffers, and initialize them. */
  53. static reg_errcode_t
  54. internal_function
  55. re_string_construct (re_string_t *pstr, const char *str, int len,
  56. __RE_TRANSLATE_TYPE trans, int icase, const re_dfa_t *dfa)
  57. {
  58. reg_errcode_t ret;
  59. memset (pstr, '\0', sizeof (re_string_t));
  60. re_string_construct_common (str, len, pstr, trans, icase, dfa);
  61. if (len > 0)
  62. {
  63. ret = re_string_realloc_buffers (pstr, len + 1);
  64. if (BE (ret != REG_NOERROR, 0))
  65. return ret;
  66. }
  67. pstr->mbs = pstr->mbs_allocated ? pstr->mbs : (unsigned char *) str;
  68. if (icase)
  69. {
  70. #ifdef RE_ENABLE_I18N
  71. if (dfa->mb_cur_max > 1)
  72. {
  73. while (1)
  74. {
  75. ret = build_wcs_upper_buffer (pstr);
  76. if (BE (ret != REG_NOERROR, 0))
  77. return ret;
  78. if (pstr->valid_raw_len >= len)
  79. break;
  80. if (pstr->bufs_len > pstr->valid_len + dfa->mb_cur_max)
  81. break;
  82. ret = re_string_realloc_buffers (pstr, pstr->bufs_len * 2);
  83. if (BE (ret != REG_NOERROR, 0))
  84. return ret;
  85. }
  86. }
  87. else
  88. #endif /* RE_ENABLE_I18N */
  89. build_upper_buffer (pstr);
  90. }
  91. else
  92. {
  93. #ifdef RE_ENABLE_I18N
  94. if (dfa->mb_cur_max > 1)
  95. build_wcs_buffer (pstr);
  96. else
  97. #endif
  98. {
  99. if (trans != NULL)
  100. re_string_translate_buffer (pstr);
  101. else
  102. {
  103. pstr->valid_len = pstr->bufs_len;
  104. pstr->valid_raw_len = pstr->bufs_len;
  105. }
  106. }
  107. }
  108. return REG_NOERROR;
  109. }
  110. /* Helper functions for re_string_allocate, and re_string_construct. */
  111. static reg_errcode_t
  112. internal_function
  113. re_string_realloc_buffers (re_string_t *pstr, int new_buf_len)
  114. {
  115. #ifdef RE_ENABLE_I18N
  116. if (pstr->mb_cur_max > 1)
  117. {
  118. wint_t *new_wcs = re_realloc (pstr->wcs, wint_t, new_buf_len);
  119. if (BE (new_wcs == NULL, 0))
  120. return REG_ESPACE;
  121. pstr->wcs = new_wcs;
  122. if (pstr->offsets != NULL)
  123. {
  124. int *new_offsets = re_realloc (pstr->offsets, int, new_buf_len);
  125. if (BE (new_offsets == NULL, 0))
  126. return REG_ESPACE;
  127. pstr->offsets = new_offsets;
  128. }
  129. }
  130. #endif /* RE_ENABLE_I18N */
  131. if (pstr->mbs_allocated)
  132. {
  133. unsigned char *new_mbs = re_realloc (pstr->mbs, unsigned char,
  134. new_buf_len);
  135. if (BE (new_mbs == NULL, 0))
  136. return REG_ESPACE;
  137. pstr->mbs = new_mbs;
  138. }
  139. pstr->bufs_len = new_buf_len;
  140. return REG_NOERROR;
  141. }
  142. static void
  143. internal_function
  144. re_string_construct_common (const char *str, int len, re_string_t *pstr,
  145. __RE_TRANSLATE_TYPE trans, int icase,
  146. const re_dfa_t *dfa)
  147. {
  148. pstr->raw_mbs = (const unsigned char *) str;
  149. pstr->len = len;
  150. pstr->raw_len = len;
  151. pstr->trans = trans;
  152. pstr->icase = icase ? 1 : 0;
  153. pstr->mbs_allocated = (trans != NULL || icase);
  154. pstr->mb_cur_max = dfa->mb_cur_max;
  155. pstr->is_utf8 = dfa->is_utf8;
  156. pstr->map_notascii = dfa->map_notascii;
  157. pstr->stop = pstr->len;
  158. pstr->raw_stop = pstr->stop;
  159. }
  160. #ifdef RE_ENABLE_I18N
  161. /* Build wide character buffer PSTR->WCS.
  162. If the byte sequence of the string are:
  163. <mb1>(0), <mb1>(1), <mb2>(0), <mb2>(1), <sb3>
  164. Then wide character buffer will be:
  165. <wc1> , WEOF , <wc2> , WEOF , <wc3>
  166. We use WEOF for padding, they indicate that the position isn't
  167. a first byte of a multibyte character.
  168. Note that this function assumes PSTR->VALID_LEN elements are already
  169. built and starts from PSTR->VALID_LEN. */
  170. static void
  171. internal_function
  172. build_wcs_buffer (re_string_t *pstr)
  173. {
  174. #if defined __UCLIBC__
  175. unsigned char buf[MB_LEN_MAX];
  176. assert (MB_LEN_MAX >= pstr->mb_cur_max);
  177. #else
  178. unsigned char buf[64];
  179. #endif
  180. mbstate_t prev_st;
  181. int byte_idx, end_idx, remain_len;
  182. size_t mbclen;
  183. /* Build the buffers from pstr->valid_len to either pstr->len or
  184. pstr->bufs_len. */
  185. end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
  186. for (byte_idx = pstr->valid_len; byte_idx < end_idx;)
  187. {
  188. wchar_t wc;
  189. const char *p;
  190. remain_len = end_idx - byte_idx;
  191. prev_st = pstr->cur_state;
  192. /* Apply the translation if we need. */
  193. if (BE (pstr->trans != NULL, 0))
  194. {
  195. int i, ch;
  196. for (i = 0; i < pstr->mb_cur_max && i < remain_len; ++i)
  197. {
  198. ch = pstr->raw_mbs [pstr->raw_mbs_idx + byte_idx + i];
  199. buf[i] = pstr->mbs[byte_idx + i] = pstr->trans[ch];
  200. }
  201. p = (const char *) buf;
  202. }
  203. else
  204. p = (const char *) pstr->raw_mbs + pstr->raw_mbs_idx + byte_idx;
  205. mbclen = mbrtowc (&wc, p, remain_len, &pstr->cur_state);
  206. if (BE (mbclen == (size_t) -2, 0))
  207. {
  208. /* The buffer doesn't have enough space, finish to build. */
  209. pstr->cur_state = prev_st;
  210. break;
  211. }
  212. else if (BE (mbclen == (size_t) -1 || mbclen == 0, 0))
  213. {
  214. /* We treat these cases as a singlebyte character. */
  215. mbclen = 1;
  216. wc = (wchar_t) pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx];
  217. if (BE (pstr->trans != NULL, 0))
  218. wc = pstr->trans[wc];
  219. pstr->cur_state = prev_st;
  220. }
  221. /* Write wide character and padding. */
  222. pstr->wcs[byte_idx++] = wc;
  223. /* Write paddings. */
  224. for (remain_len = byte_idx + mbclen - 1; byte_idx < remain_len ;)
  225. pstr->wcs[byte_idx++] = WEOF;
  226. }
  227. pstr->valid_len = byte_idx;
  228. pstr->valid_raw_len = byte_idx;
  229. }
  230. /* Build wide character buffer PSTR->WCS like build_wcs_buffer,
  231. but for REG_ICASE. */
  232. static reg_errcode_t
  233. internal_function
  234. build_wcs_upper_buffer (re_string_t *pstr)
  235. {
  236. mbstate_t prev_st;
  237. int src_idx, byte_idx, end_idx, remain_len;
  238. size_t mbclen;
  239. #if defined __UCLIBC__
  240. char buf[MB_LEN_MAX];
  241. assert (MB_LEN_MAX >= pstr->mb_cur_max);
  242. #else
  243. char buf[64];
  244. #endif
  245. byte_idx = pstr->valid_len;
  246. end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
  247. /* The following optimization assumes that ASCII characters can be
  248. mapped to wide characters with a simple cast. */
  249. if (! pstr->map_notascii && pstr->trans == NULL && !pstr->offsets_needed)
  250. {
  251. while (byte_idx < end_idx)
  252. {
  253. wchar_t wc;
  254. if (isascii (pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx])
  255. && mbsinit (&pstr->cur_state))
  256. {
  257. /* In case of a singlebyte character. */
  258. pstr->mbs[byte_idx]
  259. = toupper (pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx]);
  260. /* The next step uses the assumption that wchar_t is encoded
  261. ASCII-safe: all ASCII values can be converted like this. */
  262. pstr->wcs[byte_idx] = (wchar_t) pstr->mbs[byte_idx];
  263. ++byte_idx;
  264. continue;
  265. }
  266. remain_len = end_idx - byte_idx;
  267. prev_st = pstr->cur_state;
  268. mbclen = mbrtowc (&wc,
  269. ((const char *) pstr->raw_mbs + pstr->raw_mbs_idx
  270. + byte_idx), remain_len, &pstr->cur_state);
  271. if (BE (mbclen + 2 > 2, 1))
  272. {
  273. wchar_t wcu = wc;
  274. if (iswlower (wc))
  275. {
  276. size_t mbcdlen;
  277. wcu = towupper (wc);
  278. mbcdlen = wcrtomb (buf, wcu, &prev_st);
  279. if (BE (mbclen == mbcdlen, 1))
  280. memcpy (pstr->mbs + byte_idx, buf, mbclen);
  281. else
  282. {
  283. src_idx = byte_idx;
  284. goto offsets_needed;
  285. }
  286. }
  287. else
  288. memcpy (pstr->mbs + byte_idx,
  289. pstr->raw_mbs + pstr->raw_mbs_idx + byte_idx, mbclen);
  290. pstr->wcs[byte_idx++] = wcu;
  291. /* Write paddings. */
  292. for (remain_len = byte_idx + mbclen - 1; byte_idx < remain_len ;)
  293. pstr->wcs[byte_idx++] = WEOF;
  294. }
  295. else if (mbclen == (size_t) -1 || mbclen == 0)
  296. {
  297. /* It is an invalid character or '\0'. Just use the byte. */
  298. int ch = pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx];
  299. pstr->mbs[byte_idx] = ch;
  300. /* And also cast it to wide char. */
  301. pstr->wcs[byte_idx++] = (wchar_t) ch;
  302. if (BE (mbclen == (size_t) -1, 0))
  303. pstr->cur_state = prev_st;
  304. }
  305. else
  306. {
  307. /* The buffer doesn't have enough space, finish to build. */
  308. pstr->cur_state = prev_st;
  309. break;
  310. }
  311. }
  312. pstr->valid_len = byte_idx;
  313. pstr->valid_raw_len = byte_idx;
  314. return REG_NOERROR;
  315. }
  316. else
  317. for (src_idx = pstr->valid_raw_len; byte_idx < end_idx;)
  318. {
  319. wchar_t wc;
  320. const char *p;
  321. offsets_needed:
  322. remain_len = end_idx - byte_idx;
  323. prev_st = pstr->cur_state;
  324. if (BE (pstr->trans != NULL, 0))
  325. {
  326. int i, ch;
  327. for (i = 0; i < pstr->mb_cur_max && i < remain_len; ++i)
  328. {
  329. ch = pstr->raw_mbs [pstr->raw_mbs_idx + src_idx + i];
  330. buf[i] = pstr->trans[ch];
  331. }
  332. p = (const char *) buf;
  333. }
  334. else
  335. p = (const char *) pstr->raw_mbs + pstr->raw_mbs_idx + src_idx;
  336. mbclen = mbrtowc (&wc, p, remain_len, &pstr->cur_state);
  337. if (BE (mbclen + 2 > 2, 1))
  338. {
  339. wchar_t wcu = wc;
  340. if (iswlower (wc))
  341. {
  342. size_t mbcdlen;
  343. wcu = towupper (wc);
  344. mbcdlen = wcrtomb ((char *) buf, wcu, &prev_st);
  345. if (BE (mbclen == mbcdlen, 1))
  346. memcpy (pstr->mbs + byte_idx, buf, mbclen);
  347. else if (mbcdlen != (size_t) -1)
  348. {
  349. size_t i;
  350. if (byte_idx + mbcdlen > pstr->bufs_len)
  351. {
  352. pstr->cur_state = prev_st;
  353. break;
  354. }
  355. if (pstr->offsets == NULL)
  356. {
  357. pstr->offsets = re_malloc (int, pstr->bufs_len);
  358. if (pstr->offsets == NULL)
  359. return REG_ESPACE;
  360. }
  361. if (!pstr->offsets_needed)
  362. {
  363. for (i = 0; i < (size_t) byte_idx; ++i)
  364. pstr->offsets[i] = i;
  365. pstr->offsets_needed = 1;
  366. }
  367. memcpy (pstr->mbs + byte_idx, buf, mbcdlen);
  368. pstr->wcs[byte_idx] = wcu;
  369. pstr->offsets[byte_idx] = src_idx;
  370. for (i = 1; i < mbcdlen; ++i)
  371. {
  372. pstr->offsets[byte_idx + i]
  373. = src_idx + (i < mbclen ? i : mbclen - 1);
  374. pstr->wcs[byte_idx + i] = WEOF;
  375. }
  376. pstr->len += mbcdlen - mbclen;
  377. if (pstr->raw_stop > src_idx)
  378. pstr->stop += mbcdlen - mbclen;
  379. end_idx = (pstr->bufs_len > pstr->len)
  380. ? pstr->len : pstr->bufs_len;
  381. byte_idx += mbcdlen;
  382. src_idx += mbclen;
  383. continue;
  384. }
  385. else
  386. memcpy (pstr->mbs + byte_idx, p, mbclen);
  387. }
  388. else
  389. memcpy (pstr->mbs + byte_idx, p, mbclen);
  390. if (BE (pstr->offsets_needed != 0, 0))
  391. {
  392. size_t i;
  393. for (i = 0; i < mbclen; ++i)
  394. pstr->offsets[byte_idx + i] = src_idx + i;
  395. }
  396. src_idx += mbclen;
  397. pstr->wcs[byte_idx++] = wcu;
  398. /* Write paddings. */
  399. for (remain_len = byte_idx + mbclen - 1; byte_idx < remain_len ;)
  400. pstr->wcs[byte_idx++] = WEOF;
  401. }
  402. else if (mbclen == (size_t) -1 || mbclen == 0)
  403. {
  404. /* It is an invalid character or '\0'. Just use the byte. */
  405. int ch = pstr->raw_mbs[pstr->raw_mbs_idx + src_idx];
  406. if (BE (pstr->trans != NULL, 0))
  407. ch = pstr->trans [ch];
  408. pstr->mbs[byte_idx] = ch;
  409. if (BE (pstr->offsets_needed != 0, 0))
  410. pstr->offsets[byte_idx] = src_idx;
  411. ++src_idx;
  412. /* And also cast it to wide char. */
  413. pstr->wcs[byte_idx++] = (wchar_t) ch;
  414. if (BE (mbclen == (size_t) -1, 0))
  415. pstr->cur_state = prev_st;
  416. }
  417. else
  418. {
  419. /* The buffer doesn't have enough space, finish to build. */
  420. pstr->cur_state = prev_st;
  421. break;
  422. }
  423. }
  424. pstr->valid_len = byte_idx;
  425. pstr->valid_raw_len = src_idx;
  426. return REG_NOERROR;
  427. }
  428. /* Skip characters until the index becomes greater than NEW_RAW_IDX.
  429. Return the index. */
  430. static int
  431. internal_function
  432. re_string_skip_chars (re_string_t *pstr, int new_raw_idx, wint_t *last_wc)
  433. {
  434. mbstate_t prev_st;
  435. int rawbuf_idx;
  436. size_t mbclen;
  437. wchar_t wc = 0;
  438. /* Skip the characters which are not necessary to check. */
  439. for (rawbuf_idx = pstr->raw_mbs_idx + pstr->valid_raw_len;
  440. rawbuf_idx < new_raw_idx;)
  441. {
  442. int remain_len;
  443. remain_len = pstr->len - rawbuf_idx;
  444. prev_st = pstr->cur_state;
  445. mbclen = mbrtowc (&wc, (const char *) pstr->raw_mbs + rawbuf_idx,
  446. remain_len, &pstr->cur_state);
  447. if (BE (mbclen == (size_t) -2 || mbclen == (size_t) -1 || mbclen == 0, 0))
  448. {
  449. /* We treat these cases as a singlebyte character. */
  450. mbclen = 1;
  451. pstr->cur_state = prev_st;
  452. }
  453. /* Then proceed the next character. */
  454. rawbuf_idx += mbclen;
  455. }
  456. *last_wc = (wint_t) wc;
  457. return rawbuf_idx;
  458. }
  459. #endif /* RE_ENABLE_I18N */
  460. /* Build the buffer PSTR->MBS, and apply the translation if we need.
  461. This function is used in case of REG_ICASE. */
  462. static void
  463. internal_function
  464. build_upper_buffer (re_string_t *pstr)
  465. {
  466. int char_idx, end_idx;
  467. end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
  468. for (char_idx = pstr->valid_len; char_idx < end_idx; ++char_idx)
  469. {
  470. int ch = pstr->raw_mbs[pstr->raw_mbs_idx + char_idx];
  471. if (BE (pstr->trans != NULL, 0))
  472. ch = pstr->trans[ch];
  473. if (islower (ch))
  474. pstr->mbs[char_idx] = toupper (ch);
  475. else
  476. pstr->mbs[char_idx] = ch;
  477. }
  478. pstr->valid_len = char_idx;
  479. pstr->valid_raw_len = char_idx;
  480. }
  481. /* Apply TRANS to the buffer in PSTR. */
  482. static void
  483. internal_function
  484. re_string_translate_buffer (re_string_t *pstr)
  485. {
  486. int buf_idx, end_idx;
  487. end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
  488. for (buf_idx = pstr->valid_len; buf_idx < end_idx; ++buf_idx)
  489. {
  490. int ch = pstr->raw_mbs[pstr->raw_mbs_idx + buf_idx];
  491. pstr->mbs[buf_idx] = pstr->trans[ch];
  492. }
  493. pstr->valid_len = buf_idx;
  494. pstr->valid_raw_len = buf_idx;
  495. }
  496. /* This function re-construct the buffers.
  497. Concretely, convert to wide character in case of pstr->mb_cur_max > 1,
  498. convert to upper case in case of REG_ICASE, apply translation. */
  499. static reg_errcode_t
  500. internal_function
  501. re_string_reconstruct (re_string_t *pstr, int idx, int eflags)
  502. {
  503. int offset = idx - pstr->raw_mbs_idx;
  504. if (BE (offset < 0, 0))
  505. {
  506. /* Reset buffer. */
  507. #ifdef RE_ENABLE_I18N
  508. if (pstr->mb_cur_max > 1)
  509. memset (&pstr->cur_state, '\0', sizeof (mbstate_t));
  510. #endif
  511. pstr->len = pstr->raw_len;
  512. pstr->stop = pstr->raw_stop;
  513. pstr->valid_len = 0;
  514. pstr->raw_mbs_idx = 0;
  515. pstr->valid_raw_len = 0;
  516. pstr->offsets_needed = 0;
  517. pstr->tip_context = ((eflags & REG_NOTBOL) ? CONTEXT_BEGBUF
  518. : CONTEXT_NEWLINE | CONTEXT_BEGBUF);
  519. if (!pstr->mbs_allocated)
  520. pstr->mbs = (unsigned char *) pstr->raw_mbs;
  521. offset = idx;
  522. }
  523. if (BE (offset != 0, 1))
  524. {
  525. /* Are the characters which are already checked remain? */
  526. if (BE (offset < pstr->valid_raw_len, 1)
  527. #ifdef RE_ENABLE_I18N
  528. /* Handling this would enlarge the code too much.
  529. Accept a slowdown in that case. */
  530. && pstr->offsets_needed == 0
  531. #endif
  532. )
  533. {
  534. /* Yes, move them to the front of the buffer. */
  535. pstr->tip_context = re_string_context_at (pstr, offset - 1, eflags);
  536. #ifdef RE_ENABLE_I18N
  537. if (pstr->mb_cur_max > 1)
  538. memmove (pstr->wcs, pstr->wcs + offset,
  539. (pstr->valid_len - offset) * sizeof (wint_t));
  540. #endif
  541. if (BE (pstr->mbs_allocated, 0))
  542. memmove (pstr->mbs, pstr->mbs + offset,
  543. pstr->valid_len - offset);
  544. pstr->valid_len -= offset;
  545. pstr->valid_raw_len -= offset;
  546. #ifdef DEBUG
  547. assert (pstr->valid_len > 0);
  548. #endif
  549. }
  550. else
  551. {
  552. /* No, skip all characters until IDX. */
  553. #ifdef RE_ENABLE_I18N
  554. if (BE (pstr->offsets_needed, 0))
  555. {
  556. pstr->len = pstr->raw_len - idx + offset;
  557. pstr->stop = pstr->raw_stop - idx + offset;
  558. pstr->offsets_needed = 0;
  559. }
  560. #endif
  561. pstr->valid_len = 0;
  562. pstr->valid_raw_len = 0;
  563. #ifdef RE_ENABLE_I18N
  564. if (pstr->mb_cur_max > 1)
  565. {
  566. int wcs_idx;
  567. wint_t wc = WEOF;
  568. if (pstr->is_utf8)
  569. {
  570. const unsigned char *raw, *p, *end;
  571. /* Special case UTF-8. Multi-byte chars start with any
  572. byte other than 0x80 - 0xbf. */
  573. raw = pstr->raw_mbs + pstr->raw_mbs_idx;
  574. end = raw + (offset - pstr->mb_cur_max);
  575. p = raw + offset - 1;
  576. #if 0
  577. /* We know the wchar_t encoding is UCS4, so for the simple
  578. case, ASCII characters, skip the conversion step. */
  579. if (isascii (*p) && BE (pstr->trans == NULL, 1))
  580. {
  581. memset (&pstr->cur_state, '\0', sizeof (mbstate_t));
  582. pstr->valid_len = 0;
  583. wc = (wchar_t) *p;
  584. }
  585. else
  586. #endif
  587. for (; p >= end; --p)
  588. if ((*p & 0xc0) != 0x80)
  589. {
  590. mbstate_t cur_state;
  591. wchar_t wc2;
  592. int mlen = raw + pstr->len - p;
  593. size_t mbclen;
  594. /* XXX Don't use mbrtowc, we know which conversion
  595. to use (UTF-8 -> UCS4). */
  596. memset (&cur_state, 0, sizeof (cur_state));
  597. mbclen = mbrtowc (&wc2, (const char *) p, mlen,
  598. &cur_state);
  599. if (raw + offset - p <= mbclen
  600. && mbclen < (size_t) -2)
  601. {
  602. memset (&pstr->cur_state, '\0',
  603. sizeof (mbstate_t));
  604. pstr->valid_len = mbclen - (raw + offset - p);
  605. wc = wc2;
  606. }
  607. break;
  608. }
  609. }
  610. if (wc == WEOF)
  611. pstr->valid_len = re_string_skip_chars (pstr, idx, &wc) - idx;
  612. if (BE (pstr->valid_len, 0))
  613. {
  614. for (wcs_idx = 0; wcs_idx < pstr->valid_len; ++wcs_idx)
  615. pstr->wcs[wcs_idx] = WEOF;
  616. if (pstr->mbs_allocated)
  617. memset (pstr->mbs, 255, pstr->valid_len);
  618. }
  619. pstr->valid_raw_len = pstr->valid_len;
  620. pstr->tip_context = ((BE (pstr->word_ops_used != 0, 0)
  621. && IS_WIDE_WORD_CHAR (wc))
  622. ? CONTEXT_WORD
  623. : ((IS_WIDE_NEWLINE (wc)
  624. && pstr->newline_anchor)
  625. ? CONTEXT_NEWLINE : 0));
  626. }
  627. else
  628. #endif /* RE_ENABLE_I18N */
  629. {
  630. int c = pstr->raw_mbs[pstr->raw_mbs_idx + offset - 1];
  631. if (pstr->trans)
  632. c = pstr->trans[c];
  633. pstr->tip_context = (bitset_contain (pstr->word_char, c)
  634. ? CONTEXT_WORD
  635. : ((IS_NEWLINE (c) && pstr->newline_anchor)
  636. ? CONTEXT_NEWLINE : 0));
  637. }
  638. }
  639. if (!BE (pstr->mbs_allocated, 0))
  640. pstr->mbs += offset;
  641. }
  642. pstr->raw_mbs_idx = idx;
  643. pstr->len -= offset;
  644. pstr->stop -= offset;
  645. /* Then build the buffers. */
  646. #ifdef RE_ENABLE_I18N
  647. if (pstr->mb_cur_max > 1)
  648. {
  649. if (pstr->icase)
  650. {
  651. reg_errcode_t ret = build_wcs_upper_buffer (pstr);
  652. if (BE (ret != REG_NOERROR, 0))
  653. return ret;
  654. }
  655. else
  656. build_wcs_buffer (pstr);
  657. }
  658. else
  659. #endif
  660. if (BE (pstr->mbs_allocated, 0))
  661. {
  662. if (pstr->icase)
  663. build_upper_buffer (pstr);
  664. else if (pstr->trans != NULL)
  665. re_string_translate_buffer (pstr);
  666. }
  667. else
  668. pstr->valid_len = pstr->len;
  669. pstr->cur_idx = 0;
  670. return REG_NOERROR;
  671. }
  672. static unsigned char
  673. internal_function __attribute ((pure))
  674. re_string_peek_byte_case (const re_string_t *pstr, int idx)
  675. {
  676. int ch, off;
  677. /* Handle the common (easiest) cases first. */
  678. if (BE (!pstr->mbs_allocated, 1))
  679. return re_string_peek_byte (pstr, idx);
  680. #ifdef RE_ENABLE_I18N
  681. if (pstr->mb_cur_max > 1
  682. && ! re_string_is_single_byte_char (pstr, pstr->cur_idx + idx))
  683. return re_string_peek_byte (pstr, idx);
  684. #endif
  685. off = pstr->cur_idx + idx;
  686. #ifdef RE_ENABLE_I18N
  687. if (pstr->offsets_needed)
  688. off = pstr->offsets[off];
  689. #endif
  690. ch = pstr->raw_mbs[pstr->raw_mbs_idx + off];
  691. #ifdef RE_ENABLE_I18N
  692. /* Ensure that e.g. for tr_TR.UTF-8 BACKSLASH DOTLESS SMALL LETTER I
  693. this function returns CAPITAL LETTER I instead of first byte of
  694. DOTLESS SMALL LETTER I. The latter would confuse the parser,
  695. since peek_byte_case doesn't advance cur_idx in any way. */
  696. if (pstr->offsets_needed && !isascii (ch))
  697. return re_string_peek_byte (pstr, idx);
  698. #endif
  699. return ch;
  700. }
  701. static unsigned char
  702. internal_function __attribute ((pure))
  703. re_string_fetch_byte_case (re_string_t *pstr)
  704. {
  705. if (BE (!pstr->mbs_allocated, 1))
  706. return re_string_fetch_byte (pstr);
  707. #ifdef RE_ENABLE_I18N
  708. if (pstr->offsets_needed)
  709. {
  710. int off, ch;
  711. /* For tr_TR.UTF-8 [[:islower:]] there is
  712. [[: CAPITAL LETTER I WITH DOT lower:]] in mbs. Skip
  713. in that case the whole multi-byte character and return
  714. the original letter. On the other side, with
  715. [[: DOTLESS SMALL LETTER I return [[:I, as doing
  716. anything else would complicate things too much. */
  717. if (!re_string_first_byte (pstr, pstr->cur_idx))
  718. return re_string_fetch_byte (pstr);
  719. off = pstr->offsets[pstr->cur_idx];
  720. ch = pstr->raw_mbs[pstr->raw_mbs_idx + off];
  721. if (! isascii (ch))
  722. return re_string_fetch_byte (pstr);
  723. re_string_skip_bytes (pstr,
  724. re_string_char_size_at (pstr, pstr->cur_idx));
  725. return ch;
  726. }
  727. #endif
  728. return pstr->raw_mbs[pstr->raw_mbs_idx + pstr->cur_idx++];
  729. }
  730. static void
  731. internal_function
  732. re_string_destruct (re_string_t *pstr)
  733. {
  734. #ifdef RE_ENABLE_I18N
  735. re_free (pstr->wcs);
  736. re_free (pstr->offsets);
  737. #endif /* RE_ENABLE_I18N */
  738. if (pstr->mbs_allocated)
  739. re_free (pstr->mbs);
  740. }
  741. /* Return the context at IDX in INPUT. */
  742. static unsigned int
  743. internal_function
  744. re_string_context_at (const re_string_t *input, int idx, int eflags)
  745. {
  746. int c;
  747. if (BE (idx < 0, 0))
  748. /* In this case, we use the value stored in input->tip_context,
  749. since we can't know the character in input->mbs[-1] here. */
  750. return input->tip_context;
  751. if (BE (idx == input->len, 0))
  752. return ((eflags & REG_NOTEOL) ? CONTEXT_ENDBUF
  753. : CONTEXT_NEWLINE | CONTEXT_ENDBUF);
  754. #ifdef RE_ENABLE_I18N
  755. if (input->mb_cur_max > 1)
  756. {
  757. wint_t wc;
  758. int wc_idx = idx;
  759. while(input->wcs[wc_idx] == WEOF)
  760. {
  761. #ifdef DEBUG
  762. /* It must not happen. */
  763. assert (wc_idx >= 0);
  764. #endif
  765. --wc_idx;
  766. if (wc_idx < 0)
  767. return input->tip_context;
  768. }
  769. wc = input->wcs[wc_idx];
  770. if (BE (input->word_ops_used != 0, 0) && IS_WIDE_WORD_CHAR (wc))
  771. return CONTEXT_WORD;
  772. return (IS_WIDE_NEWLINE (wc) && input->newline_anchor
  773. ? CONTEXT_NEWLINE : 0);
  774. }
  775. #endif
  776. c = re_string_byte_at (input, idx);
  777. if (bitset_contain (input->word_char, c))
  778. return CONTEXT_WORD;
  779. return IS_NEWLINE (c) && input->newline_anchor ? CONTEXT_NEWLINE : 0;
  780. }
  781. /* Functions for set operation. */
  782. static reg_errcode_t
  783. internal_function
  784. re_node_set_alloc (re_node_set *set, int size)
  785. {
  786. set->alloc = size;
  787. set->nelem = 0;
  788. set->elems = re_malloc (int, size); /* can be NULL if size == 0
  789. (see re_node_set_init_empty(set)) */
  790. if (BE (set->elems == NULL && size != 0, 0))
  791. return REG_ESPACE;
  792. return REG_NOERROR;
  793. }
  794. static reg_errcode_t
  795. internal_function
  796. re_node_set_init_1 (re_node_set *set, int elem)
  797. {
  798. set->alloc = 1;
  799. set->nelem = 1;
  800. set->elems = re_malloc (int, 1);
  801. if (BE (set->elems == NULL, 0))
  802. {
  803. set->alloc = set->nelem = 0;
  804. return REG_ESPACE;
  805. }
  806. set->elems[0] = elem;
  807. return REG_NOERROR;
  808. }
  809. static reg_errcode_t
  810. internal_function
  811. re_node_set_init_2 (re_node_set *set, int elem1, int elem2)
  812. {
  813. set->alloc = 2;
  814. set->elems = re_malloc (int, 2);
  815. if (BE (set->elems == NULL, 0))
  816. return REG_ESPACE;
  817. if (elem1 == elem2)
  818. {
  819. set->nelem = 1;
  820. set->elems[0] = elem1;
  821. }
  822. else
  823. {
  824. set->nelem = 2;
  825. if (elem1 < elem2)
  826. {
  827. set->elems[0] = elem1;
  828. set->elems[1] = elem2;
  829. }
  830. else
  831. {
  832. set->elems[0] = elem2;
  833. set->elems[1] = elem1;
  834. }
  835. }
  836. return REG_NOERROR;
  837. }
  838. static reg_errcode_t
  839. internal_function
  840. re_node_set_init_copy (re_node_set *dest, const re_node_set *src)
  841. {
  842. dest->nelem = src->nelem;
  843. if (src->nelem > 0)
  844. {
  845. dest->alloc = dest->nelem;
  846. dest->elems = re_malloc (int, dest->alloc);
  847. if (BE (dest->elems == NULL, 0))
  848. {
  849. dest->alloc = dest->nelem = 0;
  850. return REG_ESPACE;
  851. }
  852. memcpy (dest->elems, src->elems, src->nelem * sizeof (int));
  853. }
  854. else
  855. re_node_set_init_empty (dest);
  856. return REG_NOERROR;
  857. }
  858. /* Calculate the intersection of the sets SRC1 and SRC2. And merge it to
  859. DEST. Return value indicate the error code or REG_NOERROR if succeeded.
  860. Note: We assume dest->elems is NULL, when dest->alloc is 0. */
  861. static reg_errcode_t
  862. internal_function
  863. re_node_set_add_intersect (re_node_set *dest, const re_node_set *src1,
  864. const re_node_set *src2)
  865. {
  866. int i1, i2, is, id, delta, sbase;
  867. if (src1->nelem == 0 || src2->nelem == 0)
  868. return REG_NOERROR;
  869. /* We need dest->nelem + 2 * elems_in_intersection; this is a
  870. conservative estimate. */
  871. if (src1->nelem + src2->nelem + dest->nelem > dest->alloc)
  872. {
  873. int new_alloc = src1->nelem + src2->nelem + dest->alloc;
  874. int *new_elems = re_realloc (dest->elems, int, new_alloc);
  875. if (BE (new_elems == NULL, 0))
  876. return REG_ESPACE;
  877. dest->elems = new_elems;
  878. dest->alloc = new_alloc;
  879. }
  880. /* Find the items in the intersection of SRC1 and SRC2, and copy
  881. into the top of DEST those that are not already in DEST itself. */
  882. sbase = dest->nelem + src1->nelem + src2->nelem;
  883. i1 = src1->nelem - 1;
  884. i2 = src2->nelem - 1;
  885. id = dest->nelem - 1;
  886. for (;;)
  887. {
  888. if (src1->elems[i1] == src2->elems[i2])
  889. {
  890. /* Try to find the item in DEST. Maybe we could binary search? */
  891. while (id >= 0 && dest->elems[id] > src1->elems[i1])
  892. --id;
  893. if (id < 0 || dest->elems[id] != src1->elems[i1])
  894. dest->elems[--sbase] = src1->elems[i1];
  895. if (--i1 < 0 || --i2 < 0)
  896. break;
  897. }
  898. /* Lower the highest of the two items. */
  899. else if (src1->elems[i1] < src2->elems[i2])
  900. {
  901. if (--i2 < 0)
  902. break;
  903. }
  904. else
  905. {
  906. if (--i1 < 0)
  907. break;
  908. }
  909. }
  910. id = dest->nelem - 1;
  911. is = dest->nelem + src1->nelem + src2->nelem - 1;
  912. delta = is - sbase + 1;
  913. /* Now copy. When DELTA becomes zero, the remaining
  914. DEST elements are already in place; this is more or
  915. less the same loop that is in re_node_set_merge. */
  916. dest->nelem += delta;
  917. if (delta > 0 && id >= 0)
  918. for (;;)
  919. {
  920. if (dest->elems[is] > dest->elems[id])
  921. {
  922. /* Copy from the top. */
  923. dest->elems[id + delta--] = dest->elems[is--];
  924. if (delta == 0)
  925. break;
  926. }
  927. else
  928. {
  929. /* Slide from the bottom. */
  930. dest->elems[id + delta] = dest->elems[id];
  931. if (--id < 0)
  932. break;
  933. }
  934. }
  935. /* Copy remaining SRC elements. */
  936. memcpy (dest->elems, dest->elems + sbase, delta * sizeof (int));
  937. return REG_NOERROR;
  938. }
  939. /* Calculate the union set of the sets SRC1 and SRC2. And store it to
  940. DEST. Return value indicate the error code or REG_NOERROR if succeeded. */
  941. static reg_errcode_t
  942. internal_function
  943. re_node_set_init_union (re_node_set *dest, const re_node_set *src1,
  944. const re_node_set *src2)
  945. {
  946. int i1, i2, id;
  947. if (src1 != NULL && src1->nelem > 0 && src2 != NULL && src2->nelem > 0)
  948. {
  949. dest->alloc = src1->nelem + src2->nelem;
  950. dest->elems = re_malloc (int, dest->alloc);
  951. if (BE (dest->elems == NULL, 0))
  952. return REG_ESPACE;
  953. }
  954. else
  955. {
  956. if (src1 != NULL && src1->nelem > 0)
  957. return re_node_set_init_copy (dest, src1);
  958. if (src2 != NULL && src2->nelem > 0)
  959. return re_node_set_init_copy (dest, src2);
  960. re_node_set_init_empty (dest);
  961. return REG_NOERROR;
  962. }
  963. for (i1 = i2 = id = 0 ; i1 < src1->nelem && i2 < src2->nelem ;)
  964. {
  965. if (src1->elems[i1] > src2->elems[i2])
  966. {
  967. dest->elems[id++] = src2->elems[i2++];
  968. continue;
  969. }
  970. if (src1->elems[i1] == src2->elems[i2])
  971. ++i2;
  972. dest->elems[id++] = src1->elems[i1++];
  973. }
  974. if (i1 < src1->nelem)
  975. {
  976. memcpy (dest->elems + id, src1->elems + i1,
  977. (src1->nelem - i1) * sizeof (int));
  978. id += src1->nelem - i1;
  979. }
  980. else if (i2 < src2->nelem)
  981. {
  982. memcpy (dest->elems + id, src2->elems + i2,
  983. (src2->nelem - i2) * sizeof (int));
  984. id += src2->nelem - i2;
  985. }
  986. dest->nelem = id;
  987. return REG_NOERROR;
  988. }
  989. /* Calculate the union set of the sets DEST and SRC. And store it to
  990. DEST. Return value indicate the error code or REG_NOERROR if succeeded. */
  991. static reg_errcode_t
  992. internal_function
  993. re_node_set_merge (re_node_set *dest, const re_node_set *src)
  994. {
  995. int is, id, sbase, delta;
  996. if (src == NULL || src->nelem == 0)
  997. return REG_NOERROR;
  998. if (dest->alloc < 2 * src->nelem + dest->nelem)
  999. {
  1000. int new_alloc = 2 * (src->nelem + dest->alloc);
  1001. int *new_buffer = re_realloc (dest->elems, int, new_alloc);
  1002. if (BE (new_buffer == NULL, 0))
  1003. return REG_ESPACE;
  1004. dest->elems = new_buffer;
  1005. dest->alloc = new_alloc;
  1006. }
  1007. if (BE (dest->nelem == 0, 0))
  1008. {
  1009. dest->nelem = src->nelem;
  1010. memcpy (dest->elems, src->elems, src->nelem * sizeof (int));
  1011. return REG_NOERROR;
  1012. }
  1013. /* Copy into the top of DEST the items of SRC that are not
  1014. found in DEST. Maybe we could binary search in DEST? */
  1015. for (sbase = dest->nelem + 2 * src->nelem,
  1016. is = src->nelem - 1, id = dest->nelem - 1; is >= 0 && id >= 0; )
  1017. {
  1018. if (dest->elems[id] == src->elems[is])
  1019. is--, id--;
  1020. else if (dest->elems[id] < src->elems[is])
  1021. dest->elems[--sbase] = src->elems[is--];
  1022. else /* if (dest->elems[id] > src->elems[is]) */
  1023. --id;
  1024. }
  1025. if (is >= 0)
  1026. {
  1027. /* If DEST is exhausted, the remaining items of SRC must be unique. */
  1028. sbase -= is + 1;
  1029. memcpy (dest->elems + sbase, src->elems, (is + 1) * sizeof (int));
  1030. }
  1031. id = dest->nelem - 1;
  1032. is = dest->nelem + 2 * src->nelem - 1;
  1033. delta = is - sbase + 1;
  1034. if (delta == 0)
  1035. return REG_NOERROR;
  1036. /* Now copy. When DELTA becomes zero, the remaining
  1037. DEST elements are already in place. */
  1038. dest->nelem += delta;
  1039. for (;;)
  1040. {
  1041. if (dest->elems[is] > dest->elems[id])
  1042. {
  1043. /* Copy from the top. */
  1044. dest->elems[id + delta--] = dest->elems[is--];
  1045. if (delta == 0)
  1046. break;
  1047. }
  1048. else
  1049. {
  1050. /* Slide from the bottom. */
  1051. dest->elems[id + delta] = dest->elems[id];
  1052. if (--id < 0)
  1053. {
  1054. /* Copy remaining SRC elements. */
  1055. memcpy (dest->elems, dest->elems + sbase,
  1056. delta * sizeof (int));
  1057. break;
  1058. }
  1059. }
  1060. }
  1061. return REG_NOERROR;
  1062. }
  1063. /* Insert the new element ELEM to the re_node_set* SET.
  1064. SET should not already have ELEM.
  1065. return -1 if an error is occured, return 1 otherwise. */
  1066. static int
  1067. internal_function
  1068. re_node_set_insert (re_node_set *set, int elem)
  1069. {
  1070. int idx;
  1071. /* In case the set is empty. */
  1072. if (set->alloc == 0)
  1073. {
  1074. if (BE (re_node_set_init_1 (set, elem) == REG_NOERROR, 1))
  1075. return 1;
  1076. return -1;
  1077. }
  1078. if (BE (set->nelem, 0) == 0)
  1079. {
  1080. /* We already guaranteed above that set->alloc != 0. */
  1081. set->elems[0] = elem;
  1082. ++set->nelem;
  1083. return 1;
  1084. }
  1085. /* Realloc if we need. */
  1086. if (set->alloc == set->nelem)
  1087. {
  1088. int *new_elems;
  1089. set->alloc = set->alloc * 2;
  1090. new_elems = re_realloc (set->elems, int, set->alloc);
  1091. if (BE (new_elems == NULL, 0))
  1092. return -1;
  1093. set->elems = new_elems;
  1094. }
  1095. /* Move the elements which follows the new element. Test the
  1096. first element separately to skip a check in the inner loop. */
  1097. if (elem < set->elems[0])
  1098. {
  1099. idx = 0;
  1100. for (idx = set->nelem; idx > 0; idx--)
  1101. set->elems[idx] = set->elems[idx - 1];
  1102. }
  1103. else
  1104. {
  1105. for (idx = set->nelem; set->elems[idx - 1] > elem; idx--)
  1106. set->elems[idx] = set->elems[idx - 1];
  1107. }
  1108. /* Insert the new element. */
  1109. set->elems[idx] = elem;
  1110. ++set->nelem;
  1111. return 1;
  1112. }
  1113. /* Insert the new element ELEM to the re_node_set* SET.
  1114. SET should not already have any element greater than or equal to ELEM.
  1115. Return -1 if an error is occured, return 1 otherwise. */
  1116. static int
  1117. internal_function
  1118. re_node_set_insert_last (re_node_set *set, int elem)
  1119. {
  1120. /* Realloc if we need. */
  1121. if (set->alloc == set->nelem)
  1122. {
  1123. int *new_elems;
  1124. set->alloc = (set->alloc + 1) * 2;
  1125. new_elems = re_realloc (set->elems, int, set->alloc);
  1126. if (BE (new_elems == NULL, 0))
  1127. return -1;
  1128. set->elems = new_elems;
  1129. }
  1130. /* Insert the new element. */
  1131. set->elems[set->nelem++] = elem;
  1132. return 1;
  1133. }
  1134. /* Compare two node sets SET1 and SET2.
  1135. return 1 if SET1 and SET2 are equivalent, return 0 otherwise. */
  1136. static int
  1137. internal_function __attribute ((pure))
  1138. re_node_set_compare (const re_node_set *set1, const re_node_set *set2)
  1139. {
  1140. int i;
  1141. if (set1 == NULL || set2 == NULL || set1->nelem != set2->nelem)
  1142. return 0;
  1143. for (i = set1->nelem ; --i >= 0 ; )
  1144. if (set1->elems[i] != set2->elems[i])
  1145. return 0;
  1146. return 1;
  1147. }
  1148. /* Return (idx + 1) if SET contains the element ELEM, return 0 otherwise. */
  1149. static int
  1150. internal_function __attribute ((pure))
  1151. re_node_set_contains (const re_node_set *set, int elem)
  1152. {
  1153. unsigned int idx, right, mid;
  1154. if (set->nelem <= 0)
  1155. return 0;
  1156. /* Binary search the element. */
  1157. idx = 0;
  1158. right = set->nelem - 1;
  1159. while (idx < right)
  1160. {
  1161. mid = (idx + right) / 2;
  1162. if (set->elems[mid] < elem)
  1163. idx = mid + 1;
  1164. else
  1165. right = mid;
  1166. }
  1167. return set->elems[idx] == elem ? idx + 1 : 0;
  1168. }
  1169. static void
  1170. internal_function
  1171. re_node_set_remove_at (re_node_set *set, int idx)
  1172. {
  1173. if (idx < 0 || idx >= set->nelem)
  1174. return;
  1175. --set->nelem;
  1176. for (; idx < set->nelem; idx++)
  1177. set->elems[idx] = set->elems[idx + 1];
  1178. }
  1179. /* Add the token TOKEN to dfa->nodes, and return the index of the token.
  1180. Or return -1, if an error will be occured. */
  1181. static int
  1182. internal_function
  1183. re_dfa_add_node (re_dfa_t *dfa, re_token_t token)
  1184. {
  1185. #ifdef RE_ENABLE_I18N
  1186. int type = token.type;
  1187. #endif
  1188. if (BE (dfa->nodes_len >= dfa->nodes_alloc, 0))
  1189. {
  1190. size_t new_nodes_alloc = dfa->nodes_alloc * 2;
  1191. int *new_nexts, *new_indices;
  1192. re_node_set *new_edests, *new_eclosures;
  1193. re_token_t *new_nodes;
  1194. /* Avoid overflows. */
  1195. if (BE (new_nodes_alloc < dfa->nodes_alloc, 0))
  1196. return -1;
  1197. new_nodes = re_realloc (dfa->nodes, re_token_t, new_nodes_alloc);
  1198. if (BE (new_nodes == NULL, 0))
  1199. return -1;
  1200. dfa->nodes = new_nodes;
  1201. new_nexts = re_realloc (dfa->nexts, int, new_nodes_alloc);
  1202. new_indices = re_realloc (dfa->org_indices, int, new_nodes_alloc);
  1203. new_edests = re_realloc (dfa->edests, re_node_set, new_nodes_alloc);
  1204. new_eclosures = re_realloc (dfa->eclosures, re_node_set, new_nodes_alloc);
  1205. if (BE (new_nexts == NULL || new_indices == NULL
  1206. || new_edests == NULL || new_eclosures == NULL, 0))
  1207. return -1;
  1208. dfa->nexts = new_nexts;
  1209. dfa->org_indices = new_indices;
  1210. dfa->edests = new_edests;
  1211. dfa->eclosures = new_eclosures;
  1212. dfa->nodes_alloc = new_nodes_alloc;
  1213. }
  1214. dfa->nodes[dfa->nodes_len] = token;
  1215. dfa->nodes[dfa->nodes_len].constraint = 0;
  1216. #ifdef RE_ENABLE_I18N
  1217. dfa->nodes[dfa->nodes_len].accept_mb =
  1218. (type == OP_PERIOD && dfa->mb_cur_max > 1) || type == COMPLEX_BRACKET;
  1219. #endif
  1220. dfa->nexts[dfa->nodes_len] = -1;
  1221. re_node_set_init_empty (dfa->edests + dfa->nodes_len);
  1222. re_node_set_init_empty (dfa->eclosures + dfa->nodes_len);
  1223. return dfa->nodes_len++;
  1224. }
  1225. static __inline__ unsigned int
  1226. internal_function
  1227. calc_state_hash (const re_node_set *nodes, unsigned int context)
  1228. {
  1229. unsigned int hash = nodes->nelem + context;
  1230. int i;
  1231. for (i = 0 ; i < nodes->nelem ; i++)
  1232. hash += nodes->elems[i];
  1233. return hash;
  1234. }
  1235. /* Search for the state whose node_set is equivalent to NODES.
  1236. Return the pointer to the state, if we found it in the DFA.
  1237. Otherwise create the new one and return it. In case of an error
  1238. return NULL and set the error code in ERR.
  1239. Note: - We assume NULL as the invalid state, then it is possible that
  1240. return value is NULL and ERR is REG_NOERROR.
  1241. - We never return non-NULL value in case of any errors, it is for
  1242. optimization. */
  1243. static re_dfastate_t *
  1244. internal_function
  1245. re_acquire_state (reg_errcode_t *err, const re_dfa_t *dfa,
  1246. const re_node_set *nodes)
  1247. {
  1248. unsigned int hash;
  1249. re_dfastate_t *new_state;
  1250. struct re_state_table_entry *spot;
  1251. int i;
  1252. if (BE (nodes->nelem == 0, 0))
  1253. {
  1254. *err = REG_NOERROR;
  1255. return NULL;
  1256. }
  1257. hash = calc_state_hash (nodes, 0);
  1258. spot = dfa->state_table + (hash & dfa->state_hash_mask);
  1259. for (i = 0 ; i < spot->num ; i++)
  1260. {
  1261. re_dfastate_t *state = spot->array[i];
  1262. if (hash != state->hash)
  1263. continue;
  1264. if (re_node_set_compare (&state->nodes, nodes))
  1265. return state;
  1266. }
  1267. /* There are no appropriate state in the dfa, create the new one. */
  1268. new_state = create_ci_newstate (dfa, nodes, hash);
  1269. if (BE (new_state == NULL, 0))
  1270. *err = REG_ESPACE;
  1271. return new_state;
  1272. }
  1273. /* Search for the state whose node_set is equivalent to NODES and
  1274. whose context is equivalent to CONTEXT.
  1275. Return the pointer to the state, if we found it in the DFA.
  1276. Otherwise create the new one and return it. In case of an error
  1277. return NULL and set the error code in ERR.
  1278. Note: - We assume NULL as the invalid state, then it is possible that
  1279. return value is NULL and ERR is REG_NOERROR.
  1280. - We never return non-NULL value in case of any errors, it is for
  1281. optimization. */
  1282. static re_dfastate_t *
  1283. internal_function
  1284. re_acquire_state_context (reg_errcode_t *err, const re_dfa_t *dfa,
  1285. const re_node_set *nodes, unsigned int context)
  1286. {
  1287. unsigned int hash;
  1288. re_dfastate_t *new_state;
  1289. struct re_state_table_entry *spot;
  1290. int i;
  1291. if (nodes->nelem == 0)
  1292. {
  1293. *err = REG_NOERROR;
  1294. return NULL;
  1295. }
  1296. hash = calc_state_hash (nodes, context);
  1297. spot = dfa->state_table + (hash & dfa->state_hash_mask);
  1298. for (i = 0 ; i < spot->num ; i++)
  1299. {
  1300. re_dfastate_t *state = spot->array[i];
  1301. if (state->hash == hash
  1302. && state->context == context
  1303. && re_node_set_compare (state->entrance_nodes, nodes))
  1304. return state;
  1305. }
  1306. /* There are no appropriate state in `dfa', create the new one. */
  1307. new_state = create_cd_newstate (dfa, nodes, context, hash);
  1308. if (BE (new_state == NULL, 0))
  1309. *err = REG_ESPACE;
  1310. return new_state;
  1311. }
  1312. /* Finish initialization of the new state NEWSTATE, and using its hash value
  1313. HASH put in the appropriate bucket of DFA's state table. Return value
  1314. indicates the error code if failed. */
  1315. static reg_errcode_t
  1316. register_state (const re_dfa_t *dfa, re_dfastate_t *newstate,
  1317. unsigned int hash)
  1318. {
  1319. struct re_state_table_entry *spot;
  1320. reg_errcode_t err;
  1321. int i;
  1322. newstate->hash = hash;
  1323. err = re_node_set_alloc (&newstate->non_eps_nodes, newstate->nodes.nelem);
  1324. if (BE (err != REG_NOERROR, 0))
  1325. return REG_ESPACE;
  1326. for (i = 0; i < newstate->nodes.nelem; i++)
  1327. {
  1328. int elem = newstate->nodes.elems[i];
  1329. if (!IS_EPSILON_NODE (dfa->nodes[elem].type))
  1330. re_node_set_insert_last (&newstate->non_eps_nodes, elem);
  1331. }
  1332. spot = dfa->state_table + (hash & dfa->state_hash_mask);
  1333. if (BE (spot->alloc <= spot->num, 0))
  1334. {
  1335. int new_alloc = 2 * spot->num + 2;
  1336. re_dfastate_t **new_array = re_realloc (spot->array, re_dfastate_t *,
  1337. new_alloc);
  1338. if (BE (new_array == NULL, 0))
  1339. return REG_ESPACE;
  1340. spot->array = new_array;
  1341. spot->alloc = new_alloc;
  1342. }
  1343. spot->array[spot->num++] = newstate;
  1344. return REG_NOERROR;
  1345. }
  1346. static void
  1347. free_state (re_dfastate_t *state)
  1348. {
  1349. re_node_set_free (&state->non_eps_nodes);
  1350. re_node_set_free (&state->inveclosure);
  1351. if (state->entrance_nodes != &state->nodes)
  1352. {
  1353. re_node_set_free (state->entrance_nodes);
  1354. re_free (state->entrance_nodes);
  1355. }
  1356. re_node_set_free (&state->nodes);
  1357. re_free (state->word_trtable);
  1358. re_free (state->trtable);
  1359. re_free (state);
  1360. }
  1361. /* Create the new state which is independ of contexts.
  1362. Return the new state if succeeded, otherwise return NULL. */
  1363. static re_dfastate_t *
  1364. internal_function
  1365. create_ci_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
  1366. unsigned int hash)
  1367. {
  1368. int i;
  1369. reg_errcode_t err;
  1370. re_dfastate_t *newstate;
  1371. newstate = calloc (sizeof (re_dfastate_t), 1);
  1372. if (BE (newstate == NULL, 0))
  1373. return NULL;
  1374. err = re_node_set_init_copy (&newstate->nodes, nodes);
  1375. if (BE (err != REG_NOERROR, 0))
  1376. {
  1377. re_free (newstate);
  1378. return NULL;
  1379. }
  1380. newstate->entrance_nodes = &newstate->nodes;
  1381. for (i = 0 ; i < nodes->nelem ; i++)
  1382. {
  1383. re_token_t *node = dfa->nodes + nodes->elems[i];
  1384. re_token_type_t type = node->type;
  1385. if (type == CHARACTER && !node->constraint)
  1386. continue;
  1387. #ifdef RE_ENABLE_I18N
  1388. newstate->accept_mb |= node->accept_mb;
  1389. #endif
  1390. /* If the state has the halt node, the state is a halt state. */
  1391. if (type == END_OF_RE)
  1392. newstate->halt = 1;
  1393. else if (type == OP_BACK_REF)
  1394. newstate->has_backref = 1;
  1395. else if (type == ANCHOR || node->constraint)
  1396. newstate->has_constraint = 1;
  1397. }
  1398. err = register_state (dfa, newstate, hash);
  1399. if (BE (err != REG_NOERROR, 0))
  1400. {
  1401. free_state (newstate);
  1402. newstate = NULL;
  1403. }
  1404. return newstate;
  1405. }
  1406. /* Create the new state which is depend on the context CONTEXT.
  1407. Return the new state if succeeded, otherwise return NULL. */
  1408. static re_dfastate_t *
  1409. internal_function
  1410. create_cd_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
  1411. unsigned int context, unsigned int hash)
  1412. {
  1413. int i, nctx_nodes = 0;
  1414. reg_errcode_t err;
  1415. re_dfastate_t *newstate;
  1416. newstate = calloc (sizeof (re_dfastate_t), 1);
  1417. if (BE (newstate == NULL, 0))
  1418. return NULL;
  1419. err = re_node_set_init_copy (&newstate->nodes, nodes);
  1420. if (BE (err != REG_NOERROR, 0))
  1421. {
  1422. re_free (newstate);
  1423. return NULL;
  1424. }
  1425. newstate->context = context;
  1426. newstate->entrance_nodes = &newstate->nodes;
  1427. for (i = 0 ; i < nodes->nelem ; i++)
  1428. {
  1429. unsigned int constraint = 0;
  1430. re_token_t *node = dfa->nodes + nodes->elems[i];
  1431. re_token_type_t type = node->type;
  1432. if (node->constraint)
  1433. constraint = node->constraint;
  1434. if (type == CHARACTER && !constraint)
  1435. continue;
  1436. #ifdef RE_ENABLE_I18N
  1437. newstate->accept_mb |= node->accept_mb;
  1438. #endif /* RE_ENABLE_I18N */
  1439. /* If the state has the halt node, the state is a halt state. */
  1440. if (type == END_OF_RE)
  1441. newstate->halt = 1;
  1442. else if (type == OP_BACK_REF)
  1443. newstate->has_backref = 1;
  1444. else if (type == ANCHOR)
  1445. constraint = node->opr.ctx_type;
  1446. if (constraint)
  1447. {
  1448. if (newstate->entrance_nodes == &newstate->nodes)
  1449. {
  1450. newstate->entrance_nodes = re_malloc (re_node_set, 1);
  1451. if (BE (newstate->entrance_nodes == NULL, 0))
  1452. {
  1453. free_state (newstate);
  1454. return NULL;
  1455. }
  1456. re_node_set_init_copy (newstate->entrance_nodes, nodes);
  1457. nctx_nodes = 0;
  1458. newstate->has_constraint = 1;
  1459. }
  1460. if (NOT_SATISFY_PREV_CONSTRAINT (constraint,context))
  1461. {
  1462. re_node_set_remove_at (&newstate->nodes, i - nctx_nodes);
  1463. ++nctx_nodes;
  1464. }
  1465. }
  1466. }
  1467. err = register_state (dfa, newstate, hash);
  1468. if (BE (err != REG_NOERROR, 0))
  1469. {
  1470. free_state (newstate);
  1471. newstate = NULL;
  1472. }
  1473. return newstate;
  1474. }