euc_tw.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**********************************************************************
  2. euc_tw.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 "regenc.h"
  30. static const int EncLen_EUCTW[] = {
  31. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  32. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  33. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  34. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  35. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  36. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  37. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  38. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  39. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1,
  40. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  41. 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  42. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  43. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  44. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  45. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  46. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1
  47. };
  48. static int
  49. euctw_mbc_enc_len(const UChar* p)
  50. {
  51. return EncLen_EUCTW[*p];
  52. }
  53. static OnigCodePoint
  54. euctw_mbc_to_code(const UChar* p, const UChar* end)
  55. {
  56. return onigenc_mbn_mbc_to_code(ONIG_ENCODING_EUC_TW, p, end);
  57. }
  58. static int
  59. euctw_code_to_mbc(OnigCodePoint code, UChar *buf)
  60. {
  61. return onigenc_mb4_code_to_mbc(ONIG_ENCODING_EUC_TW, code, buf);
  62. }
  63. static int
  64. euctw_mbc_case_fold(OnigCaseFoldType flag, const UChar** pp, const UChar* end,
  65. UChar* lower)
  66. {
  67. return onigenc_mbn_mbc_case_fold(ONIG_ENCODING_EUC_TW, flag,
  68. pp, end, lower);
  69. }
  70. static int
  71. euctw_is_code_ctype(OnigCodePoint code, unsigned int ctype)
  72. {
  73. return onigenc_mb4_is_code_ctype(ONIG_ENCODING_EUC_TW, code, ctype);
  74. }
  75. #define euctw_islead(c) ((UChar )((c) - 0xa1) > 0xfe - 0xa1)
  76. static UChar*
  77. euctw_left_adjust_char_head(const UChar* start, const UChar* s)
  78. {
  79. /* Assumed in this encoding,
  80. mb-trail bytes don't mix with single bytes.
  81. */
  82. const UChar *p;
  83. int len;
  84. if (s <= start) return (UChar* )s;
  85. p = s;
  86. while (!euctw_islead(*p) && p > start) p--;
  87. len = enclen(ONIG_ENCODING_EUC_TW, p);
  88. if (p + len > s) return (UChar* )p;
  89. p += len;
  90. return (UChar* )(p + ((s - p) & ~1));
  91. }
  92. static int
  93. euctw_is_allowed_reverse_match(const UChar* s, const UChar* end ARG_UNUSED)
  94. {
  95. const UChar c = *s;
  96. if (c <= 0x7e) return TRUE;
  97. else return FALSE;
  98. }
  99. OnigEncodingType OnigEncodingEUC_TW = {
  100. euctw_mbc_enc_len,
  101. "EUC-TW", /* name */
  102. 4, /* max enc length */
  103. 1, /* min enc length */
  104. onigenc_is_mbc_newline_0x0a,
  105. euctw_mbc_to_code,
  106. onigenc_mb4_code_to_mbclen,
  107. euctw_code_to_mbc,
  108. euctw_mbc_case_fold,
  109. onigenc_ascii_apply_all_case_fold,
  110. onigenc_ascii_get_case_fold_codes_by_str,
  111. onigenc_minimum_property_name_to_ctype,
  112. euctw_is_code_ctype,
  113. onigenc_not_support_get_ctype_code_range,
  114. euctw_left_adjust_char_head,
  115. euctw_is_allowed_reverse_match
  116. };