PageRenderTime 63ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/module/cpyext/test/_sre.c

https://bitbucket.org/dac_io/pypy
C | 3912 lines | 2977 code | 574 blank | 361 comment | 629 complexity | 7e13b2e9dea5e5f8bb88018fb0811076 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * Secret Labs' Regular Expression Engine
  3. *
  4. * regular expression matching engine
  5. *
  6. * partial history:
  7. * 1999-10-24 fl created (based on existing template matcher code)
  8. * 2000-03-06 fl first alpha, sort of
  9. * 2000-08-01 fl fixes for 1.6b1
  10. * 2000-08-07 fl use PyOS_CheckStack() if available
  11. * 2000-09-20 fl added expand method
  12. * 2001-03-20 fl lots of fixes for 2.1b2
  13. * 2001-04-15 fl export copyright as Python attribute, not global
  14. * 2001-04-28 fl added __copy__ methods (work in progress)
  15. * 2001-05-14 fl fixes for 1.5.2 compatibility
  16. * 2001-07-01 fl added BIGCHARSET support (from Martin von Loewis)
  17. * 2001-10-18 fl fixed group reset issue (from Matthew Mueller)
  18. * 2001-10-20 fl added split primitive; reenable unicode for 1.6/2.0/2.1
  19. * 2001-10-21 fl added sub/subn primitive
  20. * 2001-10-24 fl added finditer primitive (for 2.2 only)
  21. * 2001-12-07 fl fixed memory leak in sub/subn (Guido van Rossum)
  22. * 2002-11-09 fl fixed empty sub/subn return type
  23. * 2003-04-18 mvl fully support 4-byte codes
  24. * 2003-10-17 gn implemented non recursive scheme
  25. *
  26. * Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
  27. *
  28. * This version of the SRE library can be redistributed under CNRI's
  29. * Python 1.6 license. For any other use, please contact Secret Labs
  30. * AB (info@pythonware.com).
  31. *
  32. * Portions of this engine have been developed in cooperation with
  33. * CNRI. Hewlett-Packard provided funding for 1.6 integration and
  34. * other compatibility work.
  35. */
  36. #ifndef SRE_RECURSIVE
  37. static char copyright[] =
  38. " SRE 2.2.2 Copyright (c) 1997-2002 by Secret Labs AB ";
  39. #define PY_SSIZE_T_CLEAN
  40. #include "Python.h"
  41. #include "structmember.h" /* offsetof */
  42. #include "sre.h"
  43. #include <ctype.h>
  44. /* name of this module, minus the leading underscore */
  45. #if !defined(SRE_MODULE)
  46. #define SRE_MODULE "sre"
  47. #endif
  48. #define SRE_PY_MODULE "re"
  49. /* defining this one enables tracing */
  50. #undef VERBOSE
  51. #if PY_VERSION_HEX >= 0x01060000
  52. #if PY_VERSION_HEX < 0x02020000 || defined(Py_USING_UNICODE)
  53. /* defining this enables unicode support (default under 1.6a1 and later) */
  54. #define HAVE_UNICODE
  55. #endif
  56. #endif
  57. /* -------------------------------------------------------------------- */
  58. /* optional features */
  59. /* enables fast searching */
  60. #define USE_FAST_SEARCH
  61. /* enables aggressive inlining (always on for Visual C) */
  62. #undef USE_INLINE
  63. /* enables copy/deepcopy handling (work in progress) */
  64. #undef USE_BUILTIN_COPY
  65. #if PY_VERSION_HEX < 0x01060000
  66. #define PyObject_DEL(op) PyMem_DEL((op))
  67. #endif
  68. #define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size)
  69. #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
  70. /* -------------------------------------------------------------------- */
  71. #if defined(_MSC_VER)
  72. #pragma optimize("agtw", on) /* doesn't seem to make much difference... */
  73. #pragma warning(disable: 4710) /* who cares if functions are not inlined ;-) */
  74. /* fastest possible local call under MSVC */
  75. #define LOCAL(type) static __inline type __fastcall
  76. #elif defined(USE_INLINE)
  77. #define LOCAL(type) static inline type
  78. #else
  79. #define LOCAL(type) static type
  80. #endif
  81. /* error codes */
  82. #define SRE_ERROR_ILLEGAL -1 /* illegal opcode */
  83. #define SRE_ERROR_STATE -2 /* illegal state */
  84. #define SRE_ERROR_RECURSION_LIMIT -3 /* runaway recursion */
  85. #define SRE_ERROR_MEMORY -9 /* out of memory */
  86. #define SRE_ERROR_INTERRUPTED -10 /* signal handler raised exception */
  87. #if defined(VERBOSE)
  88. #define TRACE(v) printf v
  89. #else
  90. #define TRACE(v)
  91. #endif
  92. /* -------------------------------------------------------------------- */
  93. /* search engine state */
  94. /* default character predicates (run sre_chars.py to regenerate tables) */
  95. #define SRE_DIGIT_MASK 1
  96. #define SRE_SPACE_MASK 2
  97. #define SRE_LINEBREAK_MASK 4
  98. #define SRE_ALNUM_MASK 8
  99. #define SRE_WORD_MASK 16
  100. /* FIXME: this assumes ASCII. create tables in init_sre() instead */
  101. static char sre_char_info[128] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 6, 2,
  102. 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0,
  103. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 25, 25, 25, 25, 25, 25, 25,
  104. 25, 25, 0, 0, 0, 0, 0, 0, 0, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  105. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 0, 0,
  106. 0, 0, 16, 0, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  107. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 0, 0, 0, 0, 0 };
  108. static char sre_char_lower[128] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  109. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
  110. 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
  111. 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
  112. 61, 62, 63, 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
  113. 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
  114. 122, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105,
  115. 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
  116. 120, 121, 122, 123, 124, 125, 126, 127 };
  117. #define SRE_IS_DIGIT(ch)\
  118. ((ch) < 128 ? (sre_char_info[(ch)] & SRE_DIGIT_MASK) : 0)
  119. #define SRE_IS_SPACE(ch)\
  120. ((ch) < 128 ? (sre_char_info[(ch)] & SRE_SPACE_MASK) : 0)
  121. #define SRE_IS_LINEBREAK(ch)\
  122. ((ch) < 128 ? (sre_char_info[(ch)] & SRE_LINEBREAK_MASK) : 0)
  123. #define SRE_IS_ALNUM(ch)\
  124. ((ch) < 128 ? (sre_char_info[(ch)] & SRE_ALNUM_MASK) : 0)
  125. #define SRE_IS_WORD(ch)\
  126. ((ch) < 128 ? (sre_char_info[(ch)] & SRE_WORD_MASK) : 0)
  127. static unsigned int sre_lower(unsigned int ch)
  128. {
  129. return ((ch) < 128 ? (unsigned int)sre_char_lower[ch] : ch);
  130. }
  131. /* locale-specific character predicates */
  132. /* !(c & ~N) == (c < N+1) for any unsigned c, this avoids
  133. * warnings when c's type supports only numbers < N+1 */
  134. #define SRE_LOC_IS_DIGIT(ch) (!((ch) & ~255) ? isdigit((ch)) : 0)
  135. #define SRE_LOC_IS_SPACE(ch) (!((ch) & ~255) ? isspace((ch)) : 0)
  136. #define SRE_LOC_IS_LINEBREAK(ch) ((ch) == '\n')
  137. #define SRE_LOC_IS_ALNUM(ch) (!((ch) & ~255) ? isalnum((ch)) : 0)
  138. #define SRE_LOC_IS_WORD(ch) (SRE_LOC_IS_ALNUM((ch)) || (ch) == '_')
  139. static unsigned int sre_lower_locale(unsigned int ch)
  140. {
  141. return ((ch) < 256 ? (unsigned int)tolower((ch)) : ch);
  142. }
  143. /* unicode-specific character predicates */
  144. #if defined(HAVE_UNICODE)
  145. #define SRE_UNI_IS_DIGIT(ch) Py_UNICODE_ISDECIMAL((Py_UNICODE)(ch))
  146. #define SRE_UNI_IS_SPACE(ch) Py_UNICODE_ISSPACE((Py_UNICODE)(ch))
  147. #define SRE_UNI_IS_LINEBREAK(ch) Py_UNICODE_ISLINEBREAK((Py_UNICODE)(ch))
  148. #define SRE_UNI_IS_ALNUM(ch) Py_UNICODE_ISALNUM((Py_UNICODE)(ch))
  149. #define SRE_UNI_IS_WORD(ch) (SRE_UNI_IS_ALNUM((ch)) || (ch) == '_')
  150. static unsigned int sre_lower_unicode(unsigned int ch)
  151. {
  152. return (unsigned int) Py_UNICODE_TOLOWER((Py_UNICODE)(ch));
  153. }
  154. #endif
  155. LOCAL(int)
  156. sre_category(SRE_CODE category, unsigned int ch)
  157. {
  158. switch (category) {
  159. case SRE_CATEGORY_DIGIT:
  160. return SRE_IS_DIGIT(ch);
  161. case SRE_CATEGORY_NOT_DIGIT:
  162. return !SRE_IS_DIGIT(ch);
  163. case SRE_CATEGORY_SPACE:
  164. return SRE_IS_SPACE(ch);
  165. case SRE_CATEGORY_NOT_SPACE:
  166. return !SRE_IS_SPACE(ch);
  167. case SRE_CATEGORY_WORD:
  168. return SRE_IS_WORD(ch);
  169. case SRE_CATEGORY_NOT_WORD:
  170. return !SRE_IS_WORD(ch);
  171. case SRE_CATEGORY_LINEBREAK:
  172. return SRE_IS_LINEBREAK(ch);
  173. case SRE_CATEGORY_NOT_LINEBREAK:
  174. return !SRE_IS_LINEBREAK(ch);
  175. case SRE_CATEGORY_LOC_WORD:
  176. return SRE_LOC_IS_WORD(ch);
  177. case SRE_CATEGORY_LOC_NOT_WORD:
  178. return !SRE_LOC_IS_WORD(ch);
  179. #if defined(HAVE_UNICODE)
  180. case SRE_CATEGORY_UNI_DIGIT:
  181. return SRE_UNI_IS_DIGIT(ch);
  182. case SRE_CATEGORY_UNI_NOT_DIGIT:
  183. return !SRE_UNI_IS_DIGIT(ch);
  184. case SRE_CATEGORY_UNI_SPACE:
  185. return SRE_UNI_IS_SPACE(ch);
  186. case SRE_CATEGORY_UNI_NOT_SPACE:
  187. return !SRE_UNI_IS_SPACE(ch);
  188. case SRE_CATEGORY_UNI_WORD:
  189. return SRE_UNI_IS_WORD(ch);
  190. case SRE_CATEGORY_UNI_NOT_WORD:
  191. return !SRE_UNI_IS_WORD(ch);
  192. case SRE_CATEGORY_UNI_LINEBREAK:
  193. return SRE_UNI_IS_LINEBREAK(ch);
  194. case SRE_CATEGORY_UNI_NOT_LINEBREAK:
  195. return !SRE_UNI_IS_LINEBREAK(ch);
  196. #else
  197. case SRE_CATEGORY_UNI_DIGIT:
  198. return SRE_IS_DIGIT(ch);
  199. case SRE_CATEGORY_UNI_NOT_DIGIT:
  200. return !SRE_IS_DIGIT(ch);
  201. case SRE_CATEGORY_UNI_SPACE:
  202. return SRE_IS_SPACE(ch);
  203. case SRE_CATEGORY_UNI_NOT_SPACE:
  204. return !SRE_IS_SPACE(ch);
  205. case SRE_CATEGORY_UNI_WORD:
  206. return SRE_LOC_IS_WORD(ch);
  207. case SRE_CATEGORY_UNI_NOT_WORD:
  208. return !SRE_LOC_IS_WORD(ch);
  209. case SRE_CATEGORY_UNI_LINEBREAK:
  210. return SRE_IS_LINEBREAK(ch);
  211. case SRE_CATEGORY_UNI_NOT_LINEBREAK:
  212. return !SRE_IS_LINEBREAK(ch);
  213. #endif
  214. }
  215. return 0;
  216. }
  217. /* helpers */
  218. static void
  219. data_stack_dealloc(SRE_STATE* state)
  220. {
  221. if (state->data_stack) {
  222. PyMem_FREE(state->data_stack);
  223. state->data_stack = NULL;
  224. }
  225. state->data_stack_size = state->data_stack_base = 0;
  226. }
  227. static int
  228. data_stack_grow(SRE_STATE* state, Py_ssize_t size)
  229. {
  230. Py_ssize_t minsize, cursize;
  231. minsize = state->data_stack_base+size;
  232. cursize = state->data_stack_size;
  233. if (cursize < minsize) {
  234. void* stack;
  235. cursize = minsize+minsize/4+1024;
  236. TRACE(("allocate/grow stack %d\n", cursize));
  237. stack = PyMem_REALLOC(state->data_stack, cursize);
  238. if (!stack) {
  239. data_stack_dealloc(state);
  240. return SRE_ERROR_MEMORY;
  241. }
  242. state->data_stack = (char *)stack;
  243. state->data_stack_size = cursize;
  244. }
  245. return 0;
  246. }
  247. /* generate 8-bit version */
  248. #define SRE_CHAR unsigned char
  249. #define SRE_AT sre_at
  250. #define SRE_COUNT sre_count
  251. #define SRE_CHARSET sre_charset
  252. #define SRE_INFO sre_info
  253. #define SRE_MATCH sre_match
  254. #define SRE_MATCH_CONTEXT sre_match_context
  255. #define SRE_SEARCH sre_search
  256. #define SRE_LITERAL_TEMPLATE sre_literal_template
  257. #if defined(HAVE_UNICODE)
  258. #define SRE_RECURSIVE
  259. #include "_sre.c"
  260. #undef SRE_RECURSIVE
  261. #undef SRE_LITERAL_TEMPLATE
  262. #undef SRE_SEARCH
  263. #undef SRE_MATCH
  264. #undef SRE_MATCH_CONTEXT
  265. #undef SRE_INFO
  266. #undef SRE_CHARSET
  267. #undef SRE_COUNT
  268. #undef SRE_AT
  269. #undef SRE_CHAR
  270. /* generate 16-bit unicode version */
  271. #define SRE_CHAR Py_UNICODE
  272. #define SRE_AT sre_uat
  273. #define SRE_COUNT sre_ucount
  274. #define SRE_CHARSET sre_ucharset
  275. #define SRE_INFO sre_uinfo
  276. #define SRE_MATCH sre_umatch
  277. #define SRE_MATCH_CONTEXT sre_umatch_context
  278. #define SRE_SEARCH sre_usearch
  279. #define SRE_LITERAL_TEMPLATE sre_uliteral_template
  280. #endif
  281. #endif /* SRE_RECURSIVE */
  282. /* -------------------------------------------------------------------- */
  283. /* String matching engine */
  284. /* the following section is compiled twice, with different character
  285. settings */
  286. LOCAL(int)
  287. SRE_AT(SRE_STATE* state, SRE_CHAR* ptr, SRE_CODE at)
  288. {
  289. /* check if pointer is at given position */
  290. Py_ssize_t thisp, thatp;
  291. switch (at) {
  292. case SRE_AT_BEGINNING:
  293. case SRE_AT_BEGINNING_STRING:
  294. return ((void*) ptr == state->beginning);
  295. case SRE_AT_BEGINNING_LINE:
  296. return ((void*) ptr == state->beginning ||
  297. SRE_IS_LINEBREAK((int) ptr[-1]));
  298. case SRE_AT_END:
  299. return (((void*) (ptr+1) == state->end &&
  300. SRE_IS_LINEBREAK((int) ptr[0])) ||
  301. ((void*) ptr == state->end));
  302. case SRE_AT_END_LINE:
  303. return ((void*) ptr == state->end ||
  304. SRE_IS_LINEBREAK((int) ptr[0]));
  305. case SRE_AT_END_STRING:
  306. return ((void*) ptr == state->end);
  307. case SRE_AT_BOUNDARY:
  308. if (state->beginning == state->end)
  309. return 0;
  310. thatp = ((void*) ptr > state->beginning) ?
  311. SRE_IS_WORD((int) ptr[-1]) : 0;
  312. thisp = ((void*) ptr < state->end) ?
  313. SRE_IS_WORD((int) ptr[0]) : 0;
  314. return thisp != thatp;
  315. case SRE_AT_NON_BOUNDARY:
  316. if (state->beginning == state->end)
  317. return 0;
  318. thatp = ((void*) ptr > state->beginning) ?
  319. SRE_IS_WORD((int) ptr[-1]) : 0;
  320. thisp = ((void*) ptr < state->end) ?
  321. SRE_IS_WORD((int) ptr[0]) : 0;
  322. return thisp == thatp;
  323. case SRE_AT_LOC_BOUNDARY:
  324. if (state->beginning == state->end)
  325. return 0;
  326. thatp = ((void*) ptr > state->beginning) ?
  327. SRE_LOC_IS_WORD((int) ptr[-1]) : 0;
  328. thisp = ((void*) ptr < state->end) ?
  329. SRE_LOC_IS_WORD((int) ptr[0]) : 0;
  330. return thisp != thatp;
  331. case SRE_AT_LOC_NON_BOUNDARY:
  332. if (state->beginning == state->end)
  333. return 0;
  334. thatp = ((void*) ptr > state->beginning) ?
  335. SRE_LOC_IS_WORD((int) ptr[-1]) : 0;
  336. thisp = ((void*) ptr < state->end) ?
  337. SRE_LOC_IS_WORD((int) ptr[0]) : 0;
  338. return thisp == thatp;
  339. #if defined(HAVE_UNICODE)
  340. case SRE_AT_UNI_BOUNDARY:
  341. if (state->beginning == state->end)
  342. return 0;
  343. thatp = ((void*) ptr > state->beginning) ?
  344. SRE_UNI_IS_WORD((int) ptr[-1]) : 0;
  345. thisp = ((void*) ptr < state->end) ?
  346. SRE_UNI_IS_WORD((int) ptr[0]) : 0;
  347. return thisp != thatp;
  348. case SRE_AT_UNI_NON_BOUNDARY:
  349. if (state->beginning == state->end)
  350. return 0;
  351. thatp = ((void*) ptr > state->beginning) ?
  352. SRE_UNI_IS_WORD((int) ptr[-1]) : 0;
  353. thisp = ((void*) ptr < state->end) ?
  354. SRE_UNI_IS_WORD((int) ptr[0]) : 0;
  355. return thisp == thatp;
  356. #endif
  357. }
  358. return 0;
  359. }
  360. LOCAL(int)
  361. SRE_CHARSET(SRE_CODE* set, SRE_CODE ch)
  362. {
  363. /* check if character is a member of the given set */
  364. int ok = 1;
  365. for (;;) {
  366. switch (*set++) {
  367. case SRE_OP_FAILURE:
  368. return !ok;
  369. case SRE_OP_LITERAL:
  370. /* <LITERAL> <code> */
  371. if (ch == set[0])
  372. return ok;
  373. set++;
  374. break;
  375. case SRE_OP_CATEGORY:
  376. /* <CATEGORY> <code> */
  377. if (sre_category(set[0], (int) ch))
  378. return ok;
  379. set += 1;
  380. break;
  381. case SRE_OP_CHARSET:
  382. if (sizeof(SRE_CODE) == 2) {
  383. /* <CHARSET> <bitmap> (16 bits per code word) */
  384. if (ch < 256 && (set[ch >> 4] & (1 << (ch & 15))))
  385. return ok;
  386. set += 16;
  387. }
  388. else {
  389. /* <CHARSET> <bitmap> (32 bits per code word) */
  390. if (ch < 256 && (set[ch >> 5] & (1 << (ch & 31))))
  391. return ok;
  392. set += 8;
  393. }
  394. break;
  395. case SRE_OP_RANGE:
  396. /* <RANGE> <lower> <upper> */
  397. if (set[0] <= ch && ch <= set[1])
  398. return ok;
  399. set += 2;
  400. break;
  401. case SRE_OP_NEGATE:
  402. ok = !ok;
  403. break;
  404. case SRE_OP_BIGCHARSET:
  405. /* <BIGCHARSET> <blockcount> <256 blockindices> <blocks> */
  406. {
  407. Py_ssize_t count, block;
  408. count = *(set++);
  409. if (sizeof(SRE_CODE) == 2) {
  410. block = ((unsigned char*)set)[ch >> 8];
  411. set += 128;
  412. if (set[block*16 + ((ch & 255)>>4)] & (1 << (ch & 15)))
  413. return ok;
  414. set += count*16;
  415. }
  416. else {
  417. /* !(c & ~N) == (c < N+1) for any unsigned c, this avoids
  418. * warnings when c's type supports only numbers < N+1 */
  419. if (!(ch & ~65535))
  420. block = ((unsigned char*)set)[ch >> 8];
  421. else
  422. block = -1;
  423. set += 64;
  424. if (block >=0 &&
  425. (set[block*8 + ((ch & 255)>>5)] & (1 << (ch & 31))))
  426. return ok;
  427. set += count*8;
  428. }
  429. break;
  430. }
  431. default:
  432. /* internal error -- there's not much we can do about it
  433. here, so let's just pretend it didn't match... */
  434. return 0;
  435. }
  436. }
  437. }
  438. LOCAL(Py_ssize_t) SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern);
  439. LOCAL(Py_ssize_t)
  440. SRE_COUNT(SRE_STATE* state, SRE_CODE* pattern, Py_ssize_t maxcount)
  441. {
  442. SRE_CODE chr;
  443. SRE_CHAR* ptr = (SRE_CHAR *)state->ptr;
  444. SRE_CHAR* end = (SRE_CHAR *)state->end;
  445. Py_ssize_t i;
  446. /* adjust end */
  447. if (maxcount < end - ptr && maxcount != 65535)
  448. end = ptr + maxcount;
  449. switch (pattern[0]) {
  450. case SRE_OP_IN:
  451. /* repeated set */
  452. TRACE(("|%p|%p|COUNT IN\n", pattern, ptr));
  453. while (ptr < end && SRE_CHARSET(pattern + 2, *ptr))
  454. ptr++;
  455. break;
  456. case SRE_OP_ANY:
  457. /* repeated dot wildcard. */
  458. TRACE(("|%p|%p|COUNT ANY\n", pattern, ptr));
  459. while (ptr < end && !SRE_IS_LINEBREAK(*ptr))
  460. ptr++;
  461. break;
  462. case SRE_OP_ANY_ALL:
  463. /* repeated dot wildcard. skip to the end of the target
  464. string, and backtrack from there */
  465. TRACE(("|%p|%p|COUNT ANY_ALL\n", pattern, ptr));
  466. ptr = end;
  467. break;
  468. case SRE_OP_LITERAL:
  469. /* repeated literal */
  470. chr = pattern[1];
  471. TRACE(("|%p|%p|COUNT LITERAL %d\n", pattern, ptr, chr));
  472. while (ptr < end && (SRE_CODE) *ptr == chr)
  473. ptr++;
  474. break;
  475. case SRE_OP_LITERAL_IGNORE:
  476. /* repeated literal */
  477. chr = pattern[1];
  478. TRACE(("|%p|%p|COUNT LITERAL_IGNORE %d\n", pattern, ptr, chr));
  479. while (ptr < end && (SRE_CODE) state->lower(*ptr) == chr)
  480. ptr++;
  481. break;
  482. case SRE_OP_NOT_LITERAL:
  483. /* repeated non-literal */
  484. chr = pattern[1];
  485. TRACE(("|%p|%p|COUNT NOT_LITERAL %d\n", pattern, ptr, chr));
  486. while (ptr < end && (SRE_CODE) *ptr != chr)
  487. ptr++;
  488. break;
  489. case SRE_OP_NOT_LITERAL_IGNORE:
  490. /* repeated non-literal */
  491. chr = pattern[1];
  492. TRACE(("|%p|%p|COUNT NOT_LITERAL_IGNORE %d\n", pattern, ptr, chr));
  493. while (ptr < end && (SRE_CODE) state->lower(*ptr) != chr)
  494. ptr++;
  495. break;
  496. default:
  497. /* repeated single character pattern */
  498. TRACE(("|%p|%p|COUNT SUBPATTERN\n", pattern, ptr));
  499. while ((SRE_CHAR*) state->ptr < end) {
  500. i = SRE_MATCH(state, pattern);
  501. if (i < 0)
  502. return i;
  503. if (!i)
  504. break;
  505. }
  506. TRACE(("|%p|%p|COUNT %d\n", pattern, ptr,
  507. (SRE_CHAR*) state->ptr - ptr));
  508. return (SRE_CHAR*) state->ptr - ptr;
  509. }
  510. TRACE(("|%p|%p|COUNT %d\n", pattern, ptr, ptr - (SRE_CHAR*) state->ptr));
  511. return ptr - (SRE_CHAR*) state->ptr;
  512. }
  513. #if 0 /* not used in this release */
  514. LOCAL(int)
  515. SRE_INFO(SRE_STATE* state, SRE_CODE* pattern)
  516. {
  517. /* check if an SRE_OP_INFO block matches at the current position.
  518. returns the number of SRE_CODE objects to skip if successful, 0
  519. if no match */
  520. SRE_CHAR* end = state->end;
  521. SRE_CHAR* ptr = state->ptr;
  522. Py_ssize_t i;
  523. /* check minimal length */
  524. if (pattern[3] && (end - ptr) < pattern[3])
  525. return 0;
  526. /* check known prefix */
  527. if (pattern[2] & SRE_INFO_PREFIX && pattern[5] > 1) {
  528. /* <length> <skip> <prefix data> <overlap data> */
  529. for (i = 0; i < pattern[5]; i++)
  530. if ((SRE_CODE) ptr[i] != pattern[7 + i])
  531. return 0;
  532. return pattern[0] + 2 * pattern[6];
  533. }
  534. return pattern[0];
  535. }
  536. #endif
  537. /* The macros below should be used to protect recursive SRE_MATCH()
  538. * calls that *failed* and do *not* return immediately (IOW, those
  539. * that will backtrack). Explaining:
  540. *
  541. * - Recursive SRE_MATCH() returned true: that's usually a success
  542. * (besides atypical cases like ASSERT_NOT), therefore there's no
  543. * reason to restore lastmark;
  544. *
  545. * - Recursive SRE_MATCH() returned false but the current SRE_MATCH()
  546. * is returning to the caller: If the current SRE_MATCH() is the
  547. * top function of the recursion, returning false will be a matching
  548. * failure, and it doesn't matter where lastmark is pointing to.
  549. * If it's *not* the top function, it will be a recursive SRE_MATCH()
  550. * failure by itself, and the calling SRE_MATCH() will have to deal
  551. * with the failure by the same rules explained here (it will restore
  552. * lastmark by itself if necessary);
  553. *
  554. * - Recursive SRE_MATCH() returned false, and will continue the
  555. * outside 'for' loop: must be protected when breaking, since the next
  556. * OP could potentially depend on lastmark;
  557. *
  558. * - Recursive SRE_MATCH() returned false, and will be called again
  559. * inside a local for/while loop: must be protected between each
  560. * loop iteration, since the recursive SRE_MATCH() could do anything,
  561. * and could potentially depend on lastmark.
  562. *
  563. * For more information, check the discussion at SF patch #712900.
  564. */
  565. #define LASTMARK_SAVE() \
  566. do { \
  567. ctx->lastmark = state->lastmark; \
  568. ctx->lastindex = state->lastindex; \
  569. } while (0)
  570. #define LASTMARK_RESTORE() \
  571. do { \
  572. state->lastmark = ctx->lastmark; \
  573. state->lastindex = ctx->lastindex; \
  574. } while (0)
  575. #define RETURN_ERROR(i) do { return i; } while(0)
  576. #define RETURN_FAILURE do { ret = 0; goto exit; } while(0)
  577. #define RETURN_SUCCESS do { ret = 1; goto exit; } while(0)
  578. #define RETURN_ON_ERROR(i) \
  579. do { if (i < 0) RETURN_ERROR(i); } while (0)
  580. #define RETURN_ON_SUCCESS(i) \
  581. do { RETURN_ON_ERROR(i); if (i > 0) RETURN_SUCCESS; } while (0)
  582. #define RETURN_ON_FAILURE(i) \
  583. do { RETURN_ON_ERROR(i); if (i == 0) RETURN_FAILURE; } while (0)
  584. #define SFY(x) #x
  585. #define DATA_STACK_ALLOC(state, type, ptr) \
  586. do { \
  587. alloc_pos = state->data_stack_base; \
  588. TRACE(("allocating %s in %d (%d)\n", \
  589. SFY(type), alloc_pos, sizeof(type))); \
  590. if (state->data_stack_size < alloc_pos+sizeof(type)) { \
  591. int j = data_stack_grow(state, sizeof(type)); \
  592. if (j < 0) return j; \
  593. if (ctx_pos != -1) \
  594. DATA_STACK_LOOKUP_AT(state, SRE_MATCH_CONTEXT, ctx, ctx_pos); \
  595. } \
  596. ptr = (type*)(state->data_stack+alloc_pos); \
  597. state->data_stack_base += sizeof(type); \
  598. } while (0)
  599. #define DATA_STACK_LOOKUP_AT(state, type, ptr, pos) \
  600. do { \
  601. TRACE(("looking up %s at %d\n", SFY(type), pos)); \
  602. ptr = (type*)(state->data_stack+pos); \
  603. } while (0)
  604. #define DATA_STACK_PUSH(state, data, size) \
  605. do { \
  606. TRACE(("copy data in %p to %d (%d)\n", \
  607. data, state->data_stack_base, size)); \
  608. if (state->data_stack_size < state->data_stack_base+size) { \
  609. int j = data_stack_grow(state, size); \
  610. if (j < 0) return j; \
  611. if (ctx_pos != -1) \
  612. DATA_STACK_LOOKUP_AT(state, SRE_MATCH_CONTEXT, ctx, ctx_pos); \
  613. } \
  614. memcpy(state->data_stack+state->data_stack_base, data, size); \
  615. state->data_stack_base += size; \
  616. } while (0)
  617. #define DATA_STACK_POP(state, data, size, discard) \
  618. do { \
  619. TRACE(("copy data to %p from %d (%d)\n", \
  620. data, state->data_stack_base-size, size)); \
  621. memcpy(data, state->data_stack+state->data_stack_base-size, size); \
  622. if (discard) \
  623. state->data_stack_base -= size; \
  624. } while (0)
  625. #define DATA_STACK_POP_DISCARD(state, size) \
  626. do { \
  627. TRACE(("discard data from %d (%d)\n", \
  628. state->data_stack_base-size, size)); \
  629. state->data_stack_base -= size; \
  630. } while(0)
  631. #define DATA_PUSH(x) \
  632. DATA_STACK_PUSH(state, (x), sizeof(*(x)))
  633. #define DATA_POP(x) \
  634. DATA_STACK_POP(state, (x), sizeof(*(x)), 1)
  635. #define DATA_POP_DISCARD(x) \
  636. DATA_STACK_POP_DISCARD(state, sizeof(*(x)))
  637. #define DATA_ALLOC(t,p) \
  638. DATA_STACK_ALLOC(state, t, p)
  639. #define DATA_LOOKUP_AT(t,p,pos) \
  640. DATA_STACK_LOOKUP_AT(state,t,p,pos)
  641. #define MARK_PUSH(lastmark) \
  642. do if (lastmark > 0) { \
  643. i = lastmark; /* ctx->lastmark may change if reallocated */ \
  644. DATA_STACK_PUSH(state, state->mark, (i+1)*sizeof(void*)); \
  645. } while (0)
  646. #define MARK_POP(lastmark) \
  647. do if (lastmark > 0) { \
  648. DATA_STACK_POP(state, state->mark, (lastmark+1)*sizeof(void*), 1); \
  649. } while (0)
  650. #define MARK_POP_KEEP(lastmark) \
  651. do if (lastmark > 0) { \
  652. DATA_STACK_POP(state, state->mark, (lastmark+1)*sizeof(void*), 0); \
  653. } while (0)
  654. #define MARK_POP_DISCARD(lastmark) \
  655. do if (lastmark > 0) { \
  656. DATA_STACK_POP_DISCARD(state, (lastmark+1)*sizeof(void*)); \
  657. } while (0)
  658. #define JUMP_NONE 0
  659. #define JUMP_MAX_UNTIL_1 1
  660. #define JUMP_MAX_UNTIL_2 2
  661. #define JUMP_MAX_UNTIL_3 3
  662. #define JUMP_MIN_UNTIL_1 4
  663. #define JUMP_MIN_UNTIL_2 5
  664. #define JUMP_MIN_UNTIL_3 6
  665. #define JUMP_REPEAT 7
  666. #define JUMP_REPEAT_ONE_1 8
  667. #define JUMP_REPEAT_ONE_2 9
  668. #define JUMP_MIN_REPEAT_ONE 10
  669. #define JUMP_BRANCH 11
  670. #define JUMP_ASSERT 12
  671. #define JUMP_ASSERT_NOT 13
  672. #define DO_JUMP(jumpvalue, jumplabel, nextpattern) \
  673. DATA_ALLOC(SRE_MATCH_CONTEXT, nextctx); \
  674. nextctx->last_ctx_pos = ctx_pos; \
  675. nextctx->jump = jumpvalue; \
  676. nextctx->pattern = nextpattern; \
  677. ctx_pos = alloc_pos; \
  678. ctx = nextctx; \
  679. goto entrance; \
  680. jumplabel: \
  681. while (0) /* gcc doesn't like labels at end of scopes */ \
  682. typedef struct {
  683. Py_ssize_t last_ctx_pos;
  684. Py_ssize_t jump;
  685. SRE_CHAR* ptr;
  686. SRE_CODE* pattern;
  687. Py_ssize_t count;
  688. Py_ssize_t lastmark;
  689. Py_ssize_t lastindex;
  690. union {
  691. SRE_CODE chr;
  692. SRE_REPEAT* rep;
  693. } u;
  694. } SRE_MATCH_CONTEXT;
  695. /* check if string matches the given pattern. returns <0 for
  696. error, 0 for failure, and 1 for success */
  697. LOCAL(Py_ssize_t)
  698. SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern)
  699. {
  700. SRE_CHAR* end = (SRE_CHAR *)state->end;
  701. Py_ssize_t alloc_pos, ctx_pos = -1;
  702. Py_ssize_t i, ret = 0;
  703. Py_ssize_t jump;
  704. unsigned int sigcount=0;
  705. SRE_MATCH_CONTEXT* ctx;
  706. SRE_MATCH_CONTEXT* nextctx;
  707. TRACE(("|%p|%p|ENTER\n", pattern, state->ptr));
  708. DATA_ALLOC(SRE_MATCH_CONTEXT, ctx);
  709. ctx->last_ctx_pos = -1;
  710. ctx->jump = JUMP_NONE;
  711. ctx->pattern = pattern;
  712. ctx_pos = alloc_pos;
  713. entrance:
  714. ctx->ptr = (SRE_CHAR *)state->ptr;
  715. if (ctx->pattern[0] == SRE_OP_INFO) {
  716. /* optimization info block */
  717. /* <INFO> <1=skip> <2=flags> <3=min> ... */
  718. if (ctx->pattern[3] && (end - ctx->ptr) < ctx->pattern[3]) {
  719. TRACE(("reject (got %d chars, need %d)\n",
  720. (end - ctx->ptr), ctx->pattern[3]));
  721. RETURN_FAILURE;
  722. }
  723. ctx->pattern += ctx->pattern[1] + 1;
  724. }
  725. for (;;) {
  726. ++sigcount;
  727. if ((0 == (sigcount & 0xfff)) && PyErr_CheckSignals())
  728. RETURN_ERROR(SRE_ERROR_INTERRUPTED);
  729. switch (*ctx->pattern++) {
  730. case SRE_OP_MARK:
  731. /* set mark */
  732. /* <MARK> <gid> */
  733. TRACE(("|%p|%p|MARK %d\n", ctx->pattern,
  734. ctx->ptr, ctx->pattern[0]));
  735. i = ctx->pattern[0];
  736. if (i & 1)
  737. state->lastindex = i/2 + 1;
  738. if (i > state->lastmark) {
  739. /* state->lastmark is the highest valid index in the
  740. state->mark array. If it is increased by more than 1,
  741. the intervening marks must be set to NULL to signal
  742. that these marks have not been encountered. */
  743. Py_ssize_t j = state->lastmark + 1;
  744. while (j < i)
  745. state->mark[j++] = NULL;
  746. state->lastmark = i;
  747. }
  748. state->mark[i] = ctx->ptr;
  749. ctx->pattern++;
  750. break;
  751. case SRE_OP_LITERAL:
  752. /* match literal string */
  753. /* <LITERAL> <code> */
  754. TRACE(("|%p|%p|LITERAL %d\n", ctx->pattern,
  755. ctx->ptr, *ctx->pattern));
  756. if (ctx->ptr >= end || (SRE_CODE) ctx->ptr[0] != ctx->pattern[0])
  757. RETURN_FAILURE;
  758. ctx->pattern++;
  759. ctx->ptr++;
  760. break;
  761. case SRE_OP_NOT_LITERAL:
  762. /* match anything that is not literal character */
  763. /* <NOT_LITERAL> <code> */
  764. TRACE(("|%p|%p|NOT_LITERAL %d\n", ctx->pattern,
  765. ctx->ptr, *ctx->pattern));
  766. if (ctx->ptr >= end || (SRE_CODE) ctx->ptr[0] == ctx->pattern[0])
  767. RETURN_FAILURE;
  768. ctx->pattern++;
  769. ctx->ptr++;
  770. break;
  771. case SRE_OP_SUCCESS:
  772. /* end of pattern */
  773. TRACE(("|%p|%p|SUCCESS\n", ctx->pattern, ctx->ptr));
  774. state->ptr = ctx->ptr;
  775. RETURN_SUCCESS;
  776. case SRE_OP_AT:
  777. /* match at given position */
  778. /* <AT> <code> */
  779. TRACE(("|%p|%p|AT %d\n", ctx->pattern, ctx->ptr, *ctx->pattern));
  780. if (!SRE_AT(state, ctx->ptr, *ctx->pattern))
  781. RETURN_FAILURE;
  782. ctx->pattern++;
  783. break;
  784. case SRE_OP_CATEGORY:
  785. /* match at given category */
  786. /* <CATEGORY> <code> */
  787. TRACE(("|%p|%p|CATEGORY %d\n", ctx->pattern,
  788. ctx->ptr, *ctx->pattern));
  789. if (ctx->ptr >= end || !sre_category(ctx->pattern[0], ctx->ptr[0]))
  790. RETURN_FAILURE;
  791. ctx->pattern++;
  792. ctx->ptr++;
  793. break;
  794. case SRE_OP_ANY:
  795. /* match anything (except a newline) */
  796. /* <ANY> */
  797. TRACE(("|%p|%p|ANY\n", ctx->pattern, ctx->ptr));
  798. if (ctx->ptr >= end || SRE_IS_LINEBREAK(ctx->ptr[0]))
  799. RETURN_FAILURE;
  800. ctx->ptr++;
  801. break;
  802. case SRE_OP_ANY_ALL:
  803. /* match anything */
  804. /* <ANY_ALL> */
  805. TRACE(("|%p|%p|ANY_ALL\n", ctx->pattern, ctx->ptr));
  806. if (ctx->ptr >= end)
  807. RETURN_FAILURE;
  808. ctx->ptr++;
  809. break;
  810. case SRE_OP_IN:
  811. /* match set member (or non_member) */
  812. /* <IN> <skip> <set> */
  813. TRACE(("|%p|%p|IN\n", ctx->pattern, ctx->ptr));
  814. if (ctx->ptr >= end || !SRE_CHARSET(ctx->pattern + 1, *ctx->ptr))
  815. RETURN_FAILURE;
  816. ctx->pattern += ctx->pattern[0];
  817. ctx->ptr++;
  818. break;
  819. case SRE_OP_LITERAL_IGNORE:
  820. TRACE(("|%p|%p|LITERAL_IGNORE %d\n",
  821. ctx->pattern, ctx->ptr, ctx->pattern[0]));
  822. if (ctx->ptr >= end ||
  823. state->lower(*ctx->ptr) != state->lower(*ctx->pattern))
  824. RETURN_FAILURE;
  825. ctx->pattern++;
  826. ctx->ptr++;
  827. break;
  828. case SRE_OP_NOT_LITERAL_IGNORE:
  829. TRACE(("|%p|%p|NOT_LITERAL_IGNORE %d\n",
  830. ctx->pattern, ctx->ptr, *ctx->pattern));
  831. if (ctx->ptr >= end ||
  832. state->lower(*ctx->ptr) == state->lower(*ctx->pattern))
  833. RETURN_FAILURE;
  834. ctx->pattern++;
  835. ctx->ptr++;
  836. break;
  837. case SRE_OP_IN_IGNORE:
  838. TRACE(("|%p|%p|IN_IGNORE\n", ctx->pattern, ctx->ptr));
  839. if (ctx->ptr >= end
  840. || !SRE_CHARSET(ctx->pattern+1,
  841. (SRE_CODE)state->lower(*ctx->ptr)))
  842. RETURN_FAILURE;
  843. ctx->pattern += ctx->pattern[0];
  844. ctx->ptr++;
  845. break;
  846. case SRE_OP_JUMP:
  847. case SRE_OP_INFO:
  848. /* jump forward */
  849. /* <JUMP> <offset> */
  850. TRACE(("|%p|%p|JUMP %d\n", ctx->pattern,
  851. ctx->ptr, ctx->pattern[0]));
  852. ctx->pattern += ctx->pattern[0];
  853. break;
  854. case SRE_OP_BRANCH:
  855. /* alternation */
  856. /* <BRANCH> <0=skip> code <JUMP> ... <NULL> */
  857. TRACE(("|%p|%p|BRANCH\n", ctx->pattern, ctx->ptr));
  858. LASTMARK_SAVE();
  859. ctx->u.rep = state->repeat;
  860. if (ctx->u.rep)
  861. MARK_PUSH(ctx->lastmark);
  862. for (; ctx->pattern[0]; ctx->pattern += ctx->pattern[0]) {
  863. if (ctx->pattern[1] == SRE_OP_LITERAL &&
  864. (ctx->ptr >= end ||
  865. (SRE_CODE) *ctx->ptr != ctx->pattern[2]))
  866. continue;
  867. if (ctx->pattern[1] == SRE_OP_IN &&
  868. (ctx->ptr >= end ||
  869. !SRE_CHARSET(ctx->pattern + 3, (SRE_CODE) *ctx->ptr)))
  870. continue;
  871. state->ptr = ctx->ptr;
  872. DO_JUMP(JUMP_BRANCH, jump_branch, ctx->pattern+1);
  873. if (ret) {
  874. if (ctx->u.rep)
  875. MARK_POP_DISCARD(ctx->lastmark);
  876. RETURN_ON_ERROR(ret);
  877. RETURN_SUCCESS;
  878. }
  879. if (ctx->u.rep)
  880. MARK_POP_KEEP(ctx->lastmark);
  881. LASTMARK_RESTORE();
  882. }
  883. if (ctx->u.rep)
  884. MARK_POP_DISCARD(ctx->lastmark);
  885. RETURN_FAILURE;
  886. case SRE_OP_REPEAT_ONE:
  887. /* match repeated sequence (maximizing regexp) */
  888. /* this operator only works if the repeated item is
  889. exactly one character wide, and we're not already
  890. collecting backtracking points. for other cases,
  891. use the MAX_REPEAT operator */
  892. /* <REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */
  893. TRACE(("|%p|%p|REPEAT_ONE %d %d\n", ctx->pattern, ctx->ptr,
  894. ctx->pattern[1], ctx->pattern[2]));
  895. if (ctx->ptr + ctx->pattern[1] > end)
  896. RETURN_FAILURE; /* cannot match */
  897. state->ptr = ctx->ptr;
  898. ret = SRE_COUNT(state, ctx->pattern+3, ctx->pattern[2]);
  899. RETURN_ON_ERROR(ret);
  900. DATA_LOOKUP_AT(SRE_MATCH_CONTEXT, ctx, ctx_pos);
  901. ctx->count = ret;
  902. ctx->ptr += ctx->count;
  903. /* when we arrive here, count contains the number of
  904. matches, and ctx->ptr points to the tail of the target
  905. string. check if the rest of the pattern matches,
  906. and backtrack if not. */
  907. if (ctx->count < (Py_ssize_t) ctx->pattern[1])
  908. RETURN_FAILURE;
  909. if (ctx->pattern[ctx->pattern[0]] == SRE_OP_SUCCESS) {
  910. /* tail is empty. we're finished */
  911. state->ptr = ctx->ptr;
  912. RETURN_SUCCESS;
  913. }
  914. LASTMARK_SAVE();
  915. if (ctx->pattern[ctx->pattern[0]] == SRE_OP_LITERAL) {
  916. /* tail starts with a literal. skip positions where
  917. the rest of the pattern cannot possibly match */
  918. ctx->u.chr = ctx->pattern[ctx->pattern[0]+1];
  919. for (;;) {
  920. while (ctx->count >= (Py_ssize_t) ctx->pattern[1] &&
  921. (ctx->ptr >= end || *ctx->ptr != ctx->u.chr)) {
  922. ctx->ptr--;
  923. ctx->count--;
  924. }
  925. if (ctx->count < (Py_ssize_t) ctx->pattern[1])
  926. break;
  927. state->ptr = ctx->ptr;
  928. DO_JUMP(JUMP_REPEAT_ONE_1, jump_repeat_one_1,
  929. ctx->pattern+ctx->pattern[0]);
  930. if (ret) {
  931. RETURN_ON_ERROR(ret);
  932. RETURN_SUCCESS;
  933. }
  934. LASTMARK_RESTORE();
  935. ctx->ptr--;
  936. ctx->count--;
  937. }
  938. } else {
  939. /* general case */
  940. while (ctx->count >= (Py_ssize_t) ctx->pattern[1]) {
  941. state->ptr = ctx->ptr;
  942. DO_JUMP(JUMP_REPEAT_ONE_2, jump_repeat_one_2,
  943. ctx->pattern+ctx->pattern[0]);
  944. if (ret) {
  945. RETURN_ON_ERROR(ret);
  946. RETURN_SUCCESS;
  947. }
  948. ctx->ptr--;
  949. ctx->count--;
  950. LASTMARK_RESTORE();
  951. }
  952. }
  953. RETURN_FAILURE;
  954. case SRE_OP_MIN_REPEAT_ONE:
  955. /* match repeated sequence (minimizing regexp) */
  956. /* this operator only works if the repeated item is
  957. exactly one character wide, and we're not already
  958. collecting backtracking points. for other cases,
  959. use the MIN_REPEAT operator */
  960. /* <MIN_REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */
  961. TRACE(("|%p|%p|MIN_REPEAT_ONE %d %d\n", ctx->pattern, ctx->ptr,
  962. ctx->pattern[1], ctx->pattern[2]));
  963. if (ctx->ptr + ctx->pattern[1] > end)
  964. RETURN_FAILURE; /* cannot match */
  965. state->ptr = ctx->ptr;
  966. if (ctx->pattern[1] == 0)
  967. ctx->count = 0;
  968. else {
  969. /* count using pattern min as the maximum */
  970. ret = SRE_COUNT(state, ctx->pattern+3, ctx->pattern[1]);
  971. RETURN_ON_ERROR(ret);
  972. DATA_LOOKUP_AT(SRE_MATCH_CONTEXT, ctx, ctx_pos);
  973. if (ret < (Py_ssize_t) ctx->pattern[1])
  974. /* didn't match minimum number of times */
  975. RETURN_FAILURE;
  976. /* advance past minimum matches of repeat */
  977. ctx->count = ret;
  978. ctx->ptr += ctx->count;
  979. }
  980. if (ctx->pattern[ctx->pattern[0]] == SRE_OP_SUCCESS) {
  981. /* tail is empty. we're finished */
  982. state->ptr = ctx->ptr;
  983. RETURN_SUCCESS;
  984. } else {
  985. /* general case */
  986. LASTMARK_SAVE();
  987. while ((Py_ssize_t)ctx->pattern[2] == 65535
  988. || ctx->count <= (Py_ssize_t)ctx->pattern[2]) {
  989. state->ptr = ctx->ptr;
  990. DO_JUMP(JUMP_MIN_REPEAT_ONE,jump_min_repeat_one,
  991. ctx->pattern+ctx->pattern[0]);
  992. if (ret) {
  993. RETURN_ON_ERROR(ret);
  994. RETURN_SUCCESS;
  995. }
  996. state->ptr = ctx->ptr;
  997. ret = SRE_COUNT(state, ctx->pattern+3, 1);
  998. RETURN_ON_ERROR(ret);
  999. DATA_LOOKUP_AT(SRE_MATCH_CONTEXT, ctx, ctx_pos);
  1000. if (ret == 0)
  1001. break;
  1002. assert(ret == 1);
  1003. ctx->ptr++;
  1004. ctx->count++;
  1005. LASTMARK_RESTORE();
  1006. }
  1007. }
  1008. RETURN_FAILURE;
  1009. case SRE_OP_REPEAT:
  1010. /* create repeat context. all the hard work is done
  1011. by the UNTIL operator (MAX_UNTIL, MIN_UNTIL) */
  1012. /* <REPEAT> <skip> <1=min> <2=max> item <UNTIL> tail */
  1013. TRACE(("|%p|%p|REPEAT %d %d\n", ctx->pattern, ctx->ptr,
  1014. ctx->pattern[1], ctx->pattern[2]));
  1015. /* install new repeat context */
  1016. ctx->u.rep = (SRE_REPEAT*) PyObject_MALLOC(sizeof(*ctx->u.rep));
  1017. if (!ctx->u.rep) {
  1018. PyErr_NoMemory();
  1019. RETURN_FAILURE;
  1020. }
  1021. ctx->u.rep->count = -1;
  1022. ctx->u.rep->pattern = ctx->pattern;
  1023. ctx->u.rep->prev = state->repeat;
  1024. ctx->u.rep->last_ptr = NULL;
  1025. state->repeat = ctx->u.rep;
  1026. state->ptr = ctx->ptr;
  1027. DO_JUMP(JUMP_REPEAT, jump_repeat, ctx->pattern+ctx->pattern[0]);
  1028. state->repeat = ctx->u.rep->prev;
  1029. PyObject_FREE(ctx->u.rep);
  1030. if (ret) {
  1031. RETURN_ON_ERROR(ret);
  1032. RETURN_SUCCESS;
  1033. }
  1034. RETURN_FAILURE;
  1035. case SRE_OP_MAX_UNTIL:
  1036. /* maximizing repeat */
  1037. /* <REPEAT> <skip> <1=min> <2=max> item <MAX_UNTIL> tail */
  1038. /* FIXME: we probably need to deal with zero-width
  1039. matches in here... */
  1040. ctx->u.rep = state->repeat;
  1041. if (!ctx->u.rep)
  1042. RETURN_ERROR(SRE_ERROR_STATE);
  1043. state->ptr = ctx->ptr;
  1044. ctx->count = ctx->u.rep->count+1;
  1045. TRACE(("|%p|%p|MAX_UNTIL %d\n", ctx->pattern,
  1046. ctx->ptr, ctx->count));
  1047. if (ctx->count < ctx->u.rep->pattern[1]) {
  1048. /* not enough matches */
  1049. ctx->u.rep->count = ctx->count;
  1050. DO_JUMP(JUMP_MAX_UNTIL_1, jump_max_until_1,
  1051. ctx->u.rep->pattern+3);
  1052. if (ret) {
  1053. RETURN_ON_ERROR(ret);
  1054. RETURN_SUCCESS;
  1055. }
  1056. ctx->u.rep->count = ctx->count-1;
  1057. state->ptr = ctx->ptr;
  1058. RETURN_FAILURE;
  1059. }
  1060. if ((ctx->count < ctx->u.rep->pattern[2] ||
  1061. ctx->u.rep->pattern[2] == 65535) &&
  1062. state->ptr != ctx->u.rep->last_ptr) {
  1063. /* we may have enough matches, but if we can
  1064. match another item, do so */
  1065. ctx->u.rep->count = ctx->count;
  1066. LASTMARK_SAVE();
  1067. MARK_PUSH(ctx->lastmark);
  1068. /* zero-width match protection */
  1069. DATA_PUSH(&ctx->u.rep->last_ptr);
  1070. ctx->u.rep->last_ptr = state->ptr;
  1071. DO_JUMP(JUMP_MAX_UNTIL_2, jump_max_until_2,
  1072. ctx->u.rep->pattern+3);
  1073. DATA_POP(&ctx->u.rep->last_ptr);
  1074. if (ret) {
  1075. MARK_POP_DISCARD(ctx->lastmark);
  1076. RETURN_ON_ERROR(ret);
  1077. RETURN_SUCCESS;
  1078. }
  1079. MARK_POP(ctx->lastmark);
  1080. LASTMARK_RESTORE();
  1081. ctx->u.rep->count = ctx->count-1;
  1082. state->ptr = ctx->ptr;
  1083. }
  1084. /* cannot match more repeated items here. make sure the
  1085. tail matches */
  1086. state->repeat = ctx->u.rep->prev;
  1087. DO_JUMP(JUMP_MAX_UNTIL_3, jump_max_until_3, ctx->pattern);
  1088. RETURN_ON_SUCCESS(ret);
  1089. state->repeat = ctx->u.rep;
  1090. state->ptr = ctx->ptr;
  1091. RETURN_FAILURE;
  1092. case SRE_OP_MIN_UNTIL:
  1093. /* minimizing repeat */
  1094. /* <REPEAT> <skip> <1=min> <2=max> item <MIN_UNTIL> tail */
  1095. ctx->u.rep = state->repeat;
  1096. if (!ctx->u.rep)
  1097. RETURN_ERROR(SRE_ERROR_STATE);
  1098. state->ptr = ctx->ptr;
  1099. ctx->count = ctx->u.rep->count+1;
  1100. TRACE(("|%p|%p|MIN_UNTIL %d %p\n", ctx->pattern,
  1101. ctx->ptr, ctx->count, ctx->u.rep->pattern));
  1102. if (ctx->count < ctx->u.rep->pattern[1]) {
  1103. /* not enough matches */
  1104. ctx->u.rep->count = ctx->count;
  1105. DO_JUMP(JUMP_MIN_UNTIL_1, jump_min_until_1,
  1106. ctx->u.rep->pattern+3);
  1107. if (ret) {
  1108. RETURN_ON_ERROR(ret);
  1109. RETURN_SUCCESS;
  1110. }
  1111. ctx->u.rep->count = ctx->count-1;
  1112. state->ptr = ctx->ptr;
  1113. RETURN_FAILURE;
  1114. }
  1115. LASTMARK_SAVE();
  1116. /* see if the tail matches */
  1117. state->repeat = ctx->u.rep->prev;
  1118. DO_JUMP(JUMP_MIN_UNTIL_2, jump_min_until_2, ctx->pattern);
  1119. if (ret) {
  1120. RETURN_ON_ERROR(ret);
  1121. RETURN_SUCCESS;
  1122. }
  1123. state->repeat = ctx->u.rep;
  1124. state->ptr = ctx->ptr;
  1125. LASTMARK_RESTORE();
  1126. if (ctx->count >= ctx->u.rep->pattern[2]
  1127. && ctx->u.rep->pattern[2] != 65535)
  1128. RETURN_FAILURE;
  1129. ctx->u.rep->count = ctx->count;
  1130. DO_JUMP(JUMP_MIN_UNTIL_3,jump_min_until_3,
  1131. ctx->u.rep->pattern+3);
  1132. if (ret) {
  1133. RETURN_ON_ERROR(ret);
  1134. RETURN_SUCCESS;
  1135. }
  1136. ctx->u.rep->count = ctx->count-1;
  1137. state->ptr = ctx->ptr;
  1138. RETURN_FAILURE;
  1139. case SRE_OP_GROUPREF:
  1140. /* match backreference */
  1141. TRACE(("|%p|%p|GROUPREF %d\n", ctx->pattern,
  1142. ctx->ptr, ctx->pattern[0]));
  1143. i = ctx->pattern[0];
  1144. {
  1145. Py_ssize_t groupref = i+i;
  1146. if (groupref >= state->lastmark) {
  1147. RETURN_FAILURE;
  1148. } else {
  1149. SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
  1150. SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
  1151. if (!p || !e || e < p)
  1152. RETURN_FAILURE;
  1153. while (p < e) {
  1154. if (ctx->ptr >= end || *ctx->ptr != *p)
  1155. RETURN_FAILURE;
  1156. p++; ctx->ptr++;
  1157. }
  1158. }
  1159. }
  1160. ctx->pattern++;
  1161. break;
  1162. case SRE_OP_GROUPREF_IGNORE:
  1163. /* match backreference */
  1164. TRACE(("|%p|%p|GROUPREF_IGNORE %d\n", ctx->pattern,
  1165. ctx->ptr, ctx->pattern[0]));
  1166. i = ctx->pattern[0];
  1167. {
  1168. Py_ssize_t groupref = i+i;
  1169. if (groupref >= state->lastmark) {
  1170. RETURN_FAILURE;
  1171. } else {
  1172. SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
  1173. SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
  1174. if (!p || !e || e < p)
  1175. RETURN_FAILURE;
  1176. while (p < e) {
  1177. if (ctx->ptr >= end ||
  1178. state->lower(*ctx->ptr) != state->lower(*p))
  1179. RETURN_FAILURE;
  1180. p++; ctx->ptr++;
  1181. }
  1182. }
  1183. }
  1184. ctx->pattern++;
  1185. break;
  1186. case SRE_OP_GROUPREF_EXISTS:
  1187. TRACE(("|%p|%p|GROUPREF_EXISTS %d\n", ctx->pattern,
  1188. ctx->ptr, ctx->pattern[0]));
  1189. /* <GROUPREF_EXISTS> <group> <skip> codeyes <JUMP> codeno ... */
  1190. i = ctx->pattern[0];
  1191. {
  1192. Py_ssize_t groupref = i+i;
  1193. if (groupref >= state->lastmark) {
  1194. ctx->pattern += ctx->pattern[1];
  1195. break;
  1196. } else {
  1197. SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
  1198. SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
  1199. if (!p || !e || e < p) {
  1200. ctx->pattern += ctx->pattern[1];
  1201. break;
  1202. }
  1203. }
  1204. }
  1205. ctx->pattern += 2;
  1206. break;
  1207. case SRE_OP_ASSERT:
  1208. /* assert subpattern */
  1209. /* <ASSERT> <skip> <back> <pattern> */
  1210. TRACE(("|%p|%p|ASSERT %d\n", ctx->pattern,
  1211. ctx->ptr, ctx->pattern[1]));
  1212. state->ptr = ctx->ptr - ctx->pattern[1];
  1213. if (state->ptr < state->beginning)
  1214. RETURN_FAILURE;
  1215. DO_JUMP(JUMP_ASSERT, jump_assert, ctx->pattern+2);
  1216. RETURN_ON_FAILURE(ret);
  1217. ctx->pattern += ctx->pattern[0];
  1218. break;
  1219. case SRE_OP_ASSERT_NOT:
  1220. /* assert not subpattern */
  1221. /* <ASSERT_NOT> <skip> <back> <pattern> */
  1222. TRACE(("|%p|%p|ASSERT_NOT %d\n", ctx->pattern,
  1223. ctx->ptr, ctx->pattern[1]));
  1224. state->ptr = ctx->ptr - ctx->pattern[1];
  1225. if (state->ptr >= state->beginning) {
  1226. DO_JUMP(JUMP_ASSERT_NOT, jump_assert_not, ctx->pattern+2);
  1227. if (ret) {
  1228. RETURN_ON_ERROR(ret);
  1229. RETURN_FAILURE;
  1230. }
  1231. }
  1232. ctx->pattern += ctx->pattern[0];
  1233. break;
  1234. case SRE_OP_FAILURE:
  1235. /* immediate failure */
  1236. TRACE(("|%p|%p|FAILURE\n", ctx->pattern, ctx->ptr));
  1237. RETURN_FAILURE;
  1238. default:
  1239. TRACE(("|%p|%p|UNKNOWN %d\n", ctx->pattern, ctx->ptr,
  1240. ctx->pattern[-1]));
  1241. RETURN_ERROR(SRE_ERROR_ILLEGAL);
  1242. }
  1243. }
  1244. exit:
  1245. ctx_pos = ctx->last_ctx_pos;
  1246. jump = ctx->jump;
  1247. DATA_POP_DISCARD(ctx);
  1248. if (ctx_pos == -1)
  1249. return ret;
  1250. DATA_LOOKUP_AT(SRE_MATCH_CONTEXT, ctx, ctx_pos);
  1251. switch (jump) {
  1252. case JUMP_MAX_UNTIL_2:
  1253. TRACE(("|%p|%p|JUMP_MAX_UNTIL_2\n", ctx->pattern, ctx->ptr));
  1254. goto jump_max_until_2;
  1255. case JUMP_MAX_UNTIL_3:
  1256. TRACE(("|%p|%p|JUMP_MAX_UNTIL_3\n", ctx->pattern, ctx->ptr));
  1257. goto jump_max_until_3;
  1258. case JUMP_MIN_UNTIL_2:
  1259. TRACE(("|%p|%p|JUMP_MIN_UNTIL_2\n", ctx->pattern, ctx->ptr));
  1260. goto jump_min_until_2;
  1261. case JUMP_MIN_UNTIL_3:
  1262. TRACE(("|%p|%p|JUMP_MIN_UNTIL_3\n", ctx->pattern, ctx->ptr));
  1263. goto jump_min_until_3;
  1264. case JUMP_BRANCH:
  1265. TRACE(("|%p|%p|JUMP_BRANCH\n", ctx->pattern, ctx->ptr));
  1266. goto jump_branch;
  1267. case JUMP_MAX_UNTIL_1:
  1268. TRACE(("|%p|%p|JUMP_MAX_UNTIL_1\n", ctx->pattern, ctx->ptr));
  1269. goto jump_max_until_1;
  1270. case JUMP_MIN_UNTIL_1:
  1271. TRACE(("|%p|%p|JUMP_MIN_UNTIL_1\n", ctx->pattern, ctx->ptr));
  1272. goto jump_min_until_1;
  1273. case JUMP_REPEAT:
  1274. TRACE(("|%p|%p|JUMP_REPEAT\n", ctx->pattern, ctx->ptr));
  1275. goto jump_repeat;
  1276. case JUMP_REPEAT_ONE_1:
  1277. TRACE(("|%p|%p|JUMP_REPEAT_ONE_1\n", ctx->pattern, ctx->ptr));
  1278. goto jump_repeat_one_1;
  1279. case JUMP_REPEAT_ONE_2:
  1280. TRACE(("|%p|%p|JUMP_REPEAT_ONE_2\n", ctx->pattern, ctx->ptr));
  1281. goto jump_repeat_one_2;
  1282. case JUMP_MIN_REPEAT_ONE:
  1283. TRACE(("|%p|%p|JUMP_MIN_REPEAT_ONE\n", ctx->pattern, ctx->ptr));
  1284. goto jump_min_repeat_one;
  1285. case JUMP_ASSERT:
  1286. TRACE(("|%p|%p|JUMP_ASSERT\n", ctx->pattern, ctx->ptr));
  1287. goto jump_assert;
  1288. case JUMP_ASSERT_NOT:
  1289. TRACE(("|%p|%p|JUMP_ASSERT_NOT\n", ctx->

Large files files are truncated, but you can click here to view the full file