regex_internal.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  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, 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
  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. #if defined __UCLIBC__
  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 reg_errcode_t
  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. #if defined __UCLIBC__
  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
  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
  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. #if 0
  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. reg_errcode_t 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
  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. #endif
  786. c = re_string_byte_at (input, idx);
  787. if (bitset_contain (input->word_char, c))
  788. return CONTEXT_WORD;
  789. return IS_NEWLINE (c) && input->newline_anchor ? CONTEXT_NEWLINE : 0;
  790. }
  791. /* Functions for set operation. */
  792. static reg_errcode_t
  793. internal_function
  794. re_node_set_alloc (re_node_set *set, int size)
  795. {
  796. set->alloc = size;
  797. set->nelem = 0;
  798. set->elems = re_malloc (int, size); /* can be NULL if size == 0
  799. (see re_node_set_init_empty(set)) */
  800. if (BE (set->elems == NULL && size != 0, 0))
  801. return REG_ESPACE;
  802. return REG_NOERROR;
  803. }
  804. static reg_errcode_t
  805. internal_function
  806. re_node_set_init_1 (re_node_set *set, int elem)
  807. {
  808. set->alloc = 1;
  809. set->nelem = 1;
  810. set->elems = re_malloc (int, 1);
  811. if (BE (set->elems == NULL, 0))
  812. {
  813. set->alloc = set->nelem = 0;
  814. return REG_ESPACE;
  815. }
  816. set->elems[0] = elem;
  817. return REG_NOERROR;
  818. }
  819. static reg_errcode_t
  820. internal_function
  821. re_node_set_init_2 (re_node_set *set, int elem1, int elem2)
  822. {
  823. set->alloc = 2;
  824. set->elems = re_malloc (int, 2);
  825. if (BE (set->elems == NULL, 0))
  826. return REG_ESPACE;
  827. if (elem1 == elem2)
  828. {
  829. set->nelem = 1;
  830. set->elems[0] = elem1;
  831. }
  832. else
  833. {
  834. set->nelem = 2;
  835. if (elem1 < elem2)
  836. {
  837. set->elems[0] = elem1;
  838. set->elems[1] = elem2;
  839. }
  840. else
  841. {
  842. set->elems[0] = elem2;
  843. set->elems[1] = elem1;
  844. }
  845. }
  846. return REG_NOERROR;
  847. }
  848. static reg_errcode_t
  849. internal_function
  850. re_node_set_init_copy (re_node_set *dest, const re_node_set *src)
  851. {
  852. dest->nelem = src->nelem;
  853. if (src->nelem > 0)
  854. {
  855. dest->alloc = dest->nelem;
  856. dest->elems = re_malloc (int, dest->alloc);
  857. if (BE (dest->elems == NULL, 0))
  858. {
  859. dest->alloc = dest->nelem = 0;
  860. return REG_ESPACE;
  861. }
  862. memcpy (dest->elems, src->elems, src->nelem * sizeof (int));
  863. }
  864. else
  865. re_node_set_init_empty (dest);
  866. return REG_NOERROR;
  867. }
  868. /* Calculate the intersection of the sets SRC1 and SRC2. And merge it to
  869. DEST. Return value indicate the error code or REG_NOERROR if succeeded.
  870. Note: We assume dest->elems is NULL, when dest->alloc is 0. */
  871. static reg_errcode_t
  872. internal_function
  873. re_node_set_add_intersect (re_node_set *dest, const re_node_set *src1,
  874. const re_node_set *src2)
  875. {
  876. int i1, i2, is, id, delta, sbase;
  877. if (src1->nelem == 0 || src2->nelem == 0)
  878. return REG_NOERROR;
  879. /* We need dest->nelem + 2 * elems_in_intersection; this is a
  880. conservative estimate. */
  881. if (src1->nelem + src2->nelem + dest->nelem > dest->alloc)
  882. {
  883. int new_alloc = src1->nelem + src2->nelem + dest->alloc;
  884. int *new_elems = re_realloc (dest->elems, int, new_alloc);
  885. if (BE (new_elems == NULL, 0))
  886. return REG_ESPACE;
  887. dest->elems = new_elems;
  888. dest->alloc = new_alloc;
  889. }
  890. /* Find the items in the intersection of SRC1 and SRC2, and copy
  891. into the top of DEST those that are not already in DEST itself. */
  892. sbase = dest->nelem + src1->nelem + src2->nelem;
  893. i1 = src1->nelem - 1;
  894. i2 = src2->nelem - 1;
  895. id = dest->nelem - 1;
  896. for (;;)
  897. {
  898. if (src1->elems[i1] == src2->elems[i2])
  899. {
  900. /* Try to find the item in DEST. Maybe we could binary search? */
  901. while (id >= 0 && dest->elems[id] > src1->elems[i1])
  902. --id;
  903. if (id < 0 || dest->elems[id] != src1->elems[i1])
  904. dest->elems[--sbase] = src1->elems[i1];
  905. if (--i1 < 0 || --i2 < 0)
  906. break;
  907. }
  908. /* Lower the highest of the two items. */
  909. else if (src1->elems[i1] < src2->elems[i2])
  910. {
  911. if (--i2 < 0)
  912. break;
  913. }
  914. else
  915. {
  916. if (--i1 < 0)
  917. break;
  918. }
  919. }
  920. id = dest->nelem - 1;
  921. is = dest->nelem + src1->nelem + src2->nelem - 1;
  922. delta = is - sbase + 1;
  923. /* Now copy. When DELTA becomes zero, the remaining
  924. DEST elements are already in place; this is more or
  925. less the same loop that is in re_node_set_merge. */
  926. dest->nelem += delta;
  927. if (delta > 0 && id >= 0)
  928. for (;;)
  929. {
  930. if (dest->elems[is] > dest->elems[id])
  931. {
  932. /* Copy from the top. */
  933. dest->elems[id + delta--] = dest->elems[is--];
  934. if (delta == 0)
  935. break;
  936. }
  937. else
  938. {
  939. /* Slide from the bottom. */
  940. dest->elems[id + delta] = dest->elems[id];
  941. if (--id < 0)
  942. break;
  943. }
  944. }
  945. /* Copy remaining SRC elements. */
  946. memcpy (dest->elems, dest->elems + sbase, delta * sizeof (int));
  947. return REG_NOERROR;
  948. }
  949. /* Calculate the union set of the sets SRC1 and SRC2. And store it to
  950. DEST. Return value indicate the error code or REG_NOERROR if succeeded. */
  951. static reg_errcode_t
  952. internal_function
  953. re_node_set_init_union (re_node_set *dest, const re_node_set *src1,
  954. const re_node_set *src2)
  955. {
  956. int i1, i2, id;
  957. if (src1 != NULL && src1->nelem > 0 && src2 != NULL && src2->nelem > 0)
  958. {
  959. dest->alloc = src1->nelem + src2->nelem;
  960. dest->elems = re_malloc (int, dest->alloc);
  961. if (BE (dest->elems == NULL, 0))
  962. return REG_ESPACE;
  963. }
  964. else
  965. {
  966. if (src1 != NULL && src1->nelem > 0)
  967. return re_node_set_init_copy (dest, src1);
  968. if (src2 != NULL && src2->nelem > 0)
  969. return re_node_set_init_copy (dest, src2);
  970. re_node_set_init_empty (dest);
  971. return REG_NOERROR;
  972. }
  973. for (i1 = i2 = id = 0 ; i1 < src1->nelem && i2 < src2->nelem ;)
  974. {
  975. if (src1->elems[i1] > src2->elems[i2])
  976. {
  977. dest->elems[id++] = src2->elems[i2++];
  978. continue;
  979. }
  980. if (src1->elems[i1] == src2->elems[i2])
  981. ++i2;
  982. dest->elems[id++] = src1->elems[i1++];
  983. }
  984. if (i1 < src1->nelem)
  985. {
  986. memcpy (dest->elems + id, src1->elems + i1,
  987. (src1->nelem - i1) * sizeof (int));
  988. id += src1->nelem - i1;
  989. }
  990. else if (i2 < src2->nelem)
  991. {
  992. memcpy (dest->elems + id, src2->elems + i2,
  993. (src2->nelem - i2) * sizeof (int));
  994. id += src2->nelem - i2;
  995. }
  996. dest->nelem = id;
  997. return REG_NOERROR;
  998. }
  999. /* Calculate the union set of the sets DEST and SRC. And store it to
  1000. DEST. Return value indicate the error code or REG_NOERROR if succeeded. */
  1001. static reg_errcode_t
  1002. internal_function
  1003. re_node_set_merge (re_node_set *dest, const re_node_set *src)
  1004. {
  1005. int is, id, sbase, delta;
  1006. if (src == NULL || src->nelem == 0)
  1007. return REG_NOERROR;
  1008. if (dest->alloc < 2 * src->nelem + dest->nelem)
  1009. {
  1010. int new_alloc = 2 * (src->nelem + dest->alloc);
  1011. int *new_buffer = re_realloc (dest->elems, int, new_alloc);
  1012. if (BE (new_buffer == NULL, 0))
  1013. return REG_ESPACE;
  1014. dest->elems = new_buffer;
  1015. dest->alloc = new_alloc;
  1016. }
  1017. if (BE (dest->nelem == 0, 0))
  1018. {
  1019. dest->nelem = src->nelem;
  1020. memcpy (dest->elems, src->elems, src->nelem * sizeof (int));
  1021. return REG_NOERROR;
  1022. }
  1023. /* Copy into the top of DEST the items of SRC that are not
  1024. found in DEST. Maybe we could binary search in DEST? */
  1025. for (sbase = dest->nelem + 2 * src->nelem,
  1026. is = src->nelem - 1, id = dest->nelem - 1; is >= 0 && id >= 0; )
  1027. {
  1028. if (dest->elems[id] == src->elems[is])
  1029. is--, id--;
  1030. else if (dest->elems[id] < src->elems[is])
  1031. dest->elems[--sbase] = src->elems[is--];
  1032. else /* if (dest->elems[id] > src->elems[is]) */
  1033. --id;
  1034. }
  1035. if (is >= 0)
  1036. {
  1037. /* If DEST is exhausted, the remaining items of SRC must be unique. */
  1038. sbase -= is + 1;
  1039. memcpy (dest->elems + sbase, src->elems, (is + 1) * sizeof (int));
  1040. }
  1041. id = dest->nelem - 1;
  1042. is = dest->nelem + 2 * src->nelem - 1;
  1043. delta = is - sbase + 1;
  1044. if (delta == 0)
  1045. return REG_NOERROR;
  1046. /* Now copy. When DELTA becomes zero, the remaining
  1047. DEST elements are already in place. */
  1048. dest->nelem += delta;
  1049. for (;;)
  1050. {
  1051. if (dest->elems[is] > dest->elems[id])
  1052. {
  1053. /* Copy from the top. */
  1054. dest->elems[id + delta--] = dest->elems[is--];
  1055. if (delta == 0)
  1056. break;
  1057. }
  1058. else
  1059. {
  1060. /* Slide from the bottom. */
  1061. dest->elems[id + delta] = dest->elems[id];
  1062. if (--id < 0)
  1063. {
  1064. /* Copy remaining SRC elements. */
  1065. memcpy (dest->elems, dest->elems + sbase,
  1066. delta * sizeof (int));
  1067. break;
  1068. }
  1069. }
  1070. }
  1071. return REG_NOERROR;
  1072. }
  1073. /* Insert the new element ELEM to the re_node_set* SET.
  1074. SET should not already have ELEM.
  1075. return -1 if an error is occured, return 1 otherwise. */
  1076. static int
  1077. internal_function
  1078. re_node_set_insert (re_node_set *set, int elem)
  1079. {
  1080. int idx;
  1081. /* In case the set is empty. */
  1082. if (set->alloc == 0)
  1083. {
  1084. if (BE (re_node_set_init_1 (set, elem) == REG_NOERROR, 1))
  1085. return 1;
  1086. return -1;
  1087. }
  1088. if (BE (set->nelem, 0) == 0)
  1089. {
  1090. /* We already guaranteed above that set->alloc != 0. */
  1091. set->elems[0] = elem;
  1092. ++set->nelem;
  1093. return 1;
  1094. }
  1095. /* Realloc if we need. */
  1096. if (set->alloc == set->nelem)
  1097. {
  1098. int *new_elems;
  1099. set->alloc = set->alloc * 2;
  1100. new_elems = re_realloc (set->elems, int, set->alloc);
  1101. if (BE (new_elems == NULL, 0))
  1102. return -1;
  1103. set->elems = new_elems;
  1104. }
  1105. /* Move the elements which follows the new element. Test the
  1106. first element separately to skip a check in the inner loop. */
  1107. if (elem < set->elems[0])
  1108. {
  1109. idx = 0;
  1110. for (idx = set->nelem; idx > 0; idx--)
  1111. set->elems[idx] = set->elems[idx - 1];
  1112. }
  1113. else
  1114. {
  1115. for (idx = set->nelem; set->elems[idx - 1] > elem; idx--)
  1116. set->elems[idx] = set->elems[idx - 1];
  1117. }
  1118. /* Insert the new element. */
  1119. set->elems[idx] = elem;
  1120. ++set->nelem;
  1121. return 1;
  1122. }
  1123. /* Insert the new element ELEM to the re_node_set* SET.
  1124. SET should not already have any element greater than or equal to ELEM.
  1125. Return -1 if an error is occured, return 1 otherwise. */
  1126. static int
  1127. internal_function
  1128. re_node_set_insert_last (re_node_set *set, int elem)
  1129. {
  1130. /* Realloc if we need. */
  1131. if (set->alloc == set->nelem)
  1132. {
  1133. int *new_elems;
  1134. set->alloc = (set->alloc + 1) * 2;
  1135. new_elems = re_realloc (set->elems, int, set->alloc);
  1136. if (BE (new_elems == NULL, 0))
  1137. return -1;
  1138. set->elems = new_elems;
  1139. }
  1140. /* Insert the new element. */
  1141. set->elems[set->nelem++] = elem;
  1142. return 1;
  1143. }
  1144. /* Compare two node sets SET1 and SET2.
  1145. return 1 if SET1 and SET2 are equivalent, return 0 otherwise. */
  1146. static int
  1147. internal_function __attribute ((pure))
  1148. re_node_set_compare (const re_node_set *set1, const re_node_set *set2)
  1149. {
  1150. int i;
  1151. if (set1 == NULL || set2 == NULL || set1->nelem != set2->nelem)
  1152. return 0;
  1153. for (i = set1->nelem ; --i >= 0 ; )
  1154. if (set1->elems[i] != set2->elems[i])
  1155. return 0;
  1156. return 1;
  1157. }
  1158. /* Return (idx + 1) if SET contains the element ELEM, return 0 otherwise. */
  1159. static int
  1160. internal_function __attribute ((pure))
  1161. re_node_set_contains (const re_node_set *set, int elem)
  1162. {
  1163. unsigned int idx, right, mid;
  1164. if (set->nelem <= 0)
  1165. return 0;
  1166. /* Binary search the element. */
  1167. idx = 0;
  1168. right = set->nelem - 1;
  1169. while (idx < right)
  1170. {
  1171. mid = (idx + right) / 2;
  1172. if (set->elems[mid] < elem)
  1173. idx = mid + 1;
  1174. else
  1175. right = mid;
  1176. }
  1177. return set->elems[idx] == elem ? idx + 1 : 0;
  1178. }
  1179. static void
  1180. internal_function
  1181. re_node_set_remove_at (re_node_set *set, int idx)
  1182. {
  1183. if (idx < 0 || idx >= set->nelem)
  1184. return;
  1185. --set->nelem;
  1186. for (; idx < set->nelem; idx++)
  1187. set->elems[idx] = set->elems[idx + 1];
  1188. }
  1189. /* Add the token TOKEN to dfa->nodes, and return the index of the token.
  1190. Or return -1, if an error will be occured. */
  1191. static int
  1192. internal_function
  1193. re_dfa_add_node (re_dfa_t *dfa, re_token_t token)
  1194. {
  1195. #ifdef RE_ENABLE_I18N
  1196. int type = token.type;
  1197. #endif
  1198. if (BE (dfa->nodes_len >= dfa->nodes_alloc, 0))
  1199. {
  1200. size_t new_nodes_alloc = dfa->nodes_alloc * 2;
  1201. int *new_nexts, *new_indices;
  1202. re_node_set *new_edests, *new_eclosures;
  1203. re_token_t *new_nodes;
  1204. /* Avoid overflows. */
  1205. if (BE (new_nodes_alloc < dfa->nodes_alloc, 0))
  1206. return -1;
  1207. new_nodes = re_realloc (dfa->nodes, re_token_t, new_nodes_alloc);
  1208. if (BE (new_nodes == NULL, 0))
  1209. return -1;
  1210. dfa->nodes = new_nodes;
  1211. new_nexts = re_realloc (dfa->nexts, int, new_nodes_alloc);
  1212. new_indices = re_realloc (dfa->org_indices, int, new_nodes_alloc);
  1213. new_edests = re_realloc (dfa->edests, re_node_set, new_nodes_alloc);
  1214. new_eclosures = re_realloc (dfa->eclosures, re_node_set, new_nodes_alloc);
  1215. if (BE (new_nexts == NULL || new_indices == NULL
  1216. || new_edests == NULL || new_eclosures == NULL, 0))
  1217. return -1;
  1218. dfa->nexts = new_nexts;
  1219. dfa->org_indices = new_indices;
  1220. dfa->edests = new_edests;
  1221. dfa->eclosures = new_eclosures;
  1222. dfa->nodes_alloc = new_nodes_alloc;
  1223. }
  1224. dfa->nodes[dfa->nodes_len] = token;
  1225. dfa->nodes[dfa->nodes_len].constraint = 0;
  1226. #ifdef RE_ENABLE_I18N
  1227. dfa->nodes[dfa->nodes_len].accept_mb =
  1228. (type == OP_PERIOD && dfa->mb_cur_max > 1) || type == COMPLEX_BRACKET;
  1229. #endif
  1230. dfa->nexts[dfa->nodes_len] = -1;
  1231. re_node_set_init_empty (dfa->edests + dfa->nodes_len);
  1232. re_node_set_init_empty (dfa->eclosures + dfa->nodes_len);
  1233. return dfa->nodes_len++;
  1234. }
  1235. static __inline__ unsigned int
  1236. internal_function
  1237. calc_state_hash (const re_node_set *nodes, unsigned int context)
  1238. {
  1239. unsigned int hash = nodes->nelem + context;
  1240. int i;
  1241. for (i = 0 ; i < nodes->nelem ; i++)
  1242. hash += nodes->elems[i];
  1243. return hash;
  1244. }
  1245. /* Search for the state whose node_set is equivalent to NODES.
  1246. Return the pointer to the state, if we found it in the DFA.
  1247. Otherwise create the new one and return it. In case of an error
  1248. return NULL and set the error code in ERR.
  1249. Note: - We assume NULL as the invalid state, then it is possible that
  1250. return value is NULL and ERR is REG_NOERROR.
  1251. - We never return non-NULL value in case of any errors, it is for
  1252. optimization. */
  1253. static re_dfastate_t *
  1254. internal_function
  1255. re_acquire_state (reg_errcode_t *err, const re_dfa_t *dfa,
  1256. const re_node_set *nodes)
  1257. {
  1258. unsigned int hash;
  1259. re_dfastate_t *new_state;
  1260. struct re_state_table_entry *spot;
  1261. int i;
  1262. if (BE (nodes->nelem == 0, 0))
  1263. {
  1264. *err = REG_NOERROR;
  1265. return NULL;
  1266. }
  1267. hash = calc_state_hash (nodes, 0);
  1268. spot = dfa->state_table + (hash & dfa->state_hash_mask);
  1269. for (i = 0 ; i < spot->num ; i++)
  1270. {
  1271. re_dfastate_t *state = spot->array[i];
  1272. if (hash != state->hash)
  1273. continue;
  1274. if (re_node_set_compare (&state->nodes, nodes))
  1275. return state;
  1276. }
  1277. /* There are no appropriate state in the dfa, create the new one. */
  1278. new_state = create_ci_newstate (dfa, nodes, hash);
  1279. if (BE (new_state == NULL, 0))
  1280. *err = REG_ESPACE;
  1281. return new_state;
  1282. }
  1283. /* Search for the state whose node_set is equivalent to NODES and
  1284. whose context is equivalent to CONTEXT.
  1285. Return the pointer to the state, if we found it in the DFA.
  1286. Otherwise create the new one and return it. In case of an error
  1287. return NULL and set the error code in ERR.
  1288. Note: - We assume NULL as the invalid state, then it is possible that
  1289. return value is NULL and ERR is REG_NOERROR.
  1290. - We never return non-NULL value in case of any errors, it is for
  1291. optimization. */
  1292. static re_dfastate_t *
  1293. internal_function
  1294. re_acquire_state_context (reg_errcode_t *err, const re_dfa_t *dfa,
  1295. const re_node_set *nodes, unsigned int context)
  1296. {
  1297. unsigned int hash;
  1298. re_dfastate_t *new_state;
  1299. struct re_state_table_entry *spot;
  1300. int i;
  1301. if (nodes->nelem == 0)
  1302. {
  1303. *err = REG_NOERROR;
  1304. return NULL;
  1305. }
  1306. hash = calc_state_hash (nodes, context);
  1307. spot = dfa->state_table + (hash & dfa->state_hash_mask);
  1308. for (i = 0 ; i < spot->num ; i++)
  1309. {
  1310. re_dfastate_t *state = spot->array[i];
  1311. if (state->hash == hash
  1312. && state->context == context
  1313. && re_node_set_compare (state->entrance_nodes, nodes))
  1314. return state;
  1315. }
  1316. /* There are no appropriate state in `dfa', create the new one. */
  1317. new_state = create_cd_newstate (dfa, nodes, context, hash);
  1318. if (BE (new_state == NULL, 0))
  1319. *err = REG_ESPACE;
  1320. return new_state;
  1321. }
  1322. /* Finish initialization of the new state NEWSTATE, and using its hash value
  1323. HASH put in the appropriate bucket of DFA's state table. Return value
  1324. indicates the error code if failed. */
  1325. static reg_errcode_t
  1326. register_state (const re_dfa_t *dfa, re_dfastate_t *newstate,
  1327. unsigned int hash)
  1328. {
  1329. struct re_state_table_entry *spot;
  1330. reg_errcode_t err;
  1331. int i;
  1332. newstate->hash = hash;
  1333. err = re_node_set_alloc (&newstate->non_eps_nodes, newstate->nodes.nelem);
  1334. if (BE (err != REG_NOERROR, 0))
  1335. return REG_ESPACE;
  1336. for (i = 0; i < newstate->nodes.nelem; i++)
  1337. {
  1338. int elem = newstate->nodes.elems[i];
  1339. if (!IS_EPSILON_NODE (dfa->nodes[elem].type))
  1340. re_node_set_insert_last (&newstate->non_eps_nodes, elem);
  1341. }
  1342. spot = dfa->state_table + (hash & dfa->state_hash_mask);
  1343. if (BE (spot->alloc <= spot->num, 0))
  1344. {
  1345. int new_alloc = 2 * spot->num + 2;
  1346. re_dfastate_t **new_array = re_realloc (spot->array, re_dfastate_t *,
  1347. new_alloc);
  1348. if (BE (new_array == NULL, 0))
  1349. return REG_ESPACE;
  1350. spot->array = new_array;
  1351. spot->alloc = new_alloc;
  1352. }
  1353. spot->array[spot->num++] = newstate;
  1354. return REG_NOERROR;
  1355. }
  1356. static void
  1357. free_state (re_dfastate_t *state)
  1358. {
  1359. re_node_set_free (&state->non_eps_nodes);
  1360. re_node_set_free (&state->inveclosure);
  1361. if (state->entrance_nodes != &state->nodes)
  1362. {
  1363. re_node_set_free (state->entrance_nodes);
  1364. re_free (state->entrance_nodes);
  1365. }
  1366. re_node_set_free (&state->nodes);
  1367. re_free (state->word_trtable);
  1368. re_free (state->trtable);
  1369. re_free (state);
  1370. }
  1371. /* Create the new state which is independ of contexts.
  1372. Return the new state if succeeded, otherwise return NULL. */
  1373. static re_dfastate_t *
  1374. internal_function
  1375. create_ci_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
  1376. unsigned int hash)
  1377. {
  1378. int i;
  1379. reg_errcode_t err;
  1380. re_dfastate_t *newstate;
  1381. newstate = calloc (sizeof (re_dfastate_t), 1);
  1382. if (BE (newstate == NULL, 0))
  1383. return NULL;
  1384. err = re_node_set_init_copy (&newstate->nodes, nodes);
  1385. if (BE (err != REG_NOERROR, 0))
  1386. {
  1387. re_free (newstate);
  1388. return NULL;
  1389. }
  1390. newstate->entrance_nodes = &newstate->nodes;
  1391. for (i = 0 ; i < nodes->nelem ; i++)
  1392. {
  1393. re_token_t *node = dfa->nodes + nodes->elems[i];
  1394. re_token_type_t type = node->type;
  1395. if (type == CHARACTER && !node->constraint)
  1396. continue;
  1397. #ifdef RE_ENABLE_I18N
  1398. newstate->accept_mb |= node->accept_mb;
  1399. #endif
  1400. /* If the state has the halt node, the state is a halt state. */
  1401. if (type == END_OF_RE)
  1402. newstate->halt = 1;
  1403. else if (type == OP_BACK_REF)
  1404. newstate->has_backref = 1;
  1405. else if (type == ANCHOR || node->constraint)
  1406. newstate->has_constraint = 1;
  1407. }
  1408. err = register_state (dfa, newstate, hash);
  1409. if (BE (err != REG_NOERROR, 0))
  1410. {
  1411. free_state (newstate);
  1412. newstate = NULL;
  1413. }
  1414. return newstate;
  1415. }
  1416. /* Create the new state which is depend on the context CONTEXT.
  1417. Return the new state if succeeded, otherwise return NULL. */
  1418. static re_dfastate_t *
  1419. internal_function
  1420. create_cd_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
  1421. unsigned int context, unsigned int hash)
  1422. {
  1423. int i, nctx_nodes = 0;
  1424. reg_errcode_t err;
  1425. re_dfastate_t *newstate;
  1426. newstate = calloc (sizeof (re_dfastate_t), 1);
  1427. if (BE (newstate == NULL, 0))
  1428. return NULL;
  1429. err = re_node_set_init_copy (&newstate->nodes, nodes);
  1430. if (BE (err != REG_NOERROR, 0))
  1431. {
  1432. re_free (newstate);
  1433. return NULL;
  1434. }
  1435. newstate->context = context;
  1436. newstate->entrance_nodes = &newstate->nodes;
  1437. for (i = 0 ; i < nodes->nelem ; i++)
  1438. {
  1439. unsigned int constraint = 0;
  1440. re_token_t *node = dfa->nodes + nodes->elems[i];
  1441. re_token_type_t type = node->type;
  1442. if (node->constraint)
  1443. constraint = node->constraint;
  1444. if (type == CHARACTER && !constraint)
  1445. continue;
  1446. #ifdef RE_ENABLE_I18N
  1447. newstate->accept_mb |= node->accept_mb;
  1448. #endif /* RE_ENABLE_I18N */
  1449. /* If the state has the halt node, the state is a halt state. */
  1450. if (type == END_OF_RE)
  1451. newstate->halt = 1;
  1452. else if (type == OP_BACK_REF)
  1453. newstate->has_backref = 1;
  1454. else if (type == ANCHOR)
  1455. constraint = node->opr.ctx_type;
  1456. if (constraint)
  1457. {
  1458. if (newstate->entrance_nodes == &newstate->nodes)
  1459. {
  1460. newstate->entrance_nodes = re_malloc (re_node_set, 1);
  1461. if (BE (newstate->entrance_nodes == NULL, 0))
  1462. {
  1463. free_state (newstate);
  1464. return NULL;
  1465. }
  1466. re_node_set_init_copy (newstate->entrance_nodes, nodes);
  1467. nctx_nodes = 0;
  1468. newstate->has_constraint = 1;
  1469. }
  1470. if (NOT_SATISFY_PREV_CONSTRAINT (constraint,context))
  1471. {
  1472. re_node_set_remove_at (&newstate->nodes, i - nctx_nodes);
  1473. ++nctx_nodes;
  1474. }
  1475. }
  1476. }
  1477. err = register_state (dfa, newstate, hash);
  1478. if (BE (err != REG_NOERROR, 0))
  1479. {
  1480. free_state (newstate);
  1481. newstate = NULL;
  1482. }
  1483. return newstate;
  1484. }