PageRenderTime 58ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

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

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

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