Skip to content

Commit

Permalink
feat(stdlibs): add strings.Replacer (#2816)
Browse files Browse the repository at this point in the history
<!-- please provide a detailed description of the changes made in this
pull request. -->
prerequisite of #2802 

This pull request ports the files:
- replace.go
- replace_test.go

from the Golang standard library.

I added some tags on the code with the hope it will help to review the
code and to launch discussion if neccessary.
I could after remove these changes
```go
// Custom code: XXX_Some_Explanation
( code not present on the original go file)
.
.
.
// End of custom code
```

 

<details><summary>Contributors' checklist...</summary>

- [ ] Added new tests, or not needed, or not feasible
- [ ] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [ ] Updated the official documentation or not needed
- [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [ ] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
- [ ] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>
  • Loading branch information
Villaquiranm authored Oct 2, 2024
1 parent ee2b1fa commit a2b4d4b
Show file tree
Hide file tree
Showing 3 changed files with 1,200 additions and 0 deletions.
102 changes: 102 additions & 0 deletions gnovm/stdlibs/strings/printtrie_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package strings

import (
"testing"
)

func (r *Replacer) PrintTrie() string {
r.buildOnce()
gen := r.r.(*genericReplacer)
return gen.printNode(&gen.root, 0)
}

func (r *genericReplacer) printNode(t *trieNode, depth int) (s string) {
if t.priority > 0 {
s += "+"
} else {
s += "-"
}
s += "\n"

if t.prefix != "" {
s += Repeat(".", depth) + t.prefix
s += r.printNode(t.next, depth+len(t.prefix))
} else if t.table != nil {
for b, m := range r.mapping {
if int(m) != r.tableSize && t.table[m] != nil {
s += Repeat(".", depth) + string([]byte{byte(b)})
s += r.printNode(t.table[m], depth+1)
}
}
}
return
}

func TestGenericTrieBuilding(t *testing.T) {
testCases := []struct{ in, out string }{
{"abc;abdef;abdefgh;xx;xy;z", `-
a-
.b-
..c+
..d-
...ef+
.....gh+
x-
.x+
.y+
z+
`},
{"abracadabra;abracadabrakazam;abraham;abrasion", `-
a-
.bra-
....c-
.....adabra+
...........kazam+
....h-
.....am+
....s-
.....ion+
`},
{"aaa;aa;a;i;longerst;longer;long;xx;x;X;Y", `-
X+
Y+
a+
.a+
..a+
i+
l-
.ong+
....er+
......st+
x+
.x+
`},
{"foo;;foo;foo1", `+
f-
.oo+
...1+
`},
}

for _, tc := range testCases {
keys := Split(tc.in, ";")
args := make([]string, len(keys)*2)
for i, key := range keys {
args[i*2] = key
}

got := NewReplacer(args...).PrintTrie()
// Remove tabs from tc.out
wantbuf := make([]byte, 0, len(tc.out))
for i := 0; i < len(tc.out); i++ {
if tc.out[i] != '\t' {
wantbuf = append(wantbuf, tc.out[i])
}
}
want := string(wantbuf)

if got != want {
t.Errorf("PrintTrie(%q)\ngot\n%swant\n%s", tc.in, got, want)
}
}
}
Loading

0 comments on commit a2b4d4b

Please sign in to comment.