Skip to content

Commit

Permalink
fix(inline): refactor escape string
Browse files Browse the repository at this point in the history
  • Loading branch information
hugefiver committed Jul 24, 2024
1 parent 37ee9c6 commit aa17529
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ require (
github.com/quic-go/qpack v0.4.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/samber/lo v1.46.0
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,8 @@ github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6ke
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/samber/lo v1.46.0 h1:w8G+oaCPgz1PoCJztqymCFaKwXt+5cCXn51uPxExFfQ=
github.com/samber/lo v1.46.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU=
github.com/sashabaranov/go-openai v1.26.3 h1:Tjnh4rcvsSU68f66r05mys+Zou4vo4qyvkne6AIRJPI=
github.com/sashabaranov/go-openai v1.26.3/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
Expand Down
12 changes: 6 additions & 6 deletions util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"csust-got/config"
"csust-got/log"

"github.com/samber/lo"

Check failure on line 16 in util/utils.go

View workflow job for this annotation

GitHub Actions / Lint / Golangci Lint

import 'github.com/samber/lo' is not allowed from list 'main' (depguard)
"go.uber.org/zap"
tb "gopkg.in/telebot.v3"
)
Expand Down Expand Up @@ -286,14 +287,13 @@ func GetAllReplyMessagesText(m *tb.Message) string {
return ret
}

var reservedChars = []string{"\\", "_", "*", "[", "]", "(", ")", "~", "`", ">", "#", "+", "-", "=", "|", "{", "}", ".", "!"}
var reservedCharsPairs = lo.FlatMap(reservedChars, func(char string, _ int) []string { return []string{char, "\\" + char} })
var escapeReplacer = strings.NewReplacer(reservedCharsPairs...)

// EscapeTelegramReservedChars escape telegram reserved chars
func EscapeTelegramReservedChars(s string) string {
reservedChars := []string{"\\", "_", "*", "[", "]", "(", ")", "~", "`", ">", "#", "+", "-", "=", "|", "{", "}", ".", "!"}

for _, char := range reservedChars {
s = strings.ReplaceAll(s, char, "\\"+char)
}

s = escapeReplacer.Replace(s)
return s
}

Expand Down

0 comments on commit aa17529

Please sign in to comment.