| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | Copyright 2026 Joel Svensson svenssonjoel@yahoo.se | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 3 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program 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 | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include "extensions.h" | ||
| 19 | #include "heap.h" | ||
| 20 | #include "lbm_c_interop.h" | ||
| 21 | #include "extensions/pattern_extensions.h" | ||
| 22 | #include "luamatch.h" | ||
| 23 | |||
| 24 | #include <string.h> | ||
| 25 | |||
| 26 | 18 | static lbm_value lm_error_to_lbm(lm_error_t err) { | |
| 27 | static const char *msgs[] = { | ||
| 28 | "no error", | ||
| 29 | "pattern ends with '%'", | ||
| 30 | "missing ']' in pattern", | ||
| 31 | "missing arguments to '%b'", | ||
| 32 | "missing '[' after '%f'", | ||
| 33 | "too many captures in pattern", | ||
| 34 | "invalid pattern capture (unmatched ')')", | ||
| 35 | "invalid or unfinished capture index", | ||
| 36 | "pattern nested too deeply", | ||
| 37 | "pattern matching exceeded the iteration limit", | ||
| 38 | }; | ||
| 39 | 18 | size_t idx = (size_t)err; | |
| 40 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | if (idx >= sizeof(msgs) / sizeof(msgs[0])) idx = 0; |
| 41 | 18 | lbm_set_error_reason((char*)msgs[idx]); | |
| 42 | 18 | return ENC_SYM_EERROR; | |
| 43 | } | ||
| 44 | |||
| 45 | 120 | static lbm_value materialize_one(lm_match_state_t *ms, int i, const char *text, | |
| 46 | size_t whole_start, size_t whole_end) { | ||
| 47 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 36 times.
|
120 | if (ms->level == 0) { |
| 48 | 84 | return span_to_lbm(text + whole_start, whole_end - whole_start); | |
| 49 | } | ||
| 50 | 36 | lm_capture_t c = ms->capture[i]; | |
| 51 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 30 times.
|
36 | if (c.len == LM_CAP_POSITION) { |
| 52 | 6 | return lbm_enc_i((lbm_int)(c.init - ms->src_init)); | |
| 53 | } | ||
| 54 | 30 | return span_to_lbm(c.init, (size_t)c.len); | |
| 55 | } | ||
| 56 | |||
| 57 | 99 | static lbm_value materialize_result(lm_match_state_t *ms, const char *text, | |
| 58 | size_t whole_start, size_t whole_end) { | ||
| 59 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 15 times.
|
99 | if (ms->level == 0) { |
| 60 | 84 | return materialize_one(ms, 0, text, whole_start, whole_end); | |
| 61 | } | ||
| 62 | 15 | lbm_value res = ENC_SYM_NIL; | |
| 63 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 15 times.
|
51 | for (int i = ms->level - 1; i >= 0; i--) { |
| 64 | 36 | lbm_value v = materialize_one(ms, i, text, whole_start, whole_end); | |
| 65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
|
36 | if (v == ENC_SYM_MERROR) return v; |
| 66 | 36 | res = lbm_cons(v, res); | |
| 67 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
|
36 | if (res == ENC_SYM_MERROR) return res; |
| 68 | } | ||
| 69 | 15 | return res; | |
| 70 | } | ||
| 71 | |||
| 72 | /* shared arg decoding for (text pattern [start-offset] [max-iterations]) */ | ||
| 73 | 123 | static bool decode_find_args(lbm_value *args, lbm_uint argn, | |
| 74 | char **text, size_t *text_len, | ||
| 75 | char **pat, size_t *pat_len, | ||
| 76 | size_t *start_off, size_t *max_iter) { | ||
| 77 | char *t, *p; size_t ts, ps; | ||
| 78 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 123 times.
|
123 | if (!lbm_dec_str_size(args[0], &t, &ts)) return false; |
| 79 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 123 times.
|
123 | if (!lbm_dec_str_size(args[1], &p, &ps)) return false; |
| 80 | 123 | *text = t; *pat = p; | |
| 81 | 123 | *text_len = strlen_max(t, ts); | |
| 82 | 123 | *pat_len = strlen_max(p, ps); | |
| 83 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 123 times.
|
123 | if (!dec_opt_uint(args, argn, 2, 0, start_off)) return false; |
| 84 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 123 times.
|
123 | if (!dec_opt_uint(args, argn, 3, 0, max_iter)) return false; |
| 85 | 123 | return true; | |
| 86 | } | ||
| 87 | |||
| 88 | 45 | static lbm_value ext_str_lua_find(lbm_value *args, lbm_uint argn) { | |
| 89 |
2/4✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 45 times.
|
45 | if (argn < 2 || argn > 4) { |
| 90 | ✗ | lbm_set_error_reason((char*)lbm_error_str_num_args); | |
| 91 | ✗ | return ENC_SYM_EERROR; | |
| 92 | } | ||
| 93 | char *text, *pat; size_t text_len, pat_len, start_off, max_iter; | ||
| 94 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 45 times.
|
45 | if (!decode_find_args(args, argn, &text, &text_len, &pat, &pat_len, |
| 95 | &start_off, &max_iter)) { | ||
| 96 | ✗ | return ENC_SYM_TERROR; | |
| 97 | } | ||
| 98 | |||
| 99 | lm_match_state_t ms; | ||
| 100 | size_t s, e; | ||
| 101 | 45 | lm_status_t st = lm_find(text, text_len, pat, pat_len, start_off, max_iter, | |
| 102 | &ms, &s, &e); | ||
| 103 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
|
45 | if (st == LM_ERROR) return lm_error_to_lbm(ms.error); |
| 104 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 24 times.
|
45 | if (st == LM_NOMATCH) return ENC_SYM_NIL; |
| 105 | |||
| 106 | 24 | lbm_value result = materialize_result(&ms, text, s, e); | |
| 107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (result == ENC_SYM_MERROR) return result; |
| 108 | 24 | lbm_value lst = lbm_cons(result, ENC_SYM_NIL); | |
| 109 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (lst == ENC_SYM_MERROR) return lst; |
| 110 | 24 | lst = lbm_cons(lbm_enc_i((lbm_int)e), lst); | |
| 111 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (lst == ENC_SYM_MERROR) return lst; |
| 112 | 24 | lst = lbm_cons(lbm_enc_i((lbm_int)s), lst); | |
| 113 | 24 | return lst; | |
| 114 | } | ||
| 115 | |||
| 116 | 78 | static lbm_value ext_str_lua_match(lbm_value *args, lbm_uint argn) { | |
| 117 |
2/4✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 78 times.
|
78 | if (argn < 2 || argn > 4) { |
| 118 | ✗ | lbm_set_error_reason((char*)lbm_error_str_num_args); | |
| 119 | ✗ | return ENC_SYM_EERROR; | |
| 120 | } | ||
| 121 | char *text, *pat; size_t text_len, pat_len, start_off, max_iter; | ||
| 122 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
|
78 | if (!decode_find_args(args, argn, &text, &text_len, &pat, &pat_len, |
| 123 | &start_off, &max_iter)) { | ||
| 124 | ✗ | return ENC_SYM_TERROR; | |
| 125 | } | ||
| 126 | |||
| 127 | lm_match_state_t ms; | ||
| 128 | size_t s, e; | ||
| 129 | 78 | lm_status_t st = lm_find(text, text_len, pat, pat_len, start_off, max_iter, | |
| 130 | &ms, &s, &e); | ||
| 131 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 60 times.
|
78 | if (st == LM_ERROR) return lm_error_to_lbm(ms.error); |
| 132 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 54 times.
|
60 | if (st == LM_NOMATCH) return ENC_SYM_NIL; |
| 133 | 54 | return materialize_result(&ms, text, s, e); | |
| 134 | } | ||
| 135 | |||
| 136 | /* (str-lua-gmatch text pattern state [max-iterations]) | ||
| 137 | state: nil, or a single integer (the previous match's end) from a | ||
| 138 | prior call's next-state. */ | ||
| 139 | 27 | static lbm_value ext_str_lua_gmatch(lbm_value *args, lbm_uint argn) { | |
| 140 |
2/4✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 27 times.
|
27 | if (argn < 3 || argn > 4) { |
| 141 | ✗ | lbm_set_error_reason((char*)lbm_error_str_num_args); | |
| 142 | ✗ | return ENC_SYM_EERROR; | |
| 143 | } | ||
| 144 | char *text, *pat; size_t text_size, pat_size; | ||
| 145 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
|
27 | if (!lbm_dec_str_size(args[0], &text, &text_size)) return ENC_SYM_TERROR; |
| 146 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
|
27 | if (!lbm_dec_str_size(args[1], &pat, &pat_size)) return ENC_SYM_TERROR; |
| 147 | 27 | size_t text_len = strlen_max(text, text_size); | |
| 148 | 27 | size_t pat_len = strlen_max(pat, pat_size); | |
| 149 | |||
| 150 | 27 | long search_from = 0; | |
| 151 | 27 | long lastmatch = -1; | |
| 152 | 27 | lbm_value state = args[2]; | |
| 153 |
2/2✓ Branch 1 taken 18 times.
✓ Branch 2 taken 9 times.
|
27 | if (!lbm_is_symbol_nil(state)) { |
| 154 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
|
18 | if (!lbm_is_number(state)) return ENC_SYM_TERROR; |
| 155 | 18 | search_from = lastmatch = (long)lbm_dec_as_i32(state); | |
| 156 | } | ||
| 157 | |||
| 158 | size_t max_iter; | ||
| 159 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
|
27 | if (!dec_opt_uint(args, argn, 3, 0, &max_iter)) return ENC_SYM_TERROR; |
| 160 | |||
| 161 | ✗ | for (;;) { | |
| 162 |
2/4✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 27 times.
|
54 | if (search_from < 0 || (size_t)search_from > text_len) return ENC_SYM_NIL; |
| 163 | lm_match_state_t ms; | ||
| 164 | size_t s, e; | ||
| 165 | 27 | lm_status_t st = lm_find(text, text_len, pat, pat_len, | |
| 166 | (size_t)search_from, max_iter, &ms, &s, &e); | ||
| 167 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (st == LM_ERROR) return lm_error_to_lbm(ms.error); |
| 168 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 21 times.
|
27 | if (st == LM_NOMATCH) return ENC_SYM_NIL; |
| 169 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if ((long)e == lastmatch) { |
| 170 | /* empty match at the same spot as last time: reject, advance past | ||
| 171 | it and retry -- mirrors Lua's gmatch_aux `e != lastmatch` check. | ||
| 172 | Always terminates: search_from strictly increases and is capped | ||
| 173 | by text_len above. */ | ||
| 174 | ✗ | search_from = (long)s + 1; | |
| 175 | ✗ | continue; | |
| 176 | } | ||
| 177 | 21 | lbm_value result = materialize_result(&ms, text, s, e); | |
| 178 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (result == ENC_SYM_MERROR) return result; |
| 179 | 21 | lbm_value next_state = lbm_enc_i((lbm_int)e); | |
| 180 | 21 | return lbm_cons(result, next_state); | |
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | 711 | void lbm_pattern_extensions_init(void) { | |
| 185 | 711 | lbm_add_extension("str-lua-find", ext_str_lua_find); | |
| 186 | 711 | lbm_add_extension("str-lua-match", ext_str_lua_match); | |
| 187 | 711 | lbm_add_extension("str-lua-gmatch", ext_str_lua_gmatch); | |
| 188 | 711 | } | |
| 189 |