summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2018-07-24 15:54:35 +0200
committerDimitri Sokolyuk <demon@dim13.org>2018-07-24 15:54:35 +0200
commitbef0c7d77e17141db87f5ef24dc59941a13c94f5 (patch)
treede283e25437b52bff78dfb8496096c6ec6e05bff
parent53306e6ec77997d9fef102ec4b2cfc8c2b31e176 (diff)
no space compare
-rw-r--r--main.go13
-rw-r--r--main_test.go26
2 files changed, 38 insertions, 1 deletions
diff --git a/main.go b/main.go
index 2f9279b..9cf4fd4 100644
--- a/main.go
+++ b/main.go
@@ -7,6 +7,7 @@ import (
"log"
"strings"
"time"
+ "unicode"
irc "github.com/fluffle/goirc/client"
)
@@ -42,6 +43,16 @@ func (n *Notify) Write(p []byte) (int, error) {
return len(p), nil
}
+func noSpaceCompare(a, b string) bool {
+ dropSpaces := func(r rune) rune {
+ if unicode.IsSpace(r) {
+ return -1
+ }
+ return r
+ }
+ return strings.Map(dropSpaces, a) == strings.Map(dropSpaces, b)
+}
+
func privmsg(room string) irc.HandlerFunc {
var (
last = make(map[string]string)
@@ -59,7 +70,7 @@ func privmsg(room string) irc.HandlerFunc {
case isFlood(t):
log.Println("flood", line.Nick)
conn.Kick(room, line.Nick)
- case strings.TrimSpace(t) == strings.TrimSpace(last[line.Nick]):
+ case noSpaceCompare(t, last[line.Nick]):
log.Println("kick", line.Nick)
conn.Kick(room, line.Nick)
case strings.HasPrefix(t, "s"):
diff --git a/main_test.go b/main_test.go
new file mode 100644
index 0000000..51002d7
--- /dev/null
+++ b/main_test.go
@@ -0,0 +1,26 @@
+package main
+
+import "testing"
+
+func TestNoSpaceCompare(t *testing.T) {
+ testCases := []struct {
+ a, b string
+ ok bool
+ }{
+ {"", "", true},
+ {"x", "", false},
+ {"", "x", false},
+ {"x ", "x", true},
+ {" x", "x", true},
+ {"x x", "xx", true},
+ {"x x x", "x", false},
+ }
+ for _, tc := range testCases {
+ t.Run(tc.a+"?"+tc.b, func(t *testing.T) {
+ ok := noSpaceCompare(tc.a, tc.b)
+ if ok != tc.ok {
+ t.Errorf("got %v; want %v", ok, tc.ok)
+ }
+ })
+ }
+}