Back to index

php5  5.3.10
regint.h
Go to the documentation of this file.
00001 #ifndef REGINT_H
00002 #define REGINT_H
00003 /**********************************************************************
00004   regint.h -  Oniguruma (regular expression library)
00005 **********************************************************************/
00006 /*-
00007  * Copyright (c) 2002-2007  K.Kosako  <sndgk393 AT ybb DOT ne DOT jp>
00008  * All rights reserved.
00009  *
00010  * Redistribution and use in source and binary forms, with or without
00011  * modification, are permitted provided that the following conditions
00012  * are met:
00013  * 1. Redistributions of source code must retain the above copyright
00014  *    notice, this list of conditions and the following disclaimer.
00015  * 2. Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in the
00017  *    documentation and/or other materials provided with the distribution.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00020  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00023  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00024  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00025  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00026  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00027  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00028  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00029  * SUCH DAMAGE.
00030  */
00031 
00032 /* for debug */
00033 /* #define ONIG_DEBUG_PARSE_TREE */
00034 /* #define ONIG_DEBUG_COMPILE */
00035 /* #define ONIG_DEBUG_SEARCH */
00036 /* #define ONIG_DEBUG_MATCH */
00037 /* #define ONIG_DONT_OPTIMIZE */
00038 
00039 /* for byte-code statistical data. */
00040 /* #define ONIG_DEBUG_STATISTICS */
00041 
00042 #if defined(ONIG_DEBUG_PARSE_TREE) || defined(ONIG_DEBUG_MATCH) || \
00043     defined(ONIG_DEBUG_SEARCH) || defined(ONIG_DEBUG_COMPILE) || \
00044     defined(ONIG_DEBUG_STATISTICS)
00045 #ifndef ONIG_DEBUG
00046 #define ONIG_DEBUG
00047 #endif
00048 #endif
00049 
00050 #if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
00051     (defined(__ppc__) && defined(__APPLE__)) || \
00052     defined(__x86_64) || defined(__x86_64__) || \
00053     defined(__mc68020__)
00054 #define PLATFORM_UNALIGNED_WORD_ACCESS
00055 #endif
00056 
00057 /* config */
00058 /* spec. config */
00059 /* #define USE_UNICODE_FULL_RANGE_CTYPE */ /* --> move to regenc.h */
00060 #define USE_NAMED_GROUP
00061 #define USE_SUBEXP_CALL
00062 #define USE_INFINITE_REPEAT_MONOMANIAC_MEM_STATUS_CHECK /* /(?:()|())*\2/ */
00063 #define USE_NEWLINE_AT_END_OF_STRING_HAS_EMPTY_LINE     /* /\n$/ =~ "\n" */
00064 #define USE_WARNING_REDUNDANT_NESTED_REPEAT_OPERATOR
00065 /* #define USE_RECOMPILE_API */
00066 /* treat \r\n as line terminator.
00067    !!! NO SUPPORT !!!
00068    use this configuration on your own responsibility */
00069 /* #define USE_CRNL_AS_LINE_TERMINATOR */
00070 
00071 /* internal config */
00072 #define USE_RECYCLE_NODE
00073 #define USE_OP_PUSH_OR_JUMP_EXACT
00074 #define USE_QUANTIFIER_PEEK_NEXT
00075 #define USE_ST_HASH_TABLE
00076 #define USE_SHARED_CCLASS_TABLE
00077 
00078 #define INIT_MATCH_STACK_SIZE                     160
00079 #define DEFAULT_MATCH_STACK_LIMIT_SIZE              0 /* unlimited */
00080 
00081 /* interface to external system */
00082 #ifdef NOT_RUBY      /* given from Makefile */
00083 #include "config.h"
00084 #define USE_BACKREF_AT_LEVEL
00085 #define USE_CAPTURE_HISTORY
00086 #define USE_VARIABLE_META_CHARS
00087 #define USE_WORD_BEGIN_END          /* "<": word-begin, ">": word-end */
00088 #define USE_POSIX_REGION_OPTION     /* needed for POSIX API support */
00089 #define USE_FIND_LONGEST_SEARCH_ALL_OF_RANGE
00090 /* #define USE_COMBINATION_EXPLOSION_CHECK */     /* (X*)* */
00091 /* #define USE_MULTI_THREAD_SYSTEM */
00092 #define THREAD_SYSTEM_INIT          /* depend on thread system */
00093 #define THREAD_SYSTEM_END           /* depend on thread system */
00094 #define THREAD_ATOMIC_START         /* depend on thread system */
00095 #define THREAD_ATOMIC_END           /* depend on thread system */
00096 #define THREAD_PASS                 /* depend on thread system */
00097 #define xmalloc     malloc
00098 #define xrealloc    realloc
00099 #define xcalloc     calloc
00100 #define xfree       free
00101 #else
00102 #include "ruby.h"
00103 #include "rubysig.h"      /* for DEFER_INTS, ENABLE_INTS */
00104 
00105 #define USE_COMBINATION_EXPLOSION_CHECK        /* (X*)* */
00106 #define USE_MULTI_THREAD_SYSTEM
00107 #define THREAD_SYSTEM_INIT
00108 #define THREAD_SYSTEM_END
00109 #define THREAD_ATOMIC_START          DEFER_INTS
00110 #define THREAD_ATOMIC_END            ENABLE_INTS
00111 #define THREAD_PASS                  rb_thread_schedule()
00112 
00113 #define DEFAULT_WARN_FUNCTION        onig_rb_warn
00114 #define DEFAULT_VERB_WARN_FUNCTION   onig_rb_warning
00115 
00116 #endif /* else NOT_RUBY */
00117 
00118 #define STATE_CHECK_STRING_THRESHOLD_LEN             7
00119 #define STATE_CHECK_BUFF_MAX_SIZE               0x4000
00120 
00121 #define THREAD_PASS_LIMIT_COUNT     8
00122 #define xmemset     memset
00123 #define xmemcpy     memcpy
00124 #define xmemmove    memmove
00125 #if defined(_WIN32) && !defined(__GNUC__)
00126 #define xalloca     _alloca
00127 #if _MSC_VER < 1500
00128 #ifndef vsnprintf
00129 #define vsnprintf   _vsnprintf
00130 #endif
00131 #endif
00132 #else
00133 #define xalloca     alloca
00134 #endif
00135 
00136 #if defined(USE_RECOMPILE_API) && defined(USE_MULTI_THREAD_SYSTEM)
00137 #define ONIG_STATE_INC(reg) (reg)->state++
00138 #define ONIG_STATE_DEC(reg) (reg)->state--
00139 
00140 #define ONIG_STATE_INC_THREAD(reg) do {\
00141   THREAD_ATOMIC_START;\
00142   (reg)->state++;\
00143   THREAD_ATOMIC_END;\
00144 } while(0)
00145 #define ONIG_STATE_DEC_THREAD(reg) do {\
00146   THREAD_ATOMIC_START;\
00147   (reg)->state--;\
00148   THREAD_ATOMIC_END;\
00149 } while(0)
00150 #else
00151 #define ONIG_STATE_INC(reg)         /* Nothing */
00152 #define ONIG_STATE_DEC(reg)         /* Nothing */
00153 #define ONIG_STATE_INC_THREAD(reg)  /* Nothing */
00154 #define ONIG_STATE_DEC_THREAD(reg)  /* Nothing */
00155 #endif /* USE_RECOMPILE_API && USE_MULTI_THREAD_SYSTEM */
00156 
00157 
00158 #define onig_st_is_member              st_is_member
00159 
00160 #ifdef NOT_RUBY
00161 
00162 #define st_init_table                  onig_st_init_table
00163 #define st_init_table_with_size        onig_st_init_table_with_size
00164 #define st_init_numtable               onig_st_init_numtable
00165 #define st_init_numtable_with_size     onig_st_init_numtable_with_size
00166 #define st_init_strtable               onig_st_init_strtable
00167 #define st_init_strtable_with_size     onig_st_init_strtable_with_size
00168 #define st_init_strend_table_with_size onig_st_init_strend_table_with_size
00169 #define st_delete                      onig_st_delete
00170 #define st_delete_safe                 onig_st_delete_safe
00171 #define st_insert                      onig_st_insert
00172 #define st_insert_strend               onig_st_insert_strend
00173 #define st_lookup                      onig_st_lookup
00174 #define st_lookup_strend               onig_st_lookup_strend
00175 #define st_foreach                     onig_st_foreach
00176 #define st_add_direct                  onig_st_add_direct
00177 #define st_add_direct_strend           onig_st_add_direct_strend
00178 #define st_free_table                  onig_st_free_table
00179 #define st_cleanup_safe                onig_st_cleanup_safe
00180 #define st_copy                        onig_st_copy
00181 #define st_nothing_key_clone           onig_st_nothing_key_clone
00182 #define st_nothing_key_free            onig_st_nothing_key_free
00183 
00184 #else /* NOT_RUBY */
00185 
00186 #define onig_st_init_table                  st_init_table
00187 #define onig_st_init_table_with_size        st_init_table_with_size
00188 #define onig_st_init_numtable               st_init_numtable
00189 #define onig_st_init_numtable_with_size     st_init_numtable_with_size
00190 #define onig_st_init_strtable               st_init_strtable
00191 #define onig_st_init_strtable_with_size     st_init_strtable_with_size
00192 #define onig_st_init_strend_table_with_size st_init_strend_table_with_size
00193 #define onig_st_delete                      st_delete
00194 #define onig_st_delete_safe                 st_delete_safe
00195 #define onig_st_insert                      st_insert
00196 #define onig_st_insert_strend               st_insert_strend
00197 #define onig_st_lookup                      st_lookup
00198 #define onig_st_lookup_strend               st_lookup_strend
00199 #define onig_st_foreach                     st_foreach
00200 #define onig_st_add_direct                  st_add_direct
00201 #define onig_st_add_direct_strend           st_add_direct_strend
00202 #define onig_st_free_table                  st_free_table
00203 #define onig_st_cleanup_safe                st_cleanup_safe
00204 #define onig_st_copy                        st_copy
00205 #define onig_st_nothing_key_clone           st_nothing_key_clone
00206 #define onig_st_nothing_key_free            st_nothing_key_free
00207 
00208 #endif /* NOT_RUBY */
00209 
00210 
00211 #ifdef HAVE_STDLIB_H
00212 #include <stdlib.h>
00213 #endif
00214 
00215 #if defined(HAVE_ALLOCA_H) && !defined(__GNUC__)
00216 #include <alloca.h>
00217 #endif
00218 
00219 #ifdef HAVE_STRING_H
00220 # include <string.h>
00221 #else
00222 # include <strings.h>
00223 #endif
00224 
00225 #include <ctype.h>
00226 #ifdef HAVE_SYS_TYPES_H
00227 #ifndef __BORLANDC__
00228 #include <sys/types.h>
00229 #endif
00230 #endif
00231 
00232 #ifdef __BORLANDC__
00233 #include <malloc.h>
00234 #endif
00235 
00236 #ifdef ONIG_DEBUG
00237 # include <stdio.h>
00238 #endif
00239 
00240 #include "regenc.h"
00241 #include "oniguruma.h"
00242 
00243 #ifdef MIN
00244 #undef MIN
00245 #endif
00246 #ifdef MAX
00247 #undef MAX
00248 #endif
00249 #define MIN(a,b) (((a)>(b))?(b):(a))
00250 #define MAX(a,b) (((a)<(b))?(b):(a))
00251 
00252 #define IS_NULL(p)                    (((void*)(p)) == (void*)0)
00253 #define IS_NOT_NULL(p)                (((void*)(p)) != (void*)0)
00254 #define CHECK_NULL_RETURN(p)          if (IS_NULL(p)) return NULL
00255 #define CHECK_NULL_RETURN_VAL(p,val)  if (IS_NULL(p)) return (val)
00256 #define NULL_UCHARP                   ((UChar* )0)
00257 
00258 #ifndef PLATFORM_UNALIGNED_WORD_ACCESS
00259 /* sizeof(OnigCodePoint) */
00260 #define WORD_ALIGNMENT_SIZE     SIZEOF_LONG
00261 
00262 #define GET_ALIGNMENT_PAD_SIZE(addr,pad_size) do {\
00263   (pad_size) = WORD_ALIGNMENT_SIZE \
00264                - ((unsigned int )(addr) % WORD_ALIGNMENT_SIZE);\
00265   if ((pad_size) == WORD_ALIGNMENT_SIZE) (pad_size) = 0;\
00266 } while (0)
00267 
00268 #define ALIGNMENT_RIGHT(addr) do {\
00269   (addr) += (WORD_ALIGNMENT_SIZE - 1);\
00270   (addr) -= ((unsigned int )(addr) % WORD_ALIGNMENT_SIZE);\
00271 } while (0)
00272 
00273 
00274 #define B_SHIFT  8
00275 #define B_MASK   0xff
00276 
00277 #define SERIALIZE_2BYTE_INT(i,p) do {\
00278   *(p)     = ((i) >> B_SHIFT) & B_MASK;\
00279   *((p)+1) = (i) & B_MASK;\
00280 } while (0)
00281 
00282 #define SERIALIZE_4BYTE_INT(i,p) do {\
00283   *(p)     = ((i) >> B_SHIFT*3) & B_MASK;\
00284   *((p)+1) = ((i) >> B_SHIFT*2) & B_MASK;\
00285   *((p)+2) = ((i) >> B_SHIFT  ) & B_MASK;\
00286   *((p)+3) = (i) & B_MASK;\
00287 } while (0)
00288 
00289 #define SERIALIZE_8BYTE_INT(i,p) do {\
00290   *(p)     = ((i) >> B_SHIFT*7) & B_MASK;\
00291   *((p)+1) = ((i) >> B_SHIFT*6) & B_MASK;\
00292   *((p)+2) = ((i) >> B_SHIFT*5) & B_MASK;\
00293   *((p)+3) = ((i) >> B_SHIFT*4) & B_MASK;\
00294   *((p)+4) = ((i) >> B_SHIFT*3) & B_MASK;\
00295   *((p)+5) = ((i) >> B_SHIFT*2) & B_MASK;\
00296   *((p)+6) = ((i) >> B_SHIFT  ) & B_MASK;\
00297   *((p)+7) = (i) & B_MASK;\
00298 } while (0)
00299 
00300 #define GET_2BYTE_INT_INC(type,i,p) do {\
00301   (i) = (type )(((unsigned int )(*(p)) << B_SHIFT) | (unsigned int )((p)[1]));\
00302   (p) += 2;\
00303 } while (0)
00304 
00305 #define GET_4BYTE_INT_INC(type,i,p) do {\
00306   (i) = (type )(((unsigned int )((p)[0]) << B_SHIFT*3) | \
00307               ((unsigned int )((p)[1]) << B_SHIFT*2) | \
00308               ((unsigned int )((p)[2]) << B_SHIFT  ) | \
00309               ((unsigned int )((p)[3])             )); \
00310   (p) += 4;\
00311 } while (0)
00312 
00313 #define GET_8BYTE_INT_INC(type,i,p) do {\
00314   (i) = (type )(((unsigned long )((p)[0]) << B_SHIFT*7) | \
00315               ((unsigned long )((p)[1]) << B_SHIFT*6) | \
00316               ((unsigned long )((p)[2]) << B_SHIFT*5) | \
00317               ((unsigned long )((p)[3]) << B_SHIFT*4) | \
00318               ((unsigned long )((p)[4]) << B_SHIFT*3) | \
00319               ((unsigned long )((p)[5]) << B_SHIFT*2) | \
00320               ((unsigned long )((p)[6]) << B_SHIFT  ) | \
00321               ((unsigned long )((p)[7])             )); \
00322   (p) += 8;\
00323 } while (0)
00324 
00325 #if SIZEOF_SHORT == 2
00326 #define GET_SHORT_INC(i,p)     GET_2BYTE_INT_INC(short,i,p)
00327 #define SERIALIZE_SHORT(i,p)   SERIALIZE_2BYTE_INT(i,p)
00328 #elif SIZEOF_SHORT == 4
00329 #define GET_SHORT_INC(i,p)     GET_4BYTE_INT_INC(short,i,p)
00330 #define SERIALIZE_SHORT(i,p)   SERIALIZE_4BYTE_INT(i,p)
00331 #elif SIZEOF_SHORT == 8
00332 #define GET_SHORT_INC(i,p)     GET_8BYTE_INT_INC(short,i,p)
00333 #define SERIALIZE_SHORT(i,p)   SERIALIZE_8BYTE_INT(i,p)
00334 #endif
00335 
00336 #if SIZEOF_INT == 2
00337 #define GET_INT_INC(i,p)       GET_2BYTE_INT_INC(int,i,p)
00338 #define GET_UINT_INC(i,p)      GET_2BYTE_INT_INC(unsigned,i,p)
00339 #define SERIALIZE_INT(i,p)     SERIALIZE_2BYTE_INT(i,p)
00340 #define SERIALIZE_UINT(i,p)    SERIALIZE_2BYTE_INT(i,p)
00341 #elif SIZEOF_INT == 4
00342 #define GET_INT_INC(i,p)       GET_4BYTE_INT_INC(int,i,p)
00343 #define GET_UINT_INC(i,p)      GET_4BYTE_INT_INC(unsigned,i,p)
00344 #define SERIALIZE_INT(i,p)     SERIALIZE_4BYTE_INT(i,p)
00345 #define SERIALIZE_UINT(i,p)    SERIALIZE_4BYTE_INT(i,p)
00346 #elif SIZEOF_INT == 8
00347 #define GET_INT_INC(i,p)       GET_8BYTE_INT_INC(int,i,p)
00348 #define GET_UINT_INC(i,p)      GET_8BYTE_INT_INC(unsigned,i,p)
00349 #define SERIALIZE_INT(i,p)     SERIALIZE_8BYTE_INT(i,p)
00350 #define SERIALIZE_UINT(i,p)    SERIALIZE_8BYTE_INT(i,p)
00351 #endif
00352 
00353 #endif /* PLATFORM_UNALIGNED_WORD_ACCESS */
00354 
00355 /* stack pop level */
00356 #define STACK_POP_LEVEL_FREE        0
00357 #define STACK_POP_LEVEL_MEM_START   1
00358 #define STACK_POP_LEVEL_ALL         2
00359 
00360 /* optimize flags */
00361 #define ONIG_OPTIMIZE_NONE              0
00362 #define ONIG_OPTIMIZE_EXACT             1   /* Slow Search */
00363 #define ONIG_OPTIMIZE_EXACT_BM          2   /* Boyer Moore Search */
00364 #define ONIG_OPTIMIZE_EXACT_BM_NOT_REV  3   /* BM   (but not simple match) */
00365 #define ONIG_OPTIMIZE_EXACT_IC          4   /* Slow Search (ignore case) */
00366 #define ONIG_OPTIMIZE_MAP               5   /* char map */
00367 
00368 /* bit status */
00369 typedef unsigned int  BitStatusType;
00370 
00371 #define BIT_STATUS_BITS_NUM          (sizeof(BitStatusType) * 8)
00372 #define BIT_STATUS_CLEAR(stats)      (stats) = 0
00373 #define BIT_STATUS_ON_ALL(stats)     (stats) = ~((BitStatusType )0)
00374 #define BIT_STATUS_AT(stats,n) \
00375   ((n) < BIT_STATUS_BITS_NUM  ?  ((stats) & (1 << n)) : ((stats) & 1))
00376 
00377 #define BIT_STATUS_ON_AT(stats,n) do {\
00378   if ((n) < BIT_STATUS_BITS_NUM)\
00379     (stats) |= (1 << (n));\
00380   else\
00381     (stats) |= 1;\
00382 } while (0)
00383 
00384 #define BIT_STATUS_ON_AT_SIMPLE(stats,n) do {\
00385   if ((n) < BIT_STATUS_BITS_NUM)\
00386     (stats) |= (1 << (n));\
00387 } while (0)
00388 
00389 
00390 #define INT_MAX_LIMIT           ((1UL << (SIZEOF_INT * 8 - 1)) - 1)
00391 
00392 #define DIGITVAL(code)    ((code) - '0')
00393 #define ODIGITVAL(code)   DIGITVAL(code)
00394 #define XDIGITVAL(enc,code) \
00395   (ONIGENC_IS_CODE_DIGIT(enc,code) ? DIGITVAL(code) \
00396    : (ONIGENC_IS_CODE_UPPER(enc,code) ? (code) - 'A' + 10 : (code) - 'a' + 10))
00397 
00398 #define IS_SINGLELINE(option)     ((option) & ONIG_OPTION_SINGLELINE)
00399 #define IS_MULTILINE(option)      ((option) & ONIG_OPTION_MULTILINE)
00400 #define IS_IGNORECASE(option)     ((option) & ONIG_OPTION_IGNORECASE)
00401 #define IS_EXTEND(option)         ((option) & ONIG_OPTION_EXTEND)
00402 #define IS_FIND_LONGEST(option)   ((option) & ONIG_OPTION_FIND_LONGEST)
00403 #define IS_FIND_NOT_EMPTY(option) ((option) & ONIG_OPTION_FIND_NOT_EMPTY)
00404 #define IS_POSIXLINE(option)      (IS_SINGLELINE(option) && IS_MULTILINE(option))
00405 #define IS_FIND_CONDITION(option) ((option) & \
00406           (ONIG_OPTION_FIND_LONGEST | ONIG_OPTION_FIND_NOT_EMPTY))
00407 #define IS_NOTBOL(option)         ((option) & ONIG_OPTION_NOTBOL)
00408 #define IS_NOTEOL(option)         ((option) & ONIG_OPTION_NOTEOL)
00409 #define IS_POSIX_REGION(option)   ((option) & ONIG_OPTION_POSIX_REGION)
00410 
00411 /* OP_SET_OPTION is required for these options.
00412 #define IS_DYNAMIC_OPTION(option) \
00413   (((option) & (ONIG_OPTION_MULTILINE | ONIG_OPTION_IGNORECASE)) != 0)
00414 */
00415 /* ignore-case and multibyte status are included in compiled code. */
00416 #define IS_DYNAMIC_OPTION(option)  0
00417 
00418 #define REPEAT_INFINITE         -1
00419 #define IS_REPEAT_INFINITE(n)   ((n) == REPEAT_INFINITE)
00420 
00421 /* bitset */
00422 #define BITS_PER_BYTE      8
00423 #define SINGLE_BYTE_SIZE   (1 << BITS_PER_BYTE)
00424 #define BITS_IN_ROOM       (sizeof(Bits) * BITS_PER_BYTE)
00425 #define BITSET_SIZE        (SINGLE_BYTE_SIZE / BITS_IN_ROOM)
00426 
00427 #ifdef PLATFORM_UNALIGNED_WORD_ACCESS
00428 typedef unsigned int   Bits;
00429 #else
00430 typedef unsigned char  Bits;
00431 #endif
00432 typedef Bits           BitSet[BITSET_SIZE];
00433 typedef Bits*          BitSetRef;
00434 
00435 #define SIZE_BITSET        sizeof(BitSet)
00436 
00437 #define BITSET_CLEAR(bs) do {\
00438   int i;\
00439   for (i = 0; i < BITSET_SIZE; i++) { (bs)[i] = 0; }\
00440 } while (0)
00441 
00442 #define BS_ROOM(bs,pos)            (bs)[pos / BITS_IN_ROOM]
00443 #define BS_BIT(pos)                (1 << (pos % BITS_IN_ROOM))
00444 
00445 #define BITSET_AT(bs, pos)         (BS_ROOM(bs,pos) & BS_BIT(pos))
00446 #define BITSET_SET_BIT(bs, pos)     BS_ROOM(bs,pos) |= BS_BIT(pos)
00447 #define BITSET_CLEAR_BIT(bs, pos)   BS_ROOM(bs,pos) &= ~(BS_BIT(pos))
00448 #define BITSET_INVERT_BIT(bs, pos)  BS_ROOM(bs,pos) ^= BS_BIT(pos)
00449 
00450 /* bytes buffer */
00451 typedef struct _BBuf {
00452   UChar* p;
00453   unsigned int used;
00454   unsigned int alloc;
00455 } BBuf;
00456 
00457 #define BBUF_INIT(buf,size)    onig_bbuf_init((BBuf* )(buf), (size))
00458 
00459 #define BBUF_SIZE_INC(buf,inc) do{\
00460   (buf)->alloc += (inc);\
00461   (buf)->p = (UChar* )xrealloc((buf)->p, (buf)->alloc);\
00462   if (IS_NULL((buf)->p)) return(ONIGERR_MEMORY);\
00463 } while (0)
00464 
00465 #define BBUF_EXPAND(buf,low) do{\
00466   do { (buf)->alloc *= 2; } while ((buf)->alloc < (unsigned int )low);\
00467   (buf)->p = (UChar* )xrealloc((buf)->p, (buf)->alloc);\
00468   if (IS_NULL((buf)->p)) return(ONIGERR_MEMORY);\
00469 } while (0)
00470 
00471 #define BBUF_ENSURE_SIZE(buf,size) do{\
00472   unsigned int new_alloc = (buf)->alloc;\
00473   while (new_alloc < (unsigned int )(size)) { new_alloc *= 2; }\
00474   if ((buf)->alloc != new_alloc) {\
00475     (buf)->p = (UChar* )xrealloc((buf)->p, new_alloc);\
00476     if (IS_NULL((buf)->p)) return(ONIGERR_MEMORY);\
00477     (buf)->alloc = new_alloc;\
00478   }\
00479 } while (0)
00480 
00481 #define BBUF_WRITE(buf,pos,bytes,n) do{\
00482   int used = (pos) + (n);\
00483   if ((buf)->alloc < (unsigned int )used) BBUF_EXPAND((buf),used);\
00484   xmemcpy((buf)->p + (pos), (bytes), (n));\
00485   if ((buf)->used < (unsigned int )used) (buf)->used = used;\
00486 } while (0)
00487 
00488 #define BBUF_WRITE1(buf,pos,byte) do{\
00489   int used = (pos) + 1;\
00490   if ((buf)->alloc < (unsigned int )used) BBUF_EXPAND((buf),used);\
00491   (buf)->p[(pos)] = (byte);\
00492   if ((buf)->used < (unsigned int )used) (buf)->used = used;\
00493 } while (0)
00494 
00495 #define BBUF_ADD(buf,bytes,n)       BBUF_WRITE((buf),(buf)->used,(bytes),(n))
00496 #define BBUF_ADD1(buf,byte)         BBUF_WRITE1((buf),(buf)->used,(byte))
00497 #define BBUF_GET_ADD_ADDRESS(buf)   ((buf)->p + (buf)->used)
00498 #define BBUF_GET_OFFSET_POS(buf)    ((buf)->used)
00499 
00500 /* from < to */
00501 #define BBUF_MOVE_RIGHT(buf,from,to,n) do {\
00502   if ((unsigned int )((to)+(n)) > (buf)->alloc) BBUF_EXPAND((buf),(to) + (n));\
00503   xmemmove((buf)->p + (to), (buf)->p + (from), (n));\
00504   if ((unsigned int )((to)+(n)) > (buf)->used) (buf)->used = (to) + (n);\
00505 } while (0)
00506 
00507 /* from > to */
00508 #define BBUF_MOVE_LEFT(buf,from,to,n) do {\
00509   xmemmove((buf)->p + (to), (buf)->p + (from), (n));\
00510 } while (0)
00511 
00512 /* from > to */
00513 #define BBUF_MOVE_LEFT_REDUCE(buf,from,to) do {\
00514   xmemmove((buf)->p + (to), (buf)->p + (from), (buf)->used - (from));\
00515   (buf)->used -= (from - to);\
00516 } while (0)
00517 
00518 #define BBUF_INSERT(buf,pos,bytes,n) do {\
00519   if (pos >= (buf)->used) {\
00520     BBUF_WRITE(buf,pos,bytes,n);\
00521   }\
00522   else {\
00523     BBUF_MOVE_RIGHT((buf),(pos),(pos) + (n),((buf)->used - (pos)));\
00524     xmemcpy((buf)->p + (pos), (bytes), (n));\
00525   }\
00526 } while (0)
00527 
00528 #define BBUF_GET_BYTE(buf, pos) (buf)->p[(pos)]
00529 
00530 
00531 #define ANCHOR_BEGIN_BUF        (1<<0)
00532 #define ANCHOR_BEGIN_LINE       (1<<1)
00533 #define ANCHOR_BEGIN_POSITION   (1<<2)
00534 #define ANCHOR_END_BUF          (1<<3)
00535 #define ANCHOR_SEMI_END_BUF     (1<<4)
00536 #define ANCHOR_END_LINE         (1<<5)
00537 
00538 #define ANCHOR_WORD_BOUND       (1<<6)
00539 #define ANCHOR_NOT_WORD_BOUND   (1<<7)
00540 #define ANCHOR_WORD_BEGIN       (1<<8)
00541 #define ANCHOR_WORD_END         (1<<9)
00542 #define ANCHOR_PREC_READ        (1<<10)
00543 #define ANCHOR_PREC_READ_NOT    (1<<11)
00544 #define ANCHOR_LOOK_BEHIND      (1<<12)
00545 #define ANCHOR_LOOK_BEHIND_NOT  (1<<13)
00546 
00547 #define ANCHOR_ANYCHAR_STAR     (1<<14)   /* ".*" optimize info */
00548 #define ANCHOR_ANYCHAR_STAR_ML  (1<<15)   /* ".*" optimize info (multi-line) */
00549 
00550 /* operation code */
00551 enum OpCode {
00552   OP_FINISH = 0,        /* matching process terminator (no more alternative) */
00553   OP_END    = 1,        /* pattern code terminator (success end) */
00554 
00555   OP_EXACT1 = 2,        /* single byte, N = 1 */
00556   OP_EXACT2,            /* single byte, N = 2 */
00557   OP_EXACT3,            /* single byte, N = 3 */
00558   OP_EXACT4,            /* single byte, N = 4 */
00559   OP_EXACT5,            /* single byte, N = 5 */
00560   OP_EXACTN,            /* single byte */
00561   OP_EXACTMB2N1,        /* mb-length = 2 N = 1 */
00562   OP_EXACTMB2N2,        /* mb-length = 2 N = 2 */
00563   OP_EXACTMB2N3,        /* mb-length = 2 N = 3 */
00564   OP_EXACTMB2N,         /* mb-length = 2 */
00565   OP_EXACTMB3N,         /* mb-length = 3 */
00566   OP_EXACTMBN,          /* other length */
00567 
00568   OP_EXACT1_IC,         /* single byte, N = 1, ignore case */
00569   OP_EXACTN_IC,         /* single byte,        ignore case */
00570 
00571   OP_CCLASS,
00572   OP_CCLASS_MB,
00573   OP_CCLASS_MIX,
00574   OP_CCLASS_NOT,
00575   OP_CCLASS_MB_NOT,
00576   OP_CCLASS_MIX_NOT,
00577   OP_CCLASS_NODE,       /* pointer to CClassNode node */
00578 
00579   OP_ANYCHAR,                 /* "."  */
00580   OP_ANYCHAR_ML,              /* "."  multi-line */
00581   OP_ANYCHAR_STAR,            /* ".*" */
00582   OP_ANYCHAR_ML_STAR,         /* ".*" multi-line */
00583   OP_ANYCHAR_STAR_PEEK_NEXT,
00584   OP_ANYCHAR_ML_STAR_PEEK_NEXT,
00585 
00586   OP_WORD,
00587   OP_NOT_WORD,
00588   OP_WORD_BOUND,
00589   OP_NOT_WORD_BOUND,
00590   OP_WORD_BEGIN,
00591   OP_WORD_END,
00592 
00593   OP_BEGIN_BUF,
00594   OP_END_BUF,
00595   OP_BEGIN_LINE,
00596   OP_END_LINE,
00597   OP_SEMI_END_BUF,
00598   OP_BEGIN_POSITION,
00599 
00600   OP_BACKREF1,
00601   OP_BACKREF2,
00602   OP_BACKREFN,
00603   OP_BACKREFN_IC,
00604   OP_BACKREF_MULTI,
00605   OP_BACKREF_MULTI_IC,
00606   OP_BACKREF_AT_LEVEL,    /* \k<xxx+n>, \k<xxx-n> */
00607 
00608   OP_MEMORY_START,
00609   OP_MEMORY_START_PUSH,   /* push back-tracker to stack */
00610   OP_MEMORY_END_PUSH,     /* push back-tracker to stack */
00611   OP_MEMORY_END_PUSH_REC, /* push back-tracker to stack */
00612   OP_MEMORY_END,
00613   OP_MEMORY_END_REC,      /* push marker to stack */
00614 
00615   OP_SET_OPTION_PUSH,    /* set option and push recover option */
00616   OP_SET_OPTION,         /* set option */
00617 
00618   OP_FAIL,               /* pop stack and move */
00619   OP_JUMP,
00620   OP_PUSH,
00621   OP_POP,
00622   OP_PUSH_OR_JUMP_EXACT1,  /* if match exact then push, else jump. */
00623   OP_PUSH_IF_PEEK_NEXT,    /* if match exact then push, else none. */
00624   OP_REPEAT,               /* {n,m} */
00625   OP_REPEAT_NG,            /* {n,m}? (non greedy) */
00626   OP_REPEAT_INC,
00627   OP_REPEAT_INC_NG,        /* non greedy */
00628   OP_REPEAT_INC_SG,        /* search and get in stack */
00629   OP_REPEAT_INC_NG_SG,     /* search and get in stack (non greedy) */
00630   OP_NULL_CHECK_START,     /* null loop checker start */
00631   OP_NULL_CHECK_END,       /* null loop checker end   */
00632   OP_NULL_CHECK_END_MEMST, /* null loop checker end (with capture status) */
00633   OP_NULL_CHECK_END_MEMST_PUSH, /* with capture status and push check-end */
00634 
00635   OP_PUSH_POS,             /* (?=...)  start */
00636   OP_POP_POS,              /* (?=...)  end   */
00637   OP_PUSH_POS_NOT,         /* (?!...)  start */
00638   OP_FAIL_POS,             /* (?!...)  end   */
00639   OP_PUSH_STOP_BT,         /* (?>...)  start */
00640   OP_POP_STOP_BT,          /* (?>...)  end   */
00641   OP_LOOK_BEHIND,          /* (?<=...) start (no needs end opcode) */
00642   OP_PUSH_LOOK_BEHIND_NOT, /* (?<!...) start */
00643   OP_FAIL_LOOK_BEHIND_NOT, /* (?<!...) end   */
00644 
00645   OP_CALL,                 /* \g<name> */
00646   OP_RETURN,
00647 
00648   OP_STATE_CHECK_PUSH,         /* combination explosion check and push */
00649   OP_STATE_CHECK_PUSH_OR_JUMP, /* check ok -> push, else jump  */
00650   OP_STATE_CHECK,              /* check only */
00651   OP_STATE_CHECK_ANYCHAR_STAR,
00652   OP_STATE_CHECK_ANYCHAR_ML_STAR
00653 };
00654 
00655 typedef int RelAddrType;
00656 typedef int AbsAddrType;
00657 typedef int LengthType;
00658 typedef int RepeatNumType;
00659 typedef short int MemNumType;
00660 typedef short int StateCheckNumType;
00661 typedef void* PointerType;
00662 
00663 #define SIZE_OPCODE           1
00664 #define SIZE_RELADDR          sizeof(RelAddrType)
00665 #define SIZE_ABSADDR          sizeof(AbsAddrType)
00666 #define SIZE_LENGTH           sizeof(LengthType)
00667 #define SIZE_MEMNUM           sizeof(MemNumType)
00668 #define SIZE_STATE_CHECK_NUM  sizeof(StateCheckNumType)
00669 #define SIZE_REPEATNUM        sizeof(RepeatNumType)
00670 #define SIZE_OPTION           sizeof(OnigOptionType)
00671 #define SIZE_CODE_POINT       sizeof(OnigCodePoint)
00672 #define SIZE_POINTER          sizeof(PointerType)
00673 
00674 
00675 #ifdef PLATFORM_UNALIGNED_WORD_ACCESS
00676 
00677 #define PLATFORM_GET_INC(val,p,type) do{\
00678   val  = *(type* )p;\
00679   (p) += sizeof(type);\
00680 } while(0)
00681 
00682 #else
00683 
00684 #define PLATFORM_GET_INC(val,p,type) do{\
00685   xmemcpy(&val, (p), sizeof(type));\
00686   (p) += sizeof(type);\
00687 } while(0)
00688 
00689 #endif  /* PLATFORM_UNALIGNED_WORD_ACCESS */
00690 
00691 #define GET_RELADDR_INC(addr,p)    PLATFORM_GET_INC(addr,   p, RelAddrType)
00692 #define GET_ABSADDR_INC(addr,p)    PLATFORM_GET_INC(addr,   p, AbsAddrType)
00693 #define GET_LENGTH_INC(len,p)      PLATFORM_GET_INC(len,    p, LengthType)
00694 #define GET_MEMNUM_INC(num,p)      PLATFORM_GET_INC(num,    p, MemNumType)
00695 #define GET_REPEATNUM_INC(num,p)   PLATFORM_GET_INC(num,    p, RepeatNumType)
00696 #define GET_OPTION_INC(option,p)   PLATFORM_GET_INC(option, p, OnigOptionType)
00697 #define GET_POINTER_INC(ptr,p)     PLATFORM_GET_INC(ptr,    p, PointerType)
00698 #define GET_STATE_CHECK_NUM_INC(num,p)  PLATFORM_GET_INC(num, p, StateCheckNumType)
00699 
00700 /* code point's address must be aligned address. */
00701 #define GET_CODE_POINT(code,p)   code = *((OnigCodePoint* )(p))
00702 #define GET_BYTE_INC(byte,p) do{\
00703   byte = *(p);\
00704   (p)++;\
00705 } while(0)
00706 
00707 
00708 /* op-code + arg size */
00709 #define SIZE_OP_ANYCHAR_STAR            SIZE_OPCODE
00710 #define SIZE_OP_ANYCHAR_STAR_PEEK_NEXT (SIZE_OPCODE + 1)
00711 #define SIZE_OP_JUMP                   (SIZE_OPCODE + SIZE_RELADDR)
00712 #define SIZE_OP_PUSH                   (SIZE_OPCODE + SIZE_RELADDR)
00713 #define SIZE_OP_POP                     SIZE_OPCODE
00714 #define SIZE_OP_PUSH_OR_JUMP_EXACT1    (SIZE_OPCODE + SIZE_RELADDR + 1)
00715 #define SIZE_OP_PUSH_IF_PEEK_NEXT      (SIZE_OPCODE + SIZE_RELADDR + 1)
00716 #define SIZE_OP_REPEAT_INC             (SIZE_OPCODE + SIZE_MEMNUM)
00717 #define SIZE_OP_REPEAT_INC_NG          (SIZE_OPCODE + SIZE_MEMNUM)
00718 #define SIZE_OP_PUSH_POS                SIZE_OPCODE
00719 #define SIZE_OP_PUSH_POS_NOT           (SIZE_OPCODE + SIZE_RELADDR)
00720 #define SIZE_OP_POP_POS                 SIZE_OPCODE
00721 #define SIZE_OP_FAIL_POS                SIZE_OPCODE
00722 #define SIZE_OP_SET_OPTION             (SIZE_OPCODE + SIZE_OPTION)
00723 #define SIZE_OP_SET_OPTION_PUSH        (SIZE_OPCODE + SIZE_OPTION)
00724 #define SIZE_OP_FAIL                    SIZE_OPCODE
00725 #define SIZE_OP_MEMORY_START           (SIZE_OPCODE + SIZE_MEMNUM)
00726 #define SIZE_OP_MEMORY_START_PUSH      (SIZE_OPCODE + SIZE_MEMNUM)
00727 #define SIZE_OP_MEMORY_END_PUSH        (SIZE_OPCODE + SIZE_MEMNUM)
00728 #define SIZE_OP_MEMORY_END_PUSH_REC    (SIZE_OPCODE + SIZE_MEMNUM)
00729 #define SIZE_OP_MEMORY_END             (SIZE_OPCODE + SIZE_MEMNUM)
00730 #define SIZE_OP_MEMORY_END_REC         (SIZE_OPCODE + SIZE_MEMNUM)
00731 #define SIZE_OP_PUSH_STOP_BT            SIZE_OPCODE
00732 #define SIZE_OP_POP_STOP_BT             SIZE_OPCODE
00733 #define SIZE_OP_NULL_CHECK_START       (SIZE_OPCODE + SIZE_MEMNUM)
00734 #define SIZE_OP_NULL_CHECK_END         (SIZE_OPCODE + SIZE_MEMNUM)
00735 #define SIZE_OP_LOOK_BEHIND            (SIZE_OPCODE + SIZE_LENGTH)
00736 #define SIZE_OP_PUSH_LOOK_BEHIND_NOT   (SIZE_OPCODE + SIZE_RELADDR + SIZE_LENGTH)
00737 #define SIZE_OP_FAIL_LOOK_BEHIND_NOT    SIZE_OPCODE
00738 #define SIZE_OP_CALL                   (SIZE_OPCODE + SIZE_ABSADDR)
00739 #define SIZE_OP_RETURN                  SIZE_OPCODE
00740 
00741 #ifdef USE_COMBINATION_EXPLOSION_CHECK
00742 #define SIZE_OP_STATE_CHECK            (SIZE_OPCODE + SIZE_STATE_CHECK_NUM)
00743 #define SIZE_OP_STATE_CHECK_PUSH       (SIZE_OPCODE + SIZE_STATE_CHECK_NUM + SIZE_RELADDR)
00744 #define SIZE_OP_STATE_CHECK_PUSH_OR_JUMP (SIZE_OPCODE + SIZE_STATE_CHECK_NUM + SIZE_RELADDR)
00745 #define SIZE_OP_STATE_CHECK_ANYCHAR_STAR (SIZE_OPCODE + SIZE_STATE_CHECK_NUM)
00746 #endif
00747 
00748 #define MC_ESC(enc)               (enc)->meta_char_table.esc
00749 #define MC_ANYCHAR(enc)           (enc)->meta_char_table.anychar
00750 #define MC_ANYTIME(enc)           (enc)->meta_char_table.anytime
00751 #define MC_ZERO_OR_ONE_TIME(enc)  (enc)->meta_char_table.zero_or_one_time
00752 #define MC_ONE_OR_MORE_TIME(enc)  (enc)->meta_char_table.one_or_more_time
00753 #define MC_ANYCHAR_ANYTIME(enc)   (enc)->meta_char_table.anychar_anytime
00754 
00755 #define IS_MC_ESC_CODE(code, enc, syn) \
00756   ((code) == MC_ESC(enc) && \
00757    !IS_SYNTAX_OP2((syn), ONIG_SYN_OP2_INEFFECTIVE_ESCAPE))
00758 
00759 
00760 #define SYN_POSIX_COMMON_OP \
00761  ( ONIG_SYN_OP_DOT_ANYCHAR | ONIG_SYN_OP_POSIX_BRACKET | \
00762    ONIG_SYN_OP_DECIMAL_BACKREF | \
00763    ONIG_SYN_OP_BRACKET_CC | ONIG_SYN_OP_ASTERISK_ZERO_INF | \
00764    ONIG_SYN_OP_LINE_ANCHOR | \
00765    ONIG_SYN_OP_ESC_CONTROL_CHARS )
00766 
00767 #define SYN_GNU_REGEX_OP \
00768   ( ONIG_SYN_OP_DOT_ANYCHAR | ONIG_SYN_OP_BRACKET_CC | \
00769     ONIG_SYN_OP_POSIX_BRACKET | ONIG_SYN_OP_DECIMAL_BACKREF | \
00770     ONIG_SYN_OP_BRACE_INTERVAL | ONIG_SYN_OP_LPAREN_SUBEXP | \
00771     ONIG_SYN_OP_VBAR_ALT | \
00772     ONIG_SYN_OP_ASTERISK_ZERO_INF | ONIG_SYN_OP_PLUS_ONE_INF | \
00773     ONIG_SYN_OP_QMARK_ZERO_ONE | \
00774     ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR | ONIG_SYN_OP_ESC_CAPITAL_G_BEGIN_ANCHOR | \
00775     ONIG_SYN_OP_ESC_W_WORD | \
00776     ONIG_SYN_OP_ESC_B_WORD_BOUND | ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END | \
00777     ONIG_SYN_OP_ESC_S_WHITE_SPACE | ONIG_SYN_OP_ESC_D_DIGIT | \
00778     ONIG_SYN_OP_LINE_ANCHOR )
00779 
00780 #define SYN_GNU_REGEX_BV \
00781   ( ONIG_SYN_CONTEXT_INDEP_ANCHORS | ONIG_SYN_CONTEXT_INDEP_REPEAT_OPS | \
00782     ONIG_SYN_CONTEXT_INVALID_REPEAT_OPS | ONIG_SYN_ALLOW_INVALID_INTERVAL | \
00783     ONIG_SYN_BACKSLASH_ESCAPE_IN_CC | ONIG_SYN_ALLOW_DOUBLE_RANGE_OP_IN_CC )
00784 
00785 /* cclass node */
00786 #define FLAG_CCLASS_NOT            1
00787 #define FLAG_CCLASS_SHARE         (1<<1)
00788 
00789 #define CCLASS_SET_NOT(cc)      (cc)->flags |= FLAG_CCLASS_NOT
00790 #define CCLASS_CLEAR_NOT(cc)    (cc)->flags &= ~FLAG_CCLASS_NOT
00791 #define CCLASS_SET_SHARE(cc)    (cc)->flags |= FLAG_CCLASS_SHARE
00792 #define IS_CCLASS_NOT(cc)     (((cc)->flags & FLAG_CCLASS_NOT) != 0)
00793 #define IS_CCLASS_SHARE(cc)   (((cc)->flags & FLAG_CCLASS_SHARE) != 0)
00794 
00795 typedef struct {
00796   int    flags;
00797   BitSet bs;
00798   BBuf*  mbuf;     /* multi-byte info or NULL */
00799 } CClassNode;
00800 
00801 
00802 #ifdef ONIG_DEBUG
00803 
00804 typedef struct {
00805   short int opcode;
00806   char*     name;
00807   short int arg_type;
00808 } OnigOpInfoType;
00809 
00810 extern OnigOpInfoType OnigOpInfo[];
00811 
00812 extern void onig_print_compiled_byte_code P_((FILE* f, UChar* bp, UChar** nextp, OnigEncoding enc));
00813 
00814 #ifdef ONIG_DEBUG_STATISTICS
00815 extern void onig_statistics_init P_((void));
00816 extern void onig_print_statistics P_((FILE* f));
00817 #endif
00818 #endif
00819 
00820 extern UChar* onig_error_code_to_format P_((int code));
00821 extern void  onig_snprintf_with_pattern PV_((UChar buf[], int bufsize, OnigEncoding enc, UChar* pat, UChar* pat_end, const UChar *fmt, ...));
00822 extern int  onig_bbuf_init P_((BBuf* buf, int size));
00823 extern int  onig_alloc_init P_((regex_t** reg, OnigOptionType option, OnigAmbigType ambig_flag, OnigEncoding enc, OnigSyntaxType* syntax));
00824 extern int  onig_compile P_((regex_t* reg, const UChar* pattern, const UChar* pattern_end, OnigErrorInfo* einfo));
00825 extern void onig_chain_reduce P_((regex_t* reg));
00826 extern void onig_chain_link_add P_((regex_t* to, regex_t* add));
00827 extern void onig_transfer P_((regex_t* to, regex_t* from));
00828 extern int  onig_is_code_in_cc P_((OnigEncoding enc, OnigCodePoint code, CClassNode* cc));
00829 
00830 #endif /* REGINT_H */