| 1 | /* |
| 2 | Copyright (C) 2016-2017 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 | #include "grn_str.h" |
| 21 | |
| 22 | void |
| 23 | grn_raw_string_lstrip(grn_ctx *ctx, |
| 24 | grn_raw_string *string) |
| 25 | { |
| 26 | const char *end; |
| 27 | int space_len; |
| 28 | |
| 29 | end = string->value + string->length; |
| 30 | while (string->value < end) { |
| 31 | space_len = grn_isspace(string->value, ctx->encoding); |
| 32 | if (space_len == 0) { |
| 33 | break; |
| 34 | } |
| 35 | string->value += space_len; |
| 36 | string->length -= (size_t)space_len; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | bool |
| 41 | grn_raw_string_have_sub_string(grn_ctx *ctx, |
| 42 | grn_raw_string *string, |
| 43 | grn_raw_string *sub_string) |
| 44 | { |
| 45 | if (sub_string->length == 0) { |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | if (sub_string->length > string->length) { |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | const char *string_current = string->value; |
| 54 | const char *string_end = string->value + string->length; |
| 55 | const char *sub_string_current = sub_string->value; |
| 56 | const char *sub_string_end = sub_string->value + sub_string->length; |
| 57 | int sub_string_start_char_len; |
| 58 | int sub_string_char_len; |
| 59 | |
| 60 | sub_string_start_char_len = |
| 61 | grn_charlen(ctx, sub_string_current, sub_string_end); |
| 62 | if (sub_string_start_char_len == 0) { |
| 63 | return false; |
| 64 | } |
| 65 | sub_string_char_len = sub_string_start_char_len; |
| 66 | |
| 67 | while (string_current < string_end) { |
| 68 | int string_char_len; |
| 69 | |
| 70 | string_char_len = grn_charlen(ctx, string_current, string_end); |
| 71 | if (string_char_len == 0) { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | if (string_char_len == sub_string_char_len && |
| 76 | memcmp(string_current, |
| 77 | sub_string_current, |
| 78 | (size_t)string_char_len) == 0) { |
| 79 | sub_string_current += sub_string_char_len; |
| 80 | if (sub_string_current == sub_string_end) { |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | sub_string_char_len = grn_charlen(ctx, sub_string_current, sub_string_end); |
| 85 | if (sub_string_char_len == 0) { |
| 86 | return false; |
| 87 | } |
| 88 | } else { |
| 89 | if (sub_string_current != sub_string->value) { |
| 90 | sub_string_current = sub_string->value; |
| 91 | sub_string_char_len = sub_string_start_char_len; |
| 92 | continue; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | string_current += string_char_len; |
| 97 | } |
| 98 | |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | bool |
| 103 | grn_raw_string_have_sub_string_cstring(grn_ctx *ctx, |
| 104 | grn_raw_string *string, |
| 105 | const char *sub_cstring) |
| 106 | { |
| 107 | grn_raw_string sub_string; |
| 108 | if (sub_cstring) { |
| 109 | sub_string.value = sub_cstring; |
| 110 | sub_string.length = strlen(sub_cstring); |
| 111 | } else { |
| 112 | sub_string.value = NULL; |
| 113 | sub_string.length = 0; |
| 114 | } |
| 115 | |
| 116 | return grn_raw_string_have_sub_string(ctx, string, &sub_string); |
| 117 | } |
| 118 | |
| 119 | grn_raw_string |
| 120 | grn_raw_string_substring(grn_ctx *ctx, |
| 121 | const grn_raw_string *string, |
| 122 | size_t start, |
| 123 | int64_t length) |
| 124 | { |
| 125 | grn_raw_string substring; |
| 126 | substring.value = string->value + start; |
| 127 | if (length < 0) { |
| 128 | int64_t substring_length = (int64_t)(string->length - start) + length + 1; |
| 129 | if (substring_length < 0) { |
| 130 | substring.length = 0; |
| 131 | } else { |
| 132 | substring.length = (size_t)substring_length; |
| 133 | } |
| 134 | } else { |
| 135 | substring.length = (size_t)length; |
| 136 | } |
| 137 | return substring; |
| 138 | } |