3 /**********************************************************************
4 regint.h - Oniguruma (regular expression library)
5 **********************************************************************/
7 * Copyright (c) 2002-2019 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. 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.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 /* #define ONIG_DEBUG_PARSE */
34 /* #define ONIG_DEBUG_COMPILE */
35 /* #define ONIG_DEBUG_SEARCH */
36 /* #define ONIG_DEBUG_MATCH */
37 /* #define ONIG_DONT_OPTIMIZE */
39 /* for byte-code statistical data. */
40 /* #define ONIG_DEBUG_STATISTICS */
42 #if defined(ONIG_DEBUG_PARSE) || defined(ONIG_DEBUG_MATCH) || \
43 defined(ONIG_DEBUG_SEARCH) || defined(ONIG_DEBUG_COMPILE) || \
44 defined(ONIG_DEBUG_STATISTICS)
50 #if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
51 (defined(__ppc__) && defined(__APPLE__)) || \
52 defined(__x86_64) || defined(__x86_64__) || \
54 #define PLATFORM_UNALIGNED_WORD_ACCESS
58 #define USE_GOTO_LABELS_AS_VALUES
65 #define USE_BACKREF_WITH_LEVEL /* \k<name+n>, \k<name-n> */
66 #define USE_STUBBORN_CHECK_CAPTURES_IN_EMPTY_REPEAT /* /(?:()|())*\2/ */
67 #define USE_NEWLINE_AT_END_OF_STRING_HAS_EMPTY_LINE /* /\n$/ =~ "\n" */
68 #define USE_WARNING_REDUNDANT_NESTED_REPEAT_OPERATOR
69 #define USE_RETRY_LIMIT_IN_MATCH
70 #ifdef USE_GOTO_LABELS_AS_VALUES
71 #define USE_THREADED_CODE
72 #define USE_DIRECT_THREADED_CODE
76 #define USE_OP_PUSH_OR_JUMP_EXACT
77 #define USE_QUANT_PEEK_NEXT
78 #define USE_ST_LIBRARY
80 #define USE_WORD_BEGIN_END /* "\<", "\>" */
81 #define USE_CAPTURE_HISTORY
82 #define USE_VARIABLE_META_CHARS
83 #define USE_POSIX_API_REGION_OPTION
84 #define USE_FIND_LONGEST_SEARCH_ALL_OF_RANGE
88 #define INIT_MATCH_STACK_SIZE 160
89 #define DEFAULT_MATCH_STACK_LIMIT_SIZE 0 /* unlimited */
90 #define DEFAULT_RETRY_LIMIT_IN_MATCH 10000000
91 #define DEFAULT_PARSE_DEPTH_LIMIT 4096
94 /* escape other system UChar definition */
95 #ifdef ONIG_ESCAPE_UCHAR_COLLISION
96 #undef ONIG_ESCAPE_UCHAR_COLLISION
99 #define xmalloc malloc
100 #define xrealloc realloc
101 #define xcalloc calloc
104 #define st_init_table onig_st_init_table
105 #define st_init_table_with_size onig_st_init_table_with_size
106 #define st_init_numtable onig_st_init_numtable
107 #define st_init_numtable_with_size onig_st_init_numtable_with_size
108 #define st_init_strtable onig_st_init_strtable
109 #define st_init_strtable_with_size onig_st_init_strtable_with_size
110 #define st_delete onig_st_delete
111 #define st_delete_safe onig_st_delete_safe
112 #define st_insert onig_st_insert
113 #define st_lookup onig_st_lookup
114 #define st_foreach onig_st_foreach
115 #define st_add_direct onig_st_add_direct
116 #define st_free_table onig_st_free_table
117 #define st_cleanup_safe onig_st_cleanup_safe
118 #define st_copy onig_st_copy
119 #define st_nothing_key_clone onig_st_nothing_key_clone
120 #define st_nothing_key_free onig_st_nothing_key_free
122 #define onig_st_is_member st_is_member
124 //#define xmemset memset
125 //#define xmemcpy memcpy
126 //#define xmemmove memmove
128 #if defined(_WIN32) && !defined(__GNUC__)
129 #define xalloca _alloca
130 #define xvsnprintf(buf,size,fmt,args) _vsnprintf_s(buf,size,_TRUNCATE,fmt,args)
131 #define xsnprintf sprintf_s
132 #define xstrcat(dest,src,size) strcat_s(dest,size,src)
134 #define xalloca alloca
135 #define xvsnprintf vsnprintf
136 #define xsnprintf snprintf
137 #define xstrcat(dest,src,size) strcat(dest,src)
149 #if defined(HAVE_ALLOCA_H) && !defined(__GNUC__)
156 #ifdef HAVE_SYS_TYPES_H
158 #include <sys/types.h>
162 #ifdef HAVE_INTTYPES_H
163 #include <inttypes.h>
176 #if defined(_MSC_VER) && (_MSC_VER < 1300)
177 typedef int intptr_t;
178 typedef unsigned int uintptr_t;
182 typedef INTN
intptr_t;
183 typedef UINTN
uintptr_t;
186 #define offsetof OFFSET_OF
196 #define MIN(a,b) (((a)>(b))?(b):(a))
197 #define MAX(a,b) (((a)<(b))?(b):(a))
199 #define IS_NULL(p) (((void*)(p)) == (void*)0)
200 #define IS_NOT_NULL(p) (((void*)(p)) != (void*)0)
201 #define CHECK_NULL_RETURN(p) if (IS_NULL(p)) return NULL
202 #define CHECK_NULL_RETURN_MEMERR(p) if (IS_NULL(p)) return ONIGERR_MEMORY
203 #define NULL_UCHARP ((UChar* )0)
205 #define CHAR_MAP_SIZE 256
206 #define INFINITE_LEN ONIG_INFINITE_DISTANCE
208 #ifdef PLATFORM_UNALIGNED_WORD_ACCESS
210 #define PLATFORM_GET_INC(val,p,type) do{\
212 (p) += sizeof(type);\
217 #define PLATFORM_GET_INC(val,p,type) do{\
218 xmemcpy(&val, (p), sizeof(type));\
219 (p) += sizeof(type);\
222 /* sizeof(OnigCodePoint) */
224 # define WORD_ALIGNMENT_SIZE SIZEOF_SIZE_T
226 # define WORD_ALIGNMENT_SIZE SIZEOF_LONG
229 #define GET_ALIGNMENT_PAD_SIZE(addr,pad_size) do {\
230 (pad_size) = WORD_ALIGNMENT_SIZE - ((uintptr_t )(addr) % WORD_ALIGNMENT_SIZE);\
231 if ((pad_size) == WORD_ALIGNMENT_SIZE) (pad_size) = 0;\
234 #define ALIGNMENT_RIGHT(addr) do {\
235 (addr) += (WORD_ALIGNMENT_SIZE - 1);\
236 (addr) -= ((uintptr_t )(addr) % WORD_ALIGNMENT_SIZE);\
239 #endif /* PLATFORM_UNALIGNED_WORD_ACCESS */
249 const UChar
* tag_start
;
250 const UChar
* tag_end
;
251 OnigCalloutType type
;
252 OnigCalloutFunc start_func
;
253 OnigCalloutFunc end_func
;
262 OnigType types
[ONIG_CALLOUT_MAX_ARGS_NUM
];
263 OnigValue vals
[ONIG_CALLOUT_MAX_ARGS_NUM
];
270 /* stack pop level */
272 STACK_POP_LEVEL_FREE
= 0,
273 STACK_POP_LEVEL_MEM_START
= 1,
274 STACK_POP_LEVEL_ALL
= 2
280 OPTIMIZE_STR
, /* Slow Search */
281 OPTIMIZE_STR_FAST
, /* Sunday quick search / BMH */
282 OPTIMIZE_STR_FAST_STEP_FORWARD
, /* Sunday quick search / BMH */
283 OPTIMIZE_STR_CASE_FOLD_FAST
, /* Sunday quick search / BMH (ignore case) */
284 OPTIMIZE_STR_CASE_FOLD
, /* Slow Search (ignore case) */
285 OPTIMIZE_MAP
/* char map */
289 typedef unsigned int MemStatusType
;
291 #define MEM_STATUS_BITS_NUM (sizeof(MemStatusType) * 8)
292 #define MEM_STATUS_CLEAR(stats) (stats) = 0
293 #define MEM_STATUS_ON_ALL(stats) (stats) = ~((MemStatusType )0)
294 #define MEM_STATUS_AT(stats,n) \
295 ((n) < (int )MEM_STATUS_BITS_NUM ? ((stats) & ((MemStatusType )1 << n)) : ((stats) & 1))
296 #define MEM_STATUS_AT0(stats,n) \
297 ((n) > 0 && (n) < (int )MEM_STATUS_BITS_NUM ? ((stats) & ((MemStatusType )1 << n)) : ((stats) & 1))
299 #define MEM_STATUS_ON(stats,n) do {\
300 if ((n) < (int )MEM_STATUS_BITS_NUM) {\
302 (stats) |= ((MemStatusType )1 << (n));\
308 #define MEM_STATUS_ON_SIMPLE(stats,n) do {\
309 if ((n) < (int )MEM_STATUS_BITS_NUM)\
310 (stats) |= ((MemStatusType )1 << (n));\
314 #define INT_MAX_LIMIT ((1UL << (SIZEOF_INT * 8 - 1)) - 1)
316 #define IS_CODE_WORD_ASCII(enc,code) \
317 (ONIGENC_IS_CODE_ASCII(code) && ONIGENC_IS_CODE_WORD(enc,code))
318 #define IS_CODE_DIGIT_ASCII(enc, code) \
319 (ONIGENC_IS_CODE_ASCII(code) && ONIGENC_IS_CODE_DIGIT(enc,code))
320 #define IS_CODE_XDIGIT_ASCII(enc, code) \
321 (ONIGENC_IS_CODE_ASCII(code) && ONIGENC_IS_CODE_XDIGIT(enc,code))
323 #define DIGITVAL(code) ((code) - '0')
324 #define ODIGITVAL(code) DIGITVAL(code)
325 #define XDIGITVAL(enc,code) \
326 (IS_CODE_DIGIT_ASCII(enc,code) ? DIGITVAL(code) \
327 : (ONIGENC_IS_CODE_UPPER(enc,code) ? (code) - 'A' + 10 : (code) - 'a' + 10))
329 #define IS_SINGLELINE(option) ((option) & ONIG_OPTION_SINGLELINE)
330 #define IS_MULTILINE(option) ((option) & ONIG_OPTION_MULTILINE)
331 #define IS_IGNORECASE(option) ((option) & ONIG_OPTION_IGNORECASE)
332 #define IS_EXTEND(option) ((option) & ONIG_OPTION_EXTEND)
333 #define IS_FIND_LONGEST(option) ((option) & ONIG_OPTION_FIND_LONGEST)
334 #define IS_FIND_NOT_EMPTY(option) ((option) & ONIG_OPTION_FIND_NOT_EMPTY)
335 #define IS_FIND_CONDITION(option) ((option) & \
336 (ONIG_OPTION_FIND_LONGEST | ONIG_OPTION_FIND_NOT_EMPTY))
337 #define IS_NOTBOL(option) ((option) & ONIG_OPTION_NOTBOL)
338 #define IS_NOTEOL(option) ((option) & ONIG_OPTION_NOTEOL)
339 #define IS_POSIX_REGION(option) ((option) & ONIG_OPTION_POSIX_REGION)
341 #define IS_WORD_ASCII(option) \
342 ((option) & (ONIG_OPTION_WORD_IS_ASCII | ONIG_OPTION_POSIX_IS_ASCII))
343 #define IS_DIGIT_ASCII(option) \
344 ((option) & (ONIG_OPTION_DIGIT_IS_ASCII | ONIG_OPTION_POSIX_IS_ASCII))
345 #define IS_SPACE_ASCII(option) \
346 ((option) & (ONIG_OPTION_SPACE_IS_ASCII | ONIG_OPTION_POSIX_IS_ASCII))
347 #define IS_POSIX_ASCII(option) ((option) & ONIG_OPTION_POSIX_IS_ASCII)
349 #define IS_ASCII_MODE_CTYPE_OPTION(ctype, options) \
351 (((ctype) < ONIGENC_CTYPE_ASCII && IS_POSIX_ASCII(options)) ||\
352 ((ctype) == ONIGENC_CTYPE_WORD && IS_WORD_ASCII(options)) ||\
353 ((ctype) == ONIGENC_CTYPE_DIGIT && IS_DIGIT_ASCII(options)) ||\
354 ((ctype) == ONIGENC_CTYPE_SPACE && IS_SPACE_ASCII(options))))
356 #define DISABLE_CASE_FOLD_MULTI_CHAR(case_fold_flag) \
357 ((case_fold_flag) & ~INTERNAL_ONIGENC_CASE_FOLD_MULTI_CHAR)
359 #define INFINITE_REPEAT -1
360 #define IS_INFINITE_REPEAT(n) ((n) == INFINITE_REPEAT)
363 #define BITS_PER_BYTE 8
364 #define SINGLE_BYTE_SIZE (1 << BITS_PER_BYTE)
365 #define BITS_IN_ROOM (sizeof(Bits) * BITS_PER_BYTE)
366 #define BITSET_SIZE (SINGLE_BYTE_SIZE / BITS_IN_ROOM)
368 #ifdef PLATFORM_UNALIGNED_WORD_ACCESS
369 typedef unsigned int Bits
;
371 typedef unsigned char Bits
;
373 typedef Bits BitSet
[BITSET_SIZE
];
374 typedef Bits
* BitSetRef
;
376 #define SIZE_BITSET sizeof(BitSet)
378 #define BITSET_CLEAR(bs) do {\
380 for (i = 0; i < (int )BITSET_SIZE; i++) { (bs)[i] = 0; } \
383 #define BS_ROOM(bs,pos) (bs)[pos / BITS_IN_ROOM]
384 #define BS_BIT(pos) (1 << (pos % BITS_IN_ROOM))
386 #define BITSET_AT(bs, pos) (BS_ROOM(bs,pos) & BS_BIT(pos))
387 #define BITSET_SET_BIT(bs, pos) BS_ROOM(bs,pos) |= BS_BIT(pos)
388 #define BITSET_CLEAR_BIT(bs, pos) BS_ROOM(bs,pos) &= ~(BS_BIT(pos))
389 #define BITSET_INVERT_BIT(bs, pos) BS_ROOM(bs,pos) ^= BS_BIT(pos)
392 typedef struct _BBuf
{
398 #define BB_INIT(buf,size) bbuf_init((BBuf* )(buf), (size))
400 #define BB_SIZE_INC(buf,inc) do{\
401 (buf)->alloc += (inc);\
402 (buf)->p = (UChar* )xrealloc((buf)->p, (buf)->alloc);\
403 if (IS_NULL((buf)->p)) return(ONIGERR_MEMORY);\
406 #define BB_EXPAND(buf,low) do{\
407 unsigned int OldSize_ = (buf)->alloc * sizeof((buf)->p[0]);\
408 do { (buf)->alloc *= 2; } while ((buf)->alloc < (unsigned int )low);\
409 (buf)->p = (UChar* )xrealloc((buf)->p, (buf)->alloc, OldSize_);\
410 if (IS_NULL((buf)->p)) return(ONIGERR_MEMORY);\
413 #define BB_ENSURE_SIZE(buf,size) do{\
414 unsigned int new_alloc = (buf)->alloc;\
415 while (new_alloc < (unsigned int )(size)) { new_alloc *= 2; }\
416 if ((buf)->alloc != new_alloc) {\
417 (buf)->p = (UChar* )xrealloc((buf)->p, new_alloc, (buf)->alloc);\
418 if (IS_NULL((buf)->p)) return(ONIGERR_MEMORY);\
419 (buf)->alloc = new_alloc;\
423 #define BB_WRITE(buf,pos,bytes,n) do{\
424 int used = (pos) + (n);\
425 if ((buf)->alloc < (unsigned int )used) BB_EXPAND((buf),used);\
426 xmemcpy((buf)->p + (pos), (bytes), (n));\
427 if ((buf)->used < (unsigned int )used) (buf)->used = used;\
430 #define BB_WRITE1(buf,pos,byte) do{\
431 int used = (pos) + 1;\
432 if ((buf)->alloc < (unsigned int )used) BB_EXPAND((buf),used);\
433 (buf)->p[(pos)] = (byte);\
434 if ((buf)->used < (unsigned int )used) (buf)->used = used;\
437 #define BB_ADD(buf,bytes,n) BB_WRITE((buf),(buf)->used,(bytes),(n))
438 #define BB_ADD1(buf,byte) BB_WRITE1((buf),(buf)->used,(byte))
439 #define BB_GET_ADD_ADDRESS(buf) ((buf)->p + (buf)->used)
440 #define BB_GET_OFFSET_POS(buf) ((buf)->used)
443 #define BB_MOVE_RIGHT(buf,from,to,n) do {\
444 if ((unsigned int )((to)+(n)) > (buf)->alloc) BB_EXPAND((buf),(to) + (n));\
445 xmemmove((buf)->p + (to), (buf)->p + (from), (n));\
446 if ((unsigned int )((to)+(n)) > (buf)->used) (buf)->used = (to) + (n);\
450 #define BB_MOVE_LEFT(buf,from,to,n) do {\
451 xmemmove((buf)->p + (to), (buf)->p + (from), (n));\
455 #define BB_MOVE_LEFT_REDUCE(buf,from,to) do {\
456 xmemmove((buf)->p + (to), (buf)->p + (from), (buf)->used - (from));\
457 (buf)->used -= (from - to);\
460 #define BB_INSERT(buf,pos,bytes,n) do {\
461 if (pos >= (buf)->used) {\
462 BB_WRITE(buf,pos,bytes,n);\
465 BB_MOVE_RIGHT((buf),(pos),(pos) + (n),((buf)->used - (pos)));\
466 xmemcpy((buf)->p + (pos), (bytes), (n));\
470 #define BB_GET_BYTE(buf, pos) (buf)->p[(pos)]
474 #define ANCR_PREC_READ (1<<0)
475 #define ANCR_PREC_READ_NOT (1<<1)
476 #define ANCR_LOOK_BEHIND (1<<2)
477 #define ANCR_LOOK_BEHIND_NOT (1<<3)
479 #define ANCR_BEGIN_BUF (1<<4)
480 #define ANCR_BEGIN_LINE (1<<5)
481 #define ANCR_BEGIN_POSITION (1<<6)
482 #define ANCR_END_BUF (1<<7)
483 #define ANCR_SEMI_END_BUF (1<<8)
484 #define ANCR_END_LINE (1<<9)
485 #define ANCR_WORD_BOUNDARY (1<<10)
486 #define ANCR_NO_WORD_BOUNDARY (1<<11)
487 #define ANCR_WORD_BEGIN (1<<12)
488 #define ANCR_WORD_END (1<<13)
489 #define ANCR_ANYCHAR_INF (1<<14)
490 #define ANCR_ANYCHAR_INF_ML (1<<15)
491 #define ANCR_TEXT_SEGMENT_BOUNDARY (1<<16)
492 #define ANCR_NO_TEXT_SEGMENT_BOUNDARY (1<<17)
495 #define ANCHOR_HAS_BODY(a) ((a)->type < ANCR_BEGIN_BUF)
497 #define IS_WORD_ANCHOR_TYPE(type) \
498 ((type) == ANCR_WORD_BOUNDARY || (type) == ANCR_NO_WORD_BOUNDARY || \
499 (type) == ANCR_WORD_BEGIN || (type) == ANCR_WORD_END)
503 OP_FINISH
= 0, /* matching process terminator (no more alternative) */
504 OP_END
= 1, /* pattern code terminator (success end) */
506 OP_EXACT1
= 2, /* single byte, N = 1 */
507 OP_EXACT2
, /* single byte, N = 2 */
508 OP_EXACT3
, /* single byte, N = 3 */
509 OP_EXACT4
, /* single byte, N = 4 */
510 OP_EXACT5
, /* single byte, N = 5 */
511 OP_EXACTN
, /* single byte */
512 OP_EXACTMB2N1
, /* mb-length = 2 N = 1 */
513 OP_EXACTMB2N2
, /* mb-length = 2 N = 2 */
514 OP_EXACTMB2N3
, /* mb-length = 2 N = 3 */
515 OP_EXACTMB2N
, /* mb-length = 2 */
516 OP_EXACTMB3N
, /* mb-length = 3 */
517 OP_EXACTMBN
, /* other length */
519 OP_EXACT1_IC
, /* single byte, N = 1, ignore case */
520 OP_EXACTN_IC
, /* single byte, ignore case */
529 OP_ANYCHAR
, /* "." */
530 OP_ANYCHAR_ML
, /* "." multi-line */
531 OP_ANYCHAR_STAR
, /* ".*" */
532 OP_ANYCHAR_ML_STAR
, /* ".*" multi-line */
533 OP_ANYCHAR_STAR_PEEK_NEXT
,
534 OP_ANYCHAR_ML_STAR_PEEK_NEXT
,
545 OP_TEXT_SEGMENT_BOUNDARY
,
560 OP_BACKREF_WITH_LEVEL
, /* \k<xxx+n>, \k<xxx-n> */
561 OP_BACKREF_WITH_LEVEL_IC
, /* \k<xxx+n>, \k<xxx-n> */
562 OP_BACKREF_CHECK
, /* (?(n)), (?('name')) */
563 OP_BACKREF_CHECK_WITH_LEVEL
, /* (?(n-level)), (?('name-level')) */
566 OP_MEMORY_START_PUSH
, /* push back-tracker to stack */
567 OP_MEMORY_END_PUSH
, /* push back-tracker to stack */
568 OP_MEMORY_END_PUSH_REC
, /* push back-tracker to stack */
570 OP_MEMORY_END_REC
, /* push marker to stack */
572 OP_FAIL
, /* pop stack and move */
577 #ifdef USE_OP_PUSH_OR_JUMP_EXACT
578 OP_PUSH_OR_JUMP_EXACT1
, /* if match exact then push, else jump. */
580 OP_PUSH_IF_PEEK_NEXT
, /* if match exact then push, else none. */
581 OP_REPEAT
, /* {n,m} */
582 OP_REPEAT_NG
, /* {n,m}? (non greedy) */
584 OP_REPEAT_INC_NG
, /* non greedy */
585 OP_REPEAT_INC_SG
, /* search and get in stack */
586 OP_REPEAT_INC_NG_SG
, /* search and get in stack (non greedy) */
587 OP_EMPTY_CHECK_START
, /* null loop checker start */
588 OP_EMPTY_CHECK_END
, /* null loop checker end */
589 OP_EMPTY_CHECK_END_MEMST
, /* null loop checker end (with capture status) */
590 OP_EMPTY_CHECK_END_MEMST_PUSH
, /* with capture status and push check-end */
592 OP_PREC_READ_START
, /* (?=...) start */
593 OP_PREC_READ_END
, /* (?=...) end */
594 OP_PREC_READ_NOT_START
, /* (?!...) start */
595 OP_PREC_READ_NOT_END
, /* (?!...) end */
596 OP_ATOMIC_START
, /* (?>...) start */
597 OP_ATOMIC_END
, /* (?>...) end */
598 OP_LOOK_BEHIND
, /* (?<=...) start (no needs end opcode) */
599 OP_LOOK_BEHIND_NOT_START
, /* (?<!...) start */
600 OP_LOOK_BEHIND_NOT_END
, /* (?<!...) end */
602 OP_CALL
, /* \g<name> */
607 OP_CALLOUT_CONTENTS
, /* (?{...}) (?{{...}}) */
608 OP_CALLOUT_NAME
, /* (*name) (*name[tag](args...)) */
613 SAVE_KEEP
= 0, /* SAVE S */
615 SAVE_RIGHT_RANGE
= 2,
619 UPDATE_VAR_KEEP_FROM_STACK_LAST
= 0,
620 UPDATE_VAR_S_FROM_STACK
= 1,
621 UPDATE_VAR_RIGHT_RANGE_FROM_STACK
= 2,
622 UPDATE_VAR_RIGHT_RANGE_FROM_S_STACK
= 3,
623 UPDATE_VAR_RIGHT_RANGE_INIT
= 4,
626 enum TextSegmentBoundaryType
{
627 EXTENDED_GRAPHEME_CLUSTER_BOUNDARY
= 0,
631 typedef int RelAddrType
;
632 typedef int AbsAddrType
;
633 typedef int LengthType
;
634 typedef int RepeatNumType
;
635 typedef int MemNumType
;
636 typedef void* PointerType
;
637 typedef int SaveType
;
638 typedef int UpdateVarType
;
639 typedef int ModeType
;
641 #define SIZE_OPCODE 1
642 #define SIZE_RELADDR sizeof(RelAddrType)
643 #define SIZE_ABSADDR sizeof(AbsAddrType)
644 #define SIZE_LENGTH sizeof(LengthType)
645 #define SIZE_MEMNUM sizeof(MemNumType)
646 #define SIZE_REPEATNUM sizeof(RepeatNumType)
647 #define SIZE_OPTION sizeof(OnigOptionType)
648 #define SIZE_CODE_POINT sizeof(OnigCodePoint)
649 #define SIZE_POINTER sizeof(PointerType)
650 #define SIZE_SAVE_TYPE sizeof(SaveType)
651 #define SIZE_UPDATE_VAR_TYPE sizeof(UpdateVarType)
652 #define SIZE_MODE sizeof(ModeType)
654 #define GET_RELADDR_INC(addr,p) PLATFORM_GET_INC(addr, p, RelAddrType)
655 #define GET_ABSADDR_INC(addr,p) PLATFORM_GET_INC(addr, p, AbsAddrType)
656 #define GET_LENGTH_INC(len,p) PLATFORM_GET_INC(len, p, LengthType)
657 #define GET_MEMNUM_INC(num,p) PLATFORM_GET_INC(num, p, MemNumType)
658 #define GET_REPEATNUM_INC(num,p) PLATFORM_GET_INC(num, p, RepeatNumType)
659 #define GET_OPTION_INC(option,p) PLATFORM_GET_INC(option, p, OnigOptionType)
660 #define GET_POINTER_INC(ptr,p) PLATFORM_GET_INC(ptr, p, PointerType)
661 #define GET_SAVE_TYPE_INC(type,p) PLATFORM_GET_INC(type, p, SaveType)
662 #define GET_UPDATE_VAR_TYPE_INC(type,p) PLATFORM_GET_INC(type, p, UpdateVarType)
663 #define GET_MODE_INC(mode,p) PLATFORM_GET_INC(mode, p, ModeType)
665 /* code point's address must be aligned address. */
666 #define GET_CODE_POINT(code,p) code = *((OnigCodePoint* )(p))
667 #define GET_BYTE_INC(byte,p) do{\
673 /* op-code + arg size */
675 #define SIZE_OP_ANYCHAR_STAR SIZE_OPCODE
676 #define SIZE_OP_ANYCHAR_STAR_PEEK_NEXT (SIZE_OPCODE + 1)
677 #define SIZE_OP_JUMP (SIZE_OPCODE + SIZE_RELADDR)
678 #define SIZE_OP_PUSH (SIZE_OPCODE + SIZE_RELADDR)
679 #define SIZE_OP_PUSH_SUPER (SIZE_OPCODE + SIZE_RELADDR)
680 #define SIZE_OP_POP_OUT SIZE_OPCODE
681 #ifdef USE_OP_PUSH_OR_JUMP_EXACT
682 #define SIZE_OP_PUSH_OR_JUMP_EXACT1 (SIZE_OPCODE + SIZE_RELADDR + 1)
684 #define SIZE_OP_PUSH_IF_PEEK_NEXT (SIZE_OPCODE + SIZE_RELADDR + 1)
685 #define SIZE_OP_REPEAT_INC (SIZE_OPCODE + SIZE_MEMNUM)
686 #define SIZE_OP_REPEAT_INC_NG (SIZE_OPCODE + SIZE_MEMNUM)
687 #define SIZE_OP_WORD_BOUNDARY (SIZE_OPCODE + SIZE_MODE)
688 #define SIZE_OP_PREC_READ_START SIZE_OPCODE
689 #define SIZE_OP_PREC_READ_NOT_START (SIZE_OPCODE + SIZE_RELADDR)
690 #define SIZE_OP_PREC_READ_END SIZE_OPCODE
691 #define SIZE_OP_PREC_READ_NOT_END SIZE_OPCODE
692 #define SIZE_OP_FAIL SIZE_OPCODE
693 #define SIZE_OP_MEMORY_START (SIZE_OPCODE + SIZE_MEMNUM)
694 #define SIZE_OP_MEMORY_START_PUSH (SIZE_OPCODE + SIZE_MEMNUM)
695 #define SIZE_OP_MEMORY_END_PUSH (SIZE_OPCODE + SIZE_MEMNUM)
696 #define SIZE_OP_MEMORY_END_PUSH_REC (SIZE_OPCODE + SIZE_MEMNUM)
697 #define SIZE_OP_MEMORY_END (SIZE_OPCODE + SIZE_MEMNUM)
698 #define SIZE_OP_MEMORY_END_REC (SIZE_OPCODE + SIZE_MEMNUM)
699 #define SIZE_OP_ATOMIC_START SIZE_OPCODE
700 #define SIZE_OP_ATOMIC_END SIZE_OPCODE
701 #define SIZE_OP_EMPTY_CHECK_START (SIZE_OPCODE + SIZE_MEMNUM)
702 #define SIZE_OP_EMPTY_CHECK_END (SIZE_OPCODE + SIZE_MEMNUM)
703 #define SIZE_OP_LOOK_BEHIND (SIZE_OPCODE + SIZE_LENGTH)
704 #define SIZE_OP_LOOK_BEHIND_NOT_START (SIZE_OPCODE + SIZE_RELADDR + SIZE_LENGTH)
705 #define SIZE_OP_LOOK_BEHIND_NOT_END SIZE_OPCODE
706 #define SIZE_OP_CALL (SIZE_OPCODE + SIZE_ABSADDR)
707 #define SIZE_OP_RETURN SIZE_OPCODE
708 #define SIZE_OP_PUSH_SAVE_VAL (SIZE_OPCODE + SIZE_SAVE_TYPE + SIZE_MEMNUM)
709 #define SIZE_OP_UPDATE_VAR (SIZE_OPCODE + SIZE_UPDATE_VAR_TYPE + SIZE_MEMNUM)
712 #define SIZE_OP_CALLOUT_CONTENTS (SIZE_OPCODE + SIZE_MEMNUM)
713 #define SIZE_OP_CALLOUT_NAME (SIZE_OPCODE + SIZE_MEMNUM + SIZE_MEMNUM)
718 /* for relative address increment to go next op. */
719 #define SIZE_INC_OP 1
721 #define SIZE_OP_ANYCHAR_STAR 1
722 #define SIZE_OP_ANYCHAR_STAR_PEEK_NEXT 1
723 #define SIZE_OP_JUMP 1
724 #define SIZE_OP_PUSH 1
725 #define SIZE_OP_PUSH_SUPER 1
726 #define SIZE_OP_POP_OUT 1
727 #ifdef USE_OP_PUSH_OR_JUMP_EXACT
728 #define SIZE_OP_PUSH_OR_JUMP_EXACT1 1
730 #define SIZE_OP_PUSH_IF_PEEK_NEXT 1
731 #define SIZE_OP_REPEAT 1
732 #define SIZE_OP_REPEAT_INC 1
733 #define SIZE_OP_REPEAT_INC_NG 1
734 #define SIZE_OP_WORD_BOUNDARY 1
735 #define SIZE_OP_PREC_READ_START 1
736 #define SIZE_OP_PREC_READ_NOT_START 1
737 #define SIZE_OP_PREC_READ_END 1
738 #define SIZE_OP_PREC_READ_NOT_END 1
739 #define SIZE_OP_BACKREF 1
740 #define SIZE_OP_FAIL 1
741 #define SIZE_OP_MEMORY_START 1
742 #define SIZE_OP_MEMORY_START_PUSH 1
743 #define SIZE_OP_MEMORY_END_PUSH 1
744 #define SIZE_OP_MEMORY_END_PUSH_REC 1
745 #define SIZE_OP_MEMORY_END 1
746 #define SIZE_OP_MEMORY_END_REC 1
747 #define SIZE_OP_ATOMIC_START 1
748 #define SIZE_OP_ATOMIC_END 1
749 #define SIZE_OP_EMPTY_CHECK_START 1
750 #define SIZE_OP_EMPTY_CHECK_END 1
751 #define SIZE_OP_LOOK_BEHIND 1
752 #define SIZE_OP_LOOK_BEHIND_NOT_START 1
753 #define SIZE_OP_LOOK_BEHIND_NOT_END 1
754 #define SIZE_OP_CALL 1
755 #define SIZE_OP_RETURN 1
756 #define SIZE_OP_PUSH_SAVE_VAL 1
757 #define SIZE_OP_UPDATE_VAR 1
760 #define SIZE_OP_CALLOUT_CONTENTS 1
761 #define SIZE_OP_CALLOUT_NAME 1
766 #define MC_ESC(syn) (syn)->meta_char_table.esc
767 #define MC_ANYCHAR(syn) (syn)->meta_char_table.anychar
768 #define MC_ANYTIME(syn) (syn)->meta_char_table.anytime
769 #define MC_ZERO_OR_ONE_TIME(syn) (syn)->meta_char_table.zero_or_one_time
770 #define MC_ONE_OR_MORE_TIME(syn) (syn)->meta_char_table.one_or_more_time
771 #define MC_ANYCHAR_ANYTIME(syn) (syn)->meta_char_table.anychar_anytime
773 #define IS_MC_ESC_CODE(code, syn) \
774 ((code) == MC_ESC(syn) && \
775 !IS_SYNTAX_OP2((syn), ONIG_SYN_OP2_INEFFECTIVE_ESCAPE))
778 #define SYN_POSIX_COMMON_OP \
779 ( ONIG_SYN_OP_DOT_ANYCHAR | ONIG_SYN_OP_POSIX_BRACKET | \
780 ONIG_SYN_OP_DECIMAL_BACKREF | \
781 ONIG_SYN_OP_BRACKET_CC | ONIG_SYN_OP_ASTERISK_ZERO_INF | \
782 ONIG_SYN_OP_LINE_ANCHOR | \
783 ONIG_SYN_OP_ESC_CONTROL_CHARS )
785 #define SYN_GNU_REGEX_OP \
786 ( ONIG_SYN_OP_DOT_ANYCHAR | ONIG_SYN_OP_BRACKET_CC | \
787 ONIG_SYN_OP_POSIX_BRACKET | ONIG_SYN_OP_DECIMAL_BACKREF | \
788 ONIG_SYN_OP_BRACE_INTERVAL | ONIG_SYN_OP_LPAREN_SUBEXP | \
789 ONIG_SYN_OP_VBAR_ALT | \
790 ONIG_SYN_OP_ASTERISK_ZERO_INF | ONIG_SYN_OP_PLUS_ONE_INF | \
791 ONIG_SYN_OP_QMARK_ZERO_ONE | \
792 ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR | ONIG_SYN_OP_ESC_CAPITAL_G_BEGIN_ANCHOR | \
793 ONIG_SYN_OP_ESC_W_WORD | \
794 ONIG_SYN_OP_ESC_B_WORD_BOUND | ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END | \
795 ONIG_SYN_OP_ESC_S_WHITE_SPACE | ONIG_SYN_OP_ESC_D_DIGIT | \
796 ONIG_SYN_OP_LINE_ANCHOR )
798 #define SYN_GNU_REGEX_BV \
799 ( ONIG_SYN_CONTEXT_INDEP_ANCHORS | ONIG_SYN_CONTEXT_INDEP_REPEAT_OPS | \
800 ONIG_SYN_CONTEXT_INVALID_REPEAT_OPS | ONIG_SYN_ALLOW_INVALID_INTERVAL | \
801 ONIG_SYN_BACKSLASH_ESCAPE_IN_CC | ONIG_SYN_ALLOW_DOUBLE_RANGE_OP_IN_CC )
804 #define NCCLASS_FLAGS(cc) ((cc)->flags)
805 #define NCCLASS_FLAG_SET(cc,flag) (NCCLASS_FLAGS(cc) |= (flag))
806 #define NCCLASS_FLAG_CLEAR(cc,flag) (NCCLASS_FLAGS(cc) &= ~(flag))
807 #define IS_NCCLASS_FLAG_ON(cc,flag) ((NCCLASS_FLAGS(cc) & (flag)) != 0)
810 #define FLAG_NCCLASS_NOT (1<<0)
811 #define FLAG_NCCLASS_SHARE (1<<1)
813 #define NCCLASS_SET_NOT(nd) NCCLASS_FLAG_SET(nd, FLAG_NCCLASS_NOT)
814 #define NCCLASS_CLEAR_NOT(nd) NCCLASS_FLAG_CLEAR(nd, FLAG_NCCLASS_NOT)
815 #define IS_NCCLASS_NOT(nd) IS_NCCLASS_FLAG_ON(nd, FLAG_NCCLASS_NOT)
819 #ifdef USE_DIRECT_THREADED_CODE
826 UChar s
[16]; /* Now used first 7 bytes only. */
830 LengthType n
; /* number of chars */
831 } exact_n
; /* EXACTN, EXACTN_IC, EXACTMB2N, EXACTMB3N */
834 LengthType n
; /* number of chars */
835 LengthType len
; /* char byte length */
836 } exact_len_n
; /* EXACTMBN */
844 void* mb
; /* mb must be same position with cclass_mb for match_at(). */
849 } anychar_star_peek_next
;
852 } word_boundary
; /* OP_WORD_BOUNDARY, OP_NO_WORD_BOUNDARY, OP_WORD_BEGIN, OP_WORD_END */
854 enum TextSegmentBoundaryType type
;
856 } text_segment_boundary
;
859 MemNumType n1
; /* num == 1 */
860 MemNumType
* ns
; /* num > 1 */
864 } backref_general
; /* BACKREF_MULTI, BACKREF_MULTI_IC, BACKREF_WITH_LEVEL, BACKREF_CHECK, BACKREF_CHECK_WITH_LEVEL, */
867 } backref_n
; /* BACKREF_N, BACKREF_N_IC */
870 } memory_start
; /* MEMORY_START, MEMORY_START_PUSH */
873 } memory_end
; /* MEMORY_END, MEMORY_END_REC, MEMORY_END_PUSH, MEMORY_END_PUSH_REC */
883 } push_or_jump_exact1
;
891 } repeat
; /* REPEAT, REPEAT_NG */
894 } repeat_inc
; /* REPEAT_INC, REPEAT_INC_SG, REPEAT_INC_NG, REPEAT_INC_NG_SG */
900 } empty_check_end
; /* EMPTY_CHECK_END, EMPTY_CHECK_END_MEMST, EMPTY_CHECK_END_MEMST_PUSH */
903 } prec_read_not_start
;
910 } look_behind_not_start
;
935 const UChar
* pattern
;
936 const UChar
* pattern_end
;
940 int callout_list_alloc
;
941 CalloutListEntry
* callout_list
; /* index: callout num */
945 struct re_pattern_buffer
{
946 /* common members of BBuf(bytes-buffer) */
948 #ifdef USE_DIRECT_THREADED_CODE
952 unsigned int ops_used
; /* used space for ops */
953 unsigned int ops_alloc
; /* allocated space for ops */
954 unsigned char* string_pool
;
955 unsigned char* string_pool_end
;
957 int num_mem
; /* used memory(...) num counted from 1 */
958 int num_repeat
; /* OP_REPEAT/OP_REPEAT_NG id-counter */
959 int num_null_check
; /* OP_EMPTY_CHECK_START/END id counter */
960 int num_call
; /* number of subexp call */
961 unsigned int capture_history
; /* (?@...) flag (1-31) */
962 unsigned int bt_mem_start
; /* need backtrack flag */
963 unsigned int bt_mem_end
; /* need backtrack flag */
965 int repeat_range_alloc
;
966 OnigRepeatRange
* repeat_range
;
969 OnigOptionType options
;
970 OnigSyntaxType
* syntax
;
971 OnigCaseFoldType case_fold_flag
;
974 /* optimization info (string search, char-map and anchors) */
975 int optimize
; /* optimize flag */
976 int threshold_len
; /* search str-length for apply optimize */
977 int anchor
; /* BEGIN_BUF, BEGIN_POS, (SEMI_)END_BUF */
978 OnigLen anchor_dmin
; /* (SEMI_)END_BUF anchor distance */
979 OnigLen anchor_dmax
; /* (SEMI_)END_BUF anchor distance */
980 int sub_anchor
; /* start-anchor for exact or map */
981 unsigned char *exact
;
982 unsigned char *exact_end
;
983 unsigned char map
[CHAR_MAP_SIZE
]; /* used as BMH skip or char-map */
985 OnigLen dmin
; /* min-distance of exact or map */
986 OnigLen dmax
; /* max-distance of exact or map */
990 #define COP(reg) ((reg)->ops_curr)
991 #define COP_CURR_OFFSET(reg) ((reg)->ops_used - 1)
992 #define COP_CURR_OFFSET_BYTES(reg, p) \
993 ((int )((char* )(&((reg)->ops_curr->p)) - (char* )((reg)->ops)))
996 extern void onig_add_end_call(void (*func
)(void));
1000 #ifdef ONIG_DEBUG_COMPILE
1001 extern void onig_print_compiled_byte_code_list(FILE* f
, regex_t
* reg
);
1004 #ifdef ONIG_DEBUG_STATISTICS
1005 extern void onig_statistics_init
P_((void));
1006 extern int onig_print_statistics
P_((FILE* f
));
1010 extern void onig_warning(const char* s
);
1011 extern UChar
* onig_error_code_to_format
P_((int code
));
1012 extern void EFIAPI onig_snprintf_with_pattern
PV_((UChar buf
[], int bufsize
, OnigEncoding enc
, UChar
* pat
, UChar
* pat_end
, const UChar
*fmt
, ...));
1013 extern int onig_compile
P_((regex_t
* reg
, const UChar
* pattern
, const UChar
* pattern_end
, OnigErrorInfo
* einfo
));
1014 extern int onig_is_code_in_cc_len
P_((int enclen
, OnigCodePoint code
, void* /* CClassNode* */ cc
));
1015 extern RegexExt
* onig_get_regex_ext(regex_t
* reg
);
1016 extern int onig_ext_set_pattern(regex_t
* reg
, const UChar
* pattern
, const UChar
* pattern_end
);
1017 extern int onig_positive_int_multiply(int x
, int y
);
1021 extern OnigCalloutType
onig_get_callout_type_by_name_id(int name_id
);
1022 extern OnigCalloutFunc
onig_get_callout_start_func_by_name_id(int id
);
1023 extern OnigCalloutFunc
onig_get_callout_end_func_by_name_id(int id
);
1024 extern int onig_callout_tag_table_free(void* table
);
1025 extern void onig_free_reg_callout_list(int n
, CalloutListEntry
* list
);
1026 extern CalloutListEntry
* onig_reg_callout_list_at(regex_t
* reg
, int num
);
1027 extern OnigCalloutFunc
onig_get_callout_start_func(regex_t
* reg
, int callout_num
);
1029 /* for definition of builtin callout */
1030 #define BC0_P(name, func) do {\
1031 int len = onigenc_str_bytelen_null(enc, (UChar* )name);\
1032 id = onig_set_callout_of_name(enc, ONIG_CALLOUT_TYPE_SINGLE,\
1033 (UChar* )(name), (UChar* )((name) + len),\
1034 ONIG_CALLOUT_IN_PROGRESS,\
1035 onig_builtin_ ## func, 0, 0, 0, 0, 0);\
1036 if (id < 0) return id;\
1039 #define BC0_R(name, func) do {\
1040 int len = onigenc_str_bytelen_null(enc, (UChar* )name);\
1041 id = onig_set_callout_of_name(enc, ONIG_CALLOUT_TYPE_SINGLE,\
1042 (UChar* )(name), (UChar* )((name) + len),\
1043 ONIG_CALLOUT_IN_RETRACTION,\
1044 onig_builtin_ ## func, 0, 0, 0, 0, 0);\
1045 if (id < 0) return id;\
1048 #define BC0_B(name, func) do {\
1049 int len = onigenc_str_bytelen_null(enc, (UChar* )name);\
1050 id = onig_set_callout_of_name(enc, ONIG_CALLOUT_TYPE_SINGLE,\
1051 (UChar* )(name), (UChar* )((name) + len),\
1052 ONIG_CALLOUT_IN_BOTH,\
1053 onig_builtin_ ## func, 0, 0, 0, 0, 0);\
1054 if (id < 0) return id;\
1057 #define BC_P(name, func, na, ts) do {\
1058 int len = onigenc_str_bytelen_null(enc, (UChar* )name);\
1059 id = onig_set_callout_of_name(enc, ONIG_CALLOUT_TYPE_SINGLE,\
1060 (UChar* )(name), (UChar* )((name) + len),\
1061 ONIG_CALLOUT_IN_PROGRESS,\
1062 onig_builtin_ ## func, 0, (na), (ts), 0, 0); \
1063 if (id < 0) return id;\
1066 #define BC_P_O(name, func, nts, ts, nopts, opts) do {\
1067 int len = onigenc_str_bytelen_null(enc, (UChar* )name);\
1068 id = onig_set_callout_of_name(enc, ONIG_CALLOUT_TYPE_SINGLE,\
1069 (UChar* )(name), (UChar* )((name) + len),\
1070 ONIG_CALLOUT_IN_PROGRESS,\
1071 onig_builtin_ ## func, 0, (nts), (ts), (nopts), (opts));\
1072 if (id < 0) return id;\
1075 #define BC_B(name, func, na, ts) do {\
1076 int len = onigenc_str_bytelen_null(enc, (UChar* )name);\
1077 id = onig_set_callout_of_name(enc, ONIG_CALLOUT_TYPE_SINGLE,\
1078 (UChar* )(name), (UChar* )((name) + len),\
1079 ONIG_CALLOUT_IN_BOTH,\
1080 onig_builtin_ ## func, 0, (na), (ts), 0, 0);\
1081 if (id < 0) return id;\
1084 #define BC_B_O(name, func, nts, ts, nopts, opts) do {\
1085 int len = onigenc_str_bytelen_null(enc, (UChar* )name);\
1086 id = onig_set_callout_of_name(enc, ONIG_CALLOUT_TYPE_SINGLE,\
1087 (UChar* )(name), (UChar* )((name) + len),\
1088 ONIG_CALLOUT_IN_BOTH,\
1089 onig_builtin_ ## func, 0, (nts), (ts), (nopts), (opts));\
1090 if (id < 0) return id;\
1093 #endif /* USE_CALLOUT */
1097 typedef void hash_table_type
;
1100 # include <windows.h>
1101 typedef ULONG_PTR hash_data_type
;
1103 //typedef unsigned long hash_data_type;
1106 typedef UINTN hash_data_type
;
1108 extern hash_table_type
* onig_st_init_strend_table_with_size
P_((int size
));
1109 extern int onig_st_lookup_strend
P_((hash_table_type
* table
, const UChar
* str_key
, const UChar
* end_key
, hash_data_type
*value
));
1110 extern int onig_st_insert_strend
P_((hash_table_type
* table
, const UChar
* str_key
, const UChar
* end_key
, hash_data_type value
));
1112 typedef int (*ONIGENC_INIT_PROPERTY_LIST_FUNC_TYPE
)(void);
1114 #endif /* REGINT_H */
1115 #include <ProcessorBind.h>
1116 #define INT_MAX MAX_INTN
1117 #define LONG_MAX MAX_UINTN