From: Eduardo Date: Thu, 28 Mar 2024 19:32:56 +0000 (+0100) Subject: added some more util functions X-Git-Url: http://git.edufdez.es/?a=commitdiff_plain;h=777eaf81e8010b3bbac7d660742c24c459e66eb0;p=sylphiette-bot.git added some more util functions --- diff --git a/go.mod b/go.mod index 7c87ada..94c3408 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,8 @@ require ( github.com/gorilla/websocket v1.5.1 // indirect ) +require golang.org/x/text v0.14.0 + require ( golang.org/x/crypto v0.18.0 // indirect golang.org/x/net v0.20.0 // indirect diff --git a/go.sum b/go.sum index f488647..c97f36b 100644 --- a/go.sum +++ b/go.sum @@ -16,4 +16,6 @@ golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/utils.go b/utils.go index fd4d73b..9f1594d 100644 --- a/utils.go +++ b/utils.go @@ -3,6 +3,12 @@ package main import ( "regexp" "strings" + "unicode" + + "golang.org/x/text/runes" + "golang.org/x/text/transform" + "golang.org/x/text/unicode/norm" + "golang.org/x/text/width" ) // Replace all strings in a string array with a new string @@ -38,3 +44,51 @@ func getRequestedBy(s string) string { } return "???" } + +func normaliceString(s string) string { + + t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC, runes.If(runes.In(unicode.Latin), width.Fold, nil)) + result, _, _ := transform.String(t, s) + + return result +} + +// Remove exclamation marks, question marks, apostrophes, and punctuation from a string +func removePunctuation(s string) string { + // Define the punctuation characters to remove + punctuation := []string{"¡", "!", "¿", "?", "'", ".", ",", ";", ":", "-", "_", "\"", "\n"} + + // Replace each punctuation character with an empty string + for _, p := range punctuation { + s = strings.ReplaceAll(s, p, "") + } + + return s +} + +// Replace multiple spaces with a single space in a string +func trimMultipleSpaces(s string) string { + re := regexp.MustCompile(`\s+`) + return re.ReplaceAllString(s, " ") +} + +// Replace multiple spaces with a single space in a string +func removeCommand(s string) string { + re := regexp.MustCompile(`^/[^/\s]+`) + commandMatch := re.FindString(s) + if commandMatch != "" { + s = strings.TrimPrefix(s, commandMatch) + s = strings.TrimSpace(s) + } + return s +} + +// get only command from a string (starting /word) +func getCommand(s string) string { + re := regexp.MustCompile(`^/[^/\s]+`) + commandMatch := re.FindString(s) + if commandMatch != "" { + return commandMatch + } + return "" +}