Skip to content

Commit daba17e

Browse files
authored
Merge pull request #7 from mah0x211/refactor
Refactor
2 parents f3b6da2 + 3a668b7 commit daba17e

File tree

4 files changed

+603
-115
lines changed

4 files changed

+603
-115
lines changed

.github/workflows/test.yml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
name: test
2+
3+
on:
4+
push:
5+
paths-ignore:
6+
- '**.md'
7+
- 'LICENSE'
8+
9+
10+
jobs:
11+
luacheck:
12+
runs-on: ubuntu-latest
13+
steps:
14+
-
15+
name: Checkout
16+
uses: actions/checkout@v2
17+
-
18+
name: Setup Lua
19+
uses: leafo/gh-actions-lua@v9
20+
-
21+
name: Setup Luarocks
22+
uses: leafo/gh-actions-luarocks@v4
23+
-
24+
name: Install Tools
25+
run: luarocks install luacheck
26+
-
27+
name: Run luacheck
28+
run: |
29+
luacheck .
30+
31+
test:
32+
runs-on: ubuntu-latest
33+
strategy:
34+
matrix:
35+
lua-version:
36+
- "5.1"
37+
- "5.2"
38+
- "5.3"
39+
- "5.4"
40+
steps:
41+
-
42+
name: Checkout
43+
uses: actions/checkout@v2
44+
with:
45+
submodules: 'true'
46+
-
47+
name: Setup Lua ${{ matrix.lua-version }}
48+
uses: leafo/gh-actions-lua@v9
49+
with:
50+
luaVersion: ${{ matrix.lua-version }}
51+
-
52+
name: Setup Luarocks
53+
uses: leafo/gh-actions-luarocks@v4
54+
-
55+
name: Install Tools
56+
run: |
57+
luarocks install testcase
58+
luarocks install luacov
59+
-
60+
name: Install
61+
run: |
62+
luarocks make
63+
-
64+
name: Run Test
65+
run: |
66+
testcase --coverage ./test/
67+
-
68+
name: Upload coverage to Codecov
69+
uses: codecov/[email protected]
70+
with:
71+
token: ${{ secrets.CODECOV_TOKEN }}
72+
flags: unittests
73+

README.md

Lines changed: 86 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
# lua-regex
22

