reggnu.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**********************************************************************
  2. reggnu.c - Oniguruma (regular expression library)
  3. **********************************************************************/
  4. /*-
  5. * Copyright (c) 2002-2008 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include "regint.h"
  30. #ifndef ONIGGNU_H
  31. #include "oniggnu.h"
  32. #endif
  33. extern void
  34. re_free_registers(OnigRegion* r)
  35. {
  36. /* 0: don't free self */
  37. onig_region_free(r, 0);
  38. }
  39. extern int
  40. re_adjust_startpos(regex_t* reg, const char* string, int size,
  41. int startpos, int range)
  42. {
  43. if (startpos > 0 && ONIGENC_MBC_MAXLEN(reg->enc) != 1 && startpos < size) {
  44. UChar *p;
  45. UChar *s = (UChar* )string + startpos;
  46. if (range > 0) {
  47. p = onigenc_get_right_adjust_char_head(reg->enc, (UChar* )string, s);
  48. }
  49. else {
  50. p = ONIGENC_LEFT_ADJUST_CHAR_HEAD(reg->enc, (UChar* )string, s);
  51. }
  52. return p - (UChar* )string;
  53. }
  54. return startpos;
  55. }
  56. extern int
  57. re_match(regex_t* reg, const char* str, int size, int pos,
  58. struct re_registers* regs)
  59. {
  60. return onig_match(reg, (UChar* )str, (UChar* )(str + size),
  61. (UChar* )(str + pos), regs, ONIG_OPTION_NONE);
  62. }
  63. extern int
  64. re_search(regex_t* bufp, const char* string, int size, int startpos, int range,
  65. struct re_registers* regs)
  66. {
  67. return onig_search(bufp, (UChar* )string, (UChar* )(string + size),
  68. (UChar* )(string + startpos),
  69. (UChar* )(string + startpos + range),
  70. regs, ONIG_OPTION_NONE);
  71. }
  72. extern int
  73. re_compile_pattern(const char* pattern, int size, regex_t* reg, char* ebuf)
  74. {
  75. int r;
  76. OnigErrorInfo einfo;
  77. r = onig_compile(reg, (UChar* )pattern, (UChar* )(pattern + size), &einfo);
  78. if (r != ONIG_NORMAL) {
  79. if (IS_NOT_NULL(ebuf))
  80. (void )onig_error_code_to_str((UChar* )ebuf, r, &einfo);
  81. }
  82. return r;
  83. }
  84. #ifdef USE_RECOMPILE_API
  85. extern int
  86. re_recompile_pattern(const char* pattern, int size, regex_t* reg, char* ebuf)
  87. {
  88. int r;
  89. OnigErrorInfo einfo;
  90. OnigEncoding enc;
  91. /* I think encoding and options should be arguments of this function.
  92. But this is adapted to present re.c. (2002/11/29)
  93. */
  94. enc = OnigEncDefaultCharEncoding;
  95. r = onig_recompile(reg, (UChar* )pattern, (UChar* )(pattern + size),
  96. reg->options, enc, OnigDefaultSyntax, &einfo);
  97. if (r != ONIG_NORMAL) {
  98. if (IS_NOT_NULL(ebuf))
  99. (void )onig_error_code_to_str((UChar* )ebuf, r, &einfo);
  100. }
  101. return r;
  102. }
  103. #endif
  104. extern void
  105. re_free_pattern(regex_t* reg)
  106. {
  107. onig_free(reg);
  108. }
  109. extern int
  110. re_alloc_pattern(regex_t** reg)
  111. {
  112. *reg = (regex_t* )xmalloc(sizeof(regex_t));
  113. if (IS_NULL(*reg)) return ONIGERR_MEMORY;
  114. return onig_reg_init(*reg, ONIG_OPTION_DEFAULT,
  115. ONIGENC_CASE_FOLD_DEFAULT,
  116. OnigEncDefaultCharEncoding,
  117. OnigDefaultSyntax);
  118. }
  119. extern void
  120. re_set_casetable(const char* table)
  121. {
  122. onigenc_set_default_caseconv_table((UChar* )table);
  123. }
  124. extern void
  125. re_mbcinit(int mb_code)
  126. {
  127. OnigEncoding enc;
  128. switch (mb_code) {
  129. case RE_MBCTYPE_ASCII:
  130. enc = ONIG_ENCODING_ASCII;
  131. break;
  132. case RE_MBCTYPE_EUC:
  133. enc = ONIG_ENCODING_EUC_JP;
  134. break;
  135. case RE_MBCTYPE_SJIS:
  136. enc = ONIG_ENCODING_SJIS;
  137. break;
  138. case RE_MBCTYPE_UTF8:
  139. enc = ONIG_ENCODING_UTF8;
  140. break;
  141. default:
  142. return ;
  143. break;
  144. }
  145. onigenc_set_default_encoding(enc);
  146. }