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

/js/src/yarr/pcre/pcre_exec.cpp

http://github.com/zpao/v8monkey
C++ | 2192 lines | 1480 code | 331 blank | 381 comment | 428 complexity | 9ffcd184ede3ce61e3c420ee9f8311da MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, AGPL-1.0, LGPL-2.1, BSD-3-Clause, GPL-2.0, JSON, Apache-2.0, 0BSD
  1. /* This is JavaScriptCore's variant of the PCRE library. While this library
  2. started out as a copy of PCRE, many of the features of PCRE have been
  3. removed. This library now supports only the regular expression features
  4. required by the JavaScript language specification, and has only the functions
  5. needed by JavaScriptCore and the rest of WebKit.
  6. Originally written by Philip Hazel
  7. Copyright (c) 1997-2006 University of Cambridge
  8. Copyright (C) 2002, 2004, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
  9. Copyright (C) 2007 Eric Seidel <eric@webkit.org>
  10. -----------------------------------------------------------------------------
  11. Redistribution and use in source and binary forms, with or without
  12. modification, are permitted provided that the following conditions are met:
  13. * Redistributions of source code must retain the above copyright notice,
  14. this list of conditions and the following disclaimer.
  15. * Redistributions in binary form must reproduce the above copyright
  16. notice, this list of conditions and the following disclaimer in the
  17. documentation and/or other materials provided with the distribution.
  18. * Neither the name of the University of Cambridge nor the names of its
  19. contributors may be used to endorse or promote products derived from
  20. this software without specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. POSSIBILITY OF SUCH DAMAGE.
  32. -----------------------------------------------------------------------------
  33. */
  34. /* This module contains jsRegExpExecute(), the externally visible function
  35. that does pattern matching using an NFA algorithm, following the rules from
  36. the JavaScript specification. There are also some supporting functions. */
  37. #include "pcre_internal.h"
  38. #include <limits.h>
  39. #include "yarr/ASCIICType.h"
  40. #include "jsarena.h"
  41. #include "jscntxt.h"
  42. using namespace WTF;
  43. #if !WTF_COMPILER_MSVC && !WTF_COMPILER_SUNPRO
  44. #define USE_COMPUTED_GOTO_FOR_MATCH_RECURSION
  45. #endif
  46. /* Note: Webkit sources have USE_COMPUTED_GOTO_FOR_MATCH_OPCODE_LOOP disabled. */
  47. /* Note: There are hardcoded constants all over the place, but in the port of
  48. Yarr to TraceMonkey two bytes are added to the OP_BRA* opcodes, so the
  49. instruction stream now looks like this at the start of a bracket group:
  50. OP_BRA* [link:LINK_SIZE] [minNestedBracket,maxNestedBracket:2]
  51. Both capturing and non-capturing brackets encode this information. */
  52. /* Avoid warnings on Windows. */
  53. #undef min
  54. #undef max
  55. #ifndef USE_COMPUTED_GOTO_FOR_MATCH_RECURSION
  56. typedef int ReturnLocation;
  57. #else
  58. typedef void* ReturnLocation;
  59. #endif
  60. /* Node on a stack of brackets. This is used to detect and reject
  61. matches of the empty string per ECMAScript repeat match rules. This
  62. also prevents infinite loops on quantified empty matches. One node
  63. represents the start state at the start of this bracket group. */
  64. struct BracketChainNode {
  65. BracketChainNode* previousBracket;
  66. const UChar* bracketStart;
  67. /* True if the minimum number of matches was already satisfied
  68. when we started matching this group. */
  69. bool minSatisfied;
  70. };
  71. struct MatchFrame {
  72. ReturnLocation returnLocation;
  73. struct MatchFrame* previousFrame;
  74. int *savedOffsets;
  75. /* The frame allocates saved offsets into the regular expression arena pool so
  76. that they can be restored during backtracking. */
  77. size_t savedOffsetsSize;
  78. JSArenaPool *regExpPool;
  79. MatchFrame() : savedOffsetsSize(0), regExpPool(0) {}
  80. void init(JSArenaPool *regExpPool) { this->regExpPool = regExpPool; }
  81. /* Function arguments that may change */
  82. struct {
  83. const UChar* subjectPtr;
  84. const unsigned char* instructionPtr;
  85. int offsetTop;
  86. BracketChainNode* bracketChain;
  87. } args;
  88. /* PCRE uses "fake" recursion built off of gotos, thus
  89. stack-based local variables are not safe to use. Instead we have to
  90. store local variables on the current MatchFrame. */
  91. struct {
  92. const unsigned char* data;
  93. const unsigned char* startOfRepeatingBracket;
  94. const UChar* subjectPtrAtStartOfInstruction; // Several instrutions stash away a subjectPtr here for later compare
  95. const unsigned char* instructionPtrAtStartOfOnce;
  96. int repeatOthercase;
  97. int savedSubjectOffset;
  98. int ctype;
  99. int fc;
  100. int fi;
  101. int length;
  102. int max;
  103. int number;
  104. int offset;
  105. int skipBytes;
  106. int minBracket;
  107. int limitBracket;
  108. int bracketsBefore;
  109. bool minSatisfied;
  110. BracketChainNode bracketChainNode;
  111. } locals;
  112. void saveOffsets(int minBracket, int limitBracket, int *offsets, int offsetEnd) {
  113. JS_ASSERT(regExpPool);
  114. JS_ASSERT(minBracket >= 0);
  115. JS_ASSERT(limitBracket >= minBracket);
  116. JS_ASSERT(offsetEnd >= 0);
  117. if (minBracket == limitBracket)
  118. return;
  119. const size_t newSavedOffsetCount = 3 * (limitBracket - minBracket);
  120. /* Increase saved offset space if necessary. */
  121. {
  122. size_t targetSize = sizeof(*savedOffsets) * newSavedOffsetCount;
  123. if (savedOffsetsSize < targetSize) {
  124. JS_ARENA_ALLOCATE_CAST(savedOffsets, int *, regExpPool, targetSize);
  125. JS_ASSERT(savedOffsets); /* FIXME: error code, bug 574459. */
  126. savedOffsetsSize = targetSize;
  127. }
  128. }
  129. for (unsigned i = 0; i < unsigned(limitBracket - minBracket); ++i) {
  130. int bracketIter = minBracket + i;
  131. JS_ASSERT(2 * bracketIter + 1 <= offsetEnd);
  132. int start = offsets[2 * bracketIter];
  133. int end = offsets[2 * bracketIter + 1];
  134. JS_ASSERT(bracketIter <= offsetEnd);
  135. int offset = offsets[offsetEnd - bracketIter];
  136. DPRINTF(("saving bracket %d; start: %d; end: %d; offset: %d\n", bracketIter, start, end, offset));
  137. JS_ASSERT(start <= end);
  138. JS_ASSERT(i * 3 + 2 < newSavedOffsetCount);
  139. savedOffsets[i * 3 + 0] = start;
  140. savedOffsets[i * 3 + 1] = end;
  141. savedOffsets[i * 3 + 2] = offset;
  142. }
  143. }
  144. void clobberOffsets(int minBracket, int limitBracket, int *offsets, int offsetEnd) {
  145. for (int i = 0; i < limitBracket - minBracket; ++i) {
  146. int bracketIter = minBracket + i;
  147. JS_ASSERT(2 * bracketIter + 1 < offsetEnd);
  148. offsets[2 * bracketIter + 0] = -1;
  149. offsets[2 * bracketIter + 1] = -1;
  150. }
  151. }
  152. void restoreOffsets(int minBracket, int limitBracket, int *offsets, int offsetEnd) {
  153. JS_ASSERT(regExpPool);
  154. JS_ASSERT_IF(limitBracket > minBracket, savedOffsets);
  155. for (int i = 0; i < limitBracket - minBracket; ++i) {
  156. int bracketIter = minBracket + i;
  157. int start = savedOffsets[i * 3 + 0];
  158. int end = savedOffsets[i * 3 + 1];
  159. int offset = savedOffsets[i * 3 + 2];
  160. DPRINTF(("restoring bracket %d; start: %d; end: %d; offset: %d\n", bracketIter, start, end, offset));
  161. JS_ASSERT(start <= end);
  162. offsets[2 * bracketIter + 0] = start;
  163. offsets[2 * bracketIter + 1] = end;
  164. offsets[offsetEnd - bracketIter] = offset;
  165. }
  166. }
  167. /* Extract the bracket data after the current opcode/link at |instructionPtr| into the locals. */
  168. void extractBrackets(const unsigned char *instructionPtr) {
  169. uint16_t bracketMess = get2ByteValue(instructionPtr + 1 + LINK_SIZE);
  170. locals.minBracket = (bracketMess >> 8) & 0xff;
  171. locals.limitBracket = (bracketMess & 0xff);
  172. JS_ASSERT(locals.minBracket <= locals.limitBracket);
  173. }
  174. /* At the start of a bracketed group, add the current subject pointer to the
  175. stack of such pointers, to be re-instated at the end of the group when we hit
  176. the closing ket. When match() is called in other circumstances, we don't add to
  177. this stack. */
  178. void startNewGroup(bool minSatisfied) {
  179. locals.bracketChainNode.previousBracket = args.bracketChain;
  180. locals.bracketChainNode.bracketStart = args.subjectPtr;
  181. locals.bracketChainNode.minSatisfied = minSatisfied;
  182. args.bracketChain = &locals.bracketChainNode;
  183. }
  184. };
  185. /* Structure for passing "static" information around between the functions
  186. doing traditional NFA matching, so that they are thread-safe. */
  187. struct MatchData {
  188. int *offsetVector; /* Offset vector */
  189. int offsetEnd; /* One past the end */
  190. int offsetMax; /* The maximum usable for return data */
  191. bool offsetOverflow; /* Set if too many extractions */
  192. const UChar *startSubject; /* Start of the subject string */
  193. const UChar *endSubject; /* End of the subject string */
  194. const UChar *endMatchPtr; /* Subject position at end match */
  195. int endOffsetTop; /* Highwater mark at end of match */
  196. bool multiline;
  197. bool ignoreCase;
  198. void setOffsetPair(size_t pairNum, int start, int end) {
  199. JS_ASSERT(int(2 * pairNum + 1) < offsetEnd && int(pairNum) < offsetEnd);
  200. JS_ASSERT(start <= end);
  201. JS_ASSERT_IF(start < 0, start == end && start == -1);
  202. DPRINTF(("setting offset pair at %u (%d, %d)\n", pairNum, start, end));
  203. offsetVector[2 * pairNum + 0] = start;
  204. offsetVector[2 * pairNum + 1] = end;
  205. }
  206. };
  207. /* The maximum remaining length of subject we are prepared to search for a
  208. reqByte match. */
  209. #define REQ_BYTE_MAX 1000
  210. /* The below limit restricts the number of "recursive" match calls in order to
  211. avoid spending exponential time on complex regular expressions. */
  212. static const unsigned matchLimit = 1000000;
  213. /*************************************************
  214. * Match a back-reference *
  215. *************************************************/
  216. /* If a back reference hasn't been set, the length that is passed is greater
  217. than the number of characters left in the string, so the match fails.
  218. Arguments:
  219. offset index into the offset vector
  220. subjectPtr points into the subject
  221. length length to be matched
  222. md points to match data block
  223. Returns: true if matched
  224. */
  225. static bool matchRef(int offset, const UChar* subjectPtr, int length, const MatchData& md)
  226. {
  227. const UChar* p = md.startSubject + md.offsetVector[offset];
  228. /* Always fail if not enough characters left */
  229. if (length > md.endSubject - subjectPtr)
  230. return false;
  231. /* Separate the caselesss case for speed */
  232. if (md.ignoreCase) {
  233. while (length-- > 0) {
  234. UChar c = *p++;
  235. int othercase = jsc_pcre_ucp_othercase(c);
  236. UChar d = *subjectPtr++;
  237. if (c != d && othercase != d)
  238. return false;
  239. }
  240. }
  241. else {
  242. while (length-- > 0)
  243. if (*p++ != *subjectPtr++)
  244. return false;
  245. }
  246. return true;
  247. }
  248. #ifndef USE_COMPUTED_GOTO_FOR_MATCH_RECURSION
  249. /* Use numbered labels and switch statement at the bottom of the match function. */
  250. #define RMATCH_WHERE(num) num
  251. #define RRETURN_LABEL RRETURN_SWITCH
  252. #else
  253. /* Use GCC's computed goto extension. */
  254. /* For one test case this is more than 40% faster than the switch statement.
  255. We could avoid the use of the num argument entirely by using local labels,
  256. but using it for the GCC case as well as the non-GCC case allows us to share
  257. a bit more code and notice if we use conflicting numbers.*/
  258. #define RMATCH_WHERE(num) JS_EXTENSION(&&RRETURN_##num)
  259. #define RRETURN_LABEL *stack.currentFrame->returnLocation
  260. #endif
  261. #define RECURSIVE_MATCH_COMMON(num) \
  262. goto RECURSE;\
  263. RRETURN_##num: \
  264. stack.popCurrentFrame();
  265. #define RECURSIVE_MATCH(num, ra, rb) \
  266. do { \
  267. stack.pushNewFrame((ra), (rb), RMATCH_WHERE(num)); \
  268. RECURSIVE_MATCH_COMMON(num) \
  269. } while (0)
  270. #define RECURSIVE_MATCH_NEW_GROUP(num, ra, rb, gm) \
  271. do { \
  272. stack.pushNewFrame((ra), (rb), RMATCH_WHERE(num)); \
  273. stack.currentFrame->startNewGroup(gm); \
  274. RECURSIVE_MATCH_COMMON(num) \
  275. } while (0)
  276. #define RRETURN do { JS_EXTENSION_(goto RRETURN_LABEL); } while (0)
  277. #define RRETURN_NO_MATCH do { isMatch = false; RRETURN; } while (0)
  278. /*************************************************
  279. * Match from current position *
  280. *************************************************/
  281. /* On entry instructionPtr points to the first opcode, and subjectPtr to the first character
  282. in the subject string, while substringStart holds the value of subjectPtr at the start of the
  283. last bracketed group - used for breaking infinite loops matching zero-length
  284. strings. This function is called recursively in many circumstances. Whenever it
  285. returns a negative (error) response, the outer match() call must also return the
  286. same response.
  287. Arguments:
  288. subjectPtr pointer in subject
  289. instructionPtr position in code
  290. offsetTop current top pointer
  291. md pointer to "static" info for the match
  292. Returns: 1 if matched ) these values are >= 0
  293. 0 if failed to match )
  294. a negative error value if aborted by an error condition
  295. (e.g. stopped by repeated call or recursion limit)
  296. */
  297. static const unsigned numFramesOnStack = 16;
  298. struct MatchStack {
  299. JSArenaPool *regExpPool;
  300. void *regExpPoolMark;
  301. MatchStack(JSArenaPool *regExpPool)
  302. : regExpPool(regExpPool)
  303. , regExpPoolMark(JS_ARENA_MARK(regExpPool))
  304. , framesEnd(frames + numFramesOnStack)
  305. , currentFrame(frames)
  306. , size(1) // match() creates accesses the first frame w/o calling pushNewFrame
  307. {
  308. JS_ASSERT((sizeof(frames) / sizeof(frames[0])) == numFramesOnStack);
  309. JS_ASSERT(regExpPool);
  310. for (size_t i = 0; i < numFramesOnStack; ++i)
  311. frames[i].init(regExpPool);
  312. }
  313. ~MatchStack() { JS_ARENA_RELEASE(regExpPool, regExpPoolMark); }
  314. MatchFrame frames[numFramesOnStack];
  315. MatchFrame* framesEnd;
  316. MatchFrame* currentFrame;
  317. unsigned size;
  318. bool canUseStackBufferForNextFrame() {
  319. return size < numFramesOnStack;
  320. }
  321. MatchFrame* allocateNextFrame() {
  322. if (canUseStackBufferForNextFrame())
  323. return currentFrame + 1;
  324. // FIXME: bug 574459 -- no NULL check
  325. MatchFrame *frame = js::OffTheBooks::new_<MatchFrame>();
  326. frame->init(regExpPool);
  327. return frame;
  328. }
  329. void pushNewFrame(const unsigned char* instructionPtr, BracketChainNode* bracketChain, ReturnLocation returnLocation) {
  330. MatchFrame* newframe = allocateNextFrame();
  331. newframe->previousFrame = currentFrame;
  332. newframe->args.subjectPtr = currentFrame->args.subjectPtr;
  333. newframe->args.offsetTop = currentFrame->args.offsetTop;
  334. newframe->args.instructionPtr = instructionPtr;
  335. newframe->args.bracketChain = bracketChain;
  336. newframe->returnLocation = returnLocation;
  337. size++;
  338. currentFrame = newframe;
  339. }
  340. void popCurrentFrame() {
  341. MatchFrame* oldFrame = currentFrame;
  342. currentFrame = currentFrame->previousFrame;
  343. if (size > numFramesOnStack)
  344. js::Foreground::delete_(oldFrame);
  345. size--;
  346. }
  347. void popAllFrames() {
  348. while (size)
  349. popCurrentFrame();
  350. }
  351. };
  352. static int matchError(int errorCode, MatchStack& stack)
  353. {
  354. stack.popAllFrames();
  355. return errorCode;
  356. }
  357. /* Get the next UTF-8 character, not advancing the pointer, incrementing length
  358. if there are extra bytes. This is called when we know we are in UTF-8 mode. */
  359. static inline void getUTF8CharAndIncrementLength(int& c, const unsigned char* subjectPtr, int& len)
  360. {
  361. c = *subjectPtr;
  362. if ((c & 0xc0) == 0xc0) {
  363. int gcaa = jsc_pcre_utf8_table4[c & 0x3f]; /* Number of additional bytes */
  364. int gcss = 6 * gcaa;
  365. c = (c & jsc_pcre_utf8_table3[gcaa]) << gcss;
  366. for (int gcii = 1; gcii <= gcaa; gcii++) {
  367. gcss -= 6;
  368. c |= (subjectPtr[gcii] & 0x3f) << gcss;
  369. }
  370. len += gcaa;
  371. }
  372. }
  373. static inline void repeatInformationFromInstructionOffset(short instructionOffset, bool& minimize, int& minimumRepeats, int& maximumRepeats)
  374. {
  375. // Instruction offsets are based off of OP_CRSTAR, OP_STAR, OP_TYPESTAR, OP_NOTSTAR
  376. static const char minimumRepeatsFromInstructionOffset[] = { 0, 0, 1, 1, 0, 0 };
  377. static const int maximumRepeatsFromInstructionOffset[] = { INT_MAX, INT_MAX, INT_MAX, INT_MAX, 1, 1 };
  378. JS_ASSERT(instructionOffset >= 0);
  379. JS_ASSERT(instructionOffset <= (OP_CRMINQUERY - OP_CRSTAR));
  380. minimize = (instructionOffset & 1); // this assumes ordering: Instruction, MinimizeInstruction, Instruction2, MinimizeInstruction2
  381. minimumRepeats = minimumRepeatsFromInstructionOffset[instructionOffset];
  382. maximumRepeats = maximumRepeatsFromInstructionOffset[instructionOffset];
  383. }
  384. /* Helper class for passing a flag value from one op to the next that runs.
  385. This allows us to set the flag in certain ops. When the flag is read, it
  386. will be true only if the previous op set the flag, otherwise it is false. */
  387. class LinearFlag {
  388. public:
  389. LinearFlag() : flag(false) {}
  390. bool readAndClear() {
  391. bool rv = flag;
  392. flag = false;
  393. return rv;
  394. }
  395. void set() {
  396. flag = true;
  397. }
  398. private:
  399. bool flag;
  400. };
  401. static int
  402. match(JSArenaPool *regExpPool, const UChar* subjectPtr, const unsigned char* instructionPtr, int offsetTop, MatchData& md)
  403. {
  404. bool isMatch = false;
  405. int min;
  406. bool minimize = false; /* Initialization not really needed, but some compilers think so. */
  407. unsigned remainingMatchCount = matchLimit;
  408. int othercase; /* Declare here to avoid errors during jumps */
  409. bool minSatisfied;
  410. MatchStack stack(regExpPool);
  411. LinearFlag minSatNextBracket;
  412. /* The opcode jump table. */
  413. #ifdef USE_COMPUTED_GOTO_FOR_MATCH_OPCODE_LOOP
  414. #define EMIT_JUMP_TABLE_ENTRY(opcode) JS_EXTENSION(&&LABEL_OP_##opcode)
  415. static void* opcodeJumpTable[256] = { FOR_EACH_OPCODE(EMIT_JUMP_TABLE_ENTRY) };
  416. #undef EMIT_JUMP_TABLE_ENTRY
  417. #endif
  418. /* One-time setup of the opcode jump table. */
  419. #ifdef USE_COMPUTED_GOTO_FOR_MATCH_OPCODE_LOOP
  420. for (int i = 255; !opcodeJumpTable[i]; i--)
  421. opcodeJumpTable[i] = &&CAPTURING_BRACKET;
  422. #endif
  423. #ifdef USE_COMPUTED_GOTO_FOR_MATCH_RECURSION
  424. // Shark shows this as a hot line
  425. // Using a static const here makes this line disappear, but makes later access hotter (not sure why)
  426. stack.currentFrame->returnLocation = JS_EXTENSION(&&RETURN);
  427. #else
  428. stack.currentFrame->returnLocation = 0;
  429. #endif
  430. stack.currentFrame->args.subjectPtr = subjectPtr;
  431. stack.currentFrame->args.instructionPtr = instructionPtr;
  432. stack.currentFrame->args.offsetTop = offsetTop;
  433. stack.currentFrame->args.bracketChain = 0;
  434. stack.currentFrame->startNewGroup(false);
  435. /* This is where control jumps back to to effect "recursion" */
  436. RECURSE:
  437. if (!--remainingMatchCount)
  438. return matchError(JSRegExpErrorHitLimit, stack);
  439. /* Now start processing the operations. */
  440. #ifndef USE_COMPUTED_GOTO_FOR_MATCH_OPCODE_LOOP
  441. while (true)
  442. #endif
  443. {
  444. #ifdef USE_COMPUTED_GOTO_FOR_MATCH_OPCODE_LOOP
  445. #define BEGIN_OPCODE(opcode) LABEL_OP_##opcode
  446. #define NEXT_OPCODE goto *opcodeJumpTable[*stack.currentFrame->args.instructionPtr]
  447. #else
  448. #define BEGIN_OPCODE(opcode) case OP_##opcode
  449. #define NEXT_OPCODE continue
  450. #endif
  451. #define LOCALS(__ident) (stack.currentFrame->locals.__ident)
  452. #ifdef USE_COMPUTED_GOTO_FOR_MATCH_OPCODE_LOOP
  453. NEXT_OPCODE;
  454. #else
  455. switch (*stack.currentFrame->args.instructionPtr)
  456. #endif
  457. {
  458. /* Non-capturing bracket: optimized */
  459. BEGIN_OPCODE(BRA):
  460. NON_CAPTURING_BRACKET:
  461. DPRINTF(("start non-capturing bracket\n"));
  462. stack.currentFrame->extractBrackets(stack.currentFrame->args.instructionPtr);
  463. /* If we see no ALT, we have to skip three bytes of bracket data (link plus nested
  464. bracket data. */
  465. stack.currentFrame->locals.skipBytes = 3;
  466. /* We must compute this value at the top, before we move the instruction pointer. */
  467. stack.currentFrame->locals.minSatisfied = minSatNextBracket.readAndClear();
  468. do {
  469. /* We need to extract this into a variable so we can correctly pass it by value
  470. through RECURSIVE_MATCH_NEW_GROUP, which modifies currentFrame. */
  471. minSatisfied = stack.currentFrame->locals.minSatisfied;
  472. RECURSIVE_MATCH_NEW_GROUP(2, stack.currentFrame->args.instructionPtr + stack.currentFrame->locals.skipBytes + LINK_SIZE, stack.currentFrame->args.bracketChain, minSatisfied);
  473. if (isMatch) {
  474. DPRINTF(("non-capturing bracket succeeded\n"));
  475. RRETURN;
  476. }
  477. stack.currentFrame->locals.skipBytes = 1;
  478. stack.currentFrame->args.instructionPtr += getLinkValue(stack.currentFrame->args.instructionPtr + 1);
  479. } while (*stack.currentFrame->args.instructionPtr == OP_ALT);
  480. DPRINTF(("non-capturing bracket failed\n"));
  481. for (size_t i = LOCALS(minBracket); i < size_t(LOCALS(limitBracket)); ++i)
  482. md.setOffsetPair(i, -1, -1);
  483. RRETURN;
  484. /* Skip over large extraction number data if encountered. */
  485. BEGIN_OPCODE(BRANUMBER):
  486. stack.currentFrame->args.instructionPtr += 3;
  487. NEXT_OPCODE;
  488. /* End of the pattern. */
  489. BEGIN_OPCODE(END):
  490. md.endMatchPtr = stack.currentFrame->args.subjectPtr; /* Record where we ended */
  491. md.endOffsetTop = stack.currentFrame->args.offsetTop; /* and how many extracts were taken */
  492. isMatch = true;
  493. RRETURN;
  494. /* Assertion brackets. Check the alternative branches in turn - the
  495. matching won't pass the KET for an assertion. If any one branch matches,
  496. the assertion is true. Lookbehind assertions have an OP_REVERSE item at the
  497. start of each branch to move the current point backwards, so the code at
  498. this level is identical to the lookahead case. */
  499. BEGIN_OPCODE(ASSERT):
  500. {
  501. uint16_t bracketMess = get2ByteValue(stack.currentFrame->args.instructionPtr + 1 + LINK_SIZE);
  502. LOCALS(minBracket) = (bracketMess >> 8) & 0xff;
  503. LOCALS(limitBracket) = bracketMess & 0xff;
  504. JS_ASSERT(LOCALS(minBracket) <= LOCALS(limitBracket));
  505. }
  506. stack.currentFrame->locals.skipBytes = 3;
  507. do {
  508. RECURSIVE_MATCH_NEW_GROUP(6, stack.currentFrame->args.instructionPtr + stack.currentFrame->locals.skipBytes + LINK_SIZE, NULL, false);
  509. if (isMatch)
  510. break;
  511. stack.currentFrame->locals.skipBytes = 1;
  512. stack.currentFrame->args.instructionPtr += getLinkValue(stack.currentFrame->args.instructionPtr + 1);
  513. } while (*stack.currentFrame->args.instructionPtr == OP_ALT);
  514. if (*stack.currentFrame->args.instructionPtr == OP_KET) {
  515. for (size_t i = LOCALS(minBracket); i < size_t(LOCALS(limitBracket)); ++i)
  516. md.setOffsetPair(i, -1, -1);
  517. RRETURN_NO_MATCH;
  518. }
  519. /* Continue from after the assertion, updating the offsets high water
  520. mark, since extracts may have been taken during the assertion. */
  521. advanceToEndOfBracket(stack.currentFrame->args.instructionPtr);
  522. stack.currentFrame->args.instructionPtr += 1 + LINK_SIZE;
  523. stack.currentFrame->args.offsetTop = md.endOffsetTop;
  524. NEXT_OPCODE;
  525. /* Negative assertion: all branches must fail to match */
  526. BEGIN_OPCODE(ASSERT_NOT):
  527. stack.currentFrame->locals.skipBytes = 3;
  528. {
  529. unsigned bracketMess = get2ByteValue(stack.currentFrame->args.instructionPtr + 1 + LINK_SIZE);
  530. LOCALS(minBracket) = (bracketMess >> 8) & 0xff;
  531. LOCALS(limitBracket) = bracketMess & 0xff;
  532. }
  533. JS_ASSERT(LOCALS(minBracket) <= LOCALS(limitBracket));
  534. do {
  535. RECURSIVE_MATCH_NEW_GROUP(7, stack.currentFrame->args.instructionPtr + stack.currentFrame->locals.skipBytes + LINK_SIZE, NULL, false);
  536. if (isMatch)
  537. RRETURN_NO_MATCH;
  538. stack.currentFrame->locals.skipBytes = 1;
  539. stack.currentFrame->args.instructionPtr += getLinkValue(stack.currentFrame->args.instructionPtr + 1);
  540. } while (*stack.currentFrame->args.instructionPtr == OP_ALT);
  541. stack.currentFrame->args.instructionPtr += stack.currentFrame->locals.skipBytes + LINK_SIZE;
  542. NEXT_OPCODE;
  543. /* An alternation is the end of a branch; scan along to find the end of the
  544. bracketed group and go to there. */
  545. BEGIN_OPCODE(ALT):
  546. advanceToEndOfBracket(stack.currentFrame->args.instructionPtr);
  547. NEXT_OPCODE;
  548. /* BRAZERO and BRAMINZERO occur just before a bracket group, indicating
  549. that it may occur zero times. It may repeat infinitely, or not at all -
  550. i.e. it could be ()* or ()? in the pattern. Brackets with fixed upper
  551. repeat limits are compiled as a number of copies, with the optional ones
  552. preceded by BRAZERO or BRAMINZERO. */
  553. BEGIN_OPCODE(BRAZERO): {
  554. stack.currentFrame->locals.startOfRepeatingBracket = stack.currentFrame->args.instructionPtr + 1;
  555. stack.currentFrame->extractBrackets(stack.currentFrame->args.instructionPtr + 1);
  556. stack.currentFrame->saveOffsets(LOCALS(minBracket), LOCALS(limitBracket), md.offsetVector, md.offsetEnd);
  557. minSatNextBracket.set();
  558. RECURSIVE_MATCH_NEW_GROUP(14, stack.currentFrame->locals.startOfRepeatingBracket, stack.currentFrame->args.bracketChain, true);
  559. if (isMatch)
  560. RRETURN;
  561. stack.currentFrame->restoreOffsets(LOCALS(minBracket), LOCALS(limitBracket), md.offsetVector, md.offsetEnd);
  562. advanceToEndOfBracket(stack.currentFrame->locals.startOfRepeatingBracket);
  563. stack.currentFrame->args.instructionPtr = stack.currentFrame->locals.startOfRepeatingBracket + 1 + LINK_SIZE;
  564. NEXT_OPCODE;
  565. }
  566. BEGIN_OPCODE(BRAMINZERO): {
  567. stack.currentFrame->locals.startOfRepeatingBracket = stack.currentFrame->args.instructionPtr + 1;
  568. advanceToEndOfBracket(stack.currentFrame->locals.startOfRepeatingBracket);
  569. RECURSIVE_MATCH_NEW_GROUP(15, stack.currentFrame->locals.startOfRepeatingBracket + 1 + LINK_SIZE, stack.currentFrame->args.bracketChain, false);
  570. if (isMatch)
  571. RRETURN;
  572. stack.currentFrame->args.instructionPtr++;
  573. NEXT_OPCODE;
  574. }
  575. /* End of a group, repeated or non-repeating. If we are at the end of
  576. an assertion "group", stop matching and return 1, but record the
  577. current high water mark for use by positive assertions. Do this also
  578. for the "once" (not-backup up) groups. */
  579. BEGIN_OPCODE(KET):
  580. BEGIN_OPCODE(KETRMIN):
  581. BEGIN_OPCODE(KETRMAX):
  582. stack.currentFrame->locals.instructionPtrAtStartOfOnce = stack.currentFrame->args.instructionPtr - getLinkValue(stack.currentFrame->args.instructionPtr + 1);
  583. stack.currentFrame->locals.subjectPtrAtStartOfInstruction = stack.currentFrame->args.bracketChain->bracketStart;
  584. stack.currentFrame->locals.minSatisfied = stack.currentFrame->args.bracketChain->minSatisfied;
  585. /* Back up the stack of bracket start pointers. */
  586. stack.currentFrame->args.bracketChain = stack.currentFrame->args.bracketChain->previousBracket;
  587. if (*stack.currentFrame->locals.instructionPtrAtStartOfOnce == OP_ASSERT || *stack.currentFrame->locals.instructionPtrAtStartOfOnce == OP_ASSERT_NOT) {
  588. md.endOffsetTop = stack.currentFrame->args.offsetTop;
  589. isMatch = true;
  590. RRETURN;
  591. }
  592. /* In all other cases except a conditional group we have to check the
  593. group number back at the start and if necessary complete handling an
  594. extraction by setting the offsets and bumping the high water mark. */
  595. stack.currentFrame->locals.number = *stack.currentFrame->locals.instructionPtrAtStartOfOnce - OP_BRA;
  596. /* For extended extraction brackets (large number), we have to fish out
  597. the number from a dummy opcode at the start. */
  598. if (stack.currentFrame->locals.number > EXTRACT_BASIC_MAX)
  599. stack.currentFrame->locals.number = get2ByteValue(stack.currentFrame->locals.instructionPtrAtStartOfOnce + 4 + LINK_SIZE);
  600. stack.currentFrame->locals.offset = 2 * stack.currentFrame->locals.number;
  601. DPRINTF(("end bracket %d\n", stack.currentFrame->locals.number));
  602. /* Test for a numbered group. This includes groups called as a result
  603. of recursion. Note that whole-pattern recursion is coded as a recurse
  604. into group 0, so it won't be picked up here. Instead, we catch it when
  605. the OP_END is reached. */
  606. if (stack.currentFrame->locals.number > 0) {
  607. if (stack.currentFrame->locals.offset >= md.offsetMax)
  608. md.offsetOverflow = true;
  609. else {
  610. int start = md.offsetVector[md.offsetEnd - stack.currentFrame->locals.number];
  611. int end = stack.currentFrame->args.subjectPtr - md.startSubject;
  612. if (start == end && stack.currentFrame->locals.minSatisfied) {
  613. DPRINTF(("empty string while group already matched; bailing"));
  614. RRETURN_NO_MATCH;
  615. }
  616. DPRINTF(("saving; start: %d; end: %d\n", start, end));
  617. JS_ASSERT(start <= end);
  618. md.setOffsetPair(stack.currentFrame->locals.number, start, end);
  619. if (stack.currentFrame->args.offsetTop <= stack.currentFrame->locals.offset)
  620. stack.currentFrame->args.offsetTop = stack.currentFrame->locals.offset + 2;
  621. }
  622. }
  623. /* For a non-repeating ket, just continue at this level. This also
  624. happens for a repeating ket if no characters were matched in the group.
  625. This is the forcible breaking of infinite loops as implemented in Perl
  626. 5.005. If there is an options reset, it will get obeyed in the normal
  627. course of events. */
  628. if (*stack.currentFrame->args.instructionPtr == OP_KET || stack.currentFrame->args.subjectPtr == stack.currentFrame->locals.subjectPtrAtStartOfInstruction) {
  629. DPRINTF(("non-repeating ket or empty match\n"));
  630. if (stack.currentFrame->args.subjectPtr == stack.currentFrame->locals.subjectPtrAtStartOfInstruction && stack.currentFrame->locals.minSatisfied) {
  631. DPRINTF(("empty string while group already matched; bailing"));
  632. RRETURN_NO_MATCH;
  633. }
  634. stack.currentFrame->args.instructionPtr += 1 + LINK_SIZE;
  635. NEXT_OPCODE;
  636. }
  637. /* The repeating kets try the rest of the pattern or restart from the
  638. preceding bracket, in the appropriate order. */
  639. stack.currentFrame->extractBrackets(LOCALS(instructionPtrAtStartOfOnce));
  640. JS_ASSERT_IF(LOCALS(number), LOCALS(minBracket) <= LOCALS(number) && LOCALS(number) < LOCALS(limitBracket));
  641. if (*stack.currentFrame->args.instructionPtr == OP_KETRMIN) {
  642. stack.currentFrame->saveOffsets(LOCALS(minBracket), LOCALS(limitBracket), md.offsetVector, md.offsetEnd);
  643. RECURSIVE_MATCH(16, stack.currentFrame->args.instructionPtr + 1 + LINK_SIZE, stack.currentFrame->args.bracketChain);
  644. if (isMatch)
  645. RRETURN;
  646. else
  647. stack.currentFrame->restoreOffsets(LOCALS(minBracket), LOCALS(limitBracket), md.offsetVector, md.offsetEnd);
  648. DPRINTF(("recursively matching lazy group\n"));
  649. minSatNextBracket.set();
  650. RECURSIVE_MATCH_NEW_GROUP(17, LOCALS(instructionPtrAtStartOfOnce), stack.currentFrame->args.bracketChain, true);
  651. } else { /* OP_KETRMAX */
  652. stack.currentFrame->saveOffsets(LOCALS(minBracket), LOCALS(limitBracket), md.offsetVector, md.offsetEnd);
  653. stack.currentFrame->clobberOffsets(LOCALS(minBracket), LOCALS(limitBracket), md.offsetVector, md.offsetEnd);
  654. DPRINTF(("recursively matching greedy group\n"));
  655. minSatNextBracket.set();
  656. RECURSIVE_MATCH_NEW_GROUP(18, LOCALS(instructionPtrAtStartOfOnce), stack.currentFrame->args.bracketChain, true);
  657. if (isMatch)
  658. RRETURN;
  659. else
  660. stack.currentFrame->restoreOffsets(LOCALS(minBracket), LOCALS(limitBracket), md.offsetVector, md.offsetEnd);
  661. RECURSIVE_MATCH(19, stack.currentFrame->args.instructionPtr + 1 + LINK_SIZE, stack.currentFrame->args.bracketChain);
  662. }
  663. RRETURN;
  664. /* Start of subject. */
  665. BEGIN_OPCODE(CIRC):
  666. if (stack.currentFrame->args.subjectPtr != md.startSubject)
  667. RRETURN_NO_MATCH;
  668. stack.currentFrame->args.instructionPtr++;
  669. NEXT_OPCODE;
  670. /* After internal newline if multiline. */
  671. BEGIN_OPCODE(BOL):
  672. if (stack.currentFrame->args.subjectPtr != md.startSubject && !isNewline(stack.currentFrame->args.subjectPtr[-1]))
  673. RRETURN_NO_MATCH;
  674. stack.currentFrame->args.instructionPtr++;
  675. NEXT_OPCODE;
  676. /* End of subject. */
  677. BEGIN_OPCODE(DOLL):
  678. if (stack.currentFrame->args.subjectPtr < md.endSubject)
  679. RRETURN_NO_MATCH;
  680. stack.currentFrame->args.instructionPtr++;
  681. NEXT_OPCODE;
  682. /* Before internal newline if multiline. */
  683. BEGIN_OPCODE(EOL):
  684. if (stack.currentFrame->args.subjectPtr < md.endSubject && !isNewline(*stack.currentFrame->args.subjectPtr))
  685. RRETURN_NO_MATCH;
  686. stack.currentFrame->args.instructionPtr++;
  687. NEXT_OPCODE;
  688. /* Word boundary assertions */
  689. BEGIN_OPCODE(NOT_WORD_BOUNDARY):
  690. BEGIN_OPCODE(WORD_BOUNDARY): {
  691. bool currentCharIsWordChar = false;
  692. bool previousCharIsWordChar = false;
  693. if (stack.currentFrame->args.subjectPtr > md.startSubject)
  694. previousCharIsWordChar = isWordChar(stack.currentFrame->args.subjectPtr[-1]);
  695. if (stack.currentFrame->args.subjectPtr < md.endSubject)
  696. currentCharIsWordChar = isWordChar(*stack.currentFrame->args.subjectPtr);
  697. /* Now see if the situation is what we want */
  698. bool wordBoundaryDesired = (*stack.currentFrame->args.instructionPtr++ == OP_WORD_BOUNDARY);
  699. if (wordBoundaryDesired ? currentCharIsWordChar == previousCharIsWordChar : currentCharIsWordChar != previousCharIsWordChar)
  700. RRETURN_NO_MATCH;
  701. NEXT_OPCODE;
  702. }
  703. /* Match a single character type; inline for speed */
  704. BEGIN_OPCODE(NOT_NEWLINE):
  705. if (stack.currentFrame->args.subjectPtr >= md.endSubject)
  706. RRETURN_NO_MATCH;
  707. if (isNewline(*stack.currentFrame->args.subjectPtr++))
  708. RRETURN_NO_MATCH;
  709. stack.currentFrame->args.instructionPtr++;
  710. NEXT_OPCODE;
  711. BEGIN_OPCODE(NOT_DIGIT):
  712. if (stack.currentFrame->args.subjectPtr >= md.endSubject)
  713. RRETURN_NO_MATCH;
  714. if (isASCIIDigit(*stack.currentFrame->args.subjectPtr++))
  715. RRETURN_NO_MATCH;
  716. stack.currentFrame->args.instructionPtr++;
  717. NEXT_OPCODE;
  718. BEGIN_OPCODE(DIGIT):
  719. if (stack.currentFrame->args.subjectPtr >= md.endSubject)
  720. RRETURN_NO_MATCH;
  721. if (!isASCIIDigit(*stack.currentFrame->args.subjectPtr++))
  722. RRETURN_NO_MATCH;
  723. stack.currentFrame->args.instructionPtr++;
  724. NEXT_OPCODE;
  725. BEGIN_OPCODE(NOT_WHITESPACE):
  726. if (stack.currentFrame->args.subjectPtr >= md.endSubject)
  727. RRETURN_NO_MATCH;
  728. if (isSpaceChar(*stack.currentFrame->args.subjectPtr++))
  729. RRETURN_NO_MATCH;
  730. stack.currentFrame->args.instructionPtr++;
  731. NEXT_OPCODE;
  732. BEGIN_OPCODE(WHITESPACE):
  733. if (stack.currentFrame->args.subjectPtr >= md.endSubject)
  734. RRETURN_NO_MATCH;
  735. if (!isSpaceChar(*stack.currentFrame->args.subjectPtr++))
  736. RRETURN_NO_MATCH;
  737. stack.currentFrame->args.instructionPtr++;
  738. NEXT_OPCODE;
  739. BEGIN_OPCODE(NOT_WORDCHAR):
  740. if (stack.currentFrame->args.subjectPtr >= md.endSubject)
  741. RRETURN_NO_MATCH;
  742. if (isWordChar(*stack.currentFrame->args.subjectPtr++))
  743. RRETURN_NO_MATCH;
  744. stack.currentFrame->args.instructionPtr++;
  745. NEXT_OPCODE;
  746. BEGIN_OPCODE(WORDCHAR):
  747. if (stack.currentFrame->args.subjectPtr >= md.endSubject)
  748. RRETURN_NO_MATCH;
  749. if (!isWordChar(*stack.currentFrame->args.subjectPtr++))
  750. RRETURN_NO_MATCH;
  751. stack.currentFrame->args.instructionPtr++;
  752. NEXT_OPCODE;
  753. /* Match a back reference, possibly repeatedly. Look past the end of the
  754. item to see if there is repeat information following. The code is similar
  755. to that for character classes, but repeated for efficiency. Then obey
  756. similar code to character type repeats - written out again for speed.
  757. However, if the referenced string is the empty string, always treat
  758. it as matched, any number of times (otherwise there could be infinite
  759. loops). */
  760. BEGIN_OPCODE(REF):
  761. stack.currentFrame->locals.offset = get2ByteValue(stack.currentFrame->args.instructionPtr + 1) << 1; /* Doubled ref number */
  762. stack.currentFrame->args.instructionPtr += 3; /* Advance past item */
  763. /* If the reference is unset, set the length to be longer than the amount
  764. of subject left; this ensures that every attempt at a match fails. We
  765. can't just fail here, because of the possibility of quantifiers with zero
  766. minima. */
  767. if (stack.currentFrame->locals.offset >= stack.currentFrame->args.offsetTop || md.offsetVector[stack.currentFrame->locals.offset] < 0)
  768. stack.currentFrame->locals.length = 0;
  769. else
  770. stack.currentFrame->locals.length = md.offsetVector[stack.currentFrame->locals.offset+1] - md.offsetVector[stack.currentFrame->locals.offset];
  771. /* Set up for repetition, or handle the non-repeated case */
  772. switch (*stack.currentFrame->args.instructionPtr) {
  773. case OP_CRSTAR:
  774. case OP_CRMINSTAR:
  775. case OP_CRPLUS:
  776. case OP_CRMINPLUS:
  777. case OP_CRQUERY:
  778. case OP_CRMINQUERY:
  779. repeatInformationFromInstructionOffset(*stack.currentFrame->args.instructionPtr++ - OP_CRSTAR, minimize, min, stack.currentFrame->locals.max);
  780. break;
  781. case OP_CRRANGE:
  782. case OP_CRMINRANGE:
  783. minimize = (*stack.currentFrame->args.instructionPtr == OP_CRMINRANGE);
  784. min = get2ByteValue(stack.currentFrame->args.instructionPtr + 1);
  785. stack.currentFrame->locals.max = get2ByteValue(stack.currentFrame->args.instructionPtr + 3);
  786. if (stack.currentFrame->locals.max == 0)
  787. stack.currentFrame->locals.max = INT_MAX;
  788. stack.currentFrame->args.instructionPtr += 5;
  789. break;
  790. default: /* No repeat follows */
  791. if (!matchRef(stack.currentFrame->locals.offset, stack.currentFrame->args.subjectPtr, stack.currentFrame->locals.length, md))
  792. RRETURN_NO_MATCH;
  793. stack.currentFrame->args.subjectPtr += stack.currentFrame->locals.length;
  794. NEXT_OPCODE;
  795. }
  796. /* If the length of the reference is zero, just continue with the
  797. main loop. */
  798. if (stack.currentFrame->locals.length == 0)
  799. NEXT_OPCODE;
  800. /* First, ensure the minimum number of matches are present. */
  801. for (int i = 1; i <= min; i++) {
  802. if (!matchRef(stack.currentFrame->locals.offset, stack.currentFrame->args.subjectPtr, stack.currentFrame->locals.length, md))
  803. RRETURN_NO_MATCH;
  804. stack.currentFrame->args.subjectPtr += stack.currentFrame->locals.length;
  805. }
  806. /* If min = max, continue at the same level without recursion.
  807. They are not both allowed to be zero. */
  808. if (min == stack.currentFrame->locals.max)
  809. NEXT_OPCODE;
  810. /* If minimizing, keep trying and advancing the pointer */
  811. if (minimize) {
  812. for (stack.currentFrame->locals.fi = min;; stack.currentFrame->locals.fi++) {
  813. RECURSIVE_MATCH(20, stack.currentFrame->args.instructionPtr, stack.currentFrame->args.bracketChain);
  814. if (isMatch)
  815. RRETURN;
  816. if (stack.currentFrame->locals.fi >= stack.currentFrame->locals.max || !matchRef(stack.currentFrame->locals.offset, stack.currentFrame->args.subjectPtr, stack.currentFrame->locals.length, md))
  817. RRETURN;
  818. stack.currentFrame->args.subjectPtr += stack.currentFrame->locals.length;
  819. }
  820. /* Control never reaches here */
  821. }
  822. /* If maximizing, find the longest string and work backwards */
  823. else {
  824. stack.currentFrame->locals.subjectPtrAtStartOfInstruction = stack.currentFrame->args.subjectPtr;
  825. for (int i = min; i < stack.currentFrame->locals.max; i++) {
  826. if (!matchRef(stack.currentFrame->locals.offset, stack.currentFrame->args.subjectPtr, stack.currentFrame->locals.length, md))
  827. break;
  828. stack.currentFrame->args.subjectPtr += stack.currentFrame->locals.length;
  829. }
  830. while (stack.currentFrame->args.subjectPtr >= stack.currentFrame->locals.subjectPtrAtStartOfInstruction) {
  831. RECURSIVE_MATCH(21, stack.currentFrame->args.instructionPtr, stack.currentFrame->args.bracketChain);
  832. if (isMatch)
  833. RRETURN;
  834. stack.currentFrame->args.subjectPtr -= stack.currentFrame->locals.length;
  835. }
  836. RRETURN_NO_MATCH;
  837. }
  838. /* Control never reaches here */
  839. /* Match a bit-mapped character class, possibly repeatedly. This op code is
  840. used when all the characters in the class have values in the range 0-255,
  841. and either the matching is caseful, or the characters are in the range
  842. 0-127 when UTF-8 processing is enabled. The only difference between
  843. OP_CLASS and OP_NCLASS occurs when a data character outside the range is
  844. encountered.
  845. First, look past the end of the item to see if there is repeat information
  846. following. Then obey similar code to character type repeats - written out
  847. again for speed. */
  848. BEGIN_OPCODE(NCLASS):
  849. BEGIN_OPCODE(CLASS):
  850. stack.currentFrame->locals.data = stack.currentFrame->args.instructionPtr + 1; /* Save for matching */
  851. stack.currentFrame->args.instructionPtr += 33; /* Advance past the item */
  852. switch (*stack.currentFrame->args.instructionPtr) {
  853. case OP_CRSTAR:
  854. case OP_CRMINSTAR:
  855. case OP_CRPLUS:
  856. case OP_CRMINPLUS:
  857. case OP_CRQUERY:
  858. case OP_CRMINQUERY:
  859. repeatInformationFromInstructionOffset(*stack.currentFrame->args.instructionPtr++ - OP_CRSTAR, minimize, min, stack.currentFrame->locals.max);
  860. break;
  861. case OP_CRRANGE:
  862. case OP_CRMINRANGE:
  863. minimize = (*stack.currentFrame->args.instructionPtr == OP_CRMINRANGE);
  864. min = get2ByteValue(stack.currentFrame->args.instructionPtr + 1);
  865. stack.currentFrame->locals.max = get2ByteValue(stack.currentFrame->args.instructionPtr + 3);
  866. if (stack.currentFrame->locals.max == 0)
  867. stack.currentFrame->locals.max = INT_MAX;
  868. stack.currentFrame->args.instructionPtr += 5;
  869. break;
  870. default: /* No repeat follows */
  871. min = stack.currentFrame->locals.max = 1;
  872. break;
  873. }
  874. /* First, ensure the minimum number of matches are present. */
  875. for (int i = 1; i <= min; i++) {
  876. if (stack.currentFrame->args.subjectPtr >= md.endSubject)
  877. RRETURN_NO_MATCH;
  878. int c = *stack.currentFrame->args.subjectPtr++;
  879. if (c > 255) {
  880. if (stack.currentFrame->locals.data[-1] == OP_CLASS)
  881. RRETURN_NO_MATCH;
  882. } else {
  883. if (!(stack.currentFrame->locals.data[c / 8] & (1 << (c & 7))))
  884. RRETURN_NO_MATCH;
  885. }
  886. }
  887. /* If max == min we can continue with the main loop without the
  888. need to recurse. */
  889. if (min == stack.currentFrame->locals.max)
  890. NEXT_OPCODE;
  891. /* If minimizing, keep testing the rest of the expression and advancing
  892. the pointer while it matches the class. */
  893. if (minimize) {
  894. for (stack.currentFrame->locals.fi = min;; stack.currentFrame->locals.fi++) {
  895. RECURSIVE_MATCH(22, stack.currentFrame->args.instructionPtr, stack.currentFrame->args.bracketChain);
  896. if (isMatch)
  897. RRETURN;
  898. if (stack.currentFrame->locals.fi >= stack.currentFrame->locals.max || stack.currentFrame->args.subjectPtr >= md.endSubject)
  899. RRETURN;
  900. int c = *stack.currentFrame->args.subjectPtr++;
  901. if (c > 255) {
  902. if (stack.currentFrame->locals.data[-1] == OP_CLASS)
  903. RRETURN;
  904. } else {
  905. if ((stack.currentFrame->locals.data[c/8] & (1 << (c&7))) == 0)
  906. RRETURN;
  907. }
  908. }
  909. /* Control never reaches here */
  910. }
  911. /* If maximizing, find the longest possible run, then work backwards. */
  912. else {
  913. stack.currentFrame->locals.subjectPtrAtStartOfInstruction = stack.currentFrame->args.subjectPtr;
  914. for (int i = min; i < stack.currentFrame->locals.max; i++) {
  915. if (stack.currentFrame->args.subjectPtr >= md.endSubject)
  916. break;
  917. int c = *stack.currentFrame->args.subjectPtr;
  918. if (c > 255) {
  919. if (stack.currentFrame->locals.data[-1] == OP_CLASS)
  920. break;
  921. } else {
  922. if (!(stack.currentFrame->locals.data[c / 8] & (1 << (c & 7))))
  923. break;
  924. }
  925. ++stack.currentFrame->args.subjectPtr;
  926. }
  927. for (;;) {
  928. RECURSIVE_MATCH(24, stack.currentFrame->args.instructionPtr, stack.currentFrame->args.bracketChain);
  929. if (isMatch)
  930. RRETURN;
  931. if (stack.currentFrame->args.subjectPtr-- == stack.currentFrame->locals.subjectPtrAtStartOfInstruction)
  932. break; /* Stop if tried at original pos */
  933. }
  934. RRETURN;
  935. }
  936. /* Control never reaches here */
  937. /* Match an extended character class. */
  938. BEGIN_OPCODE(XCLASS):
  939. stack.currentFrame->locals.data = stack.currentFrame->args.instructionPtr + 1 + LINK_SIZE; /* Save for matching */
  940. stack.currentFrame->args.instructionPtr += getLinkValue(stack.currentFrame->args.instructionPtr + 1); /* Advance past the item */
  941. switch (*stack.currentFrame->args.instructionPtr) {
  942. case OP_CRSTAR:
  943. case OP_CRMINSTAR:
  944. case OP_CRPLUS:
  945. case OP_CRMINPLUS:
  946. case OP_CRQUERY:
  947. case OP_CRMINQUERY:
  948. repeatInformationFromInstructionOffset(*stack.currentFrame->args.instructionPtr++ - OP_CRSTAR, minimize, min, stack.currentFrame->locals.max);
  949. break;
  950. case OP_CRRANGE:
  951. case OP_CRMINRANGE:
  952. minimize = (*stack.currentFrame->args.instructionPtr == OP_CRMINRANGE);
  953. min = get2ByteValue(stack.currentFrame->args.instructionPtr + 1);
  954. stack.currentFrame->locals.max = get2ByteValue(stack.currentFrame->args.instructionPtr + 3);
  955. if (stack.currentFrame->locals.max == 0)
  956. stack.currentFrame->locals.max = INT_MAX;
  957. stack.currentFrame->args.instructionPtr += 5;
  958. break;
  959. default: /* No repeat follows */
  960. min = stack.currentFrame->locals.max = 1;
  961. }
  962. /* First, ensure the minimum number of matches are present. */
  963. for (int i = 1; i <= min; i++) {
  964. if (stack.currentFrame->args.subjectPtr >= md.endSubject)
  965. RRETURN_NO_MATCH;
  966. int c = *stack.currentFrame->args.subjectPtr++;
  967. if (!jsc_pcre_xclass(c, stack.currentFrame->locals.data))
  968. RRETURN_NO_MATCH;
  969. }
  970. /* If max == min we can continue with the main loop without the
  971. need to recurse. */
  972. if (min == stack.currentFrame->locals.max)
  973. NEXT_OPCODE;
  974. /* If minimizing, keep testing the rest of the expression and advancing
  975. the pointer while it matches the class. */
  976. if (minimize) {
  977. for (stack.currentFrame->locals.fi = min;; stack.currentFrame->locals.fi++) {
  978. RECURSIVE_MATCH(26, stack.currentFrame->args.instructionPtr, stack.currentFrame->args.bracketChain);
  979. if (isMatch)
  980. RRETURN;
  981. if (stack.currentFrame->locals.fi >= stack.currentFrame->locals.max || stack.currentFrame->args.subjectPtr >= md.endSubject)
  982. RRETURN;
  983. int c = *stack.currentFrame->args.subjectPtr++;
  984. if (!jsc_pcre_xclass(c, stack.currentFrame->locals.data))
  985. RRETURN;
  986. }
  987. /* Control never reaches here */
  988. }
  989. /* If maximizing, find the longest possible run, then work backwards. */
  990. else {
  991. stack.currentFrame->locals.subjectPtrAtStartOfInstruction = stack.currentFrame->args.subjectPtr;
  992. for (int i = min; i < stack.currentFrame->locals.max; i++) {
  993. if (stack.currentFrame->args.subjectPtr >= md.endSubject)
  994. break;
  995. int c = *stack.currentFrame->args.subjectPtr;
  996. if (!jsc_pcre_xclass(c, stack.currentFrame->locals.data))
  997. break;
  998. ++stack.currentFrame->args.subjectPtr;
  999. }
  1000. for(;;) {
  1001. RECURSIVE_MATCH(27, stack.currentFrame->args.instructionPtr, stack.currentFrame->args.bracketChain);
  1002. if (isMatch)
  1003. RRETURN;
  1004. if (stack.currentFrame->args.subjectPtr-- == stack.currentFrame->locals.subjectPtrAtStartOfInstruction)
  1005. break; /* Stop if tried at original pos */
  1006. }
  1007. RRETURN;
  1008. }
  1009. /* Control never reaches here */
  1010. /* Match a single character, casefully */
  1011. BEGIN_OPCODE(CHAR):
  1012. stack.currentFrame->locals.length = 1;
  1013. stack.currentFrame->args.instructionPtr++;
  1014. getUTF8CharAndIncrementLength(stack.currentFrame->locals.fc, stack.currentFrame->args.instructionPtr, stack.currentFrame->locals.length);
  1015. stack.currentFrame->args.instructionPtr += stack.currentFrame->locals.length;
  1016. if (stack.currentFrame->args.subjectPtr >= md.endSubject)
  1017. RRETURN_NO_MATCH;
  1018. if (stack.currentFrame->locals.fc != *stack.currentFrame->args.subjectPtr++)
  1019. RRETURN_NO_MATCH;
  1020. NEXT_OPCODE;
  1021. /* Match a single character, caselessly */
  1022. BEGIN_OPCODE(CHAR_IGNORING_CASE): {
  1023. stack.currentFrame->locals.length = 1;
  1024. stack.currentFrame->args.instructionPtr++;
  1025. getUTF8CharAndIncrementLength(stack.currentFrame->locals.fc, stack.currentFrame->args.instructionPtr, stack.currentFrame->locals.length);
  1026. stack.currentFrame->args.instructionPtr += stack.currentFrame->locals.length;
  1027. if (stack.currentFrame->args.subjectPtr >= md.endSubject)
  1028. RRETURN_NO_MATCH;
  1029. int dc = *stack.currentFrame->args.subjectPtr++;
  1030. if (stack.currentFrame->locals.fc != dc && jsc_pcre_ucp_othercase(stack.currentFrame->locals.fc) != dc)
  1031. RRETURN_NO_MATCH;
  1032. NEXT_OPCODE;
  1033. }
  1034. /* Match a single ASCII character. */
  1035. BEGIN_OPCODE(ASCII_CHAR):
  1036. if (md.endSubject == stack.currentFrame->args.subjectPtr)
  1037. RRETURN_NO_MATCH;
  1038. if (*stack.currentFrame->args.subjectPtr != stack.currentFrame->args.instructionPtr[1])
  1039. RRETURN_NO_MATCH;
  1040. ++stack.currentFrame->args.subjectPtr;
  1041. stack.currentFrame->args.instructionPtr += 2;
  1042. NEXT_OPCODE;
  1043. /* Match one of two cases of an ASCII letter. */
  1044. BEGIN_OPCODE(ASCII_LETTER_IGNORING_CASE):
  1045. if (md.endSubject == stack.currentFrame->args.subjectPtr)
  1046. RRETURN_NO_MATCH;
  1047. if ((*stack.currentFrame->args.subjectPtr | 0x20) != stack.currentFrame->args.instructionPtr[1])
  1048. RRETURN_NO_MATCH;
  1049. ++stack.currentFrame->args.subjectPtr;
  1050. stack.currentFrame->args.instructionPtr += 2;
  1051. NEXT_OPCODE;
  1052. /* Match a single character repeatedly; different opcodes share code. */
  1053. BEGIN_OPCODE(EXACT):
  1054. min = stack.currentFrame->locals.max = get2ByteValue(stack.currentFrame->args.instructionPtr + 1);
  1055. minimize = false;
  1056. stack.currentFrame->args.instructionPtr += 3;
  1057. goto REPEATCHAR;
  1058. BEGIN_OPCODE(UPTO):
  1059. BEGIN_OPCODE(MINUPTO):
  1060. min = 0;
  1061. stack.currentFrame->locals.max = get2ByteValue(stack.currentFrame->args.instructionPtr + 1);
  1062. minimize = *stack.currentFrame->args.instructionPtr == OP_MINUPTO;
  1063. stack.currentFrame->args.instructionPtr += 3;
  1064. goto REPEATCHAR;
  1065. BEGIN_OPCODE(STAR):
  1066. BEGIN_OPCODE(MINSTAR):
  1067. BEGIN_OPCODE(PLUS):
  1068. BEGIN_OPCODE(MINPLUS):
  1069. BEGIN_OPCODE(QUERY):
  1070. BEGIN_OPCODE(MINQUERY):
  1071. repeatInformationFromInstructionOffset(*stack.currentFrame->args.instructionPtr++ - OP_STAR, minimize, min, stack.currentFrame->locals.max);
  1072. /* Common code for all repeated single-character matches. We can give
  1073. up quickly if there are fewer than the minimum number of characters left in
  1074. the subject. */
  1075. REPEATCHAR:
  1076. stack.currentFrame->locals.length = 1;
  1077. getUTF8CharAndIncrementLength(stack.currentFrame->locals.fc, stack.currentFrame->args.instructionPtr, stack.currentFrame->locals.length);
  1078. if (min * (stack.currentFrame->locals.fc > 0xFFFF ? 2 : 1) > md.endSubject - stack.currentFrame->args.subjectPtr)
  1079. RRETURN_NO_MATCH;
  1080. stack.currentFrame->args.instructionPtr += stack.currentFrame->locals.length;
  1081. if (stack.currentFrame->locals.fc <= 0xFFFF) {
  1082. othercase = md.ignoreCase ? jsc_pcre_ucp_othercase(stack.currentFrame->locals.fc) : -1;
  1083. for (int i = 1; i <= min; i++) {
  1084. if (*stack.currentFrame->args.subjectPtr != stack.currentFrame->locals.fc && *stack.currentFrame->args.subjectPtr != othercase)
  1085. RRETURN_NO_MATCH;
  1086. ++stack.currentFrame->args.subjectPtr;
  1087. }
  1088. if (min == stack.currentFrame->locals.max)
  1089. NEXT_OPCODE;
  1090. if (minimize) {
  1091. stack.currentFrame->locals.repeatOthercase = othercase;
  1092. for (stack.currentFrame->locals.fi = min;; stack.currentFrame->locals.fi++) {
  1093. RECURSIVE_MATCH(28, stack.currentFrame->args.instructionPtr, stack.currentFrame->args.bracketChain);
  1094. if (isMatch)
  1095. RRETURN;
  1096. if (stack.currentFrame->locals.fi >= stack.currentFrame->locals.max || stack.currentFrame->args.subjectPtr >= md.endSubject)
  1097. RRETURN;
  1098. if (*stack.currentFrame->args.subjectPtr != stack.currentFrame->locals.fc && *stack.currentFrame->args.subjectPtr != stack.currentFrame->locals.repeatOthercase)
  1099. RRETURN;
  1100. ++stack.currentFrame->args.subjectPtr;
  1101. }
  1102. /* Control never reaches here */
  1103. } else {
  1104. stack.currentFrame->locals.subjectPtrAtStartOfInstruction = stack.currentFrame->args.subjectPtr;
  1105. for (int i = min; i < stack.currentFrame->locals.max; i++) {
  1106. if (stack.currentFrame->args.subjectPtr >= md.endSubject)
  1107. break;
  1108. if (*stack.currentFrame->args.subjectPtr != stack.currentFrame->locals.fc && *stack.currentFrame->args.subjectPtr != othercase)
  1109. break;
  1110. ++stack.currentFrame->args.subjectPtr;
  1111. }
  1112. while (stack.currentFrame->args.subjectPtr >= stack.currentFrame->locals.subjectPtrAtStartOfInstruction) {
  1113. RECURSIVE_MATCH(29, stack.currentFrame->args.instructionPtr, stack.currentFrame->args.bracketChain);
  1114. if (isMatch)
  1115. RRETURN;
  1116. --stack.currentFrame->args.subjectPtr;
  1117. }
  1118. RRETURN_NO_MATCH;
  1119. }
  1120. /* Control never reaches here */
  1121. } else {
  1122. /* No case on surrogate pairs, so no need to bother with "othercase". */
  1123. for (int i = 1; i <= min; i++) {
  1124. if (*stack.currentFrame->args.subjectPtr != stack.currentFrame->locals.fc)
  1125. RRETURN_NO_MATCH;
  1126. stack.currentFrame->args.subjectPtr += 2;
  1127. }
  1128. if (min == stack.currentFrame->locals.max)
  1129. NEXT_OPCODE;
  1130. if (minimize) {
  1131. for (stack.currentFrame->locals.fi = min;; stack.currentFrame->locals.fi++) {
  1132. RECURSIVE_MATCH(30, stack.currentFrame->args.instructionPtr, stack.currentFrame->args.bracketChain);
  1133. if (isMatch)
  1134. RRETURN;
  1135. if (stack.currentFrame->locals.fi >= stack.currentFrame->locals.max || stack.currentFrame->args.subjectPtr >= md.endSubject)
  1136. RRETURN;
  1137. if (*stack.currentFrame->args.subjectPtr != stack.currentFrame->locals.fc)
  1138. RRETURN;
  1139. stack.currentFrame->args.subjectPtr += 2;
  1140. }
  1141. /* Control never reaches here */
  1142. } else {
  1143. stack.currentFrame->locals.subjectPtrAtStartOfInstruction = stack.currentFrame->args.subjectPtr;
  1144. for (int i = min; i < stack.currentFrame->locals.max; i++) {
  1145. if (stack.currentFrame->args.subjectPtr > md.endSubject - 2)
  1146. break;
  1147. if (*stack.currentFrame->args.subjectPtr != stack.currentFrame->locals.fc)
  1148. break;
  1149. stack.currentFrame->args.subjectPtr += 2;
  1150. }
  1151. while (stack.currentFrame->args.subjectPtr >= stack.currentFrame->locals.subjectPtrAtStartOfInstruction) {
  1152. RECURSIVE_MATCH(31, stack.currentFrame->args.instructionPtr, stack.currentFrame->args.bracketChain);
  1153. if (isMatch)
  1154. RRETURN;
  1155. stack.currentFrame->args.subjectPtr -= 2;
  1156. }
  1157. RRETURN_NO_MATCH;
  1158. }
  1159. /* Control never reaches here */
  1160. }
  1161. /* Control never reaches here */
  1162. /* Match a negated single one-byte character. */
  1163. BEGIN_OPCODE(NOT): {
  1164. if (stack.currentFrame->args.subjectPtr >= md.endSubject)
  1165. RRETURN_NO_MATCH;
  1166. int b = stack.currentFrame->args.instructionPtr[1];
  1167. int c = *stack.currentFrame->args.subjectPtr++;
  1168. stack.currentFrame->args.instructionPtr += 2;
  1169. if (md.ignoreCase) {
  1170. if (c < 128)
  1171. c = toLowerCase(c);
  1172. if (toLowerCase(b) == c)
  1173. RRETURN_NO_MATCH;
  1174. } else {
  1175. if (b == c)
  1176. RRETURN_NO_MATCH;
  1177. }
  1178. NEXT_OPCODE;
  1179. }
  1180. /* Match a negated single one-byte character repeatedly. This is almost a
  1181. repeat of the code for a repeated single character, but I haven't found a
  1182. nice way of commoning these up that doesn't require a test of the
  1183. positive/negative option for each character match. Maybe that wouldn't add
  1184. very much to the time taken, but character matching *is* what this is all
  1185. about... */
  1186. BEGIN_OPCODE(NOTEXACT):
  1187. min = stack.currentFrame->locals.max = get2ByteValue(stack.currentFrame->args.instructionPtr + 1);
  1188. minimize = false;
  1189. stack.currentFrame->args.instructionPtr += 3;
  1190. goto REPEATNOTCHAR;
  1191. BEGIN_OPCODE(NOTUPTO):
  1192. BEGIN_OPCODE(NOTMINUPTO):
  1193. min = 0;
  1194. stack.currentFrame->locals.max = get2ByteValue(stack.currentFrame->args.instructionPtr + 1);
  1195. minimize = *stack.currentFrame->args.instructionPtr == OP_NOTMINUPTO;
  1196. stack.currentFrame->args.instructionPtr += 3;
  1197. goto REPEATNOTCHAR;
  1198. BEGIN_OPCODE(NOTSTAR):
  1199. BEGIN_OPCODE(NOTMINSTAR):
  1200. BEGIN_OPCODE(NOTPLUS):
  1201. BEGIN_OPCODE(NOTMINPLUS):
  1202. BEGIN_OPCODE(NOTQUERY):
  1203. BEGIN_OPCODE(NOTMINQUERY):
  1204. repeatInformationFromInstructionOffset(*stack.currentFrame->args.instructionPtr++ - OP_NOTSTAR, minimize, min, stack.currentFrame->locals.max);
  1205. /* Common code for all repeated single-byte matches. We can give up quickly
  1206. if there are fewer than the minimum number of bytes left in the
  1207. subject. */
  1208. REPEATNOTCHAR:
  1209. if (min > md.endSubject - stack.currentFrame->args.subjectPtr)
  1210. RRETURN_NO_MATCH;
  1211. stack.currentFrame->locals.fc = *stack.currentFrame->args.instructionPtr++;
  1212. /* The code is duplicated for the caseless and caseful cases, for speed,
  1213. since matching characters is likely to be quite common. First, ensure the
  1214. minimum number of matches are present. If min = max, continue at the same
  1215. level without recursing. Otherwise, if minimizing, keep trying the rest of
  1216. the expression and advancing one matching character if failing, up to the
  1217. maximum. Alternatively, if maximizing, find the maximum number of
  1218. characters and work backwards. */
  1219. DPRINTF(("negative matching %c{%d,%d}\n", stack.currentFrame->locals.fc, min, stack.currentFrame->locals.max));
  1220. if (md.ignoreCase) {
  1221. if (stack.currentFrame->locals.fc < 128)
  1222. stack.currentFrame->locals.fc = toLowerCase(stack.currentFrame->locals.fc);
  1223. for (int i = 1; i <= min; i++) {
  1224. int d = *stack.currentFrame->args.subjectPtr++;
  1225. if (d < 128)
  1226. d = toLowerCase(d);
  1227. if (stack.currentFrame->locals.fc == d)
  1228. RRETURN_NO_MATCH;
  1229. }
  1230. if (min == stack.currentFrame->locals.max)
  1231. NEXT_OPCODE;
  1232. if (minimize) {
  1233. for (stack.currentFrame->locals.fi = min;; stack.currentFrame->locals.fi++) {
  1234. RECURSIVE_MATCH(38, stack.currentFrame->args.instructionPtr, stack.currentFrame->args.bracketChain);
  1235. if (isMatch)
  1236. RRETURN;
  1237. int d = *stack.currentFrame->args.subjectPtr++;
  1238. if (d < 128)
  1239. d = toLowerCase(d);
  1240. if (stack.currentFrame->locals.fi >= stack.currentFrame->locals.max || stack.currentFrame->args.subjectPtr >= md.endSubject || stack.currentFrame->locals.fc == d)
  1241. RRETURN;
  1242. }
  1243. /* Control never reaches here */
  1244. }
  1245. /* Maximize case */
  1246. else {
  1247. stack.currentFrame->locals.subjectPtrAtStartOfInstruction = stack.currentFrame->args.subjectPtr;
  1248. for (int i = min; i < stack.currentFrame->locals.max; i++) {
  1249. if (stack.currentFrame->args.subjectPtr >= md.endSubject)
  1250. break;
  1251. int d = *stack.currentFrame->args.subjectPtr;
  1252. if (d < 128)
  1253. d = toLowerCase(d);
  1254. if (stack.currentFrame->locals.fc == d)
  1255. break;
  1256. ++stack.currentFrame->args.subjectPtr;
  1257. }
  1258. for (;;) {
  1259. RECURSIVE_MATCH(40, stack.currentFrame->args.instructionPtr, stack.currentFrame->args.bracketChain);
  1260. if (isMatch)
  1261. RRETURN;
  1262. if (stack.currentFrame->args.subjectPtr-- == stack.currentFrame->locals.subjectPtrAtStartOfInstruction)
  1263. break; /* Stop if tried at original pos */
  1264. }
  1265. RRETURN;
  1266. }
  1267. /* Control never reaches here */
  1268. }
  1269. /* Caseful comparisons */
  1270. else {
  1271. for (int i = 1; i <= min; i++) {
  1272. int d = *stack.currentFrame->args.subjectPtr++;
  1273. if (stack.currentFrame->locals.fc == d)
  1274. RRETURN_NO_MATCH;
  1275. }
  1276. if (min == stack.currentFrame->locals.max)
  1277. NEXT_OPCODE;
  1278. if (minimize) {
  1279. for (stack.currentFrame->locals.fi = min;; stack.currentFrame->locals.fi++) {
  1280. RECURSIVE_MATCH(42, stack.currentFrame->args.instructionPtr, stack.currentFrame->args.bracketChain);
  1281. if (isMatch)
  1282. RRETURN;
  1283. int d = *stack.currentFrame->args.subjectPtr++;
  1284. if (stack.currentFrame->locals.fi >= stack.currentFrame->locals.max || stack.currentFrame->args.subjectPtr >= md.endSubject || stack.currentFrame->locals.fc == d)
  1285. RRETURN;
  1286. }
  1287. /* Control never reaches here */
  1288. }
  1289. /* Maximize case */
  1290. else {
  1291. stack.currentFrame->locals.subjectPtrAtStartOfInstruction = stack.currentFrame->args.subjectPtr;
  1292. for (int i = min; i < stack.currentFrame->locals.max; i++) {
  1293. if (stack.currentFrame->args.subjectPtr >= md.endSubject)
  1294. break;
  1295. int d = *stack.currentFrame->args.subjectPtr;
  1296. if (stack.currentFrame->locals.fc == d)
  1297. break;
  1298. ++stack.currentFrame->args.subjectPtr;
  1299. }
  1300. for (;;) {
  1301. RECURSIVE_MATCH(44, stack.currentFrame->args.instructionPtr, stack.currentFrame->args.bracketChain);
  1302. if (isMatch)
  1303. RRETURN;
  1304. if (stack.currentFrame->args.subjectPtr-- == stack.currentFrame->locals.subjectPtrAtStartOfInstruction)
  1305. break; /* Stop if tried at original pos */
  1306. }
  1307. RRETURN;
  1308. }
  1309. }
  1310. /* Control never reaches here */
  1311. /* Match a single character type repeatedly; several different opcodes
  1312. share code. This is very similar to the code for single characters, but we
  1313. repeat it in the interests of efficiency. */
  1314. BEGIN_OPCODE(TYPEEXACT):
  1315. min = stack.currentFrame->locals.max = get2ByteValue(stack.currentFrame->args.instructionPtr + 1);
  1316. minimize = true;
  1317. stack.currentFrame->args.instructionPtr += 3;
  1318. goto REPEATTYPE;
  1319. BEGIN_OPCODE(TYPEUPTO):
  1320. BEGIN_OPCODE(TYPEMINUPTO):
  1321. min = 0;
  1322. stack.currentFrame->locals.max = get2ByteValue(stack.currentFrame->args.instructionPtr + 1);
  1323. minimize = *stack.currentFrame->args.instructionPtr == OP_TYPEMINUPTO;
  1324. stack.currentFrame->args.instructionPtr += 3;
  1325. goto REPEATTYPE;
  1326. BEGIN_OPCODE(TYPESTAR):
  1327. BEGIN_OPCODE(TYPEMINSTAR):
  1328. BEGIN_OPCODE(TYPEPLUS):
  1329. BEGIN_OPCODE(TYPEMINPLUS):
  1330. BEGIN_OPCODE(TYPEQUERY):
  1331. BEGIN_OPCODE(TYPEMINQUERY):
  1332. repeatInformationFromInstructionOffset(*stack.currentFrame->args.instructionPtr++ - OP_TYPESTAR, minimize, min, stack.currentFrame->locals.max);
  1333. /* Common code for all repeated single character type matches. Note that
  1334. in UTF-8 mode, '.' matches a character of any length, but for the other
  1335. character types, the valid characters are all one-byte long. */
  1336. REPEATTYPE:
  1337. stack.currentFrame->locals.ctype = *stack.currentFrame->args.instructionPtr++; /* Code for the character type */
  1338. /* First, ensure the minimum number of matches are present. Use inline
  1339. code for maximizing the speed, and do the type test once at the start
  1340. (i.e. keep it out of the loop). Also we can test that there are at least
  1341. the minimum number of characters before we start. */
  1342. if (min > md.endSubject - stack.currentFrame->args.subjectPtr)
  1343. RRETURN_NO_MATCH;
  1344. if (min > 0) {
  1345. switch (stack.currentFrame->locals.ctype) {
  1346. case OP_NOT_NEWLINE:
  1347. for (int i = 1; i <= min; i++) {
  1348. if (isNewline(*stack.currentFrame->args.subjectPtr))
  1349. RRETURN_NO_MATCH;
  1350. ++stack.currentFrame->args.subjectPtr;
  1351. }
  1352. break;
  1353. case OP_NOT_DIGIT:
  1354. for (int i = 1; i <= min; i++) {
  1355. if (isASCIIDigit(*stack.currentFrame->args.subjectPtr))
  1356. RRETURN_NO_MATCH;
  1357. ++stack.currentFrame->args.subjectPtr;
  1358. }
  1359. break;
  1360. case OP_DIGIT:
  1361. for (int i = 1; i <= min; i++) {
  1362. if (!isASCIIDigit(*stack.currentFrame->args.subjectPtr))
  1363. RRETURN_NO_MATCH;
  1364. ++stack.currentFrame->args.subjectPtr;
  1365. }
  1366. break;
  1367. case OP_NOT_WHITESPACE:
  1368. for (int i = 1; i <= min; i++) {
  1369. if (isSpaceChar(*stack.currentFrame->args.subjectPtr))
  1370. RRETURN_NO_MATCH;
  1371. ++stack.currentFrame->args.subjectPtr;
  1372. }
  1373. break;
  1374. case OP_WHITESPACE:
  1375. for (int i = 1; i <= min; i++) {
  1376. if (!isSpaceChar(*stack.currentFrame->args.subjectPtr))
  1377. RRETURN_NO_MATCH;
  1378. ++stack.currentFrame->args.subjectPtr;
  1379. }
  1380. break;
  1381. case OP_NOT_WORDCHAR:
  1382. for (int i = 1; i <= min; i++) {
  1383. if (isWordChar(*stack.currentFrame->args.subjectPtr))
  1384. RRETURN_NO_MATCH;
  1385. ++stack.currentFrame->args.subjectPtr;
  1386. }
  1387. break;
  1388. case OP_WORDCHAR:
  1389. for (int i = 1; i <= min; i++) {
  1390. if (!isWordChar(*stack.currentFrame->args.subjectPtr))
  1391. RRETURN_NO_MATCH;
  1392. ++stack.currentFrame->args.subjectPtr;
  1393. }
  1394. break;
  1395. default:
  1396. JS_NOT_REACHED("Invalid character type.");
  1397. return matchError(JSRegExpErrorInternal, stack);
  1398. } /* End switch(stack.currentFrame->locals.ctype) */
  1399. }
  1400. /* If min = max, continue at the same level without recursing */
  1401. if (min == stack.currentFrame->locals.max)
  1402. NEXT_OPCODE;
  1403. /* If minimizing, we have to test the rest of the pattern before each
  1404. subsequent match. */
  1405. if (minimize) {
  1406. for (stack.currentFrame->locals.fi = min;; stack.currentFrame->locals.fi++) {
  1407. RECURSIVE_MATCH(48, stack.currentFrame->args.instructionPtr, stack.currentFrame->args.bracketChain);
  1408. if (isMatch)
  1409. RRETURN;
  1410. if (stack.currentFrame->locals.fi >= stack.currentFrame->locals.max || stack.currentFrame->args.subjectPtr >= md.endSubject)
  1411. RRETURN;
  1412. int c = *stack.currentFrame->args.subjectPtr++;
  1413. switch (stack.currentFrame->locals.ctype) {
  1414. case OP_NOT_NEWLINE:
  1415. if (isNewline(c))
  1416. RRETURN;
  1417. break;
  1418. case OP_NOT_DIGIT:
  1419. if (isASCIIDigit(c))
  1420. RRETURN;
  1421. break;
  1422. case OP_DIGIT:
  1423. if (!isASCIIDigit(c))
  1424. RRETURN;
  1425. break;
  1426. case OP_NOT_WHITESPACE:
  1427. if (isSpaceChar(c))
  1428. RRETURN;
  1429. break;
  1430. case OP_WHITESPACE:
  1431. if (!isSpaceChar(c))
  1432. RRETURN;
  1433. break;
  1434. case OP_NOT_WORDCHAR:
  1435. if (isWordChar(c))
  1436. RRETURN;
  1437. break;
  1438. case OP_WORDCHAR:
  1439. if (!isWordChar(c))
  1440. RRETURN;
  1441. break;
  1442. default:
  1443. JS_NOT_REACHED("Invalid character type.");
  1444. return matchError(JSRegExpErrorInternal, stack);
  1445. }
  1446. }
  1447. /* Control never reaches here */
  1448. }
  1449. /* If maximizing it is worth using inline code for speed, doing the type
  1450. test once at the start (i.e. keep it out of the loop). */
  1451. else {
  1452. stack.currentFrame->locals.subjectPtrAtStartOfInstruction = stack.currentFrame->args.subjectPtr; /* Remember where we started */
  1453. switch (stack.currentFrame->locals.ctype) {
  1454. case OP_NOT_NEWLINE:
  1455. for (int i = min; i < stack.currentFrame->locals.max; i++) {
  1456. if (stack.currentFrame->args.subjectPtr >= md.endSubject || isNewline(*stack.currentFrame->args.subjectPtr))
  1457. break;
  1458. stack.currentFrame->args.subjectPtr++;
  1459. }
  1460. break;
  1461. case OP_NOT_DIGIT:
  1462. for (int i = min; i < stack.currentFrame->locals.max; i++) {
  1463. if (stack.currentFrame->args.subjectPtr >= md.endSubject)
  1464. break;
  1465. int c = *stack.currentFrame->args.subjectPtr;
  1466. if (isASCIIDigit(c))
  1467. break;
  1468. ++stack.currentFrame->args.subjectPtr;
  1469. }
  1470. break;
  1471. case OP_DIGIT:
  1472. for (int i = min; i < stack.currentFrame->locals.max; i++) {
  1473. if (stack.currentFrame->args.subjectPtr >= md.endSubject)
  1474. break;
  1475. int c = *stack.currentFrame->args.subjectPtr;
  1476. if (!isASCIIDigit(c))
  1477. break;
  1478. ++stack.currentFrame->args.subjectPtr;
  1479. }
  1480. break;
  1481. case OP_NOT_WHITESPACE:
  1482. for (int i = min; i < stack.currentFrame->locals.max; i++) {
  1483. if (stack.currentFrame->args.subjectPtr >= md.endSubject)
  1484. break;
  1485. int c = *stack.currentFrame->args.subjectPtr;
  1486. if (isSpaceChar(c))
  1487. break;
  1488. ++stack.currentFrame->args.subjectPtr;
  1489. }
  1490. break;
  1491. case OP_WHITESPACE:
  1492. for (int i = min; i < stack.currentFrame->locals.max; i++) {
  1493. if (stack.currentFrame->args.subjectPtr >= md.endSubject)
  1494. break;
  1495. int c = *stack.currentFrame->args.subjectPtr;
  1496. if (!isSpaceChar(c))
  1497. break;
  1498. ++stack.currentFrame->args.subjectPtr;
  1499. }
  1500. break;
  1501. case OP_NOT_WORDCHAR:
  1502. for (int i = min; i < stack.currentFrame->locals.max; i++) {
  1503. if (stack.currentFrame->args.subjectPtr >= md.endSubject)
  1504. break;
  1505. int c = *stack.currentFrame->args.subjectPtr;
  1506. if (isWordChar(c))
  1507. break;
  1508. ++stack.currentFrame->args.subjectPtr;
  1509. }
  1510. break;
  1511. case OP_WORDCHAR:
  1512. for (int i = min; i < stack.currentFrame->locals.max; i++) {
  1513. if (stack.currentFrame->args.subjectPtr >= md.endSubject)
  1514. break;
  1515. int c = *stack.currentFrame->args.subjectPtr;
  1516. if (!isWordChar(c))
  1517. break;
  1518. ++stack.currentFrame->args.subjectPtr;
  1519. }
  1520. break;
  1521. default:
  1522. JS_NOT_REACHED("Invalid character type.");
  1523. return matchError(JSRegExpErrorInternal, stack);
  1524. }
  1525. /* stack.currentFrame->args.subjectPtr is now past the end of the maximum run */
  1526. for (;;) {
  1527. RECURSIVE_MATCH(52, stack.currentFrame->args.instructionPtr, stack.currentFrame->args.bracketChain);
  1528. if (isMatch)
  1529. RRETURN;
  1530. if (stack.currentFrame->args.subjectPtr-- == stack.currentFrame->locals.subjectPtrAtStartOfInstruction)
  1531. break; /* Stop if tried at original pos */
  1532. }
  1533. /* Get here if we can't make it match with any permitted repetitions */
  1534. RRETURN;
  1535. }
  1536. /* Control never reaches here */
  1537. BEGIN_OPCODE(CRMINPLUS):
  1538. BEGIN_OPCODE(CRMINQUERY):
  1539. BEGIN_OPCODE(CRMINRANGE):
  1540. BEGIN_OPCODE(CRMINSTAR):
  1541. BEGIN_OPCODE(CRPLUS):
  1542. BEGIN_OPCODE(CRQUERY):
  1543. BEGIN_OPCODE(CRRANGE):
  1544. BEGIN_OPCODE(CRSTAR):
  1545. JS_NOT_REACHED("Invalid opcode.");
  1546. return matchError(JSRegExpErrorInternal, stack);
  1547. #ifdef USE_COMPUTED_GOTO_FOR_MATCH_OPCODE_LOOP
  1548. CAPTURING_BRACKET:
  1549. #else
  1550. default:
  1551. #endif
  1552. /* Opening capturing bracket. If there is space in the offset vector, save
  1553. the current subject position in the working slot at the top of the vector. We
  1554. mustn't change the current values of the data slot, because they may be set
  1555. from a previous iteration of this group, and be referred to by a reference
  1556. inside the group.
  1557. If the bracket fails to match, we need to restore this value and also the
  1558. values of the final offsets, in case they were set by a previous iteration of
  1559. the same bracket.
  1560. If there isn't enough space in the offset vector, treat this as if it were a
  1561. non-capturing bracket. Don't worry about setting the flag for the error case
  1562. here; that is handled in the code for KET. */
  1563. JS_ASSERT(*stack.currentFrame->args.instructionPtr > OP_BRA);
  1564. LOCALS(number) = *stack.currentFrame->args.instructionPtr - OP_BRA;
  1565. stack.currentFrame->extractBrackets(stack.currentFrame->args.instructionPtr);
  1566. DPRINTF(("opening capturing bracket %d\n", stack.currentFrame->locals.number));
  1567. /* For extended extraction brackets (large number), we have to fish out the
  1568. number from a dummy opcode at the start. */
  1569. if (stack.currentFrame->locals.number > EXTRACT_BASIC_MAX)
  1570. stack.currentFrame->locals.number = get2ByteValue(stack.currentFrame->args.instructionPtr + 4 + LINK_SIZE);
  1571. stack.currentFrame->locals.offset = 2 * stack.currentFrame->locals.number;
  1572. JS_ASSERT_IF(LOCALS(number), LOCALS(minBracket) <= LOCALS(number) && LOCALS(number) < LOCALS(limitBracket));
  1573. if (stack.currentFrame->locals.offset < md.offsetMax) {
  1574. stack.currentFrame->locals.savedSubjectOffset = md.offsetVector[md.offsetEnd - stack.currentFrame->locals.number];
  1575. DPRINTF(("setting subject offset for bracket to %d\n", stack.currentFrame->args.subjectPtr - md.startSubject));
  1576. md.offsetVector[md.offsetEnd - stack.currentFrame->locals.number] = stack.currentFrame->args.subjectPtr - md.startSubject;
  1577. stack.currentFrame->locals.skipBytes = 3; /* For OP_BRAs. */
  1578. /* We must compute this value at the top, before we move the instruction pointer. */
  1579. stack.currentFrame->locals.minSatisfied = minSatNextBracket.readAndClear();
  1580. do {
  1581. /* We need to extract this into a variable so we can correctly pass it by value
  1582. through RECURSIVE_MATCH_NEW_GROUP, which modifies currentFrame. */
  1583. minSatisfied = stack.currentFrame->locals.minSatisfied;
  1584. RECURSIVE_MATCH_NEW_GROUP(1, stack.currentFrame->args.instructionPtr + stack.currentFrame->locals.skipBytes + LINK_SIZE, stack.currentFrame->args.bracketChain, minSatisfied);
  1585. if (isMatch)
  1586. RRETURN;
  1587. stack.currentFrame->locals.skipBytes = 1; /* For OP_ALTs. */
  1588. stack.currentFrame->args.instructionPtr += getLinkValue(stack.currentFrame->args.instructionPtr + 1);
  1589. } while (*stack.currentFrame->args.instructionPtr == OP_ALT);
  1590. DPRINTF(("bracket %d failed\n", stack.currentFrame->locals.number));
  1591. for (size_t i = LOCALS(minBracket); i < size_t(LOCALS(limitBracket)); ++i)
  1592. md.setOffsetPair(i, -1, -1);
  1593. DPRINTF(("restoring subject offset for bracket to %d\n", stack.currentFrame->locals.savedSubjectOffset));
  1594. md.offsetVector[md.offsetEnd - stack.currentFrame->locals.number] = stack.currentFrame->locals.savedSubjectOffset;
  1595. RRETURN;
  1596. }
  1597. /* Insufficient room for saving captured contents */
  1598. goto NON_CAPTURING_BRACKET;
  1599. }
  1600. /* Do not stick any code in here without much thought; it is assumed
  1601. that "continue" in the code above comes out to here to repeat the main
  1602. loop. */
  1603. } /* End of main loop */
  1604. JS_NOT_REACHED("Loop does not fallthru.");
  1605. #ifndef USE_COMPUTED_GOTO_FOR_MATCH_RECURSION
  1606. RRETURN_SWITCH:
  1607. switch (stack.currentFrame->returnLocation) {
  1608. case 0: goto RETURN;
  1609. case 1: goto RRETURN_1;
  1610. case 2: goto RRETURN_2;
  1611. case 6: goto RRETURN_6;
  1612. case 7: goto RRETURN_7;
  1613. case 14: goto RRETURN_14;
  1614. case 15: goto RRETURN_15;
  1615. case 16: goto RRETURN_16;
  1616. case 17: goto RRETURN_17;
  1617. case 18: goto RRETURN_18;
  1618. case 19: goto RRETURN_19;
  1619. case 20: goto RRETURN_20;
  1620. case 21: goto RRETURN_21;
  1621. case 22: goto RRETURN_22;
  1622. case 24: goto RRETURN_24;
  1623. case 26: goto RRETURN_26;
  1624. case 27: goto RRETURN_27;
  1625. case 28: goto RRETURN_28;
  1626. case 29: goto RRETURN_29;
  1627. case 30: goto RRETURN_30;
  1628. case 31: goto RRETURN_31;
  1629. case 38: goto RRETURN_38;
  1630. case 40: goto RRETURN_40;
  1631. case 42: goto RRETURN_42;
  1632. case 44: goto RRETURN_44;
  1633. case 48: goto RRETURN_48;
  1634. case 52: goto RRETURN_52;
  1635. }
  1636. JS_NOT_REACHED("Bad computed return location.");
  1637. return matchError(JSRegExpErrorInternal, stack);
  1638. #endif
  1639. RETURN:
  1640. return isMatch;
  1641. }
  1642. /*************************************************
  1643. * Execute a Regular Expression *
  1644. *************************************************/
  1645. /* This function applies a compiled re to a subject string and picks out
  1646. portions of the string if it matches. Two elements in the vector are set for
  1647. each substring: the offsets to the start and end of the substring.
  1648. Arguments:
  1649. re points to the compiled expression
  1650. extra_data points to extra data or is NULL
  1651. subject points to the subject string
  1652. length length of subject string (may contain binary zeros)
  1653. start_offset where to start in the subject string
  1654. options option bits
  1655. offsets points to a vector of ints to be filled in with offsets
  1656. offsetCount the number of elements in the vector
  1657. Returns: > 0 => success; value is the number of elements filled in
  1658. = 0 => success, but offsets is not big enough
  1659. -1 => failed to match
  1660. < -1 => some kind of unexpected problem
  1661. */
  1662. static void tryFirstByteOptimization(const UChar*& subjectPtr, const UChar* endSubject, int firstByte, bool firstByteIsCaseless, bool useMultiLineFirstCharOptimization, const UChar* originalSubjectStart)
  1663. {
  1664. // If firstByte is set, try scanning to the first instance of that byte
  1665. // no need to try and match against any earlier part of the subject string.
  1666. if (firstByte >= 0) {
  1667. UChar firstChar = firstByte;
  1668. if (firstByteIsCaseless)
  1669. while (subjectPtr < endSubject) {
  1670. int c = *subjectPtr;
  1671. if (c > 127)
  1672. break;
  1673. if (toLowerCase(c) == firstChar)
  1674. break;
  1675. subjectPtr++;
  1676. }
  1677. else {
  1678. while (subjectPtr < endSubject && *subjectPtr != firstChar)
  1679. subjectPtr++;
  1680. }
  1681. } else if (useMultiLineFirstCharOptimization) {
  1682. /* Or to just after \n for a multiline match if possible */
  1683. // I'm not sure why this != originalSubjectStart check is necessary -- ecs 11/18/07
  1684. if (subjectPtr > originalSubjectStart) {
  1685. while (subjectPtr < endSubject && !isNewline(subjectPtr[-1]))
  1686. subjectPtr++;
  1687. }
  1688. }
  1689. }
  1690. static bool tryRequiredByteOptimization(const UChar*& subjectPtr, const UChar* endSubject, int reqByte, int reqByte2, bool reqByteIsCaseless, bool hasFirstByte, const UChar*& reqBytePtr)
  1691. {
  1692. /* If reqByte is set, we know that that character must appear in the subject
  1693. for the match to succeed. If the first character is set, reqByte must be
  1694. later in the subject; otherwise the test starts at the match point. This
  1695. optimization can save a huge amount of backtracking in patterns with nested
  1696. unlimited repeats that aren't going to match. Writing separate code for
  1697. cased/caseless versions makes it go faster, as does using an autoincrement
  1698. and backing off on a match.
  1699. HOWEVER: when the subject string is very, very long, searching to its end can
  1700. take a long time, and give bad performance on quite ordinary patterns. This
  1701. showed up when somebody was matching /^C/ on a 32-megabyte string... so we
  1702. don't do this when the string is sufficiently long.
  1703. */
  1704. if (reqByte >= 0 && endSubject - subjectPtr < REQ_BYTE_MAX) {
  1705. const UChar* p = subjectPtr + (hasFirstByte ? 1 : 0);
  1706. /* We don't need to repeat the search if we haven't yet reached the
  1707. place we found it at last time. */
  1708. if (p > reqBytePtr) {
  1709. if (reqByteIsCaseless) {
  1710. while (p < endSubject) {
  1711. int pp = *p++;
  1712. if (pp == reqByte || pp == reqByte2) {
  1713. p--;
  1714. break;
  1715. }
  1716. }
  1717. } else {
  1718. while (p < endSubject) {
  1719. if (*p++ == reqByte) {
  1720. p--;
  1721. break;
  1722. }
  1723. }
  1724. }
  1725. /* If we can't find the required character, break the matching loop */
  1726. if (p >= endSubject)
  1727. return true;
  1728. /* If we have found the required character, save the point where we
  1729. found it, so that we don't search again next time round the loop if
  1730. the start hasn't passed this character yet. */
  1731. reqBytePtr = p;
  1732. }
  1733. }
  1734. return false;
  1735. }
  1736. int jsRegExpExecute(JSContext *cx, const JSRegExp* re,
  1737. const UChar* subject, int length, int start_offset, int* offsets,
  1738. int offsetCount)
  1739. {
  1740. JS_ASSERT(re);
  1741. JS_ASSERT(subject || !length);
  1742. JS_ASSERT(offsetCount >= 0);
  1743. JS_ASSERT(offsets || offsetCount == 0);
  1744. MatchData matchBlock;
  1745. matchBlock.startSubject = subject;
  1746. matchBlock.endSubject = matchBlock.startSubject + length;
  1747. const UChar* endSubject = matchBlock.endSubject;
  1748. matchBlock.multiline = (re->options & MatchAcrossMultipleLinesOption);
  1749. matchBlock.ignoreCase = (re->options & IgnoreCaseOption);
  1750. /* Use the vector supplied, rounding down its size to a multiple of 3. */
  1751. int ocount = offsetCount - (offsetCount % 3);
  1752. matchBlock.offsetVector = offsets;
  1753. matchBlock.offsetEnd = ocount;
  1754. matchBlock.offsetMax = (2*ocount)/3;
  1755. matchBlock.offsetOverflow = false;
  1756. /* Compute the minimum number of offsets that we need to reset each time. Doing
  1757. this makes a huge difference to execution time when there aren't many brackets
  1758. in the pattern. */
  1759. int resetCount = 2 + re->topBracket * 2;
  1760. if (resetCount > offsetCount)
  1761. resetCount = ocount;
  1762. /* Reset the working variable associated with each extraction. These should
  1763. never be used unless previously set, but they get saved and restored, and so we
  1764. initialize them to avoid reading uninitialized locations. */
  1765. if (matchBlock.offsetVector) {
  1766. int* iptr = matchBlock.offsetVector + ocount;
  1767. int* iend = iptr - resetCount/2 + 1;
  1768. while (--iptr >= iend)
  1769. *iptr = -1;
  1770. }
  1771. /* Set up the first character to match, if available. The firstByte value is
  1772. never set for an anchored regular expression, but the anchoring may be forced
  1773. at run time, so we have to test for anchoring. The first char may be unset for
  1774. an unanchored pattern, of course. If there's no first char and the pattern was
  1775. studied, there may be a bitmap of possible first characters. */
  1776. bool firstByteIsCaseless = false;
  1777. int firstByte = -1;
  1778. if (re->options & UseFirstByteOptimizationOption) {
  1779. firstByte = re->firstByte & 255;
  1780. if ((firstByteIsCaseless = (re->firstByte & REQ_IGNORE_CASE)))
  1781. firstByte = toLowerCase(firstByte);
  1782. }
  1783. /* For anchored or unanchored matches, there may be a "last known required
  1784. character" set. */
  1785. bool reqByteIsCaseless = false;
  1786. int reqByte = -1;
  1787. int reqByte2 = -1;
  1788. if (re->options & UseRequiredByteOptimizationOption) {
  1789. reqByte = re->reqByte & 255;
  1790. reqByteIsCaseless = (re->reqByte & REQ_IGNORE_CASE);
  1791. reqByte2 = flipCase(reqByte);
  1792. }
  1793. /* Loop for handling unanchored repeated matching attempts; for anchored regexs
  1794. the loop runs just once. */
  1795. const UChar* startMatch = subject + start_offset;
  1796. const UChar* reqBytePtr = startMatch - 1;
  1797. bool useMultiLineFirstCharOptimization = re->options & UseMultiLineFirstByteOptimizationOption;
  1798. do {
  1799. /* Reset the maximum number of extractions we might see. */
  1800. if (matchBlock.offsetVector) {
  1801. int* iptr = matchBlock.offsetVector;
  1802. int* iend = iptr + resetCount;
  1803. while (iptr < iend)
  1804. *iptr++ = -1;
  1805. }
  1806. tryFirstByteOptimization(startMatch, endSubject, firstByte, firstByteIsCaseless, useMultiLineFirstCharOptimization, matchBlock.startSubject + start_offset);
  1807. if (tryRequiredByteOptimization(startMatch, endSubject, reqByte, reqByte2, reqByteIsCaseless, firstByte >= 0, reqBytePtr))
  1808. break;
  1809. /* When a match occurs, substrings will be set for all internal extractions;
  1810. we just need to set up the whole thing as substring 0 before returning. If
  1811. there were too many extractions, set the return code to zero. In the case
  1812. where we had to get some local store to hold offsets for backreferences, copy
  1813. those back references that we can. In this case there need not be overflow
  1814. if certain parts of the pattern were not used. */
  1815. /* The code starts after the JSRegExp block and the capture name table. */
  1816. const unsigned char* start_code = (const unsigned char*)(re + 1);
  1817. int returnCode = match(&cx->regExpPool, startMatch, start_code, 2, matchBlock);
  1818. /* When the result is no match, advance the pointer to the next character
  1819. and continue. */
  1820. if (returnCode == 0) {
  1821. startMatch++;
  1822. continue;
  1823. }
  1824. if (returnCode != 1) {
  1825. JS_ASSERT(returnCode == JSRegExpErrorHitLimit);
  1826. DPRINTF((">>>> error: returning %d\n", returnCode));
  1827. return returnCode;
  1828. }
  1829. /* We have a match! */
  1830. returnCode = matchBlock.offsetOverflow ? 0 : matchBlock.endOffsetTop / 2;
  1831. if (offsetCount < 2)
  1832. returnCode = 0;
  1833. else {
  1834. offsets[0] = startMatch - matchBlock.startSubject;
  1835. offsets[1] = matchBlock.endMatchPtr - matchBlock.startSubject;
  1836. }
  1837. JS_ASSERT(returnCode >= 0);
  1838. DPRINTF((">>>> returning %d\n", returnCode));
  1839. return returnCode;
  1840. } while (!(re->options & IsAnchoredOption) && startMatch <= endSubject);
  1841. DPRINTF((">>>> returning PCRE_ERROR_NOMATCH\n"));
  1842. return JSRegExpErrorNoMatch;
  1843. }