| 1 | /* |
| 2 | Copyright(C) 2018 Kouhei Sutou <[email protected]> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | This library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with this library; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | #include "grn_ctx.h" |
| 20 | #include "grn_onigmo.h" |
| 21 | |
| 22 | #ifdef GRN_SUPPORT_REGEXP |
| 23 | bool |
| 24 | grn_onigmo_is_valid_encoding(grn_ctx *ctx) |
| 25 | { |
| 26 | switch (ctx->encoding) { |
| 27 | case GRN_ENC_EUC_JP : |
| 28 | case GRN_ENC_UTF8 : |
| 29 | case GRN_ENC_SJIS : |
| 30 | case GRN_ENC_LATIN1 : |
| 31 | case GRN_ENC_KOI8R : |
| 32 | return true; |
| 33 | default : |
| 34 | return false; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | OnigRegex |
| 39 | grn_onigmo_new(grn_ctx *ctx, |
| 40 | const char *pattern, |
| 41 | size_t pattern_length, |
| 42 | OnigOptionType option, |
| 43 | const OnigSyntaxType *syntax, |
| 44 | const char *context) |
| 45 | { |
| 46 | OnigEncoding onig_encoding; |
| 47 | OnigRegex onig_regex; |
| 48 | int onig_result; |
| 49 | OnigErrorInfo onig_error_info; |
| 50 | |
| 51 | switch (ctx->encoding) { |
| 52 | case GRN_ENC_EUC_JP : |
| 53 | onig_encoding = ONIG_ENCODING_EUC_JP; |
| 54 | break; |
| 55 | case GRN_ENC_UTF8 : |
| 56 | onig_encoding = ONIG_ENCODING_UTF8; |
| 57 | break; |
| 58 | case GRN_ENC_SJIS : |
| 59 | onig_encoding = ONIG_ENCODING_CP932; |
| 60 | break; |
| 61 | case GRN_ENC_LATIN1 : |
| 62 | onig_encoding = ONIG_ENCODING_ISO_8859_1; |
| 63 | break; |
| 64 | case GRN_ENC_KOI8R : |
| 65 | onig_encoding = ONIG_ENCODING_KOI8_R; |
| 66 | break; |
| 67 | default : |
| 68 | ERR(GRN_INVALID_ARGUMENT, |
| 69 | "%s[regexp][new] invalid encoding: <%.*s>: <%s>", |
| 70 | context, |
| 71 | (int)pattern_length, |
| 72 | pattern, |
| 73 | grn_encoding_to_string(ctx->encoding)); |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | onig_result = onig_new(&onig_regex, |
| 78 | pattern, |
| 79 | pattern + pattern_length, |
| 80 | option, |
| 81 | onig_encoding, |
| 82 | syntax, |
| 83 | &onig_error_info); |
| 84 | if (onig_result != ONIG_NORMAL) { |
| 85 | char message[ONIG_MAX_ERROR_MESSAGE_LEN]; |
| 86 | onig_error_code_to_str(message, onig_result, &onig_error_info); |
| 87 | ERR(GRN_INVALID_ARGUMENT, |
| 88 | "%s[regexp][new] " |
| 89 | "failed to create regular expression object: <%.*s>: %s", |
| 90 | context, |
| 91 | (int)pattern_length, |
| 92 | pattern, |
| 93 | message); |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | return onig_regex; |
| 98 | } |
| 99 | |
| 100 | #endif /* GRN_SUPPORT_REGEXP */ |