sre_lib.h 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818
  1. /*
  2. * Secret Labs' Regular Expression Engine
  3. *
  4. * regular expression matching engine
  5. *
  6. * Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
  7. *
  8. * See the sre.c file for information on usage and redistribution.
  9. */
  10. /* String matching engine */
  11. /* This file is included three times, with different character settings */
  12. LOCAL(int)
  13. SRE(at)(SRE_STATE* state, const SRE_CHAR* ptr, SRE_CODE at)
  14. {
  15. /* check if pointer is at given position */
  16. Py_ssize_t thisp, thatp;
  17. switch (at) {
  18. case SRE_AT_BEGINNING:
  19. case SRE_AT_BEGINNING_STRING:
  20. return ((void*) ptr == state->beginning);
  21. case SRE_AT_BEGINNING_LINE:
  22. return ((void*) ptr == state->beginning ||
  23. SRE_IS_LINEBREAK((int) ptr[-1]));
  24. case SRE_AT_END:
  25. return (((SRE_CHAR *)state->end - ptr == 1 &&
  26. SRE_IS_LINEBREAK((int) ptr[0])) ||
  27. ((void*) ptr == state->end));
  28. case SRE_AT_END_LINE:
  29. return ((void*) ptr == state->end ||
  30. SRE_IS_LINEBREAK((int) ptr[0]));
  31. case SRE_AT_END_STRING:
  32. return ((void*) ptr == state->end);
  33. case SRE_AT_BOUNDARY:
  34. if (state->beginning == state->end)
  35. return 0;
  36. thatp = ((void*) ptr > state->beginning) ?
  37. SRE_IS_WORD((int) ptr[-1]) : 0;
  38. thisp = ((void*) ptr < state->end) ?
  39. SRE_IS_WORD((int) ptr[0]) : 0;
  40. return thisp != thatp;
  41. case SRE_AT_NON_BOUNDARY:
  42. if (state->beginning == state->end)
  43. return 0;
  44. thatp = ((void*) ptr > state->beginning) ?
  45. SRE_IS_WORD((int) ptr[-1]) : 0;
  46. thisp = ((void*) ptr < state->end) ?
  47. SRE_IS_WORD((int) ptr[0]) : 0;
  48. return thisp == thatp;
  49. case SRE_AT_LOC_BOUNDARY:
  50. if (state->beginning == state->end)
  51. return 0;
  52. thatp = ((void*) ptr > state->beginning) ?
  53. SRE_LOC_IS_WORD((int) ptr[-1]) : 0;
  54. thisp = ((void*) ptr < state->end) ?
  55. SRE_LOC_IS_WORD((int) ptr[0]) : 0;
  56. return thisp != thatp;
  57. case SRE_AT_LOC_NON_BOUNDARY:
  58. if (state->beginning == state->end)
  59. return 0;
  60. thatp = ((void*) ptr > state->beginning) ?
  61. SRE_LOC_IS_WORD((int) ptr[-1]) : 0;
  62. thisp = ((void*) ptr < state->end) ?
  63. SRE_LOC_IS_WORD((int) ptr[0]) : 0;
  64. return thisp == thatp;
  65. case SRE_AT_UNI_BOUNDARY:
  66. if (state->beginning == state->end)
  67. return 0;
  68. thatp = ((void*) ptr > state->beginning) ?
  69. SRE_UNI_IS_WORD((int) ptr[-1]) : 0;
  70. thisp = ((void*) ptr < state->end) ?
  71. SRE_UNI_IS_WORD((int) ptr[0]) : 0;
  72. return thisp != thatp;
  73. case SRE_AT_UNI_NON_BOUNDARY:
  74. if (state->beginning == state->end)
  75. return 0;
  76. thatp = ((void*) ptr > state->beginning) ?
  77. SRE_UNI_IS_WORD((int) ptr[-1]) : 0;
  78. thisp = ((void*) ptr < state->end) ?
  79. SRE_UNI_IS_WORD((int) ptr[0]) : 0;
  80. return thisp == thatp;
  81. }
  82. return 0;
  83. }
  84. LOCAL(int)
  85. SRE(charset)(SRE_STATE* state, const SRE_CODE* set, SRE_CODE ch)
  86. {
  87. /* check if character is a member of the given set */
  88. int ok = 1;
  89. for (;;) {
  90. switch (*set++) {
  91. case SRE_OP_FAILURE:
  92. return !ok;
  93. case SRE_OP_LITERAL:
  94. /* <LITERAL> <code> */
  95. if (ch == set[0])
  96. return ok;
  97. set++;
  98. break;
  99. case SRE_OP_CATEGORY:
  100. /* <CATEGORY> <code> */
  101. if (sre_category(set[0], (int) ch))
  102. return ok;
  103. set++;
  104. break;
  105. case SRE_OP_CHARSET:
  106. /* <CHARSET> <bitmap> */
  107. if (ch < 256 &&
  108. (set[ch/SRE_CODE_BITS] & (1u << (ch & (SRE_CODE_BITS-1)))))
  109. return ok;
  110. set += 256/SRE_CODE_BITS;
  111. break;
  112. case SRE_OP_RANGE:
  113. /* <RANGE> <lower> <upper> */
  114. if (set[0] <= ch && ch <= set[1])
  115. return ok;
  116. set += 2;
  117. break;
  118. case SRE_OP_RANGE_UNI_IGNORE:
  119. /* <RANGE_UNI_IGNORE> <lower> <upper> */
  120. {
  121. SRE_CODE uch;
  122. /* ch is already lower cased */
  123. if (set[0] <= ch && ch <= set[1])
  124. return ok;
  125. uch = sre_upper_unicode(ch);
  126. if (set[0] <= uch && uch <= set[1])
  127. return ok;
  128. set += 2;
  129. break;
  130. }
  131. case SRE_OP_NEGATE:
  132. ok = !ok;
  133. break;
  134. case SRE_OP_BIGCHARSET:
  135. /* <BIGCHARSET> <blockcount> <256 blockindices> <blocks> */
  136. {
  137. Py_ssize_t count, block;
  138. count = *(set++);
  139. if (ch < 0x10000u)
  140. block = ((unsigned char*)set)[ch >> 8];
  141. else
  142. block = -1;
  143. set += 256/sizeof(SRE_CODE);
  144. if (block >=0 &&
  145. (set[(block * 256 + (ch & 255))/SRE_CODE_BITS] &
  146. (1u << (ch & (SRE_CODE_BITS-1)))))
  147. return ok;
  148. set += count * (256/SRE_CODE_BITS);
  149. break;
  150. }
  151. default:
  152. /* internal error -- there's not much we can do about it
  153. here, so let's just pretend it didn't match... */
  154. return 0;
  155. }
  156. }
  157. }
  158. LOCAL(int)
  159. SRE(charset_loc_ignore)(SRE_STATE* state, const SRE_CODE* set, SRE_CODE ch)
  160. {
  161. SRE_CODE lo, up;
  162. lo = sre_lower_locale(ch);
  163. if (SRE(charset)(state, set, lo))
  164. return 1;
  165. up = sre_upper_locale(ch);
  166. return up != lo && SRE(charset)(state, set, up);
  167. }
  168. LOCAL(Py_ssize_t) SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel);
  169. LOCAL(Py_ssize_t)
  170. SRE(count)(SRE_STATE* state, const SRE_CODE* pattern, Py_ssize_t maxcount)
  171. {
  172. SRE_CODE chr;
  173. SRE_CHAR c;
  174. const SRE_CHAR* ptr = (const SRE_CHAR *)state->ptr;
  175. const SRE_CHAR* end = (const SRE_CHAR *)state->end;
  176. Py_ssize_t i;
  177. /* adjust end */
  178. if (maxcount < end - ptr && maxcount != SRE_MAXREPEAT)
  179. end = ptr + maxcount;
  180. switch (pattern[0]) {
  181. case SRE_OP_IN:
  182. /* repeated set */
  183. TRACE(("|%p|%p|COUNT IN\n", pattern, ptr));
  184. while (ptr < end && SRE(charset)(state, pattern + 2, *ptr))
  185. ptr++;
  186. break;
  187. case SRE_OP_ANY:
  188. /* repeated dot wildcard. */
  189. TRACE(("|%p|%p|COUNT ANY\n", pattern, ptr));
  190. while (ptr < end && !SRE_IS_LINEBREAK(*ptr))
  191. ptr++;
  192. break;
  193. case SRE_OP_ANY_ALL:
  194. /* repeated dot wildcard. skip to the end of the target
  195. string, and backtrack from there */
  196. TRACE(("|%p|%p|COUNT ANY_ALL\n", pattern, ptr));
  197. ptr = end;
  198. break;
  199. case SRE_OP_LITERAL:
  200. /* repeated literal */
  201. chr = pattern[1];
  202. TRACE(("|%p|%p|COUNT LITERAL %d\n", pattern, ptr, chr));
  203. c = (SRE_CHAR) chr;
  204. #if SIZEOF_SRE_CHAR < 4
  205. if ((SRE_CODE) c != chr)
  206. ; /* literal can't match: doesn't fit in char width */
  207. else
  208. #endif
  209. while (ptr < end && *ptr == c)
  210. ptr++;
  211. break;
  212. case SRE_OP_LITERAL_IGNORE:
  213. /* repeated literal */
  214. chr = pattern[1];
  215. TRACE(("|%p|%p|COUNT LITERAL_IGNORE %d\n", pattern, ptr, chr));
  216. while (ptr < end && (SRE_CODE) sre_lower_ascii(*ptr) == chr)
  217. ptr++;
  218. break;
  219. case SRE_OP_LITERAL_UNI_IGNORE:
  220. /* repeated literal */
  221. chr = pattern[1];
  222. TRACE(("|%p|%p|COUNT LITERAL_UNI_IGNORE %d\n", pattern, ptr, chr));
  223. while (ptr < end && (SRE_CODE) sre_lower_unicode(*ptr) == chr)
  224. ptr++;
  225. break;
  226. case SRE_OP_LITERAL_LOC_IGNORE:
  227. /* repeated literal */
  228. chr = pattern[1];
  229. TRACE(("|%p|%p|COUNT LITERAL_LOC_IGNORE %d\n", pattern, ptr, chr));
  230. while (ptr < end && char_loc_ignore(chr, *ptr))
  231. ptr++;
  232. break;
  233. case SRE_OP_NOT_LITERAL:
  234. /* repeated non-literal */
  235. chr = pattern[1];
  236. TRACE(("|%p|%p|COUNT NOT_LITERAL %d\n", pattern, ptr, chr));
  237. c = (SRE_CHAR) chr;
  238. #if SIZEOF_SRE_CHAR < 4
  239. if ((SRE_CODE) c != chr)
  240. ptr = end; /* literal can't match: doesn't fit in char width */
  241. else
  242. #endif
  243. while (ptr < end && *ptr != c)
  244. ptr++;
  245. break;
  246. case SRE_OP_NOT_LITERAL_IGNORE:
  247. /* repeated non-literal */
  248. chr = pattern[1];
  249. TRACE(("|%p|%p|COUNT NOT_LITERAL_IGNORE %d\n", pattern, ptr, chr));
  250. while (ptr < end && (SRE_CODE) sre_lower_ascii(*ptr) != chr)
  251. ptr++;
  252. break;
  253. case SRE_OP_NOT_LITERAL_UNI_IGNORE:
  254. /* repeated non-literal */
  255. chr = pattern[1];
  256. TRACE(("|%p|%p|COUNT NOT_LITERAL_UNI_IGNORE %d\n", pattern, ptr, chr));
  257. while (ptr < end && (SRE_CODE) sre_lower_unicode(*ptr) != chr)
  258. ptr++;
  259. break;
  260. case SRE_OP_NOT_LITERAL_LOC_IGNORE:
  261. /* repeated non-literal */
  262. chr = pattern[1];
  263. TRACE(("|%p|%p|COUNT NOT_LITERAL_LOC_IGNORE %d\n", pattern, ptr, chr));
  264. while (ptr < end && !char_loc_ignore(chr, *ptr))
  265. ptr++;
  266. break;
  267. default:
  268. /* repeated single character pattern */
  269. TRACE(("|%p|%p|COUNT SUBPATTERN\n", pattern, ptr));
  270. while ((SRE_CHAR*) state->ptr < end) {
  271. i = SRE(match)(state, pattern, 0);
  272. if (i < 0)
  273. return i;
  274. if (!i)
  275. break;
  276. }
  277. TRACE(("|%p|%p|COUNT %zd\n", pattern, ptr,
  278. (SRE_CHAR*) state->ptr - ptr));
  279. return (SRE_CHAR*) state->ptr - ptr;
  280. }
  281. TRACE(("|%p|%p|COUNT %zd\n", pattern, ptr,
  282. ptr - (SRE_CHAR*) state->ptr));
  283. return ptr - (SRE_CHAR*) state->ptr;
  284. }
  285. /* The macros below should be used to protect recursive SRE(match)()
  286. * calls that *failed* and do *not* return immediately (IOW, those
  287. * that will backtrack). Explaining:
  288. *
  289. * - Recursive SRE(match)() returned true: that's usually a success
  290. * (besides atypical cases like ASSERT_NOT), therefore there's no
  291. * reason to restore lastmark;
  292. *
  293. * - Recursive SRE(match)() returned false but the current SRE(match)()
  294. * is returning to the caller: If the current SRE(match)() is the
  295. * top function of the recursion, returning false will be a matching
  296. * failure, and it doesn't matter where lastmark is pointing to.
  297. * If it's *not* the top function, it will be a recursive SRE(match)()
  298. * failure by itself, and the calling SRE(match)() will have to deal
  299. * with the failure by the same rules explained here (it will restore
  300. * lastmark by itself if necessary);
  301. *
  302. * - Recursive SRE(match)() returned false, and will continue the
  303. * outside 'for' loop: must be protected when breaking, since the next
  304. * OP could potentially depend on lastmark;
  305. *
  306. * - Recursive SRE(match)() returned false, and will be called again
  307. * inside a local for/while loop: must be protected between each
  308. * loop iteration, since the recursive SRE(match)() could do anything,
  309. * and could potentially depend on lastmark.
  310. *
  311. * For more information, check the discussion at SF patch #712900.
  312. */
  313. #define LASTMARK_SAVE() \
  314. do { \
  315. ctx->lastmark = state->lastmark; \
  316. ctx->lastindex = state->lastindex; \
  317. } while (0)
  318. #define LASTMARK_RESTORE() \
  319. do { \
  320. state->lastmark = ctx->lastmark; \
  321. state->lastindex = ctx->lastindex; \
  322. } while (0)
  323. #define RETURN_ERROR(i) do { return i; } while(0)
  324. #define RETURN_FAILURE do { ret = 0; goto exit; } while(0)
  325. #define RETURN_SUCCESS do { ret = 1; goto exit; } while(0)
  326. #define RETURN_ON_ERROR(i) \
  327. do { if (i < 0) RETURN_ERROR(i); } while (0)
  328. #define RETURN_ON_SUCCESS(i) \
  329. do { RETURN_ON_ERROR(i); if (i > 0) RETURN_SUCCESS; } while (0)
  330. #define RETURN_ON_FAILURE(i) \
  331. do { RETURN_ON_ERROR(i); if (i == 0) RETURN_FAILURE; } while (0)
  332. #define DATA_STACK_ALLOC(state, type, ptr) \
  333. do { \
  334. alloc_pos = state->data_stack_base; \
  335. TRACE(("allocating %s in %zd (%zd)\n", \
  336. Py_STRINGIFY(type), alloc_pos, sizeof(type))); \
  337. if (sizeof(type) > state->data_stack_size - alloc_pos) { \
  338. int j = data_stack_grow(state, sizeof(type)); \
  339. if (j < 0) return j; \
  340. if (ctx_pos != -1) \
  341. DATA_STACK_LOOKUP_AT(state, SRE(match_context), ctx, ctx_pos); \
  342. } \
  343. ptr = (type*)(state->data_stack+alloc_pos); \
  344. state->data_stack_base += sizeof(type); \
  345. } while (0)
  346. #define DATA_STACK_LOOKUP_AT(state, type, ptr, pos) \
  347. do { \
  348. TRACE(("looking up %s at %zd\n", Py_STRINGIFY(type), pos)); \
  349. ptr = (type*)(state->data_stack+pos); \
  350. } while (0)
  351. #define DATA_STACK_PUSH(state, data, size) \
  352. do { \
  353. TRACE(("copy data in %p to %zd (%zd)\n", \
  354. data, state->data_stack_base, size)); \
  355. if (size > state->data_stack_size - state->data_stack_base) { \
  356. int j = data_stack_grow(state, size); \
  357. if (j < 0) return j; \
  358. if (ctx_pos != -1) \
  359. DATA_STACK_LOOKUP_AT(state, SRE(match_context), ctx, ctx_pos); \
  360. } \
  361. memcpy(state->data_stack+state->data_stack_base, data, size); \
  362. state->data_stack_base += size; \
  363. } while (0)
  364. /* We add an explicit cast to memcpy here because MSVC has a bug when
  365. compiling C code where it believes that `const void**` cannot be
  366. safely casted to `void*`, see bpo-39943 for details. */
  367. #define DATA_STACK_POP(state, data, size, discard) \
  368. do { \
  369. TRACE(("copy data to %p from %zd (%zd)\n", \
  370. data, state->data_stack_base-size, size)); \
  371. memcpy((void*) data, state->data_stack+state->data_stack_base-size, size); \
  372. if (discard) \
  373. state->data_stack_base -= size; \
  374. } while (0)
  375. #define DATA_STACK_POP_DISCARD(state, size) \
  376. do { \
  377. TRACE(("discard data from %zd (%zd)\n", \
  378. state->data_stack_base-size, size)); \
  379. state->data_stack_base -= size; \
  380. } while(0)
  381. #define DATA_PUSH(x) \
  382. DATA_STACK_PUSH(state, (x), sizeof(*(x)))
  383. #define DATA_POP(x) \
  384. DATA_STACK_POP(state, (x), sizeof(*(x)), 1)
  385. #define DATA_POP_DISCARD(x) \
  386. DATA_STACK_POP_DISCARD(state, sizeof(*(x)))
  387. #define DATA_ALLOC(t,p) \
  388. DATA_STACK_ALLOC(state, t, p)
  389. #define DATA_LOOKUP_AT(t,p,pos) \
  390. DATA_STACK_LOOKUP_AT(state,t,p,pos)
  391. #define MARK_PUSH(lastmark) \
  392. do if (lastmark >= 0) { \
  393. size_t _marks_size = (lastmark+1) * sizeof(void*); \
  394. DATA_STACK_PUSH(state, state->mark, _marks_size); \
  395. } while (0)
  396. #define MARK_POP(lastmark) \
  397. do if (lastmark >= 0) { \
  398. size_t _marks_size = (lastmark+1) * sizeof(void*); \
  399. DATA_STACK_POP(state, state->mark, _marks_size, 1); \
  400. } while (0)
  401. #define MARK_POP_KEEP(lastmark) \
  402. do if (lastmark >= 0) { \
  403. size_t _marks_size = (lastmark+1) * sizeof(void*); \
  404. DATA_STACK_POP(state, state->mark, _marks_size, 0); \
  405. } while (0)
  406. #define MARK_POP_DISCARD(lastmark) \
  407. do if (lastmark >= 0) { \
  408. size_t _marks_size = (lastmark+1) * sizeof(void*); \
  409. DATA_STACK_POP_DISCARD(state, _marks_size); \
  410. } while (0)
  411. #define JUMP_NONE 0
  412. #define JUMP_MAX_UNTIL_1 1
  413. #define JUMP_MAX_UNTIL_2 2
  414. #define JUMP_MAX_UNTIL_3 3
  415. #define JUMP_MIN_UNTIL_1 4
  416. #define JUMP_MIN_UNTIL_2 5
  417. #define JUMP_MIN_UNTIL_3 6
  418. #define JUMP_REPEAT 7
  419. #define JUMP_REPEAT_ONE_1 8
  420. #define JUMP_REPEAT_ONE_2 9
  421. #define JUMP_MIN_REPEAT_ONE 10
  422. #define JUMP_BRANCH 11
  423. #define JUMP_ASSERT 12
  424. #define JUMP_ASSERT_NOT 13
  425. #define JUMP_POSS_REPEAT_1 14
  426. #define JUMP_POSS_REPEAT_2 15
  427. #define JUMP_ATOMIC_GROUP 16
  428. #define DO_JUMPX(jumpvalue, jumplabel, nextpattern, toplevel_) \
  429. ctx->pattern = pattern; \
  430. ctx->ptr = ptr; \
  431. DATA_ALLOC(SRE(match_context), nextctx); \
  432. nextctx->pattern = nextpattern; \
  433. nextctx->toplevel = toplevel_; \
  434. nextctx->jump = jumpvalue; \
  435. nextctx->last_ctx_pos = ctx_pos; \
  436. pattern = nextpattern; \
  437. ctx_pos = alloc_pos; \
  438. ctx = nextctx; \
  439. goto entrance; \
  440. jumplabel: \
  441. pattern = ctx->pattern; \
  442. ptr = ctx->ptr;
  443. #define DO_JUMP(jumpvalue, jumplabel, nextpattern) \
  444. DO_JUMPX(jumpvalue, jumplabel, nextpattern, ctx->toplevel)
  445. #define DO_JUMP0(jumpvalue, jumplabel, nextpattern) \
  446. DO_JUMPX(jumpvalue, jumplabel, nextpattern, 0)
  447. typedef struct {
  448. Py_ssize_t count;
  449. union {
  450. SRE_CODE chr;
  451. SRE_REPEAT* rep;
  452. } u;
  453. int lastmark;
  454. int lastindex;
  455. const SRE_CODE* pattern;
  456. const SRE_CHAR* ptr;
  457. int toplevel;
  458. int jump;
  459. Py_ssize_t last_ctx_pos;
  460. } SRE(match_context);
  461. #define MAYBE_CHECK_SIGNALS \
  462. do { \
  463. if ((0 == (++sigcount & 0xfff)) && PyErr_CheckSignals()) { \
  464. RETURN_ERROR(SRE_ERROR_INTERRUPTED); \
  465. } \
  466. } while (0)
  467. #ifdef HAVE_COMPUTED_GOTOS
  468. #ifndef USE_COMPUTED_GOTOS
  469. #define USE_COMPUTED_GOTOS 1
  470. #endif
  471. #elif defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
  472. #error "Computed gotos are not supported on this compiler."
  473. #else
  474. #undef USE_COMPUTED_GOTOS
  475. #define USE_COMPUTED_GOTOS 0
  476. #endif
  477. #if USE_COMPUTED_GOTOS
  478. #define TARGET(OP) TARGET_ ## OP
  479. #define DISPATCH \
  480. do { \
  481. MAYBE_CHECK_SIGNALS; \
  482. goto *sre_targets[*pattern++]; \
  483. } while (0)
  484. #else
  485. #define TARGET(OP) case OP
  486. #define DISPATCH goto dispatch
  487. #endif
  488. /* check if string matches the given pattern. returns <0 for
  489. error, 0 for failure, and 1 for success */
  490. LOCAL(Py_ssize_t)
  491. SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
  492. {
  493. const SRE_CHAR* end = (const SRE_CHAR *)state->end;
  494. Py_ssize_t alloc_pos, ctx_pos = -1;
  495. Py_ssize_t ret = 0;
  496. int jump;
  497. unsigned int sigcount = state->sigcount;
  498. SRE(match_context)* ctx;
  499. SRE(match_context)* nextctx;
  500. TRACE(("|%p|%p|ENTER\n", pattern, state->ptr));
  501. DATA_ALLOC(SRE(match_context), ctx);
  502. ctx->last_ctx_pos = -1;
  503. ctx->jump = JUMP_NONE;
  504. ctx->toplevel = toplevel;
  505. ctx_pos = alloc_pos;
  506. #if USE_COMPUTED_GOTOS
  507. #include "sre_targets.h"
  508. #endif
  509. entrance:
  510. ; // Fashion statement.
  511. const SRE_CHAR *ptr = (SRE_CHAR *)state->ptr;
  512. if (pattern[0] == SRE_OP_INFO) {
  513. /* optimization info block */
  514. /* <INFO> <1=skip> <2=flags> <3=min> ... */
  515. if (pattern[3] && (uintptr_t)(end - ptr) < pattern[3]) {
  516. TRACE(("reject (got %tu chars, need %zu)\n",
  517. end - ptr, (size_t) pattern[3]));
  518. RETURN_FAILURE;
  519. }
  520. pattern += pattern[1] + 1;
  521. }
  522. #if USE_COMPUTED_GOTOS
  523. DISPATCH;
  524. #else
  525. dispatch:
  526. MAYBE_CHECK_SIGNALS;
  527. switch (*pattern++)
  528. #endif
  529. {
  530. TARGET(SRE_OP_MARK):
  531. /* set mark */
  532. /* <MARK> <gid> */
  533. TRACE(("|%p|%p|MARK %d\n", pattern,
  534. ptr, pattern[0]));
  535. {
  536. int i = pattern[0];
  537. if (i & 1)
  538. state->lastindex = i/2 + 1;
  539. if (i > state->lastmark) {
  540. /* state->lastmark is the highest valid index in the
  541. state->mark array. If it is increased by more than 1,
  542. the intervening marks must be set to NULL to signal
  543. that these marks have not been encountered. */
  544. int j = state->lastmark + 1;
  545. while (j < i)
  546. state->mark[j++] = NULL;
  547. state->lastmark = i;
  548. }
  549. state->mark[i] = ptr;
  550. }
  551. pattern++;
  552. DISPATCH;
  553. TARGET(SRE_OP_LITERAL):
  554. /* match literal string */
  555. /* <LITERAL> <code> */
  556. TRACE(("|%p|%p|LITERAL %d\n", pattern,
  557. ptr, *pattern));
  558. if (ptr >= end || (SRE_CODE) ptr[0] != pattern[0])
  559. RETURN_FAILURE;
  560. pattern++;
  561. ptr++;
  562. DISPATCH;
  563. TARGET(SRE_OP_NOT_LITERAL):
  564. /* match anything that is not literal character */
  565. /* <NOT_LITERAL> <code> */
  566. TRACE(("|%p|%p|NOT_LITERAL %d\n", pattern,
  567. ptr, *pattern));
  568. if (ptr >= end || (SRE_CODE) ptr[0] == pattern[0])
  569. RETURN_FAILURE;
  570. pattern++;
  571. ptr++;
  572. DISPATCH;
  573. TARGET(SRE_OP_SUCCESS):
  574. /* end of pattern */
  575. TRACE(("|%p|%p|SUCCESS\n", pattern, ptr));
  576. if (ctx->toplevel &&
  577. ((state->match_all && ptr != state->end) ||
  578. (state->must_advance && ptr == state->start)))
  579. {
  580. RETURN_FAILURE;
  581. }
  582. state->ptr = ptr;
  583. RETURN_SUCCESS;
  584. TARGET(SRE_OP_AT):
  585. /* match at given position */
  586. /* <AT> <code> */
  587. TRACE(("|%p|%p|AT %d\n", pattern, ptr, *pattern));
  588. if (!SRE(at)(state, ptr, *pattern))
  589. RETURN_FAILURE;
  590. pattern++;
  591. DISPATCH;
  592. TARGET(SRE_OP_CATEGORY):
  593. /* match at given category */
  594. /* <CATEGORY> <code> */
  595. TRACE(("|%p|%p|CATEGORY %d\n", pattern,
  596. ptr, *pattern));
  597. if (ptr >= end || !sre_category(pattern[0], ptr[0]))
  598. RETURN_FAILURE;
  599. pattern++;
  600. ptr++;
  601. DISPATCH;
  602. TARGET(SRE_OP_ANY):
  603. /* match anything (except a newline) */
  604. /* <ANY> */
  605. TRACE(("|%p|%p|ANY\n", pattern, ptr));
  606. if (ptr >= end || SRE_IS_LINEBREAK(ptr[0]))
  607. RETURN_FAILURE;
  608. ptr++;
  609. DISPATCH;
  610. TARGET(SRE_OP_ANY_ALL):
  611. /* match anything */
  612. /* <ANY_ALL> */
  613. TRACE(("|%p|%p|ANY_ALL\n", pattern, ptr));
  614. if (ptr >= end)
  615. RETURN_FAILURE;
  616. ptr++;
  617. DISPATCH;
  618. TARGET(SRE_OP_IN):
  619. /* match set member (or non_member) */
  620. /* <IN> <skip> <set> */
  621. TRACE(("|%p|%p|IN\n", pattern, ptr));
  622. if (ptr >= end ||
  623. !SRE(charset)(state, pattern + 1, *ptr))
  624. RETURN_FAILURE;
  625. pattern += pattern[0];
  626. ptr++;
  627. DISPATCH;
  628. TARGET(SRE_OP_LITERAL_IGNORE):
  629. TRACE(("|%p|%p|LITERAL_IGNORE %d\n",
  630. pattern, ptr, pattern[0]));
  631. if (ptr >= end ||
  632. sre_lower_ascii(*ptr) != *pattern)
  633. RETURN_FAILURE;
  634. pattern++;
  635. ptr++;
  636. DISPATCH;
  637. TARGET(SRE_OP_LITERAL_UNI_IGNORE):
  638. TRACE(("|%p|%p|LITERAL_UNI_IGNORE %d\n",
  639. pattern, ptr, pattern[0]));
  640. if (ptr >= end ||
  641. sre_lower_unicode(*ptr) != *pattern)
  642. RETURN_FAILURE;
  643. pattern++;
  644. ptr++;
  645. DISPATCH;
  646. TARGET(SRE_OP_LITERAL_LOC_IGNORE):
  647. TRACE(("|%p|%p|LITERAL_LOC_IGNORE %d\n",
  648. pattern, ptr, pattern[0]));
  649. if (ptr >= end
  650. || !char_loc_ignore(*pattern, *ptr))
  651. RETURN_FAILURE;
  652. pattern++;
  653. ptr++;
  654. DISPATCH;
  655. TARGET(SRE_OP_NOT_LITERAL_IGNORE):
  656. TRACE(("|%p|%p|NOT_LITERAL_IGNORE %d\n",
  657. pattern, ptr, *pattern));
  658. if (ptr >= end ||
  659. sre_lower_ascii(*ptr) == *pattern)
  660. RETURN_FAILURE;
  661. pattern++;
  662. ptr++;
  663. DISPATCH;
  664. TARGET(SRE_OP_NOT_LITERAL_UNI_IGNORE):
  665. TRACE(("|%p|%p|NOT_LITERAL_UNI_IGNORE %d\n",
  666. pattern, ptr, *pattern));
  667. if (ptr >= end ||
  668. sre_lower_unicode(*ptr) == *pattern)
  669. RETURN_FAILURE;
  670. pattern++;
  671. ptr++;
  672. DISPATCH;
  673. TARGET(SRE_OP_NOT_LITERAL_LOC_IGNORE):
  674. TRACE(("|%p|%p|NOT_LITERAL_LOC_IGNORE %d\n",
  675. pattern, ptr, *pattern));
  676. if (ptr >= end
  677. || char_loc_ignore(*pattern, *ptr))
  678. RETURN_FAILURE;
  679. pattern++;
  680. ptr++;
  681. DISPATCH;
  682. TARGET(SRE_OP_IN_IGNORE):
  683. TRACE(("|%p|%p|IN_IGNORE\n", pattern, ptr));
  684. if (ptr >= end
  685. || !SRE(charset)(state, pattern+1,
  686. (SRE_CODE)sre_lower_ascii(*ptr)))
  687. RETURN_FAILURE;
  688. pattern += pattern[0];
  689. ptr++;
  690. DISPATCH;
  691. TARGET(SRE_OP_IN_UNI_IGNORE):
  692. TRACE(("|%p|%p|IN_UNI_IGNORE\n", pattern, ptr));
  693. if (ptr >= end
  694. || !SRE(charset)(state, pattern+1,
  695. (SRE_CODE)sre_lower_unicode(*ptr)))
  696. RETURN_FAILURE;
  697. pattern += pattern[0];
  698. ptr++;
  699. DISPATCH;
  700. TARGET(SRE_OP_IN_LOC_IGNORE):
  701. TRACE(("|%p|%p|IN_LOC_IGNORE\n", pattern, ptr));
  702. if (ptr >= end
  703. || !SRE(charset_loc_ignore)(state, pattern+1, *ptr))
  704. RETURN_FAILURE;
  705. pattern += pattern[0];
  706. ptr++;
  707. DISPATCH;
  708. TARGET(SRE_OP_JUMP):
  709. TARGET(SRE_OP_INFO):
  710. /* jump forward */
  711. /* <JUMP> <offset> */
  712. TRACE(("|%p|%p|JUMP %d\n", pattern,
  713. ptr, pattern[0]));
  714. pattern += pattern[0];
  715. DISPATCH;
  716. TARGET(SRE_OP_BRANCH):
  717. /* alternation */
  718. /* <BRANCH> <0=skip> code <JUMP> ... <NULL> */
  719. TRACE(("|%p|%p|BRANCH\n", pattern, ptr));
  720. LASTMARK_SAVE();
  721. if (state->repeat)
  722. MARK_PUSH(ctx->lastmark);
  723. for (; pattern[0]; pattern += pattern[0]) {
  724. if (pattern[1] == SRE_OP_LITERAL &&
  725. (ptr >= end ||
  726. (SRE_CODE) *ptr != pattern[2]))
  727. continue;
  728. if (pattern[1] == SRE_OP_IN &&
  729. (ptr >= end ||
  730. !SRE(charset)(state, pattern + 3,
  731. (SRE_CODE) *ptr)))
  732. continue;
  733. state->ptr = ptr;
  734. DO_JUMP(JUMP_BRANCH, jump_branch, pattern+1);
  735. if (ret) {
  736. if (state->repeat)
  737. MARK_POP_DISCARD(ctx->lastmark);
  738. RETURN_ON_ERROR(ret);
  739. RETURN_SUCCESS;
  740. }
  741. if (state->repeat)
  742. MARK_POP_KEEP(ctx->lastmark);
  743. LASTMARK_RESTORE();
  744. }
  745. if (state->repeat)
  746. MARK_POP_DISCARD(ctx->lastmark);
  747. RETURN_FAILURE;
  748. TARGET(SRE_OP_REPEAT_ONE):
  749. /* match repeated sequence (maximizing regexp) */
  750. /* this operator only works if the repeated item is
  751. exactly one character wide, and we're not already
  752. collecting backtracking points. for other cases,
  753. use the MAX_REPEAT operator */
  754. /* <REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */
  755. TRACE(("|%p|%p|REPEAT_ONE %d %d\n", pattern, ptr,
  756. pattern[1], pattern[2]));
  757. if ((Py_ssize_t) pattern[1] > end - ptr)
  758. RETURN_FAILURE; /* cannot match */
  759. state->ptr = ptr;
  760. ret = SRE(count)(state, pattern+3, pattern[2]);
  761. RETURN_ON_ERROR(ret);
  762. DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
  763. ctx->count = ret;
  764. ptr += ctx->count;
  765. /* when we arrive here, count contains the number of
  766. matches, and ptr points to the tail of the target
  767. string. check if the rest of the pattern matches,
  768. and backtrack if not. */
  769. if (ctx->count < (Py_ssize_t) pattern[1])
  770. RETURN_FAILURE;
  771. if (pattern[pattern[0]] == SRE_OP_SUCCESS &&
  772. ptr == state->end &&
  773. !(ctx->toplevel && state->must_advance && ptr == state->start))
  774. {
  775. /* tail is empty. we're finished */
  776. state->ptr = ptr;
  777. RETURN_SUCCESS;
  778. }
  779. LASTMARK_SAVE();
  780. if (state->repeat)
  781. MARK_PUSH(ctx->lastmark);
  782. if (pattern[pattern[0]] == SRE_OP_LITERAL) {
  783. /* tail starts with a literal. skip positions where
  784. the rest of the pattern cannot possibly match */
  785. ctx->u.chr = pattern[pattern[0]+1];
  786. for (;;) {
  787. while (ctx->count >= (Py_ssize_t) pattern[1] &&
  788. (ptr >= end || *ptr != ctx->u.chr)) {
  789. ptr--;
  790. ctx->count--;
  791. }
  792. if (ctx->count < (Py_ssize_t) pattern[1])
  793. break;
  794. state->ptr = ptr;
  795. DO_JUMP(JUMP_REPEAT_ONE_1, jump_repeat_one_1,
  796. pattern+pattern[0]);
  797. if (ret) {
  798. if (state->repeat)
  799. MARK_POP_DISCARD(ctx->lastmark);
  800. RETURN_ON_ERROR(ret);
  801. RETURN_SUCCESS;
  802. }
  803. if (state->repeat)
  804. MARK_POP_KEEP(ctx->lastmark);
  805. LASTMARK_RESTORE();
  806. ptr--;
  807. ctx->count--;
  808. }
  809. if (state->repeat)
  810. MARK_POP_DISCARD(ctx->lastmark);
  811. } else {
  812. /* general case */
  813. while (ctx->count >= (Py_ssize_t) pattern[1]) {
  814. state->ptr = ptr;
  815. DO_JUMP(JUMP_REPEAT_ONE_2, jump_repeat_one_2,
  816. pattern+pattern[0]);
  817. if (ret) {
  818. if (state->repeat)
  819. MARK_POP_DISCARD(ctx->lastmark);
  820. RETURN_ON_ERROR(ret);
  821. RETURN_SUCCESS;
  822. }
  823. if (state->repeat)
  824. MARK_POP_KEEP(ctx->lastmark);
  825. LASTMARK_RESTORE();
  826. ptr--;
  827. ctx->count--;
  828. }
  829. if (state->repeat)
  830. MARK_POP_DISCARD(ctx->lastmark);
  831. }
  832. RETURN_FAILURE;
  833. TARGET(SRE_OP_MIN_REPEAT_ONE):
  834. /* match repeated sequence (minimizing regexp) */
  835. /* this operator only works if the repeated item is
  836. exactly one character wide, and we're not already
  837. collecting backtracking points. for other cases,
  838. use the MIN_REPEAT operator */
  839. /* <MIN_REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */
  840. TRACE(("|%p|%p|MIN_REPEAT_ONE %d %d\n", pattern, ptr,
  841. pattern[1], pattern[2]));
  842. if ((Py_ssize_t) pattern[1] > end - ptr)
  843. RETURN_FAILURE; /* cannot match */
  844. state->ptr = ptr;
  845. if (pattern[1] == 0)
  846. ctx->count = 0;
  847. else {
  848. /* count using pattern min as the maximum */
  849. ret = SRE(count)(state, pattern+3, pattern[1]);
  850. RETURN_ON_ERROR(ret);
  851. DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
  852. if (ret < (Py_ssize_t) pattern[1])
  853. /* didn't match minimum number of times */
  854. RETURN_FAILURE;
  855. /* advance past minimum matches of repeat */
  856. ctx->count = ret;
  857. ptr += ctx->count;
  858. }
  859. if (pattern[pattern[0]] == SRE_OP_SUCCESS &&
  860. !(ctx->toplevel &&
  861. ((state->match_all && ptr != state->end) ||
  862. (state->must_advance && ptr == state->start))))
  863. {
  864. /* tail is empty. we're finished */
  865. state->ptr = ptr;
  866. RETURN_SUCCESS;
  867. } else {
  868. /* general case */
  869. LASTMARK_SAVE();
  870. if (state->repeat)
  871. MARK_PUSH(ctx->lastmark);
  872. while ((Py_ssize_t)pattern[2] == SRE_MAXREPEAT
  873. || ctx->count <= (Py_ssize_t)pattern[2]) {
  874. state->ptr = ptr;
  875. DO_JUMP(JUMP_MIN_REPEAT_ONE,jump_min_repeat_one,
  876. pattern+pattern[0]);
  877. if (ret) {
  878. if (state->repeat)
  879. MARK_POP_DISCARD(ctx->lastmark);
  880. RETURN_ON_ERROR(ret);
  881. RETURN_SUCCESS;
  882. }
  883. if (state->repeat)
  884. MARK_POP_KEEP(ctx->lastmark);
  885. LASTMARK_RESTORE();
  886. state->ptr = ptr;
  887. ret = SRE(count)(state, pattern+3, 1);
  888. RETURN_ON_ERROR(ret);
  889. DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
  890. if (ret == 0)
  891. break;
  892. assert(ret == 1);
  893. ptr++;
  894. ctx->count++;
  895. }
  896. if (state->repeat)
  897. MARK_POP_DISCARD(ctx->lastmark);
  898. }
  899. RETURN_FAILURE;
  900. TARGET(SRE_OP_POSSESSIVE_REPEAT_ONE):
  901. /* match repeated sequence (maximizing regexp) without
  902. backtracking */
  903. /* this operator only works if the repeated item is
  904. exactly one character wide, and we're not already
  905. collecting backtracking points. for other cases,
  906. use the MAX_REPEAT operator */
  907. /* <POSSESSIVE_REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS>
  908. tail */
  909. TRACE(("|%p|%p|POSSESSIVE_REPEAT_ONE %d %d\n", pattern,
  910. ptr, pattern[1], pattern[2]));
  911. if (ptr + pattern[1] > end) {
  912. RETURN_FAILURE; /* cannot match */
  913. }
  914. state->ptr = ptr;
  915. ret = SRE(count)(state, pattern + 3, pattern[2]);
  916. RETURN_ON_ERROR(ret);
  917. DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
  918. ctx->count = ret;
  919. ptr += ctx->count;
  920. /* when we arrive here, count contains the number of
  921. matches, and ptr points to the tail of the target
  922. string. check if the rest of the pattern matches,
  923. and fail if not. */
  924. /* Test for not enough repetitions in match */
  925. if (ctx->count < (Py_ssize_t) pattern[1]) {
  926. RETURN_FAILURE;
  927. }
  928. /* Update the pattern to point to the next op code */
  929. pattern += pattern[0];
  930. /* Let the tail be evaluated separately and consider this
  931. match successful. */
  932. if (*pattern == SRE_OP_SUCCESS &&
  933. ptr == state->end &&
  934. !(ctx->toplevel && state->must_advance && ptr == state->start))
  935. {
  936. /* tail is empty. we're finished */
  937. state->ptr = ptr;
  938. RETURN_SUCCESS;
  939. }
  940. /* Attempt to match the rest of the string */
  941. DISPATCH;
  942. TARGET(SRE_OP_REPEAT):
  943. /* create repeat context. all the hard work is done
  944. by the UNTIL operator (MAX_UNTIL, MIN_UNTIL) */
  945. /* <REPEAT> <skip> <1=min> <2=max>
  946. <3=repeat_index> item <UNTIL> tail */
  947. TRACE(("|%p|%p|REPEAT %d %d\n", pattern, ptr,
  948. pattern[1], pattern[2]));
  949. /* install new repeat context */
  950. /* TODO(https://github.com/python/cpython/issues/67877): Fix this
  951. * potential memory leak. */
  952. ctx->u.rep = (SRE_REPEAT*) PyObject_Malloc(sizeof(*ctx->u.rep));
  953. if (!ctx->u.rep) {
  954. PyErr_NoMemory();
  955. RETURN_FAILURE;
  956. }
  957. ctx->u.rep->count = -1;
  958. ctx->u.rep->pattern = pattern;
  959. ctx->u.rep->prev = state->repeat;
  960. ctx->u.rep->last_ptr = NULL;
  961. state->repeat = ctx->u.rep;
  962. state->ptr = ptr;
  963. DO_JUMP(JUMP_REPEAT, jump_repeat, pattern+pattern[0]);
  964. state->repeat = ctx->u.rep->prev;
  965. PyObject_Free(ctx->u.rep);
  966. if (ret) {
  967. RETURN_ON_ERROR(ret);
  968. RETURN_SUCCESS;
  969. }
  970. RETURN_FAILURE;
  971. TARGET(SRE_OP_MAX_UNTIL):
  972. /* maximizing repeat */
  973. /* <REPEAT> <skip> <1=min> <2=max> item <MAX_UNTIL> tail */
  974. /* FIXME: we probably need to deal with zero-width
  975. matches in here... */
  976. ctx->u.rep = state->repeat;
  977. if (!ctx->u.rep)
  978. RETURN_ERROR(SRE_ERROR_STATE);
  979. state->ptr = ptr;
  980. ctx->count = ctx->u.rep->count+1;
  981. TRACE(("|%p|%p|MAX_UNTIL %zd\n", pattern,
  982. ptr, ctx->count));
  983. if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) {
  984. /* not enough matches */
  985. ctx->u.rep->count = ctx->count;
  986. DO_JUMP(JUMP_MAX_UNTIL_1, jump_max_until_1,
  987. ctx->u.rep->pattern+3);
  988. if (ret) {
  989. RETURN_ON_ERROR(ret);
  990. RETURN_SUCCESS;
  991. }
  992. ctx->u.rep->count = ctx->count-1;
  993. state->ptr = ptr;
  994. RETURN_FAILURE;
  995. }
  996. if ((ctx->count < (Py_ssize_t) ctx->u.rep->pattern[2] ||
  997. ctx->u.rep->pattern[2] == SRE_MAXREPEAT) &&
  998. state->ptr != ctx->u.rep->last_ptr) {
  999. /* we may have enough matches, but if we can
  1000. match another item, do so */
  1001. ctx->u.rep->count = ctx->count;
  1002. LASTMARK_SAVE();
  1003. MARK_PUSH(ctx->lastmark);
  1004. /* zero-width match protection */
  1005. DATA_PUSH(&ctx->u.rep->last_ptr);
  1006. ctx->u.rep->last_ptr = state->ptr;
  1007. DO_JUMP(JUMP_MAX_UNTIL_2, jump_max_until_2,
  1008. ctx->u.rep->pattern+3);
  1009. DATA_POP(&ctx->u.rep->last_ptr);
  1010. if (ret) {
  1011. MARK_POP_DISCARD(ctx->lastmark);
  1012. RETURN_ON_ERROR(ret);
  1013. RETURN_SUCCESS;
  1014. }
  1015. MARK_POP(ctx->lastmark);
  1016. LASTMARK_RESTORE();
  1017. ctx->u.rep->count = ctx->count-1;
  1018. state->ptr = ptr;
  1019. }
  1020. /* cannot match more repeated items here. make sure the
  1021. tail matches */
  1022. state->repeat = ctx->u.rep->prev;
  1023. DO_JUMP(JUMP_MAX_UNTIL_3, jump_max_until_3, pattern);
  1024. state->repeat = ctx->u.rep; // restore repeat before return
  1025. RETURN_ON_SUCCESS(ret);
  1026. state->ptr = ptr;
  1027. RETURN_FAILURE;
  1028. TARGET(SRE_OP_MIN_UNTIL):
  1029. /* minimizing repeat */
  1030. /* <REPEAT> <skip> <1=min> <2=max> item <MIN_UNTIL> tail */
  1031. ctx->u.rep = state->repeat;
  1032. if (!ctx->u.rep)
  1033. RETURN_ERROR(SRE_ERROR_STATE);
  1034. state->ptr = ptr;
  1035. ctx->count = ctx->u.rep->count+1;
  1036. TRACE(("|%p|%p|MIN_UNTIL %zd %p\n", pattern,
  1037. ptr, ctx->count, ctx->u.rep->pattern));
  1038. if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) {
  1039. /* not enough matches */
  1040. ctx->u.rep->count = ctx->count;
  1041. DO_JUMP(JUMP_MIN_UNTIL_1, jump_min_until_1,
  1042. ctx->u.rep->pattern+3);
  1043. if (ret) {
  1044. RETURN_ON_ERROR(ret);
  1045. RETURN_SUCCESS;
  1046. }
  1047. ctx->u.rep->count = ctx->count-1;
  1048. state->ptr = ptr;
  1049. RETURN_FAILURE;
  1050. }
  1051. /* see if the tail matches */
  1052. state->repeat = ctx->u.rep->prev;
  1053. LASTMARK_SAVE();
  1054. if (state->repeat)
  1055. MARK_PUSH(ctx->lastmark);
  1056. DO_JUMP(JUMP_MIN_UNTIL_2, jump_min_until_2, pattern);
  1057. SRE_REPEAT *repeat_of_tail = state->repeat;
  1058. state->repeat = ctx->u.rep; // restore repeat before return
  1059. if (ret) {
  1060. if (repeat_of_tail)
  1061. MARK_POP_DISCARD(ctx->lastmark);
  1062. RETURN_ON_ERROR(ret);
  1063. RETURN_SUCCESS;
  1064. }
  1065. if (repeat_of_tail)
  1066. MARK_POP(ctx->lastmark);
  1067. LASTMARK_RESTORE();
  1068. state->ptr = ptr;
  1069. if ((ctx->count >= (Py_ssize_t) ctx->u.rep->pattern[2]
  1070. && ctx->u.rep->pattern[2] != SRE_MAXREPEAT) ||
  1071. state->ptr == ctx->u.rep->last_ptr)
  1072. RETURN_FAILURE;
  1073. ctx->u.rep->count = ctx->count;
  1074. /* zero-width match protection */
  1075. DATA_PUSH(&ctx->u.rep->last_ptr);
  1076. ctx->u.rep->last_ptr = state->ptr;
  1077. DO_JUMP(JUMP_MIN_UNTIL_3,jump_min_until_3,
  1078. ctx->u.rep->pattern+3);
  1079. DATA_POP(&ctx->u.rep->last_ptr);
  1080. if (ret) {
  1081. RETURN_ON_ERROR(ret);
  1082. RETURN_SUCCESS;
  1083. }
  1084. ctx->u.rep->count = ctx->count-1;
  1085. state->ptr = ptr;
  1086. RETURN_FAILURE;
  1087. TARGET(SRE_OP_POSSESSIVE_REPEAT):
  1088. /* create possessive repeat contexts. */
  1089. /* <POSSESSIVE_REPEAT> <skip> <1=min> <2=max> pattern
  1090. <SUCCESS> tail */
  1091. TRACE(("|%p|%p|POSSESSIVE_REPEAT %d %d\n", pattern,
  1092. ptr, pattern[1], pattern[2]));
  1093. /* Set the global Input pointer to this context's Input
  1094. pointer */
  1095. state->ptr = ptr;
  1096. /* Initialize Count to 0 */
  1097. ctx->count = 0;
  1098. /* Check for minimum required matches. */
  1099. while (ctx->count < (Py_ssize_t)pattern[1]) {
  1100. /* not enough matches */
  1101. DO_JUMP0(JUMP_POSS_REPEAT_1, jump_poss_repeat_1,
  1102. &pattern[3]);
  1103. if (ret) {
  1104. RETURN_ON_ERROR(ret);
  1105. ctx->count++;
  1106. }
  1107. else {
  1108. state->ptr = ptr;
  1109. RETURN_FAILURE;
  1110. }
  1111. }
  1112. /* Clear the context's Input stream pointer so that it
  1113. doesn't match the global state so that the while loop can
  1114. be entered. */
  1115. ptr = NULL;
  1116. /* Keep trying to parse the <pattern> sub-pattern until the
  1117. end is reached, creating a new context each time. */
  1118. while ((ctx->count < (Py_ssize_t)pattern[2] ||
  1119. (Py_ssize_t)pattern[2] == SRE_MAXREPEAT) &&
  1120. state->ptr != ptr) {
  1121. /* Save the Capture Group Marker state into the current
  1122. Context and back up the current highest number
  1123. Capture Group marker. */
  1124. LASTMARK_SAVE();
  1125. MARK_PUSH(ctx->lastmark);
  1126. /* zero-width match protection */
  1127. /* Set the context's Input Stream pointer to be the
  1128. current Input Stream pointer from the global
  1129. state. When the loop reaches the next iteration,
  1130. the context will then store the last known good
  1131. position with the global state holding the Input
  1132. Input Stream position that has been updated with
  1133. the most recent match. Thus, if state's Input
  1134. stream remains the same as the one stored in the
  1135. current Context, we know we have successfully
  1136. matched an empty string and that all subsequent
  1137. matches will also be the empty string until the
  1138. maximum number of matches are counted, and because
  1139. of this, we could immediately stop at that point and
  1140. consider this match successful. */
  1141. ptr = state->ptr;
  1142. /* We have not reached the maximin matches, so try to
  1143. match once more. */
  1144. DO_JUMP0(JUMP_POSS_REPEAT_2, jump_poss_repeat_2,
  1145. &pattern[3]);
  1146. /* Check to see if the last attempted match
  1147. succeeded. */
  1148. if (ret) {
  1149. /* Drop the saved highest number Capture Group
  1150. marker saved above and use the newly updated
  1151. value. */
  1152. MARK_POP_DISCARD(ctx->lastmark);
  1153. RETURN_ON_ERROR(ret);
  1154. /* Success, increment the count. */
  1155. ctx->count++;
  1156. }
  1157. /* Last attempted match failed. */
  1158. else {
  1159. /* Restore the previously saved highest number
  1160. Capture Group marker since the last iteration
  1161. did not match, then restore that to the global
  1162. state. */
  1163. MARK_POP(ctx->lastmark);
  1164. LASTMARK_RESTORE();
  1165. /* Restore the global Input Stream pointer
  1166. since it can change after jumps. */
  1167. state->ptr = ptr;
  1168. /* We have sufficient matches, so exit loop. */
  1169. break;
  1170. }
  1171. }
  1172. /* Evaluate Tail */
  1173. /* Jump to end of pattern indicated by skip, and then skip
  1174. the SUCCESS op code that follows it. */
  1175. pattern += pattern[0] + 1;
  1176. ptr = state->ptr;
  1177. DISPATCH;
  1178. TARGET(SRE_OP_ATOMIC_GROUP):
  1179. /* Atomic Group Sub Pattern */
  1180. /* <ATOMIC_GROUP> <skip> pattern <SUCCESS> tail */
  1181. TRACE(("|%p|%p|ATOMIC_GROUP\n", pattern, ptr));
  1182. /* Set the global Input pointer to this context's Input
  1183. pointer */
  1184. state->ptr = ptr;
  1185. /* Evaluate the Atomic Group in a new context, terminating
  1186. when the end of the group, represented by a SUCCESS op
  1187. code, is reached. */
  1188. /* Group Pattern begins at an offset of 1 code. */
  1189. DO_JUMP0(JUMP_ATOMIC_GROUP, jump_atomic_group,
  1190. &pattern[1]);
  1191. /* Test Exit Condition */
  1192. RETURN_ON_ERROR(ret);
  1193. if (ret == 0) {
  1194. /* Atomic Group failed to Match. */
  1195. state->ptr = ptr;
  1196. RETURN_FAILURE;
  1197. }
  1198. /* Evaluate Tail */
  1199. /* Jump to end of pattern indicated by skip, and then skip
  1200. the SUCCESS op code that follows it. */
  1201. pattern += pattern[0];
  1202. ptr = state->ptr;
  1203. DISPATCH;
  1204. TARGET(SRE_OP_GROUPREF):
  1205. /* match backreference */
  1206. TRACE(("|%p|%p|GROUPREF %d\n", pattern,
  1207. ptr, pattern[0]));
  1208. {
  1209. int groupref = pattern[0] * 2;
  1210. if (groupref >= state->lastmark) {
  1211. RETURN_FAILURE;
  1212. } else {
  1213. SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
  1214. SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
  1215. if (!p || !e || e < p)
  1216. RETURN_FAILURE;
  1217. while (p < e) {
  1218. if (ptr >= end || *ptr != *p)
  1219. RETURN_FAILURE;
  1220. p++;
  1221. ptr++;
  1222. }
  1223. }
  1224. }
  1225. pattern++;
  1226. DISPATCH;
  1227. TARGET(SRE_OP_GROUPREF_IGNORE):
  1228. /* match backreference */
  1229. TRACE(("|%p|%p|GROUPREF_IGNORE %d\n", pattern,
  1230. ptr, pattern[0]));
  1231. {
  1232. int groupref = pattern[0] * 2;
  1233. if (groupref >= state->lastmark) {
  1234. RETURN_FAILURE;
  1235. } else {
  1236. SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
  1237. SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
  1238. if (!p || !e || e < p)
  1239. RETURN_FAILURE;
  1240. while (p < e) {
  1241. if (ptr >= end ||
  1242. sre_lower_ascii(*ptr) != sre_lower_ascii(*p))
  1243. RETURN_FAILURE;
  1244. p++;
  1245. ptr++;
  1246. }
  1247. }
  1248. }
  1249. pattern++;
  1250. DISPATCH;
  1251. TARGET(SRE_OP_GROUPREF_UNI_IGNORE):
  1252. /* match backreference */
  1253. TRACE(("|%p|%p|GROUPREF_UNI_IGNORE %d\n", pattern,
  1254. ptr, pattern[0]));
  1255. {
  1256. int groupref = pattern[0] * 2;
  1257. if (groupref >= state->lastmark) {
  1258. RETURN_FAILURE;
  1259. } else {
  1260. SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
  1261. SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
  1262. if (!p || !e || e < p)
  1263. RETURN_FAILURE;
  1264. while (p < e) {
  1265. if (ptr >= end ||
  1266. sre_lower_unicode(*ptr) != sre_lower_unicode(*p))
  1267. RETURN_FAILURE;
  1268. p++;
  1269. ptr++;
  1270. }
  1271. }
  1272. }
  1273. pattern++;
  1274. DISPATCH;
  1275. TARGET(SRE_OP_GROUPREF_LOC_IGNORE):
  1276. /* match backreference */
  1277. TRACE(("|%p|%p|GROUPREF_LOC_IGNORE %d\n", pattern,
  1278. ptr, pattern[0]));
  1279. {
  1280. int groupref = pattern[0] * 2;
  1281. if (groupref >= state->lastmark) {
  1282. RETURN_FAILURE;
  1283. } else {
  1284. SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
  1285. SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
  1286. if (!p || !e || e < p)
  1287. RETURN_FAILURE;
  1288. while (p < e) {
  1289. if (ptr >= end ||
  1290. sre_lower_locale(*ptr) != sre_lower_locale(*p))
  1291. RETURN_FAILURE;
  1292. p++;
  1293. ptr++;
  1294. }
  1295. }
  1296. }
  1297. pattern++;
  1298. DISPATCH;
  1299. TARGET(SRE_OP_GROUPREF_EXISTS):
  1300. TRACE(("|%p|%p|GROUPREF_EXISTS %d\n", pattern,
  1301. ptr, pattern[0]));
  1302. /* <GROUPREF_EXISTS> <group> <skip> codeyes <JUMP> codeno ... */
  1303. {
  1304. int groupref = pattern[0] * 2;
  1305. if (groupref >= state->lastmark) {
  1306. pattern += pattern[1];
  1307. DISPATCH;
  1308. } else {
  1309. SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
  1310. SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
  1311. if (!p || !e || e < p) {
  1312. pattern += pattern[1];
  1313. DISPATCH;
  1314. }
  1315. }
  1316. }
  1317. pattern += 2;
  1318. DISPATCH;
  1319. TARGET(SRE_OP_ASSERT):
  1320. /* assert subpattern */
  1321. /* <ASSERT> <skip> <back> <pattern> */
  1322. TRACE(("|%p|%p|ASSERT %d\n", pattern,
  1323. ptr, pattern[1]));
  1324. if ((uintptr_t)(ptr - (SRE_CHAR *)state->beginning) < pattern[1])
  1325. RETURN_FAILURE;
  1326. state->ptr = ptr - pattern[1];
  1327. DO_JUMP0(JUMP_ASSERT, jump_assert, pattern+2);
  1328. RETURN_ON_FAILURE(ret);
  1329. pattern += pattern[0];
  1330. DISPATCH;
  1331. TARGET(SRE_OP_ASSERT_NOT):
  1332. /* assert not subpattern */
  1333. /* <ASSERT_NOT> <skip> <back> <pattern> */
  1334. TRACE(("|%p|%p|ASSERT_NOT %d\n", pattern,
  1335. ptr, pattern[1]));
  1336. if ((uintptr_t)(ptr - (SRE_CHAR *)state->beginning) >= pattern[1]) {
  1337. state->ptr = ptr - pattern[1];
  1338. LASTMARK_SAVE();
  1339. if (state->repeat)
  1340. MARK_PUSH(ctx->lastmark);
  1341. DO_JUMP0(JUMP_ASSERT_NOT, jump_assert_not, pattern+2);
  1342. if (ret) {
  1343. if (state->repeat)
  1344. MARK_POP_DISCARD(ctx->lastmark);
  1345. RETURN_ON_ERROR(ret);
  1346. RETURN_FAILURE;
  1347. }
  1348. if (state->repeat)
  1349. MARK_POP(ctx->lastmark);
  1350. LASTMARK_RESTORE();
  1351. }
  1352. pattern += pattern[0];
  1353. DISPATCH;
  1354. TARGET(SRE_OP_FAILURE):
  1355. /* immediate failure */
  1356. TRACE(("|%p|%p|FAILURE\n", pattern, ptr));
  1357. RETURN_FAILURE;
  1358. #if !USE_COMPUTED_GOTOS
  1359. default:
  1360. #endif
  1361. // Also any unused opcodes:
  1362. TARGET(SRE_OP_RANGE_UNI_IGNORE):
  1363. TARGET(SRE_OP_SUBPATTERN):
  1364. TARGET(SRE_OP_RANGE):
  1365. TARGET(SRE_OP_NEGATE):
  1366. TARGET(SRE_OP_BIGCHARSET):
  1367. TARGET(SRE_OP_CHARSET):
  1368. TRACE(("|%p|%p|UNKNOWN %d\n", pattern, ptr,
  1369. pattern[-1]));
  1370. RETURN_ERROR(SRE_ERROR_ILLEGAL);
  1371. }
  1372. exit:
  1373. ctx_pos = ctx->last_ctx_pos;
  1374. jump = ctx->jump;
  1375. DATA_POP_DISCARD(ctx);
  1376. if (ctx_pos == -1) {
  1377. state->sigcount = sigcount;
  1378. return ret;
  1379. }
  1380. DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
  1381. switch (jump) {
  1382. case JUMP_MAX_UNTIL_2:
  1383. TRACE(("|%p|%p|JUMP_MAX_UNTIL_2\n", pattern, ptr));
  1384. goto jump_max_until_2;
  1385. case JUMP_MAX_UNTIL_3:
  1386. TRACE(("|%p|%p|JUMP_MAX_UNTIL_3\n", pattern, ptr));
  1387. goto jump_max_until_3;
  1388. case JUMP_MIN_UNTIL_2:
  1389. TRACE(("|%p|%p|JUMP_MIN_UNTIL_2\n", pattern, ptr));
  1390. goto jump_min_until_2;
  1391. case JUMP_MIN_UNTIL_3:
  1392. TRACE(("|%p|%p|JUMP_MIN_UNTIL_3\n", pattern, ptr));
  1393. goto jump_min_until_3;
  1394. case JUMP_BRANCH:
  1395. TRACE(("|%p|%p|JUMP_BRANCH\n", pattern, ptr));
  1396. goto jump_branch;
  1397. case JUMP_MAX_UNTIL_1:
  1398. TRACE(("|%p|%p|JUMP_MAX_UNTIL_1\n", pattern, ptr));
  1399. goto jump_max_until_1;
  1400. case JUMP_MIN_UNTIL_1:
  1401. TRACE(("|%p|%p|JUMP_MIN_UNTIL_1\n", pattern, ptr));
  1402. goto jump_min_until_1;
  1403. case JUMP_POSS_REPEAT_1:
  1404. TRACE(("|%p|%p|JUMP_POSS_REPEAT_1\n", pattern, ptr));
  1405. goto jump_poss_repeat_1;
  1406. case JUMP_POSS_REPEAT_2:
  1407. TRACE(("|%p|%p|JUMP_POSS_REPEAT_2\n", pattern, ptr));
  1408. goto jump_poss_repeat_2;
  1409. case JUMP_REPEAT:
  1410. TRACE(("|%p|%p|JUMP_REPEAT\n", pattern, ptr));
  1411. goto jump_repeat;
  1412. case JUMP_REPEAT_ONE_1:
  1413. TRACE(("|%p|%p|JUMP_REPEAT_ONE_1\n", pattern, ptr));
  1414. goto jump_repeat_one_1;
  1415. case JUMP_REPEAT_ONE_2:
  1416. TRACE(("|%p|%p|JUMP_REPEAT_ONE_2\n", pattern, ptr));
  1417. goto jump_repeat_one_2;
  1418. case JUMP_MIN_REPEAT_ONE:
  1419. TRACE(("|%p|%p|JUMP_MIN_REPEAT_ONE\n", pattern, ptr));
  1420. goto jump_min_repeat_one;
  1421. case JUMP_ATOMIC_GROUP:
  1422. TRACE(("|%p|%p|JUMP_ATOMIC_GROUP\n", pattern, ptr));
  1423. goto jump_atomic_group;
  1424. case JUMP_ASSERT:
  1425. TRACE(("|%p|%p|JUMP_ASSERT\n", pattern, ptr));
  1426. goto jump_assert;
  1427. case JUMP_ASSERT_NOT:
  1428. TRACE(("|%p|%p|JUMP_ASSERT_NOT\n", pattern, ptr));
  1429. goto jump_assert_not;
  1430. case JUMP_NONE:
  1431. TRACE(("|%p|%p|RETURN %zd\n", pattern,
  1432. ptr, ret));
  1433. break;
  1434. }
  1435. return ret; /* should never get here */
  1436. }
  1437. /* need to reset capturing groups between two SRE(match) callings in loops */
  1438. #define RESET_CAPTURE_GROUP() \
  1439. do { state->lastmark = state->lastindex = -1; } while (0)
  1440. LOCAL(Py_ssize_t)
  1441. SRE(search)(SRE_STATE* state, SRE_CODE* pattern)
  1442. {
  1443. SRE_CHAR* ptr = (SRE_CHAR *)state->start;
  1444. SRE_CHAR* end = (SRE_CHAR *)state->end;
  1445. Py_ssize_t status = 0;
  1446. Py_ssize_t prefix_len = 0;
  1447. Py_ssize_t prefix_skip = 0;
  1448. SRE_CODE* prefix = NULL;
  1449. SRE_CODE* charset = NULL;
  1450. SRE_CODE* overlap = NULL;
  1451. int flags = 0;
  1452. if (ptr > end)
  1453. return 0;
  1454. if (pattern[0] == SRE_OP_INFO) {
  1455. /* optimization info block */
  1456. /* <INFO> <1=skip> <2=flags> <3=min> <4=max> <5=prefix info> */
  1457. flags = pattern[2];
  1458. if (pattern[3] && (uintptr_t)(end - ptr) < pattern[3]) {
  1459. TRACE(("reject (got %tu chars, need %zu)\n",
  1460. end - ptr, (size_t) pattern[3]));
  1461. return 0;
  1462. }
  1463. if (pattern[3] > 1) {
  1464. /* adjust end point (but make sure we leave at least one
  1465. character in there, so literal search will work) */
  1466. end -= pattern[3] - 1;
  1467. if (end <= ptr)
  1468. end = ptr;
  1469. }
  1470. if (flags & SRE_INFO_PREFIX) {
  1471. /* pattern starts with a known prefix */
  1472. /* <length> <skip> <prefix data> <overlap data> */
  1473. prefix_len = pattern[5];
  1474. prefix_skip = pattern[6];
  1475. prefix = pattern + 7;
  1476. overlap = prefix + prefix_len - 1;
  1477. } else if (flags & SRE_INFO_CHARSET)
  1478. /* pattern starts with a character from a known set */
  1479. /* <charset> */
  1480. charset = pattern + 5;
  1481. pattern += 1 + pattern[1];
  1482. }
  1483. TRACE(("prefix = %p %zd %zd\n",
  1484. prefix, prefix_len, prefix_skip));
  1485. TRACE(("charset = %p\n", charset));
  1486. if (prefix_len == 1) {
  1487. /* pattern starts with a literal character */
  1488. SRE_CHAR c = (SRE_CHAR) prefix[0];
  1489. #if SIZEOF_SRE_CHAR < 4
  1490. if ((SRE_CODE) c != prefix[0])
  1491. return 0; /* literal can't match: doesn't fit in char width */
  1492. #endif
  1493. end = (SRE_CHAR *)state->end;
  1494. state->must_advance = 0;
  1495. while (ptr < end) {
  1496. while (*ptr != c) {
  1497. if (++ptr >= end)
  1498. return 0;
  1499. }
  1500. TRACE(("|%p|%p|SEARCH LITERAL\n", pattern, ptr));
  1501. state->start = ptr;
  1502. state->ptr = ptr + prefix_skip;
  1503. if (flags & SRE_INFO_LITERAL)
  1504. return 1; /* we got all of it */
  1505. status = SRE(match)(state, pattern + 2*prefix_skip, 0);
  1506. if (status != 0)
  1507. return status;
  1508. ++ptr;
  1509. RESET_CAPTURE_GROUP();
  1510. }
  1511. return 0;
  1512. }
  1513. if (prefix_len > 1) {
  1514. /* pattern starts with a known prefix. use the overlap
  1515. table to skip forward as fast as we possibly can */
  1516. Py_ssize_t i = 0;
  1517. end = (SRE_CHAR *)state->end;
  1518. if (prefix_len > end - ptr)
  1519. return 0;
  1520. #if SIZEOF_SRE_CHAR < 4
  1521. for (i = 0; i < prefix_len; i++)
  1522. if ((SRE_CODE)(SRE_CHAR) prefix[i] != prefix[i])
  1523. return 0; /* literal can't match: doesn't fit in char width */
  1524. #endif
  1525. while (ptr < end) {
  1526. SRE_CHAR c = (SRE_CHAR) prefix[0];
  1527. while (*ptr++ != c) {
  1528. if (ptr >= end)
  1529. return 0;
  1530. }
  1531. if (ptr >= end)
  1532. return 0;
  1533. i = 1;
  1534. state->must_advance = 0;
  1535. do {
  1536. if (*ptr == (SRE_CHAR) prefix[i]) {
  1537. if (++i != prefix_len) {
  1538. if (++ptr >= end)
  1539. return 0;
  1540. continue;
  1541. }
  1542. /* found a potential match */
  1543. TRACE(("|%p|%p|SEARCH SCAN\n", pattern, ptr));
  1544. state->start = ptr - (prefix_len - 1);
  1545. state->ptr = ptr - (prefix_len - prefix_skip - 1);
  1546. if (flags & SRE_INFO_LITERAL)
  1547. return 1; /* we got all of it */
  1548. status = SRE(match)(state, pattern + 2*prefix_skip, 0);
  1549. if (status != 0)
  1550. return status;
  1551. /* close but no cigar -- try again */
  1552. if (++ptr >= end)
  1553. return 0;
  1554. RESET_CAPTURE_GROUP();
  1555. }
  1556. i = overlap[i];
  1557. } while (i != 0);
  1558. }
  1559. return 0;
  1560. }
  1561. if (charset) {
  1562. /* pattern starts with a character from a known set */
  1563. end = (SRE_CHAR *)state->end;
  1564. state->must_advance = 0;
  1565. for (;;) {
  1566. while (ptr < end && !SRE(charset)(state, charset, *ptr))
  1567. ptr++;
  1568. if (ptr >= end)
  1569. return 0;
  1570. TRACE(("|%p|%p|SEARCH CHARSET\n", pattern, ptr));
  1571. state->start = ptr;
  1572. state->ptr = ptr;
  1573. status = SRE(match)(state, pattern, 0);
  1574. if (status != 0)
  1575. break;
  1576. ptr++;
  1577. RESET_CAPTURE_GROUP();
  1578. }
  1579. } else {
  1580. /* general case */
  1581. assert(ptr <= end);
  1582. TRACE(("|%p|%p|SEARCH\n", pattern, ptr));
  1583. state->start = state->ptr = ptr;
  1584. status = SRE(match)(state, pattern, 1);
  1585. state->must_advance = 0;
  1586. if (status == 0 && pattern[0] == SRE_OP_AT &&
  1587. (pattern[1] == SRE_AT_BEGINNING ||
  1588. pattern[1] == SRE_AT_BEGINNING_STRING))
  1589. {
  1590. state->start = state->ptr = ptr = end;
  1591. return 0;
  1592. }
  1593. while (status == 0 && ptr < end) {
  1594. ptr++;
  1595. RESET_CAPTURE_GROUP();
  1596. TRACE(("|%p|%p|SEARCH\n", pattern, ptr));
  1597. state->start = state->ptr = ptr;
  1598. status = SRE(match)(state, pattern, 0);
  1599. }
  1600. }
  1601. return status;
  1602. }
  1603. #undef SRE_CHAR
  1604. #undef SIZEOF_SRE_CHAR
  1605. #undef SRE
  1606. /* vim:ts=4:sw=4:et
  1607. */