3-
regular expression for lua.
3+
[![test](https://github.com/mah0x211/lua-regex/actions/workflows/test.yml/badge.svg)](https://github.com/mah0x211/lua-regex/actions/workflows/test.yml)
4+
[![codecov](https://codecov.io/gh/mah0x211/lua-regex/branch/master/graph/badge.svg)](https://codecov.io/gh/mah0x211/lua-regex)
45

5-
**NOTE:** this module is under heavy development.
66

7+
simple regular expression module for lua.
78

8-
## Dependencies
99

10-
- lua-pcre2: <https://github.com/mah0x211/lua-pcre2>
10+
## Installation
1111

12-
---
13-
14-
## regex module
15-
16-
```lua
17-
local regex = require('regex')
12+
```sh
13+
luarocks install regex
1814
```
1915

16+
***
17+
2018

2119
## Regular expression flags
2220

@@ -37,7 +35,7 @@ local regex = require('regex')
3735

3836
creates a new regex object.
3937

40-
**Params**
38+
**Parameters**
4139

4240
- `pattern:string`: string containing expression to be compiled.
4341
- `flgs:string`: [regular expression flags](#regular-expression-flags).
@@ -47,15 +45,34 @@ creates a new regex object.
4745
- `re:table`: regex object.
4846
- `err:string`: error message.
4947

48+
**Example**
49+
50+
```lua
51+
local regex = require('regex')
52+
local re, err = regex.new('a(b+)(c+)', 'i')
53+
if re then
54+
local arr, err = re:match('ABBBCCC')
55+
if arr then
56+
print(arr[1]) -- 'ABBBCCC'
57+
print(arr[2]) -- 'BBB'
58+
print(arr[3]) -- 'CCC'
59+
else
60+
print(err)
61+
end
62+
else
63+
print(err)
64+
end
65+
```
66+
5067

5168
## Instance Methods
5269

5370

54-
### arr, err = re:match( sbj [, offset] )
71+
## arr, err = regex:match( sbj [, offset] )
5572

5673
matches a compiled regular expression against a given subject string. It returns matched substrings.
5774

58-
**Params**
75+
**Parameters**
5976

6077
- `sbj:string`: the subject string.
6178
- `offset:number`: offset in the subject at which to start matching.
@@ -66,11 +83,11 @@ matches a compiled regular expression against a given subject string. It returns
6683
- `err:string`: error message.
6784

6885

69-
### arr, err = re:matches( sbj [, offset] )
86+
## arr, err = regex:matches( sbj [, offset] )
7087

7188
almost same as `match` method but it returns all matched substrings except capture strings.
7289

73-
**Params**
90+
**Parameters**
7491

7592
- `sbj:string`: the subject string.
7693
- `offset:number`: offset in the subject at which to start matching.
@@ -81,43 +98,41 @@ almost same as `match` method but it returns all matched substrings except captu
8198
- `err:string`: error message.
8299

83100

84-
### heads, tails, err = re:indexof( sbj [, offset] )
101+
## arr, err = regex:indexof( sbj [, offset] )
85102

86103
almost same as `match` method but it returns offsets of matched substrings.
87104

88-
**Params**
105+
**Parameters**
89106

90107
- `sbj:string`: the subject string.
91108
- `offset:number`: offset in the subject at which to start matching.
92109

93110
**Returns**
94111

95-
- `heads:table`: array of head offset of matched substrings.
96-
- `tails:table`: array of tail offset of matched substrings.
112+
- `arr:table`: array of offsets of matched substrings. 1st index is the start offset of matched substring, and 2nd index is the end offset of matched substring, and 3rd index is the start offset of 1st capture string, and 4th index is the end offset of 1st capture string, and so on.
97113
- `err:string`: error message.
98114

99115

100-
### heads, tails, err = re:indexesof( sbj [, offset] )
116+
## arr, err = regex:indexesof( sbj [, offset] )
101117

102-
almost same as `match` method but it returns all offsets of matched substrings except capture strings.
118+
almost same as `match` method but it returns all offsets of matched substrings **except capture strings**.
103119

104-
**Params**
120+
**Parameters**
105121

106122
- `sbj:string`: the subject string.
107123
- `offset:number`: offset in the subject at which to start matching.
108124

109125
**Returns**
110126

111-
- `heads:table`: array of head offset of matched substrings.
112-
- `tails:table`: array of tail offset of matched substrings.
127+
- `arr:table`: array of offsets of matched substrings. 1st index is the start offset of matched substring, and 2nd index is the end offset of matched substring, and so on.
113128
- `err:string`: error message.
114129

115130

116-
### ok, err = re:test( sbj [, offset] )
131+
## ok, err = regex:test( sbj [, offset] )
117132

118133
returns true if there is a matched.
119134

120-
**Params**
135+
**Parameters**
121136

122137
- `sbj:string`: the subject string.
123138
- `offset:number`: offset in the subject at which to start matching.
@@ -132,89 +147,67 @@ returns true if there is a matched.
132147
## Static Methods
133148

134149

135-
### arr, err = regex.match( sbj, pattern [, flgs [, offset]] )
136-
137-
same as `match` instance method.
138-
139-
**Params**
140-
141-
- `sbj:string`: the subject string.
142-
- `pattern:string`: string containing expression to be compiled.
143-
- `flgs:string`: [regular expression flags](#regular-expression-flags).
144-
- `offset:number`: offset in the subject at which to start matching.
145-
146-
**Returns**
147-
148-
- `arr:table`: array of matched substrings.
149-
- `err:string`: error message.
150-
151-
152-
### arr, err = regex.matches( sbj, pattern [, flgs [, offset]] )
153-
154-
same as `matches` instance method.
155-
156-
**Params**
157-
158-
- `sbj:string`: the subject string.
159-
- `pattern:string`: string containing expression to be compiled.
160-
- `flgs:string`: [regular expression flags](#regular-expression-flags).
161-
- `offset:number`: offset in the subject at which to start matching.
162-
163-
**Returns**
164-
165-
- `arr:table`: array of matched substrings.
166-
- `err:string`: error message.
167-
168-
169-
### heads, tails, err = regex.indexof( sbj, pattern [, flgs [, offset]] )
170-
171-
same as `indexof` instance method.
150+
## arr, err = regex.match( sbj, pattern [, flgs [, offset]] )
172151

173-
**Params**
152+
same as the following code:
174153

175-
- `sbj:string`: the subject string.
176-
- `pattern:string`: string containing expression to be compiled.
177-
- `flgs:string`: [regular expression flags](#regular-expression-flags).
178-
- `offset:number`: offset in the subject at which to start matching.
154+
```lua
155+
local re, err = regex.new( pattern, flgs )
156+
if re then
157+
return re:match( sbj, offset )
158+
end
159+
return nil, err
160+
```
179161

180-
**Returns**
181162

182-
- `heads:table`: array of head offset of matched substrings.
183-
- `tails:table`: array of tail offset of matched substrings.
184-
- `err:string`: error message.
163+
## arr, err = regex.matches( sbj, pattern [, flgs [, offset]] )
185164

165+
same as the following code:
186166

187-
### heads, tails, err = regex.indexesof( sbj, pattern [, flgs [, offset]] )
167+
```lua
168+
local re, err = regex.new( pattern, flgs )
169+
if re then
170+
return re:matches( sbj, offset )
171+
end
172+
return nil, err
173+
```
188174

189-
same as `indexesof` instance method.
190175

191-
**Params**
176+
## arr, err = regex.indexof( sbj, pattern [, flgs [, offset]] )
192177

193-
- `sbj:string`: the subject string.
194-
- `pattern:string`: string containing expression to be compiled.
195-
- `flgs:string`: [regular expression flags](#regular-expression-flags).
196-
- `offset:number`: offset in the subject at which to start matching.
178+
same as the following code:
197179

198-
**Returns**
180+
```lua
181+
local re, err = regex.new( pattern, flgs )
182+
if re then
183+
return re:indexof( sbj, offset )
184+
end
185+
return nil, err
186+
```
199187

200-
- `heads:table`: array of head offset of matched substrings.
201-
- `tails:table`: array of tail offset of matched substrings.
202-
- `err:string`: error message.
203188

189+
## arr, err = regex.indexesof( sbj, pattern [, flgs [, offset]] )
204190

205-
### ok, err = regex.test( sbj, pattern [, flgs [, offset]] )
191+
same as the following code:
206192

207-
same as `test` instance method.
193+
```lua
194+
local re, err = regex.new( pattern, flgs )
195+
if re then
196+
return re:indexesof( sbj, offset )
197+
end
198+
return nil, err
199+
```
208200

209-
**Params**
210201

211-
- `sbj:string`: the subject string.
212-
- `pattern:string`: string containing expression to be compiled.
213-
- `flgs:string`: [regular expression flags](#regular-expression-flags).
214-
- `offset:number`: offset in the subject at which to start matching.
202+
## ok, err = regex.test( sbj, pattern [, flgs [, offset]] )
215203

216-
**Returns**
204+
same as the following code:
217205

218-
- `ok:boolean`: true on matched.
219-
- `err:string`: error message.
206+
```lua
207+
local re, err = regex.new( pattern, flgs )
208+
if re then
209+
return re:test( sbj, offset )
210+
end
211+
return nil, err
212+
```
220213

0 commit comments

Comments
 (0)