regex_internal.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  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. #ifdef 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, *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. if (BE (pstr->trans != NULL, 0))
  597. {
  598. int i = mlen < 6 ? mlen : 6;
  599. while (--i >= 0)
  600. buf[i] = pstr->trans[p[i]];
  601. }
  602. /* XXX Don't use mbrtowc, we know which conversion
  603. to use (UTF-8 -> UCS4). */
  604. memset (&cur_state, 0, sizeof (cur_state));
  605. mbclen = mbrtowc (&wc2, (const char *) p, mlen,
  606. &cur_state);
  607. if (raw + offset - p <= mbclen
  608. && mbclen < (size_t) -2)
  609. {
  610. memset (&pstr->cur_state, '\0',
  611. sizeof (mbstate_t));
  612. pstr->valid_len = mbclen - (raw + offset - p);
  613. wc = wc2;
  614. }
  615. break;
  616. }
  617. }
  618. if (wc == WEOF)
  619. pstr->valid_len = re_string_skip_chars (pstr, idx, &wc) - idx;
  620. if (BE (pstr->valid_len, 0))
  621. {
  622. for (wcs_idx = 0; wcs_idx < pstr->valid_len; ++wcs_idx)
  623. pstr->wcs[wcs_idx] = WEOF;
  624. if (pstr->mbs_allocated)
  625. memset (pstr->mbs, 255, pstr->valid_len);
  626. }
  627. pstr->valid_raw_len = pstr->valid_len;
  628. pstr->tip_context = ((BE (pstr->word_ops_used != 0, 0)
  629. && IS_WIDE_WORD_CHAR (wc))
  630. ? CONTEXT_WORD
  631. : ((IS_WIDE_NEWLINE (wc)
  632. && pstr->newline_anchor)
  633. ? CONTEXT_NEWLINE : 0));
  634. }
  635. else
  636. #endif /* RE_ENABLE_I18N */
  637. {
  638. int c = pstr->raw_mbs[pstr->raw_mbs_idx + offset - 1];
  639. if (pstr->trans)
  640. c = pstr->trans[c];
  641. pstr->tip_context = (bitset_contain (pstr->word_char, c)
  642. ? CONTEXT_WORD
  643. : ((IS_NEWLINE (c) && pstr->newline_anchor)
  644. ? CONTEXT_NEWLINE : 0));
  645. }
  646. }
  647. if (!BE (pstr->mbs_allocated, 0))
  648. pstr->mbs += offset;
  649. }
  650. pstr->raw_mbs_idx = idx;
  651. pstr->len -= offset;
  652. pstr->stop -= offset;
  653. /* Then build the buffers. */
  654. #ifdef RE_ENABLE_I18N
  655. if (pstr->mb_cur_max > 1)
  656. {
  657. if (pstr->icase)
  658. {
  659. reg_errcode_t ret = build_wcs_upper_buffer (pstr);
  660. if (BE (ret != REG_NOERROR, 0))
  661. return ret;
  662. }
  663. else
  664. build_wcs_buffer (pstr);
  665. }
  666. else
  667. #endif
  668. if (BE (pstr->mbs_allocated, 0))
  669. {
  670. if (pstr->icase)
  671. build_upper_buffer (pstr);
  672. else if (pstr->trans != NULL)
  673. re_string_translate_buffer (pstr);
  674. }
  675. else
  676. pstr->valid_len = pstr->len;
  677. pstr->cur_idx = 0;
  678. return REG_NOERROR;
  679. }
  680. static unsigned char
  681. internal_function __attribute ((pure))
  682. re_string_peek_byte_case (const re_string_t *pstr, int idx)
  683. {
  684. int ch, off;
  685. /* Handle the common (easiest) cases first. */
  686. if (BE (!pstr->mbs_allocated, 1))
  687. return re_string_peek_byte (pstr, idx);
  688. #ifdef RE_ENABLE_I18N
  689. if (pstr->mb_cur_max > 1
  690. && ! re_string_is_single_byte_char (pstr, pstr->cur_idx + idx))
  691. return re_string_peek_byte (pstr, idx);
  692. #endif
  693. off = pstr->cur_idx + idx;
  694. #ifdef RE_ENABLE_I18N
  695. if (pstr->offsets_needed)
  696. off = pstr->offsets[off];
  697. #endif
  698. ch = pstr->raw_mbs[pstr->raw_mbs_idx + off];
  699. #ifdef RE_ENABLE_I18N
  700. /* Ensure that e.g. for tr_TR.UTF-8 BACKSLASH DOTLESS SMALL LETTER I
  701. this function returns CAPITAL LETTER I instead of first byte of
  702. DOTLESS SMALL LETTER I. The latter would confuse the parser,
  703. since peek_byte_case doesn't advance cur_idx in any way. */
  704. if (pstr->offsets_needed && !isascii (ch))
  705. return re_string_peek_byte (pstr, idx);
  706. #endif
  707. return ch;
  708. }
  709. static unsigned char
  710. internal_function __attribute ((pure))
  711. re_string_fetch_byte_case (re_string_t *pstr)
  712. {
  713. if (BE (!pstr->mbs_allocated, 1))
  714. return re_string_fetch_byte (pstr);
  715. #ifdef RE_ENABLE_I18N
  716. if (pstr->offsets_needed)
  717. {
  718. int off, ch;
  719. /* For tr_TR.UTF-8 [[:islower:]] there is
  720. [[: CAPITAL LETTER I WITH DOT lower:]] in mbs. Skip
  721. in that case the whole multi-byte character and return
  722. the original letter. On the other side, with
  723. [[: DOTLESS SMALL LETTER I return [[:I, as doing
  724. anything else would complicate things too much. */
  725. if (!re_string_first_byte (pstr, pstr->cur_idx))
  726. return re_string_fetch_byte (pstr);
  727. off = pstr->offsets[pstr->cur_idx];
  728. ch = pstr->raw_mbs[pstr->raw_mbs_idx + off];
  729. if (! isascii (ch))
  730. return re_string_fetch_byte (pstr);
  731. re_string_skip_bytes (pstr,
  732. re_string_char_size_at (pstr, pstr->cur_idx));
  733. return ch;
  734. }
  735. #endif
  736. return pstr->raw_mbs[pstr->raw_mbs_idx + pstr->cur_idx++];
  737. }
  738. static void
  739. internal_function
  740. re_string_destruct (re_string_t *pstr)
  741. {
  742. #ifdef RE_ENABLE_I18N
  743. re_free (pstr->wcs);
  744. re_free (pstr->offsets);
  745. #endif /* RE_ENABLE_I18N */
  746. if (pstr->mbs_allocated)
  747. re_free (pstr->mbs);
  748. }
  749. /* Return the context at IDX in INPUT. */
  750. static unsigned int
  751. internal_function
  752. re_string_context_at (const re_string_t *input, int idx, int eflags)
  753. {
  754. int c;
  755. if (BE (idx < 0, 0))
  756. /* In this case, we use the value stored in input->tip_context,
  757. since we can't know the character in input->mbs[-1] here. */
  758. return input->tip_context;
  759. if (BE (idx == input->len, 0))
  760. return ((eflags & REG_NOTEOL) ? CONTEXT_ENDBUF
  761. : CONTEXT_NEWLINE | CONTEXT_ENDBUF);
  762. #ifdef RE_ENABLE_I18N
  763. if (input->mb_cur_max > 1)
  764. {
  765. wint_t wc;
  766. int wc_idx = idx;
  767. while(input->wcs[wc_idx] == WEOF)
  768. {
  769. #ifdef DEBUG
  770. /* It must not happen. */
  771. assert (wc_idx >= 0);
  772. #endif
  773. --wc_idx;
  774. if (wc_idx < 0)
  775. return input->tip_context;
  776. }
  777. wc = input->wcs[wc_idx];
  778. if (BE (input->word_ops_used != 0, 0) && IS_WIDE_WORD_CHAR (wc))
  779. return CONTEXT_WORD;
  780. return (IS_WIDE_NEWLINE (wc) && input->newline_anchor
  781. ? CONTEXT_NEWLINE : 0);
  782. }
  783. #endif
  784. c = re_string_byte_at (input, idx);
  785. if (bitset_contain (input->word_char, c))
  786. return CONTEXT_WORD;
  787. return IS_NEWLINE (c) && input->newline_anchor ? CONTEXT_NEWLINE : 0;
  788. }
  789. /* Functions for set operation. */
  790. static reg_errcode_t
  791. internal_function
  792. re_node_set_alloc (re_node_set *set, int size)
  793. {
  794. set->alloc = size;
  795. set->nelem = 0;
  796. set->elems = re_malloc (int, size); /* can be NULL if size == 0
  797. (see re_node_set_init_empty(set)) */
  798. if (BE (set->elems == NULL && size != 0, 0))
  799. return REG_ESPACE;
  800. return REG_NOERROR;
  801. }
  802. static reg_errcode_t
  803. internal_function
  804. re_node_set_init_1 (re_node_set *set, int elem)
  805. {
  806. set->alloc = 1;
  807. set->nelem = 1;
  808. set->elems = re_malloc (int, 1);
  809. if (BE (set->elems == NULL, 0))
  810. {
  811. set->alloc = set->nelem = 0;
  812. return REG_ESPACE;
  813. }
  814. set->elems[0] = elem;
  815. return REG_NOERROR;
  816. }
  817. static reg_errcode_t
  818. internal_function
  819. re_node_set_init_2 (re_node_set *set, int elem1, int elem2)
  820. {
  821. set->alloc = 2;
  822. set->elems = re_malloc (int, 2);
  823. if (BE (set->elems == NULL, 0))
  824. return REG_ESPACE;
  825. if (elem1 == elem2)
  826. {
  827. set->nelem = 1;
  828. set->elems[0] = elem1;
  829. }
  830. else
  831. {
  832. set->nelem = 2;
  833. if (elem1 < elem2)
  834. {
  835. set->elems[0] = elem1;
  836. set->elems[1] = elem2;
  837. }
  838. else
  839. {
  840. set->elems[0] = elem2;
  841. set->elems[1] = elem1;
  842. }
  843. }
  844. return REG_NOERROR;
  845. }
  846. static reg_errcode_t
  847. internal_function
  848. re_node_set_init_copy (re_node_set *dest, const re_node_set *src)
  849. {
  850. dest->nelem = src->nelem;
  851. if (src->nelem > 0)
  852. {
  853. dest->alloc = dest->nelem;
  854. dest->elems = re_malloc (int, dest->alloc);
  855. if (BE (dest->elems == NULL, 0))
  856. {
  857. dest->alloc = dest->nelem = 0;
  858. return REG_ESPACE;
  859. }
  860. memcpy (dest->elems, src->elems, src->nelem * sizeof (int));
  861. }
  862. else
  863. re_node_set_init_empty (dest);
  864. return REG_NOERROR;
  865. }
  866. /* Calculate the intersection of the sets SRC1 and SRC2. And merge it to
  867. DEST. Return value indicate the error code or REG_NOERROR if succeeded.
  868. Note: We assume dest->elems is NULL, when dest->alloc is 0. */
  869. static reg_errcode_t
  870. internal_function
  871. re_node_set_add_intersect (re_node_set *dest, const re_node_set *src1,
  872. const re_node_set *src2)
  873. {
  874. int i1, i2, is, id, delta, sbase;
  875. if (src1->nelem == 0 || src2->nelem == 0)
  876. return REG_NOERROR;
  877. /* We need dest->nelem + 2 * elems_in_intersection; this is a
  878. conservative estimate. */
  879. if (src1->nelem + src2->nelem + dest->nelem > dest->alloc)
  880. {
  881. int new_alloc = src1->nelem + src2->nelem + dest->alloc;
  882. int *new_elems = re_realloc (dest->elems, int, new_alloc);
  883. if (BE (new_elems == NULL, 0))
  884. return REG_ESPACE;
  885. dest->elems = new_elems;
  886. dest->alloc = new_alloc;
  887. }
  888. /* Find the items in the intersection of SRC1 and SRC2, and copy
  889. into the top of DEST those that are not already in DEST itself. */
  890. sbase = dest->nelem + src1->nelem + src2->nelem;
  891. i1 = src1->nelem - 1;
  892. i2 = src2->nelem - 1;
  893. id = dest->nelem - 1;
  894. for (;;)
  895. {
  896. if (src1->elems[i1] == src2->elems[i2])
  897. {
  898. /* Try to find the item in DEST. Maybe we could binary search? */
  899. while (id >= 0 && dest->elems[id] > src1->elems[i1])
  900. --id;
  901. if (id < 0 || dest->elems[id] != src1->elems[i1])
  902. dest->elems[--sbase] = src1->elems[i1];
  903. if (--i1 < 0 || --i2 < 0)
  904. break;
  905. }
  906. /* Lower the highest of the two items. */
  907. else if (src1->elems[i1] < src2->elems[i2])
  908. {
  909. if (--i2 < 0)
  910. break;
  911. }
  912. else
  913. {
  914. if (--i1 < 0)
  915. break;
  916. }
  917. }
  918. id = dest->nelem - 1;
  919. is = dest->nelem + src1->nelem + src2->nelem - 1;
  920. delta = is - sbase + 1;
  921. /* Now copy. When DELTA becomes zero, the remaining
  922. DEST elements are already in place; this is more or
  923. less the same loop that is in re_node_set_merge. */
  924. dest->nelem += delta;
  925. if (delta > 0 && id >= 0)
  926. for (;;)
  927. {
  928. if (dest->elems[is] > dest->elems[id])
  929. {
  930. /* Copy from the top. */
  931. dest->elems[id + delta--] = dest->elems[is--];
  932. if (delta == 0)
  933. break;
  934. }
  935. else
  936. {
  937. /* Slide from the bottom. */
  938. dest->elems[id + delta] = dest->elems[id];
  939. if (--id < 0)
  940. break;
  941. }
  942. }
  943. /* Copy remaining SRC elements. */
  944. memcpy (dest->elems, dest->elems + sbase, delta * sizeof (int));
  945. return REG_NOERROR;
  946. }
  947. /* Calculate the union set of the sets SRC1 and SRC2. And store it to
  948. DEST. Return value indicate the error code or REG_NOERROR if succeeded. */
  949. static reg_errcode_t
  950. internal_function
  951. re_node_set_init_union (re_node_set *dest, const re_node_set *src1,
  952. const re_node_set *src2)
  953. {
  954. int i1, i2, id;
  955. if (src1 != NULL && src1->nelem > 0 && src2 != NULL && src2->nelem > 0)
  956. {
  957. dest->alloc = src1->nelem + src2->nelem;
  958. dest->elems = re_malloc (int, dest->alloc);
  959. if (BE (dest->elems == NULL, 0))
  960. return REG_ESPACE;
  961. }
  962. else
  963. {
  964. if (src1 != NULL && src1->nelem > 0)
  965. return re_node_set_init_copy (dest, src1);
  966. if (src2 != NULL && src2->nelem > 0)
  967. return re_node_set_init_copy (dest, src2);
  968. re_node_set_init_empty (dest);
  969. return REG_NOERROR;
  970. }
  971. for (i1 = i2 = id = 0 ; i1 < src1->nelem && i2 < src2->nelem ;)
  972. {
  973. if (src1->elems[i1] > src2->elems[i2])
  974. {
  975. dest->elems[id++] = src2->elems[i2++];
  976. continue;
  977. }
  978. if (src1->elems[i1] == src2->elems[i2])
  979. ++i2;
  980. dest->elems[id++] = src1->elems[i1++];
  981. }
  982. if (i1 < src1->nelem)
  983. {
  984. memcpy (dest->elems + id, src1->elems + i1,
  985. (src1->nelem - i1) * sizeof (int));
  986. id += src1->nelem - i1;
  987. }
  988. else if (i2 < src2->nelem)
  989. {
  990. memcpy (dest->elems + id, src2->elems + i2,
  991. (src2->nelem - i2) * sizeof (int));
  992. id += src2->nelem - i2;
  993. }
  994. dest->nelem = id;
  995. return REG_NOERROR;
  996. }
  997. /* Calculate the union set of the sets DEST and SRC. And store it to
  998. DEST. Return value indicate the error code or REG_NOERROR if succeeded. */
  999. static reg_errcode_t
  1000. internal_function
  1001. re_node_set_merge (re_node_set *dest, const re_node_set *src)
  1002. {
  1003. int is, id, sbase, delta;
  1004. if (src == NULL || src->nelem == 0)
  1005. return REG_NOERROR;
  1006. if (dest->alloc < 2 * src->nelem + dest->nelem)
  1007. {
  1008. int new_alloc = 2 * (src->nelem + dest->alloc);
  1009. int *new_buffer = re_realloc (dest->elems, int, new_alloc);
  1010. if (BE (new_buffer == NULL, 0))
  1011. return REG_ESPACE;
  1012. dest->elems = new_buffer;
  1013. dest->alloc = new_alloc;
  1014. }
  1015. if (BE (dest->nelem == 0, 0))
  1016. {
  1017. dest->nelem = src->nelem;
  1018. memcpy (dest->elems, src->elems, src->nelem * sizeof (int));
  1019. return REG_NOERROR;
  1020. }
  1021. /* Copy into the top of DEST the items of SRC that are not
  1022. found in DEST. Maybe we could binary search in DEST? */
  1023. for (sbase = dest->nelem + 2 * src->nelem,
  1024. is = src->nelem - 1, id = dest->nelem - 1; is >= 0 && id >= 0; )
  1025. {
  1026. if (dest->elems[id] == src->elems[is])
  1027. is--, id--;
  1028. else if (dest->elems[id] < src->elems[is])
  1029. dest->elems[--sbase] = src->elems[is--];
  1030. else /* if (dest->elems[id] > src->elems[is]) */
  1031. --id;
  1032. }
  1033. if (is >= 0)
  1034. {
  1035. /* If DEST is exhausted, the remaining items of SRC must be unique. */
  1036. sbase -= is + 1;
  1037. memcpy (dest->elems + sbase, src->elems, (is + 1) * sizeof (int));
  1038. }
  1039. id = dest->nelem - 1;
  1040. is = dest->nelem + 2 * src->nelem - 1;
  1041. delta = is - sbase + 1;
  1042. if (delta == 0)
  1043. return REG_NOERROR;
  1044. /* Now copy. When DELTA becomes zero, the remaining
  1045. DEST elements are already in place. */
  1046. dest->nelem += delta;
  1047. for (;;)
  1048. {
  1049. if (dest->elems[is] > dest->elems[id])
  1050. {
  1051. /* Copy from the top. */
  1052. dest->elems[id + delta--] = dest->elems[is--];
  1053. if (delta == 0)
  1054. break;
  1055. }
  1056. else
  1057. {
  1058. /* Slide from the bottom. */
  1059. dest->elems[id + delta] = dest->elems[id];
  1060. if (--id < 0)
  1061. {
  1062. /* Copy remaining SRC elements. */
  1063. memcpy (dest->elems, dest->elems + sbase,
  1064. delta * sizeof (int));
  1065. break;
  1066. }
  1067. }
  1068. }
  1069. return REG_NOERROR;
  1070. }
  1071. /* Insert the new element ELEM to the re_node_set* SET.
  1072. SET should not already have ELEM.
  1073. return -1 if an error is occured, return 1 otherwise. */
  1074. static int
  1075. internal_function
  1076. re_node_set_insert (re_node_set *set, int elem)
  1077. {
  1078. int idx;
  1079. /* In case the set is empty. */
  1080. if (set->alloc == 0)
  1081. {
  1082. if (BE (re_node_set_init_1 (set, elem) == REG_NOERROR, 1))
  1083. return 1;
  1084. return -1;
  1085. }
  1086. if (BE (set->nelem, 0) == 0)
  1087. {
  1088. /* We already guaranteed above that set->alloc != 0. */
  1089. set->elems[0] = elem;
  1090. ++set->nelem;
  1091. return 1;
  1092. }
  1093. /* Realloc if we need. */
  1094. if (set->alloc == set->nelem)
  1095. {
  1096. int *new_elems;
  1097. set->alloc = set->alloc * 2;
  1098. new_elems = re_realloc (set->elems, int, set->alloc);
  1099. if (BE (new_elems == NULL, 0))
  1100. return -1;
  1101. set->elems = new_elems;
  1102. }
  1103. /* Move the elements which follows the new element. Test the
  1104. first element separately to skip a check in the inner loop. */
  1105. if (elem < set->elems[0])
  1106. {
  1107. idx = 0;
  1108. for (idx = set->nelem; idx > 0; idx--)
  1109. set->elems[idx] = set->elems[idx - 1];
  1110. }
  1111. else
  1112. {
  1113. for (idx = set->nelem; set->elems[idx - 1] > elem; idx--)
  1114. set->elems[idx] = set->elems[idx - 1];
  1115. }
  1116. /* Insert the new element. */
  1117. set->elems[idx] = elem;
  1118. ++set->nelem;
  1119. return 1;
  1120. }
  1121. /* Insert the new element ELEM to the re_node_set* SET.
  1122. SET should not already have any element greater than or equal to ELEM.
  1123. Return -1 if an error is occured, return 1 otherwise. */
  1124. static int
  1125. internal_function
  1126. re_node_set_insert_last (re_node_set *set, int elem)
  1127. {
  1128. /* Realloc if we need. */
  1129. if (set->alloc == set->nelem)
  1130. {
  1131. int *new_elems;
  1132. set->alloc = (set->alloc + 1) * 2;
  1133. new_elems = re_realloc (set->elems, int, set->alloc);
  1134. if (BE (new_elems == NULL, 0))
  1135. return -1;
  1136. set->elems = new_elems;
  1137. }
  1138. /* Insert the new element. */
  1139. set->elems[set->nelem++] = elem;
  1140. return 1;
  1141. }
  1142. /* Compare two node sets SET1 and SET2.
  1143. return 1 if SET1 and SET2 are equivalent, return 0 otherwise. */
  1144. static int
  1145. internal_function __attribute ((pure))
  1146. re_node_set_compare (const re_node_set *set1, const re_node_set *set2)
  1147. {
  1148. int i;
  1149. if (set1 == NULL || set2 == NULL || set1->nelem != set2->nelem)
  1150. return 0;
  1151. for (i = set1->nelem ; --i >= 0 ; )
  1152. if (set1->elems[i] != set2->elems[i])
  1153. return 0;
  1154. return 1;
  1155. }
  1156. /* Return (idx + 1) if SET contains the element ELEM, return 0 otherwise. */
  1157. static int
  1158. internal_function __attribute ((pure))
  1159. re_node_set_contains (const re_node_set *set, int elem)
  1160. {
  1161. unsigned int idx, right, mid;
  1162. if (set->nelem <= 0)
  1163. return 0;
  1164. /* Binary search the element. */
  1165. idx = 0;
  1166. right = set->nelem - 1;
  1167. while (idx < right)
  1168. {
  1169. mid = (idx + right) / 2;
  1170. if (set->elems[mid] < elem)
  1171. idx = mid + 1;
  1172. else
  1173. right = mid;
  1174. }
  1175. return set->elems[idx] == elem ? idx + 1 : 0;
  1176. }
  1177. static void
  1178. internal_function
  1179. re_node_set_remove_at (re_node_set *set, int idx)
  1180. {
  1181. if (idx < 0 || idx >= set->nelem)
  1182. return;
  1183. --set->nelem;
  1184. for (; idx < set->nelem; idx++)
  1185. set->elems[idx] = set->elems[idx + 1];
  1186. }
  1187. /* Add the token TOKEN to dfa->nodes, and return the index of the token.
  1188. Or return -1, if an error will be occured. */
  1189. static int
  1190. internal_function
  1191. re_dfa_add_node (re_dfa_t *dfa, re_token_t token)
  1192. {
  1193. #ifdef RE_ENABLE_I18N
  1194. int type = token.type;
  1195. #endif
  1196. if (BE (dfa->nodes_len >= dfa->nodes_alloc, 0))
  1197. {
  1198. size_t new_nodes_alloc = dfa->nodes_alloc * 2;
  1199. int *new_nexts, *new_indices;
  1200. re_node_set *new_edests, *new_eclosures;
  1201. re_token_t *new_nodes;
  1202. /* Avoid overflows. */
  1203. if (BE (new_nodes_alloc < dfa->nodes_alloc, 0))
  1204. return -1;
  1205. new_nodes = re_realloc (dfa->nodes, re_token_t, new_nodes_alloc);
  1206. if (BE (new_nodes == NULL, 0))
  1207. return -1;
  1208. dfa->nodes = new_nodes;
  1209. new_nexts = re_realloc (dfa->nexts, int, new_nodes_alloc);
  1210. new_indices = re_realloc (dfa->org_indices, int, new_nodes_alloc);
  1211. new_edests = re_realloc (dfa->edests, re_node_set, new_nodes_alloc);
  1212. new_eclosures = re_realloc (dfa->eclosures, re_node_set, new_nodes_alloc);
  1213. if (BE (new_nexts == NULL || new_indices == NULL
  1214. || new_edests == NULL || new_eclosures == NULL, 0))
  1215. return -1;
  1216. dfa->nexts = new_nexts;
  1217. dfa->org_indices = new_indices;
  1218. dfa->edests = new_edests;
  1219. dfa->eclosures = new_eclosures;
  1220. dfa->nodes_alloc = new_nodes_alloc;
  1221. }
  1222. dfa->nodes[dfa->nodes_len] = token;
  1223. dfa->nodes[dfa->nodes_len].constraint = 0;
  1224. #ifdef RE_ENABLE_I18N
  1225. dfa->nodes[dfa->nodes_len].accept_mb =
  1226. (type == OP_PERIOD && dfa->mb_cur_max > 1) || type == COMPLEX_BRACKET;
  1227. #endif
  1228. dfa->nexts[dfa->nodes_len] = -1;
  1229. re_node_set_init_empty (dfa->edests + dfa->nodes_len);
  1230. re_node_set_init_empty (dfa->eclosures + dfa->nodes_len);
  1231. return dfa->nodes_len++;
  1232. }
  1233. static __inline__ unsigned int
  1234. internal_function
  1235. calc_state_hash (const re_node_set *nodes, unsigned int context)
  1236. {
  1237. unsigned int hash = nodes->nelem + context;
  1238. int i;
  1239. for (i = 0 ; i < nodes->nelem ; i++)
  1240. hash += nodes->elems[i];
  1241. return hash;
  1242. }
  1243. /* Search for the state whose node_set is equivalent to NODES.
  1244. Return the pointer to the state, if we found it in the DFA.
  1245. Otherwise create the new one and return it. In case of an error
  1246. return NULL and set the error code in ERR.
  1247. Note: - We assume NULL as the invalid state, then it is possible that
  1248. return value is NULL and ERR is REG_NOERROR.
  1249. - We never return non-NULL value in case of any errors, it is for
  1250. optimization. */
  1251. static re_dfastate_t *
  1252. internal_function
  1253. re_acquire_state (reg_errcode_t *err, const re_dfa_t *dfa,
  1254. const re_node_set *nodes)
  1255. {
  1256. unsigned int hash;
  1257. re_dfastate_t *new_state;
  1258. struct re_state_table_entry *spot;
  1259. int i;
  1260. if (BE (nodes->nelem == 0, 0))
  1261. {
  1262. *err = REG_NOERROR;
  1263. return NULL;
  1264. }
  1265. hash = calc_state_hash (nodes, 0);
  1266. spot = dfa->state_table + (hash & dfa->state_hash_mask);
  1267. for (i = 0 ; i < spot->num ; i++)
  1268. {
  1269. re_dfastate_t *state = spot->array[i];
  1270. if (hash != state->hash)
  1271. continue;
  1272. if (re_node_set_compare (&state->nodes, nodes))
  1273. return state;
  1274. }
  1275. /* There are no appropriate state in the dfa, create the new one. */
  1276. new_state = create_ci_newstate (dfa, nodes, hash);
  1277. if (BE (new_state == NULL, 0))
  1278. *err = REG_ESPACE;
  1279. return new_state;
  1280. }
  1281. /* Search for the state whose node_set is equivalent to NODES and
  1282. whose context is equivalent to CONTEXT.
  1283. Return the pointer to the state, if we found it in the DFA.
  1284. Otherwise create the new one and return it. In case of an error
  1285. return NULL and set the error code in ERR.
  1286. Note: - We assume NULL as the invalid state, then it is possible that
  1287. return value is NULL and ERR is REG_NOERROR.
  1288. - We never return non-NULL value in case of any errors, it is for
  1289. optimization. */
  1290. static re_dfastate_t *
  1291. internal_function
  1292. re_acquire_state_context (reg_errcode_t *err, const re_dfa_t *dfa,
  1293. const re_node_set *nodes, unsigned int context)
  1294. {
  1295. unsigned int hash;
  1296. re_dfastate_t *new_state;
  1297. struct re_state_table_entry *spot;
  1298. int i;
  1299. if (nodes->nelem == 0)
  1300. {
  1301. *err = REG_NOERROR;
  1302. return NULL;
  1303. }
  1304. hash = calc_state_hash (nodes, context);
  1305. spot = dfa->state_table + (hash & dfa->state_hash_mask);
  1306. for (i = 0 ; i < spot->num ; i++)
  1307. {
  1308. re_dfastate_t *state = spot->array[i];
  1309. if (state->hash == hash
  1310. && state->context == context
  1311. && re_node_set_compare (state->entrance_nodes, nodes))
  1312. return state;
  1313. }
  1314. /* There are no appropriate state in `dfa', create the new one. */
  1315. new_state = create_cd_newstate (dfa, nodes, context, hash);
  1316. if (BE (new_state == NULL, 0))
  1317. *err = REG_ESPACE;
  1318. return new_state;
  1319. }
  1320. /* Finish initialization of the new state NEWSTATE, and using its hash value
  1321. HASH put in the appropriate bucket of DFA's state table. Return value
  1322. indicates the error code if failed. */
  1323. static reg_errcode_t
  1324. register_state (const re_dfa_t *dfa, re_dfastate_t *newstate,
  1325. unsigned int hash)
  1326. {
  1327. struct re_state_table_entry *spot;
  1328. reg_errcode_t err;
  1329. int i;
  1330. newstate->hash = hash;
  1331. err = re_node_set_alloc (&newstate->non_eps_nodes, newstate->nodes.nelem);
  1332. if (BE (err != REG_NOERROR, 0))
  1333. return REG_ESPACE;
  1334. for (i = 0; i < newstate->nodes.nelem; i++)
  1335. {
  1336. int elem = newstate->nodes.elems[i];
  1337. if (!IS_EPSILON_NODE (dfa->nodes[elem].type))
  1338. re_node_set_insert_last (&newstate->non_eps_nodes, elem);
  1339. }
  1340. spot = dfa->state_table + (hash & dfa->state_hash_mask);
  1341. if (BE (spot->alloc <= spot->num, 0))
  1342. {
  1343. int new_alloc = 2 * spot->num + 2;
  1344. re_dfastate_t **new_array = re_realloc (spot->array, re_dfastate_t *,
  1345. new_alloc);
  1346. if (BE (new_array == NULL, 0))
  1347. return REG_ESPACE;
  1348. spot->array = new_array;
  1349. spot->alloc = new_alloc;
  1350. }
  1351. spot->array[spot->num++] = newstate;
  1352. return REG_NOERROR;
  1353. }
  1354. static void
  1355. free_state (re_dfastate_t *state)
  1356. {
  1357. re_node_set_free (&state->non_eps_nodes);
  1358. re_node_set_free (&state->inveclosure);
  1359. if (state->entrance_nodes != &state->nodes)
  1360. {
  1361. re_node_set_free (state->entrance_nodes);
  1362. re_free (state->entrance_nodes);
  1363. }
  1364. re_node_set_free (&state->nodes);
  1365. re_free (state->word_trtable);
  1366. re_free (state->trtable);
  1367. re_free (state);
  1368. }
  1369. /* Create the new state which is independ of contexts.
  1370. Return the new state if succeeded, otherwise return NULL. */
  1371. static re_dfastate_t *
  1372. internal_function
  1373. create_ci_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
  1374. unsigned int hash)
  1375. {
  1376. int i;
  1377. reg_errcode_t err;
  1378. re_dfastate_t *newstate;
  1379. newstate = calloc (sizeof (re_dfastate_t), 1);
  1380. if (BE (newstate == NULL, 0))
  1381. return NULL;
  1382. err = re_node_set_init_copy (&newstate->nodes, nodes);
  1383. if (BE (err != REG_NOERROR, 0))
  1384. {
  1385. re_free (newstate);
  1386. return NULL;
  1387. }
  1388. newstate->entrance_nodes = &newstate->nodes;
  1389. for (i = 0 ; i < nodes->nelem ; i++)
  1390. {
  1391. re_token_t *node = dfa->nodes + nodes->elems[i];
  1392. re_token_type_t type = node->type;
  1393. if (type == CHARACTER && !node->constraint)
  1394. continue;
  1395. #ifdef RE_ENABLE_I18N
  1396. newstate->accept_mb |= node->accept_mb;
  1397. #endif
  1398. /* If the state has the halt node, the state is a halt state. */
  1399. if (type == END_OF_RE)
  1400. newstate->halt = 1;
  1401. else if (type == OP_BACK_REF)
  1402. newstate->has_backref = 1;
  1403. else if (type == ANCHOR || node->constraint)
  1404. newstate->has_constraint = 1;
  1405. }
  1406. err = register_state (dfa, newstate, hash);
  1407. if (BE (err != REG_NOERROR, 0))
  1408. {
  1409. free_state (newstate);
  1410. newstate = NULL;
  1411. }
  1412. return newstate;
  1413. }
  1414. /* Create the new state which is depend on the context CONTEXT.
  1415. Return the new state if succeeded, otherwise return NULL. */
  1416. static re_dfastate_t *
  1417. internal_function
  1418. create_cd_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
  1419. unsigned int context, unsigned int hash)
  1420. {
  1421. int i, nctx_nodes = 0;
  1422. reg_errcode_t err;
  1423. re_dfastate_t *newstate;
  1424. newstate = calloc (sizeof (re_dfastate_t), 1);
  1425. if (BE (newstate == NULL, 0))
  1426. return NULL;
  1427. err = re_node_set_init_copy (&newstate->nodes, nodes);
  1428. if (BE (err != REG_NOERROR, 0))
  1429. {
  1430. re_free (newstate);
  1431. return NULL;
  1432. }
  1433. newstate->context = context;
  1434. newstate->entrance_nodes = &newstate->nodes;
  1435. for (i = 0 ; i < nodes->nelem ; i++)
  1436. {
  1437. unsigned int constraint = 0;
  1438. re_token_t *node = dfa->nodes + nodes->elems[i];
  1439. re_token_type_t type = node->type;
  1440. if (node->constraint)
  1441. constraint = node->constraint;
  1442. if (type == CHARACTER && !constraint)
  1443. continue;
  1444. #ifdef RE_ENABLE_I18N
  1445. newstate->accept_mb |= node->accept_mb;
  1446. #endif /* RE_ENABLE_I18N */
  1447. /* If the state has the halt node, the state is a halt state. */
  1448. if (type == END_OF_RE)
  1449. newstate->halt = 1;
  1450. else if (type == OP_BACK_REF)
  1451. newstate->has_backref = 1;
  1452. else if (type == ANCHOR)
  1453. constraint = node->opr.ctx_type;
  1454. if (constraint)
  1455. {
  1456. if (newstate->entrance_nodes == &newstate->nodes)
  1457. {
  1458. newstate->entrance_nodes = re_malloc (re_node_set, 1);
  1459. if (BE (newstate->entrance_nodes == NULL, 0))
  1460. {
  1461. free_state (newstate);
  1462. return NULL;
  1463. }
  1464. re_node_set_init_copy (newstate->entrance_nodes, nodes);
  1465. nctx_nodes = 0;
  1466. newstate->has_constraint = 1;
  1467. }
  1468. if (NOT_SATISFY_PREV_CONSTRAINT (constraint,context))
  1469. {
  1470. re_node_set_remove_at (&newstate->nodes, i - nctx_nodes);
  1471. ++nctx_nodes;
  1472. }
  1473. }
  1474. }
  1475. err = register_state (dfa, newstate, hash);
  1476. if (BE (err != REG_NOERROR, 0))
  1477. {
  1478. free_state (newstate);
  1479. newstate = NULL;
  1480. }
  1481. return newstate;
  1482. }