summaryrefslogtreecommitdiff
path: root/internal/re/re.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/re/re.go')
-rw-r--r--internal/re/re.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/internal/re/re.go b/internal/re/re.go
index b235282..f7e9543 100644
--- a/internal/re/re.go
+++ b/internal/re/re.go
@@ -2,12 +2,40 @@ package re
import (
"errors"
+ "fmt"
+ "io"
"regexp"
"strings"
+
+ lru "github.com/hashicorp/golang-lru"
)
var errNotRE = errors.New("not re")
+type RE struct {
+ last *lru.Cache
+ w io.Writer
+}
+
+func NewRE(w io.Writer) *RE {
+ last, _ := lru.New(100)
+ return &RE{last: last, w: w}
+}
+
+func (r *RE) Replace(text, nick string) {
+ defer r.last.Add(nick, text)
+ if !strings.HasPrefix(text, "s") {
+ return
+ }
+ if tofix, ok := r.last.Get(nick); ok {
+ global := strings.HasSuffix(text, "g")
+ fixed, err := Replace(tofix.(string), text[1:], global)
+ if err == nil && fixed != tofix {
+ fmt.Fprintf(r.w, "%v meant to say: %s", nick, fixed)
+ }
+ }
+}
+
func Replace(s, r string, global bool) (string, error) {
// min: at least two separators
if len(r) < 2 {