From 81086b8fac0682cc09f1dd15d978698b82a1c0ed Mon Sep 17 00:00:00 2001 From: E Sequeira <5458743+geseq@users.noreply.github.com> Date: Fri, 23 May 2025 14:21:47 +0100 Subject: [PATCH 1/3] fix regex alternation --- stdlib/text.go | 28 +++++++++++--------- stdlib/text_regexp_test.go | 52 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 stdlib/text_regexp_test.go diff --git a/stdlib/text.go b/stdlib/text.go index d7d5d1da..c40b68a5 100644 --- a/stdlib/text.go +++ b/stdlib/text.go @@ -284,12 +284,14 @@ func textREFind(args ...tengo.Object) (ret tengo.Object, err error) { arr := &tengo.Array{} for i := 0; i < len(m); i += 2 { - arr.Value = append(arr.Value, - &tengo.ImmutableMap{Value: map[string]tengo.Object{ - "text": &tengo.String{Value: s2[m[i]:m[i+1]]}, - "begin": &tengo.Int{Value: int64(m[i])}, - "end": &tengo.Int{Value: int64(m[i+1])}, - }}) + if m[i] >= 0 && m[i+1] >= 0 { + arr.Value = append(arr.Value, + &tengo.ImmutableMap{Value: map[string]tengo.Object{ + "text": &tengo.String{Value: s2[m[i]:m[i+1]]}, + "begin": &tengo.Int{Value: int64(m[i])}, + "end": &tengo.Int{Value: int64(m[i+1])}, + }}) + } } ret = &tengo.Array{Value: []tengo.Object{arr}} @@ -316,12 +318,14 @@ func textREFind(args ...tengo.Object) (ret tengo.Object, err error) { for _, m := range m { subMatch := &tengo.Array{} for i := 0; i < len(m); i += 2 { - subMatch.Value = append(subMatch.Value, - &tengo.ImmutableMap{Value: map[string]tengo.Object{ - "text": &tengo.String{Value: s2[m[i]:m[i+1]]}, - "begin": &tengo.Int{Value: int64(m[i])}, - "end": &tengo.Int{Value: int64(m[i+1])}, - }}) + if m[i] >= 0 && m[i+1] >= 0 { + subMatch.Value = append(subMatch.Value, + &tengo.ImmutableMap{Value: map[string]tengo.Object{ + "text": &tengo.String{Value: s2[m[i]:m[i+1]]}, + "begin": &tengo.Int{Value: int64(m[i])}, + "end": &tengo.Int{Value: int64(m[i+1])}, + }}) + } } arr.Value = append(arr.Value, subMatch) diff --git a/stdlib/text_regexp_test.go b/stdlib/text_regexp_test.go new file mode 100644 index 00000000..6d253866 --- /dev/null +++ b/stdlib/text_regexp_test.go @@ -0,0 +1,52 @@ +package stdlib_test + +import ( + "testing" + + "github.com/d5/tengo/v2" +) + +func TestTextREAlternation(t *testing.T) { + module(t, "text").call("re_find", "([a-zA-Z])|([0-9])", "a").expect(ARR{ + ARR{ + IMAP{"text": "a", "begin": 0, "end": 1}, + IMAP{"text": "a", "begin": 0, "end": 1}, + }, + }, "alternation with letter") + + module(t, "text").call("re_find", "([a-zA-Z])|([0-9])", "5").expect(ARR{ + ARR{ + IMAP{"text": "5", "begin": 0, "end": 1}, + IMAP{"text": "5", "begin": 0, "end": 1}, + }, + }, "alternation with number") + + module(t, "text").call("re_find", "([a-zA-Z])|([0-9])", "").expect(tengo.UndefinedValue, "empty input") + + module(t, "text").call("re_find", "([a-zA-Z])|([0-9])", "!").expect(tengo.UndefinedValue, "non-matching input") + + module(t, "text").call("re_find", "(?:([a-zA-Z])|([0-9]))+", "a5b").expect(ARR{ + ARR{ + IMAP{"text": "a5b", "begin": 0, "end": 3}, + IMAP{"text": "b", "begin": 2, "end": 3}, + IMAP{"text": "5", "begin": 1, "end": 2}, + }, + }, "multiple alternations") + + module(t, "text").call("re_find", "(foo)|(bar)|(baz)", "foo").expect(ARR{ + ARR{ + IMAP{"text": "foo", "begin": 0, "end": 3}, + IMAP{"text": "foo", "begin": 0, "end": 3}, + }, + }, "multiple groups with non-matches") + + module(t, "text").call("re_find", "((cat)|(dog))((run)|(walk))", "catrun").expect(ARR{ + ARR{ + IMAP{"text": "catrun", "begin": 0, "end": 6}, + IMAP{"text": "cat", "begin": 0, "end": 3}, + IMAP{"text": "cat", "begin": 0, "end": 3}, + IMAP{"text": "run", "begin": 3, "end": 6}, + IMAP{"text": "run", "begin": 3, "end": 6}, + }, + }, "nested groups with alternation") +} From 7bf39c24a2eed01577f6873a2940b4bab9574979 Mon Sep 17 00:00:00 2001 From: E Sequeira <5458743+geseq@users.noreply.github.com> Date: Fri, 23 May 2025 14:34:38 +0100 Subject: [PATCH 2/3] update ci --- .github/workflows/test.yml | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5edb1a3a..64f2ff6b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,25 +9,19 @@ jobs: name: build runs-on: ubuntu-latest steps: + - name: check out code + uses: actions/checkout@v4 - name: set up Go - uses: actions/setup-go@v1 + uses: actions/setup-go@v4 with: go-version: 1.18 - id: go - name: set up Go module cache - uses: actions/cache@v1 + uses: actions/cache@v4 with: path: ~/go/pkg/mod key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-go- - - name: setup env - run: | - echo "name=GOPATH::$(go env GOPATH)" >> $GITHUB_ENV - echo "$(go env GOPATH)/bin" >> $GITHUB_PATH - shell: bash - - name: check out code - uses: actions/checkout@v2 - name: install golint run: go install golang.org/x/lint/golint@latest - name: run tests From cd20ab8988b392628ce3a6a52aa1b72f5e17c3d9 Mon Sep 17 00:00:00 2001 From: E Sequeira <5458743+geseq@users.noreply.github.com> Date: Fri, 23 May 2025 14:38:12 +0100 Subject: [PATCH 3/3] Update release.yml --- .github/workflows/release.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index cb363d99..1fbf3789 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -8,17 +8,17 @@ jobs: runs-on: ubuntu-latest steps: - name: check out - uses: actions/checkout@v2 - - name: Unshallow - run: git fetch --prune --unshallow + uses: actions/checkout@v4 + with: + fetch-depth: 0 - name: set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v4 with: go-version: 1.18 - name: run goreleaser - uses: goreleaser/goreleaser-action@v3 + uses: goreleaser/goreleaser-action@v5 with: version: latest - args: release --rm-dist + args: release --clean env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}