:eachmatch() -> :matches()

This commit is contained in:
Bruce Hill 2021-09-25 19:05:15 -07:00
parent fb1840e07a
commit 3fe77cbf3e
3 changed files with 12 additions and 12 deletions

View File

@ -12,11 +12,11 @@ The Lua `bp` bindings provide the following methods:
bp.match(pattern, text, [start_index]) --> match / nil
bp.replace(pattern, replacement, text, [start_index]) --> text_with_replacements, num_replacements
bp.compile(pattern) --> pattern_object
for m in bp.eachmatch(pattern, text, [start_index]) do ... end
for m in bp.matches(pattern, text, [start_index]) do ... end
pattern_object:match(text, [start_index]) --> match / nil
pattern_object:replace(replacement, text, [start_index]) --> text_with_replacements, num_replacements
for m in pattern_object:eachmatch(text, [start_index]) do ... end
for m in pattern_object:matches(text, [start_index]) do ... end
```
Match objects returned by `bp.match()` are tables whose `__tostring` will
@ -32,7 +32,7 @@ unambiguous.
Pattern objects returned by `bp.compile()` are pre-compiled patterns that are
slightly faster to reuse than just calling `bp.match()` repeatedly. They have a
`.source` attribute that holds the original text used to compile them and have
`:match()`, `:replace()`, and `:eachmatch()` methods as described above.
`:match()`, `:replace()`, and `:matches()` methods as described above.
All methods will raise an error with a descriptive message if the given pattern
has a syntax error.
@ -47,13 +47,13 @@ local m = bp.match('"n" @Es=+`e "dle"', "like finding a needle in a haystack")
local replaced, nreplacements = bp.match('"n" +`e "dle"', "cat", "like finding a needle in a haystack")
--> "like finding a cat in a haystack", 1
for word in bp.eachmatch("+`A-Z,a-z", "one, two three... four!") do
for word in bp.matches("+`A-Z,a-z", "one, two three... four!") do
print(word) --> prints "one" "two" "three" "four"
end
local pat = bp.compile("word parens")
for _,s in ipairs(my_strings) do
for fncall in pat:eachmatch(s) do
for fncall in pat:matches(s) do
print(fncall)
end
end

View File

@ -3,11 +3,11 @@
* API:
* bp.match(pat, str, [start_index]) -> nil or match_table
* bp.replace(pat, replacement, str, [start_index]) -> str with replacements, num_replacements
* for match_table in bp.eachmatch(pat, str, [start_index]) do ... end
* for match_table in bp.matches(pat, str, [start_index]) do ... end
* bp.compile(pat) -> pattern object
* pat:match(str, [start_index])
* pat:replace(replacement, str, [start_index])
* for match in pat:eachmatch(str, [start_index]) do ... end
* for match in pat:matches(str, [start_index]) do ... end
*/
#include <fcntl.h>
@ -240,7 +240,7 @@ static int iter(lua_State *L)
return Lmatch(L);
}
static int Leachmatch(lua_State *L)
static int Lmatches(lua_State *L)
{
int nargs = lua_gettop(L);
lua_pushcfunction(L, iter); // iter
@ -294,7 +294,7 @@ static const luaL_Reg match_metamethods[] = {
static const luaL_Reg pat_methods[] = {
{"match", Lmatch},
{"replace", Lreplace},
{"eachmatch", Leachmatch},
{"matches", Lmatches},
{NULL, NULL}
};
@ -309,7 +309,7 @@ static const luaL_Reg bp_methods[] = {
{"match", Lmatch},
{"replace", Lreplace},
{"compile", Lcompile},
{"eachmatch", Leachmatch},
{"matches", Lmatches},
{NULL, NULL}
};

View File

@ -13,7 +13,7 @@ local function repr(obj)
end
print("Matching:")
for m in bp.eachmatch("(*`a-z) => '(@0)'", "one two three") do
for m in bp.matches("(*`a-z) => '(@0)'", "one two three") do
print(repr(m))
end
@ -45,6 +45,6 @@ print(pat:match("...foo..."))
print(pat:match("...baz..."))
print(pat:replace("{@0}", "...baz..."))
for m in pat:eachmatch("hello world") do
for m in pat:matches("hello world") do
print(m)
end