Skip to content
Snippets Groups Projects
Commit ce10f748 authored by Benjamin Wenger's avatar Benjamin Wenger
Browse files

improved the efficency of the emoji checker

parent beaf4ced
No related branches found
No related tags found
5 merge requests!510Release,!419rewrote the health tracker to both consider if there are waiting rounds and...,!371[Channel RSAtoPrivate] Implement Reverse Asymmetric in Client/Broadcast,!354Channels impl,!340Project/channels
...@@ -2,35 +2,43 @@ package channels ...@@ -2,35 +2,43 @@ package channels
import ( import (
"github.com/pkg/errors" "github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman"
"regexp" "regexp"
) )
const findEmoji = "(\\\\u00a9|\\\\u00ae|[\\\\u2000-\\\\u3300]|\\\\ud83c[\\\\ud000-\\\\udfff]|\\\\ud83d[\\\\ud000-\\\\udfff]|\\\\ud83e[\\\\ud000-\\\\udfff])" // found at https://www.regextester.com/106421
const findEmoji = `(\\u00a9|\\u00ae|[\\u2000-\\u3300]|\\ud83c[\\ud000-` +
`\\udfff]|\\ud83d[\\ud000-\\udfff]|\\ud83e[\\ud000-\\udfff])`
var InvalidReaction = errors.New( var InvalidReaction = errors.New(
"The reaction is not valid, it must be a single emoji") "The reaction is not valid, it must be a single emoji")
var compiledRegex *regexp.Regexp
// compile the regular expression in an init so it is only
// compiled once
func init() {
var err error
compiledRegex, err = regexp.Compile(findEmoji)
if err != nil {
jww.FATAL.Panicf("failed to compile the regex for emoji finding " +
"within channels")
}
}
// ValidateReaction checks that the reaction only contains a single emoji. // ValidateReaction checks that the reaction only contains a single emoji.
func ValidateReaction(reaction string) error { func ValidateReaction(reaction string) error {
//make sure it is only only character
reactRunes := []rune(reaction) reactRunes := []rune(reaction)
if len(reactRunes) > 1 { if len(reactRunes) > 1 {
return InvalidReaction return InvalidReaction
} }
reg, err := regexp.Compile(findEmoji) // make sure it is only one emoji
if err != nil { if !compiledRegex.Match([]byte(reaction)) {
return err
}
if !reg.Match([]byte(reaction)) {
return InvalidReaction return InvalidReaction
} }
/*
fmt.Println(string(reactRunes[0]))
fmt.Println(reactRunes[1])
fmt.Println(reactRunes[2])
jww.WARN.Printf("Reaction Validation Not Yet Implemented")*/
return nil return nil
} }
package channels package channels
/* import (
"fmt"
"regexp"
"testing"
)
func TestValidateReaction(t *testing.T) { func TestValidateReaction(t *testing.T) {
r := "🍆" r := "🍆"
...@@ -18,4 +23,4 @@ func TestValidateReaction(t *testing.T) { ...@@ -18,4 +23,4 @@ func TestValidateReaction(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("Got error: %+v", err) t.Errorf("Got error: %+v", err)
} }
}*/ }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment