Skip to content

Commit

Permalink
Optimize decode
Browse files Browse the repository at this point in the history
  • Loading branch information
Ty3uK committed Sep 27, 2024
1 parent 203170d commit ac9d274
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions internal/resolvers/snap/Decode.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
package snap

import (
"fmt"
"strconv"
"strings"
"unicode"
)

func Decode(h string, _ int, n string, t uint64, e int, _ int) (string, error) {
var result strings.Builder
var replaces []string
result.Grow(len(h))

replaces := make([]string, 0, len(n)*2)
for j := 0; j < len(n); j++ {
replaces = append(replaces, string(n[j]), strconv.Itoa(j))
}
replacer := strings.NewReplacer(replaces...)

var sb strings.Builder
for i := 0; i < len(h); i++ {
s := ""
sb.Reset()

for i < len(h) && h[i] != n[e] {
s += string(h[i])
sb.WriteByte(h[i])
i++
}

replaces = []string{}
for j := 0; j < len(n); j++ {
replaces = append(replaces, string(n[j]), fmt.Sprint(j))
}
s = strings.NewReplacer(replaces...).Replace(s)
s := replacer.Replace(sb.String())

if !unicode.IsDigit(rune(s[0])) {
result.WriteString(s)
Expand Down

0 comments on commit ac9d274

Please sign in to comment.