euc_kr.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**********************************************************************
  2. euc_kr.c - Oniguruma (regular expression library)
  3. **********************************************************************/
  4. /*-
  5. * Copyright (c) 2002-2007 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_EUCKR[] = {
  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, 1, 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. euckr_mbc_enc_len(const UChar* p)
  50. {
  51. return EncLen_EUCKR[*p];
  52. }
  53. static OnigCodePoint
  54. euckr_mbc_to_code(const UChar* p, const UChar* end)
  55. {
  56. return onigenc_mbn_mbc_to_code(ONIG_ENCODING_EUC_KR, p, end);
  57. }
  58. static int
  59. euckr_code_to_mbc(OnigCodePoint code, UChar *buf)
  60. {
  61. return onigenc_mb2_code_to_mbc(ONIG_ENCODING_EUC_KR, code, buf);
  62. }
  63. static int
  64. euckr_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_KR, flag,
  68. pp, end, lower);
  69. }
  70. #if 0
  71. static int
  72. euckr_is_mbc_ambiguous(OnigCaseFoldType flag,
  73. const UChar** pp, const UChar* end)
  74. {
  75. return onigenc_mbn_is_mbc_ambiguous(ONIG_ENCODING_EUC_KR, flag, pp, end);
  76. }
  77. #endif
  78. static int
  79. euckr_is_code_ctype(OnigCodePoint code, unsigned int ctype)
  80. {
  81. return onigenc_mb2_is_code_ctype(ONIG_ENCODING_EUC_KR, code, ctype);
  82. }
  83. #define euckr_islead(c) ((c) < 0xa1 || (c) == 0xff)
  84. static UChar*
  85. euckr_left_adjust_char_head(const UChar* start, const UChar* s)
  86. {
  87. /* Assumed in this encoding,
  88. mb-trail bytes don't mix with single bytes.
  89. */
  90. const UChar *p;
  91. int len;
  92. if (s <= start) return (UChar* )s;
  93. p = s;
  94. while (!euckr_islead(*p) && p > start) p--;
  95. len = enclen(ONIG_ENCODING_EUC_KR, p);
  96. if (p + len > s) return (UChar* )p;
  97. p += len;
  98. return (UChar* )(p + ((s - p) & ~1));
  99. }
  100. static int
  101. euckr_is_allowed_reverse_match(const UChar* s, const UChar* end ARG_UNUSED)
  102. {
  103. const UChar c = *s;
  104. if (c <= 0x7e) return TRUE;
  105. else return FALSE;
  106. }
  107. OnigEncodingType OnigEncodingEUC_KR = {
  108. euckr_mbc_enc_len,
  109. "EUC-KR", /* name */
  110. 2, /* max enc length */
  111. 1, /* min enc length */
  112. onigenc_is_mbc_newline_0x0a,
  113. euckr_mbc_to_code,
  114. onigenc_mb2_code_to_mbclen,
  115. euckr_code_to_mbc,
  116. euckr_mbc_case_fold,
  117. onigenc_ascii_apply_all_case_fold,
  118. onigenc_ascii_get_case_fold_codes_by_str,
  119. onigenc_minimum_property_name_to_ctype,
  120. euckr_is_code_ctype,
  121. onigenc_not_support_get_ctype_code_range,
  122. euckr_left_adjust_char_head,
  123. euckr_is_allowed_reverse_match
  124. };
  125. /* Same with OnigEncodingEUC_KR except the name */
  126. OnigEncodingType OnigEncodingEUC_CN = {
  127. euckr_mbc_enc_len,
  128. "EUC-CN", /* name */
  129. 2, /* max enc length */
  130. 1, /* min enc length */
  131. onigenc_is_mbc_newline_0x0a,
  132. euckr_mbc_to_code,
  133. onigenc_mb2_code_to_mbclen,
  134. euckr_code_to_mbc,
  135. euckr_mbc_case_fold,
  136. onigenc_ascii_apply_all_case_fold,
  137. onigenc_ascii_get_case_fold_codes_by_str,
  138. onigenc_minimum_property_name_to_ctype,
  139. euckr_is_code_ctype,
  140. onigenc_not_support_get_ctype_code_range,
  141. euckr_left_adjust_char_head,
  142. euckr_is_allowed_reverse_match
  143. };