8000 fix regex alternation by geseq · Pull Request #460 · d5/tengo · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix regex alternation #460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
14 changes: 4 additions & 10 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 16 additions & 12 deletions stdlib/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand All @@ -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)
Expand Down
52 changes: 52 additions & 0 deletions stdlib/text_regexp_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
Loading
0