mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-10 05:57:51 +00:00
tools: Shuffle go code into an idiomatic tree
main packages usually go under a `cmd` directory. Hoist utility packages to the root `src` directroy so they can be shared. Change-Id: I0c221f6cd39980f5c202c030cd5134d775533efa Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/50901 Commit-Queue: Ben Clayton <bclayton@google.com> Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
committed by
Commit Bot service account
parent
dc4e6c1844
commit
54f4a21ee0
52
tools/src/substr/substr.go
Normal file
52
tools/src/substr/substr.go
Normal file
@@ -0,0 +1,52 @@
|
||||
// Copyright 2021 The Tint Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package substr
|
||||
|
||||
import (
|
||||
diff "github.com/sergi/go-diff/diffmatchpatch"
|
||||
)
|
||||
|
||||
// Fix attempts to reconstruct substr by comparing it to body.
|
||||
// substr is a fuzzy substring of body.
|
||||
// Fix returns a new exact substring of body, by calculating a diff of the text.
|
||||
// If no match could be made, Fix() returns an empty string.
|
||||
func Fix(body, substr string) string {
|
||||
dmp := diff.New()
|
||||
|
||||
diffs := dmp.DiffMain(body, substr, false)
|
||||
if len(diffs) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
front := func() diff.Diff { return diffs[0] }
|
||||
back := func() diff.Diff { return diffs[len(diffs)-1] }
|
||||
|
||||
start, end := 0, len(body)
|
||||
|
||||
// Trim edits that remove text from body start
|
||||
for len(diffs) > 0 && front().Type == diff.DiffDelete {
|
||||
start += len(front().Text)
|
||||
diffs = diffs[1:]
|
||||
}
|
||||
|
||||
// Trim edits that remove text from body end
|
||||
for len(diffs) > 0 && back().Type == diff.DiffDelete {
|
||||
end -= len(back().Text)
|
||||
diffs = diffs[:len(diffs)-1]
|
||||
}
|
||||
|
||||
// New substring is the span for the remainder of the edits
|
||||
return body[start:end]
|
||||
}
|
||||
275
tools/src/substr/substr_test.go
Normal file
275
tools/src/substr/substr_test.go
Normal file
@@ -0,0 +1,275 @@
|
||||
// Copyright 2021 The Tint Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package substr
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFixSubstr(t *testing.T) {
|
||||
type test struct {
|
||||
body string
|
||||
substr string
|
||||
expect string
|
||||
}
|
||||
|
||||
for _, test := range []test{
|
||||
{
|
||||
body: "abc_def_ghi_jkl_mno",
|
||||
substr: "def_XXX_jkl",
|
||||
expect: "def_ghi_jkl",
|
||||
},
|
||||
{
|
||||
body: "abc\ndef\nghi\njkl\nmno",
|
||||
substr: "def\nXXX\njkl",
|
||||
expect: "def\nghi\njkl",
|
||||
},
|
||||
{
|
||||
body: "aaaaa12345ccccc",
|
||||
substr: "1x345",
|
||||
expect: "12345",
|
||||
},
|
||||
{
|
||||
body: "aaaaa12345ccccc",
|
||||
substr: "12x45",
|
||||
expect: "12345",
|
||||
},
|
||||
{
|
||||
body: "aaaaa12345ccccc",
|
||||
substr: "123x5",
|
||||
expect: "12345",
|
||||
},
|
||||
{
|
||||
body: "aaaaaaaaaaaaa",
|
||||
substr: "bbbbbbbbbbbbb",
|
||||
expect: "", // cannot produce a sensible diff
|
||||
}, { ///////////////////////////////////////////////////////////////////
|
||||
body: `Return{
|
||||
{
|
||||
ScalarConstructor[not set]{42u}
|
||||
}
|
||||
}
|
||||
`,
|
||||
substr: `Return{
|
||||
{
|
||||
ScalarConstructor[not set]{42}
|
||||
}
|
||||
}`,
|
||||
expect: `Return{
|
||||
{
|
||||
ScalarConstructor[not set]{42u}
|
||||
}
|
||||
}`,
|
||||
}, { ///////////////////////////////////////////////////////////////////
|
||||
body: `VariableDeclStatement{
|
||||
Variable{
|
||||
x_1
|
||||
function
|
||||
__u32
|
||||
}
|
||||
}
|
||||
Assignment{
|
||||
Identifier[not set]{x_1}
|
||||
ScalarConstructor[not set]{42u}
|
||||
}
|
||||
Assignment{
|
||||
Identifier[not set]{x_1}
|
||||
ScalarConstructor[not set]{0u}
|
||||
}
|
||||
Return{}
|
||||
`,
|
||||
substr: `Assignment{
|
||||
Identifier[not set]{x_1}
|
||||
ScalarConstructor[not set]{42}
|
||||
}
|
||||
Assignment{
|
||||
Identifier[not set]{x_1}
|
||||
ScalarConstructor[not set]{0}
|
||||
}`,
|
||||
expect: `Assignment{
|
||||
Identifier[not set]{x_1}
|
||||
ScalarConstructor[not set]{42u}
|
||||
}
|
||||
Assignment{
|
||||
Identifier[not set]{x_1}
|
||||
ScalarConstructor[not set]{0u}
|
||||
}`,
|
||||
}, { ///////////////////////////////////////////////////////////////////
|
||||
body: `VariableDeclStatement{
|
||||
Variable{
|
||||
a
|
||||
function
|
||||
__bool
|
||||
{
|
||||
ScalarConstructor[not set]{true}
|
||||
}
|
||||
}
|
||||
}
|
||||
VariableDeclStatement{
|
||||
Variable{
|
||||
b
|
||||
function
|
||||
__bool
|
||||
{
|
||||
ScalarConstructor[not set]{false}
|
||||
}
|
||||
}
|
||||
}
|
||||
VariableDeclStatement{
|
||||
Variable{
|
||||
c
|
||||
function
|
||||
__i32
|
||||
{
|
||||
ScalarConstructor[not set]{-1}
|
||||
}
|
||||
}
|
||||
}
|
||||
VariableDeclStatement{
|
||||
Variable{
|
||||
d
|
||||
function
|
||||
__u32
|
||||
{
|
||||
ScalarConstructor[not set]{1u}
|
||||
}
|
||||
}
|
||||
}
|
||||
VariableDeclStatement{
|
||||
Variable{
|
||||
e
|
||||
function
|
||||
__f32
|
||||
{
|
||||
ScalarConstructor[not set]{1.500000}
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
substr: `VariableDeclStatement{
|
||||
Variable{
|
||||
a
|
||||
function
|
||||
__bool
|
||||
{
|
||||
ScalarConstructor[not set]{true}
|
||||
}
|
||||
}
|
||||
}
|
||||
VariableDeclStatement{
|
||||
Variable{
|
||||
b
|
||||
function
|
||||
__bool
|
||||
{
|
||||
ScalarConstructor[not set]{false}
|
||||
}
|
||||
}
|
||||
}
|
||||
VariableDeclStatement{
|
||||
Variable{
|
||||
c
|
||||
function
|
||||
__i32
|
||||
{
|
||||
ScalarConstructor[not set]{-1}
|
||||
}
|
||||
}
|
||||
}
|
||||
VariableDeclStatement{
|
||||
Variable{
|
||||
d
|
||||
function
|
||||
__u32
|
||||
{
|
||||
ScalarConstructor[not set]{1}
|
||||
}
|
||||
}
|
||||
}
|
||||
VariableDeclStatement{
|
||||
Variable{
|
||||
e
|
||||
function
|
||||
__f32
|
||||
{
|
||||
ScalarConstructor[not set]{1.500000}
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
expect: `VariableDeclStatement{
|
||||
Variable{
|
||||
a
|
||||
function
|
||||
__bool
|
||||
{
|
||||
ScalarConstructor[not set]{true}
|
||||
}
|
||||
}
|
||||
}
|
||||
VariableDeclStatement{
|
||||
Variable{
|
||||
b
|
||||
function
|
||||
__bool
|
||||
{
|
||||
ScalarConstructor[not set]{false}
|
||||
}
|
||||
}
|
||||
}
|
||||
VariableDeclStatement{
|
||||
Variable{
|
||||
c
|
||||
function
|
||||
__i32
|
||||
{
|
||||
ScalarConstructor[not set]{-1}
|
||||
}
|
||||
}
|
||||
}
|
||||
VariableDeclStatement{
|
||||
Variable{
|
||||
d
|
||||
function
|
||||
__u32
|
||||
{
|
||||
ScalarConstructor[not set]{1u}
|
||||
}
|
||||
}
|
||||
}
|
||||
VariableDeclStatement{
|
||||
Variable{
|
||||
e
|
||||
function
|
||||
__f32
|
||||
{
|
||||
ScalarConstructor[not set]{1.500000}
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
},
|
||||
} {
|
||||
body := strings.ReplaceAll(test.body, "\n", "")
|
||||
substr := strings.ReplaceAll(test.substr, "\n", "")
|
||||
expect := strings.ReplaceAll(test.expect, "\n", ``)
|
||||
got := strings.ReplaceAll(Fix(test.body, test.substr), "\n", "")
|
||||
if got != expect {
|
||||
t.Errorf("Test failure:\nbody: '%v'\nsubstr: '%v'\nexpect: '%v'\ngot: '%v'\n\n", body, substr, expect, got)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user