| 1 | /* |
| 2 | Copyright (C) 2016-2018 Brazil |
| 3 | Copyright (C) 2019-2022 Sutou Kouhei <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #pragma once |
| 21 | |
| 22 | #ifdef __cplusplus |
| 23 | extern "C" { |
| 24 | #endif |
| 25 | |
| 26 | #define GRN_RAW_STRING_INIT(string) do { \ |
| 27 | string.value = NULL; \ |
| 28 | string.length = 0; \ |
| 29 | } while (false) |
| 30 | |
| 31 | #define GRN_RAW_STRING_SET(string, bulk) do { \ |
| 32 | if (bulk && GRN_TEXT_LEN(bulk) > 0) { \ |
| 33 | string.value = GRN_TEXT_VALUE(bulk); \ |
| 34 | string.length = GRN_TEXT_LEN(bulk); \ |
| 35 | } else { \ |
| 36 | string.value = NULL; \ |
| 37 | string.length = 0; \ |
| 38 | } \ |
| 39 | } while (false) |
| 40 | |
| 41 | #define GRN_RAW_STRING_SET_CSTRING(string, cstring) do { \ |
| 42 | string.value = cstring; \ |
| 43 | string.length = (cstring == NULL) ? 0 : strlen(cstring); \ |
| 44 | } while (false) |
| 45 | |
| 46 | #define GRN_RAW_STRING_FILL(string, bulk) do { \ |
| 47 | if (bulk && GRN_TEXT_LEN(bulk) > 0) { \ |
| 48 | string.value = GRN_TEXT_VALUE(bulk); \ |
| 49 | string.length = GRN_TEXT_LEN(bulk); \ |
| 50 | } \ |
| 51 | } while (false) |
| 52 | |
| 53 | #define GRN_RAW_STRING_EQUAL(string, other_string) \ |
| 54 | (string.length == other_string.length && \ |
| 55 | memcmp(string.value, other_string.value, string.length) == 0) |
| 56 | |
| 57 | #define GRN_RAW_STRING_EQUAL_CSTRING(string, cstring) \ |
| 58 | (cstring == NULL ? \ |
| 59 | (string.length == 0) : \ |
| 60 | (string.length == strlen(cstring) && \ |
| 61 | memcmp(string.value, cstring, string.length) == 0)) |
| 62 | |
| 63 | #define GRN_RAW_STRING_EQUAL_CSTRING_CI(string, cstring) \ |
| 64 | (cstring == NULL ? \ |
| 65 | (string.length == 0) : \ |
| 66 | (string.length == strlen(cstring) && \ |
| 67 | grn_strncasecmp(string.value, cstring, string.length) == 0)) |
| 68 | |
| 69 | #define GRN_RAW_STRING_START_WITH_CSTRING(string, cstring) \ |
| 70 | (cstring == NULL ? \ |
| 71 | true : \ |
| 72 | (string.length >= strlen(cstring) && \ |
| 73 | memcmp(string.value, cstring, strlen(cstring)) == 0)) |
| 74 | |
| 75 | #define GRN_RAW_STRING_END_WITH_CSTRING(string, cstring) \ |
| 76 | (cstring == NULL ? \ |
| 77 | true : \ |
| 78 | (string.length >= strlen(cstring) && \ |
| 79 | memcmp(string.value + string.length - strlen(cstring), \ |
| 80 | cstring, \ |
| 81 | strlen(cstring)) == 0)) |
| 82 | |
| 83 | typedef struct { |
| 84 | const char *value; |
| 85 | size_t length; |
| 86 | } grn_raw_string; |
| 87 | |
| 88 | GRN_API void grn_raw_string_lstrip(grn_ctx *ctx, grn_raw_string *string); |
| 89 | GRN_API bool |
| 90 | grn_raw_string_have_sub_string(grn_ctx *ctx, |
| 91 | grn_raw_string *string, |
| 92 | grn_raw_string *sub_string); |
| 93 | GRN_API bool |
| 94 | grn_raw_string_have_sub_string_cstring(grn_ctx *ctx, |
| 95 | grn_raw_string *string, |
| 96 | const char *sub_cstring); |
| 97 | GRN_API grn_raw_string |
| 98 | grn_raw_string_substring(grn_ctx *ctx, |
| 99 | const grn_raw_string *string, |
| 100 | size_t start, |
| 101 | int64_t length); |
| 102 | |
| 103 | #ifdef __cplusplus |
| 104 | } |
| 105 | #endif |