added some more util functions
authorEduardo <[email protected]>
Thu, 28 Mar 2024 19:32:56 +0000 (20:32 +0100)
committerEduardo <[email protected]>
Thu, 28 Mar 2024 19:33:19 +0000 (20:33 +0100)
go.mod
go.sum
utils.go

diff --git a/go.mod b/go.mod
index 7c87ada55455107fa9e7a67c815dbd3d61b35385..94c340842a099011da85773542c99be58fbbedda 100644 (file)
--- 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 f48864768bbb3578c5ed68175dee993065c9f2b8..c97f36bce269c977d7181c9800ab883e5ed07edc 100644 (file)
--- 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=
index fd4d73b508d2d047000c28547b87f0a21789bc69..9f1594d2e06206106661c31208189c4b62e9594a 100644 (file)
--- 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 ""
+}