regex_internal.c 45 KB

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