Skip to content

Commit 008fe1b

Browse files
authored
Merge pull request #29 from kmarius/remove-deprecated-funcs
Remove __call metamethod
2 parents d3b36a0 + a1e8115 commit 008fe1b

File tree

2 files changed

+1
-329
lines changed

2 files changed

+1
-329
lines changed

jsregexp.c

Lines changed: 0 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -188,144 +188,6 @@ static inline struct jsstring *lua_tojsstring(lua_State *lstate, int arg) {
188188
}
189189
}
190190

191-
static int regexp_call(lua_State *lstate) {
192-
uint8_t *capture[CAPTURE_COUNT_MAX * 2];
193-
194-
struct regexp *r = luaL_checkudata(lstate, 1, JSREGEXP_MT);
195-
const int global = lre_get_flags(r->bc) & LRE_FLAG_GLOBAL;
196-
const int named_groups = lre_get_flags(r->bc) & LRE_FLAG_NAMED_GROUPS;
197-
const int capture_count = lre_get_capture_count(r->bc);
198-
199-
struct jsstring *input = lua_tojsstring(lstate, 2);
200-
201-
int nmatch = 0;
202-
int cindex = 0;
203-
204-
if (input->is_wide_char) {
205-
lua_newtable(lstate);
206-
while (lre_exec(capture, r->bc, input->u.str8, cindex, input->len, 1,
207-
NULL) == 1) {
208-
if (capture[0] == capture[1]) {
209-
// empty match -> continue matching from next character (to prevent an
210-
// endless loop). This is basically the same implementation as in
211-
// quickjs, see
212-
// https://github.com/bellard/quickjs/blob/2788d71e823b522b178db3b3660ce93689534e6d/quickjs.c#L42857-L42869
213-
214-
cindex++;
215-
if ((*(input->u.str16 + cindex) >> 10) == 0x37) {
216-
// surrogate pair (vscode doesn't always do this?)
217-
cindex++;
218-
}
219-
} else {
220-
cindex = (capture[1] - (uint8_t *)input->u.str16) / 2;
221-
}
222-
223-
lua_newtable(lstate);
224-
225-
lua_pushnumber(lstate,
226-
1 + input->indices[(capture[0] - input->u.str8) / 2]);
227-
lua_setfield(lstate, -2, "begin_ind");
228-
229-
lua_pushnumber(lstate, input->indices[(capture[1] - input->u.str8) / 2]);
230-
lua_setfield(lstate, -2, "end_ind");
231-
232-
lua_newtable(lstate);
233-
234-
const char *group_names = NULL;
235-
if (named_groups) {
236-
lua_newtable(lstate);
237-
group_names = lre_get_groupnames(r->bc);
238-
}
239-
for (int i = 1; i < capture_count; i++) {
240-
const uint32_t a = input->indices[(capture[2 * i] - input->u.str8) / 2];
241-
const uint32_t b =
242-
input->indices[(capture[2 * i + 1] - input->u.str8) / 2];
243-
lua_pushlstring(lstate, input->bstr + a, b - a);
244-
lua_rawseti(lstate, -2, i);
245-
if (named_groups && group_names != NULL) {
246-
if (*group_names != '\0') { // check if current group is named
247-
lua_pushlstring(lstate, input->bstr + a, b - a);
248-
lua_setfield(lstate, -3, group_names);
249-
group_names +=
250-
strlen(group_names) + 1; // move to the next group name
251-
} else {
252-
group_names += 1; // move to the next group name
253-
}
254-
}
255-
}
256-
257-
if (named_groups) {
258-
lua_setfield(lstate, -3, "groups");
259-
lua_setfield(lstate, -2, "named_groups");
260-
} else {
261-
lua_setfield(lstate, -2, "groups");
262-
}
263-
264-
lua_rawseti(lstate, -2, ++nmatch);
265-
266-
if (!global || cindex > input->len) {
267-
break;
268-
}
269-
}
270-
} else {
271-
lua_newtable(lstate);
272-
while (lre_exec(capture, r->bc, input->u.str8, cindex, input->len, 0,
273-
NULL) == 1) {
274-
if (capture[0] == capture[1]) {
275-
cindex++;
276-
} else {
277-
cindex = capture[1] - input->u.str8;
278-
}
279-
280-
lua_newtable(lstate);
281-
282-
lua_pushnumber(lstate, 1 + capture[0] - input->u.str8);
283-
lua_setfield(lstate, -2, "begin_ind");
284-
285-
lua_pushnumber(lstate, capture[1] - input->u.str8);
286-
lua_setfield(lstate, -2, "end_ind");
287-
288-
lua_newtable(lstate);
289-
290-
const char *group_names = NULL;
291-
if (named_groups) {
292-
lua_newtable(lstate);
293-
group_names = lre_get_groupnames(r->bc);
294-
}
295-
for (int i = 1; i < capture_count; i++) {
296-
lua_pushlstring(lstate, (char *)capture[2 * i],
297-
capture[2 * i + 1] - capture[2 * i]);
298-
lua_rawseti(lstate, -2, i);
299-
if (named_groups && group_names != NULL) {
300-
if (*group_names != '\0') { // check if current group is named
301-
lua_pushlstring(lstate, (char *)capture[2 * i],
302-
capture[2 * i + 1] - capture[2 * i]);
303-
lua_setfield(lstate, -3, group_names);
304-
group_names +=
305-
strlen(group_names) + 1; // move to the next group name
306-
} else {
307-
group_names += 1; // move to the next group name
308-
}
309-
}
310-
}
311-
312-
if (named_groups) {
313-
lua_setfield(lstate, -3, "groups");
314-
lua_setfield(lstate, -2, "named_groups");
315-
} else {
316-
lua_setfield(lstate, -2, "groups");
317-
}
318-
319-
lua_rawseti(lstate, -2, ++nmatch);
320-
321-
if (!global || cindex > input->len) {
322-
break;
323-
}
324-
}
325-
}
326-
return 1;
327-
}
328-
329191
static int regexp_gc(lua_State *lstate) {
330192
struct regexp *r = lua_touserdata(lstate, 1);
331193
free(r->bc);
@@ -601,7 +463,6 @@ static int regexp_newindex(lua_State *lstate) {
601463
static struct luaL_Reg jsregexp_meta[] = {{"exec", regexp_exec},
602464
{"test", regexp_test},
603465
{"__gc", regexp_gc},
604-
{"__call", regexp_call},
605466
{"__tostring", regexp_tostring},
606467
{"__index", regexp_index},
607468
{"__newindex", regexp_newindex},

test.lua

Lines changed: 1 addition & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -22,69 +22,6 @@ local function test_compile(str, regex, flags, want)
2222
successes = successes + 1
2323
end
2424

25-
local function test_call(str, regex, flags, want)
26-
local function fail(...)
27-
print(str, regex, flags, want)
28-
print(...)
29-
fails = fails + 1
30-
end
31-
32-
tests = tests + 1
33-
local r = jsregexp.compile_safe(regex, flags)
34-
if not r then
35-
return fail("compilation error")
36-
end
37-
local res = r(str)
38-
if #res ~= #want then
39-
return fail("match count mismatch: wanted", #want, "got ", #res)
40-
end
41-
for i, val in pairs(res) do
42-
local want = want[i]
43-
if not want then
44-
return fail("compilation should have failed")
45-
end
46-
local match = string.sub(str, val.begin_ind, val.end_ind)
47-
if match ~= want[1] then
48-
return fail("global mismatch:", match, want[1])
49-
end
50-
if #val.groups > 0 then
51-
if not want.groups or #want.groups ~= #val.groups then
52-
return fail("number of match groups mismatch")
53-
end
54-
for j, v in pairs(val.groups) do
55-
if v ~= want.groups[j] then
56-
return fail("match group mismatch", i, v, want.groups[j])
57-
end
58-
end
59-
else
60-
if want.groups and #want.groups > 0 then
61-
return fail("number of match groups mismatch")
62-
end
63-
end
64-
if want.named_groups ~= nil then
65-
if val.named_groups == nil then
66-
fails = fails + 1
67-
return
68-
end
69-
for k, v in pairs(want.named_groups) do
70-
if val.named_groups[k] ~= v then
71-
fails = fails + 1
72-
print(
73-
string.format(
74-
"named group mismatch group '%s': expected '%s', actual '%s'",
75-
k,
76-
v,
77-
val.named_groups[k]
78-
)
79-
)
80-
return
81-
end
82-
end
83-
end
84-
end
85-
successes = successes + 1
86-
end
87-
8825
local function test_exec(str, regex, flags, want)
8926
local function fail(...)
9027
print(str, regex, flags, want)
@@ -241,6 +178,7 @@ local function test_match(str, regex, flags, want)
241178
end
242179

243180
local function test_match_all_list(str, regex, flags, want)
181+
print(str, "~", regex, "flags")
244182
local function fail(fmt, ...)
245183
print(str, regex, flags, want)
246184
print(string.format(fmt, ...))
@@ -340,133 +278,6 @@ test_compile("dummy", "[", "", nil)
340278
-- (luajit at least..)
341279
test_compile("dummy", string.char(0xfd, 166, 178, 165, 138, 183), "", nil)
342280

343-
test_call("dummy", ".", "", { { "d" } })
344-
test_call("du", ".", "g", { { "d" }, { "u" } })
345-
346-
test_call("dummy", "c", "", {})
347-
test_call("dummy", "c", "g", {})
348-
test_call("dummy", "d", "", { { "d" } })
349-
test_call("dummy", "m", "", { { "m" } })
350-
test_call("dummy", "m", "g", { { "m" }, { "m" } })
351-
352-
test_call("dummy", "(dummy)", "", { { "dummy", groups = { "dummy" } } })
353-
test_call("The quick brown fox jumps over the lazy dog", "\\w+", "", { { "The" } })
354-
test_call(
355-
"The quick brown fox jumps over the lazy dog",
356-
"\\w+",
357-
"g",
358-
{ { "The" }, { "quick" }, { "brown" }, { "fox" }, { "jumps" }, { "over" }, { "the" }, { "lazy" }, { "dog" } }
359-
)
360-
test_call("The quick brown fox jumps over the lazy dog", "[aeiou]{2,}", "g", { { "ui" } })
361-
362-
test_call("äöü", ".", "g", { { "ä" }, { "ö" }, { "ü" } })
363-
test_call("äöü", ".", "", { { "ä" } })
364-
test_call("ÄÖÜ", ".", "", { { "Ä" } })
365-
test_call("äöü", "[äöü]", "g", { { "ä" }, { "ö" }, { "ü" } })
366-
test_call("äöü", "[äöü]*", "g", { { "äöü" }, { "" } })
367-
test_call("äÄ", "ä", "gi", { { "ä" }, { "Ä" } })
368-
test_call("öäü.haha", "([^.]*)\\.(.*)", "", { { "öäü.haha", groups = { "öäü", "haha" } } })
369-
370-
test_call("𝄞", "𝄞", "", { { "𝄞" } })
371-
-- these empty matches are expected and consistent with vscode
372-
test_call("öö öö", "ö*", "g", { { "öö" }, { "" }, { "öö" }, { "" } })
373-
test_call("𝄞𝄞 𝄞𝄞", "[^ ]*", "g", { { "𝄞𝄞" }, { "" }, { "𝄞𝄞" }, { "" } })
374-
test_call("𝄞𝄞", "𝄞*", "", { { "𝄞𝄞" } })
375-
-- doesn't work in vscode, matches only a single 𝄞 each time:
376-
test_call("𝄞𝄞𐐷𝄞𝄞", "𝄞*", "g", { { "𝄞𝄞" }, { "" }, { "𝄞𝄞" }, { "" } })
377-
-- vscode actually splits the center unicode character and produces an extra empty match. we don't.
378-
test_call("öö𐐷öö", "ö*", "g", { { "öö" }, { "" }, { "öö" }, { "" } })
379-
test_call("a", "𝄞|a", "g", { { "a" } }) -- utf16 regex, ascii input
380-
381-
test_call("κόσμε", "(κόσμε)", "", { { "κόσμε", groups = { "κόσμε" } } })
382-
383-
test_call(
384-
"jordbær fløde på",
385-
"(jordbær fløde på)",
386-
"",
387-
{ { "jordbær fløde på", groups = { "jordbær fløde på" } } }
388-
)
389-
390-
test_call(
391-
"Heizölrückstoßabdämpfung",
392-
"(Heizölrückstoßabdämpfung)",
393-
"",
394-
{ { "Heizölrückstoßabdämpfung", groups = { "Heizölrückstoßabdämpfung" } } }
395-
)
396-
397-
test_call(
398-
"Fête l'haï volapük",
399-
"(Fête l'haï volapük)",
400-
"",
401-
{ { "Fête l'haï volapük", groups = { "Fête l'haï volapük" } } }
402-
)
403-
404-
test_call(
405-
"Árvíztűrő tükörfúrógép",
406-
"(Árvíztűrő tükörfúrógép)",
407-
"",
408-
{ { "Árvíztűrő tükörfúrógép", groups = { "Árvíztűrő tükörfúrógép" } } }
409-
)
410-
411-
test_call(
412-
"いろはにほへとちりぬるを",
413-
"(いろはにほへとちりぬるを)",
414-
"",
415-
{ { "いろはにほへとちりぬるを", groups = { "いろはにほへとちりぬるを" } } }
416-
)
417-
418-
test_call(
419-
"Съешь же ещё этих мягких французских булок да выпей чаю",
420-
"(Съешь же ещё этих мягких французских булок да выпей чаю)",
421-
"",
422-
{
423-
{
424-
"Съешь же ещё этих мягких французских булок да выпей чаю",
425-
groups = {
426-
"Съешь же ещё этих мягких французских булок да выпей чаю",
427-
},
428-
},
429-
}
430-
)
431-
432-
-- no idea how thai works
433-
-- test("จงฝ่าฟันพัฒนาวิชาการ", "(จงฝ่าฟันพัฒนาวิชาการ)", "", {{"จงฝ่าฟันพัฒนาวิชาการ", groups="จงฝ่าฟันพัฒนาวิชาการ"}})
434-
435-
-- named groups:
436-
test_call("The quick brown fox jumps over the lazy dog", "(?<first_word>\\w+) (\\w+) (?<third_word>\\w+)", "n", {
437-
{
438-
"The quick brown",
439-
groups = { "The", "quick", "brown" },
440-
named_groups = { first_word = "The", third_word = "brown" },
441-
},
442-
})
443-
test_call(
444-
"The qüick bröwn föx jümps över the lazy dög",
445-
"(?<first_word>[^ ]+) ([^ ]+) (?<third_word>[^ ]+)",
446-
"n",
447-
{
448-
{
449-
"The qüick bröwn",
450-
groups = { "The", "qüick", "bröwn" },
451-
named_groups = {
452-
first_word = "The",
453-
third_word = "bröwn",
454-
},
455-
},
456-
}
457-
)
458-
test_call("The quick bröwn föx", "(?<first_wörd>[^ ]+) ([^ ]+) (?<third_wörd>[^ ]+)", "n", {
459-
{
460-
"The quick bröwn",
461-
groups = { "The", "quick", "bröwn" },
462-
named_groups = { ["first_wörd"] = "The", ["third_wörd"] = "bröwn" },
463-
},
464-
})
465-
test_call("𝄞𝄞 𐐷", "(?<word>[^ ]+)", "ng", {
466-
{ "𝄞𝄞", groups = { "𝄞𝄞" }, named_groups = { word = "𝄞𝄞" } },
467-
{ "𐐷", groups = { "𐐷" }, named_groups = { word = "𐐷" } },
468-
})
469-
470281
test_exec("The quick brown", "\\w+", "g", { { [0] = "The" }, { [0] = "quick" }, { [0] = "brown" } })
471282
test_exec(
472283
"The quick brown fox",

0 commit comments

Comments
 (0)