Skip to content

Commit e78e18a

Browse files
authored
fix regex alternation (#460)
* fix regex alternation * update ci * Update release.yml
1 parent c461a7f commit e78e18a

4 files changed

Lines changed: 78 additions & 28 deletions

File tree

.github/workflows/release.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ jobs:
88
runs-on: ubuntu-latest
99
steps:
1010
- name: check out
11-
uses: actions/checkout@v2
12-
- name: Unshallow
13-
run: git fetch --prune --unshallow
11+
uses: actions/checkout@v4
12+
with:
13+
fetch-depth: 0
1414
- name: set up Go
15-
uses: actions/setup-go@v2
15+
uses: actions/setup-go@v4
1616
with:
1717
go-version: 1.18
1818
- name: run goreleaser
19-
uses: goreleaser/goreleaser-action@v3
19+
uses: goreleaser/goreleaser-action@v5
2020
with:
2121
version: latest
22-
args: release --rm-dist
22+
args: release --clean
2323
env:
2424
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/test.yml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,19 @@ jobs:
99
name: build
1010
runs-on: ubuntu-latest
1111
steps:
12+
- name: check out code
13+
uses: actions/checkout@v4
1214
- name: set up Go
13-
uses: actions/setup-go@v1
15+
uses: actions/setup-go@v4
1416
with:
1517
go-version: 1.18
16-
id: go
1718
- name: set up Go module cache
18-
uses: actions/cache@v1
19+
uses: actions/cache@v4
1920
with:
2021
path: ~/go/pkg/mod
2122
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
2223
restore-keys: |
2324
${{ runner.os }}-go-
24-
- name: setup env
25-
run: |
26-
echo "name=GOPATH::$(go env GOPATH)" >> $GITHUB_ENV
27-
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
28-
shell: bash
29-
- name: check out code
30-
uses: actions/checkout@v2
3125
- name: install golint
3226
run: go install golang.org/x/lint/golint@latest
3327
- name: run tests

stdlib/text.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,14 @@ func textREFind(args ...tengo.Object) (ret tengo.Object, err error) {
284284

285285
arr := &tengo.Array{}
286286
for i := 0; i < len(m); i += 2 {
287-
arr.Value = append(arr.Value,
288-
&tengo.ImmutableMap{Value: map[string]tengo.Object{
289-
"text": &tengo.String{Value: s2[m[i]:m[i+1]]},
290-
"begin": &tengo.Int{Value: int64(m[i])},
291-
"end": &tengo.Int{Value: int64(m[i+1])},
292-
}})
287+
if m[i] >= 0 && m[i+1] >= 0 {
288+
arr.Value = append(arr.Value,
289+
&tengo.ImmutableMap{Value: map[string]tengo.Object{
290+
"text": &tengo.String{Value: s2[m[i]:m[i+1]]},
291+
"begin": &tengo.Int{Value: int64(m[i])},
292+
"end": &tengo.Int{Value: int64(m[i+1])},
293+
}})
294+
}
293295
}
294296

295297
ret = &tengo.Array{Value: []tengo.Object{arr}}
@@ -316,12 +318,14 @@ func textREFind(args ...tengo.Object) (ret tengo.Object, err error) {
316318
for _, m := range m {
317319
subMatch := &tengo.Array{}
318320
for i := 0; i < len(m); i += 2 {
319-
subMatch.Value = append(subMatch.Value,
320-
&tengo.ImmutableMap{Value: map[string]tengo.Object{
321-
"text": &tengo.String{Value: s2[m[i]:m[i+1]]},
322-
"begin": &tengo.Int{Value: int64(m[i])},
323-
"end": &tengo.Int{Value: int64(m[i+1])},
324-
}})
321+
if m[i] >= 0 && m[i+1] >= 0 {
322+
subMatch.Value = append(subMatch.Value,
323+
&tengo.ImmutableMap{Value: map[string]tengo.Object{
324+
"text": &tengo.String{Value: s2[m[i]:m[i+1]]},
325+
"begin": &tengo.Int{Value: int64(m[i])},
326+
"end": &tengo.Int{Value: int64(m[i+1])},
327+
}})
328+
}
325329
}
326330

327331
arr.Value = append(arr.Value, subMatch)

stdlib/text_regexp_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package stdlib_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/d5/tengo/v2"
7+
)
8+
9+
func TestTextREAlternation(t *testing.T) {
10+
module(t, "text").call("re_find", "([a-zA-Z])|([0-9])", "a").expect(ARR{
11+
ARR{
12+
IMAP{"text": "a", "begin": 0, "end": 1},
13+
IMAP{"text": "a", "begin": 0, "end": 1},
14+
},
15+
}, "alternation with letter")
16+
17+
module(t, "text").call("re_find", "([a-zA-Z])|([0-9])", "5").expect(ARR{
18+
ARR{
19+
IMAP{"text": "5", "begin": 0, "end": 1},
20+
IMAP{"text": "5", "begin": 0, "end": 1},
21+
},
22+
}, "alternation with number")
23+
24+
module(t, "text").call("re_find", "([a-zA-Z])|([0-9])", "").expect(tengo.UndefinedValue, "empty input")
25+
26+
module(t, "text").call("re_find", "([a-zA-Z])|([0-9])", "!").expect(tengo.UndefinedValue, "non-matching input")
27+
28+
module(t, "text").call("re_find", "(?:([a-zA-Z])|([0-9]))+", "a5b").expect(ARR{
29+
ARR{
30+
IMAP{"text": "a5b", "begin": 0, "end": 3},
31+
IMAP{"text": "b", "begin": 2, "end": 3},
32+
IMAP{"text": "5", "begin": 1, "end": 2},
33+
},
34+
}, "multiple alternations")
35+
36+
module(t, "text").call("re_find", "(foo)|(bar)|(baz)", "foo").expect(ARR{
37+
ARR{
38+
IMAP{"text": "foo", "begin": 0, "end": 3},
39+
IMAP{"text": "foo", "begin": 0, "end": 3},
40+
},
41+
}, "multiple groups with non-matches")
42+
43+
module(t, "text").call("re_find", "((cat)|(dog))((run)|(walk))", "catrun").expect(ARR{
44+
ARR{
45+
IMAP{"text": "catrun", "begin": 0, "end": 6},
46+
IMAP{"text": "cat", "begin": 0, "end": 3},
47+
IMAP{"text": "cat", "begin": 0, "end": 3},
48+
IMAP{"text": "run", "begin": 3, "end": 6},
49+
IMAP{"text": "run", "begin": 3, "end": 6},
50+
},
51+
}, "nested groups with alternation")
52+
}

0 commit comments

Comments
 (0)