regex_internal.c 44 KB

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