From 7dc14d5b5e7aad503e4939e95e9ec657a3026b21 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Tue, 29 Aug 2023 23:06:38 +0000 Subject: [PATCH] lexer: IsNot(), IsIn(), IsOneOf() Signed-off-by: Alejandro Mery --- lexer/runes.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/lexer/runes.go b/lexer/runes.go index 9b13b5a..d77cf7e 100644 --- a/lexer/runes.go +++ b/lexer/runes.go @@ -1,6 +1,38 @@ package lexer -import "unicode" +import ( + "strings" + "unicode" +) + +// IsNot generates a rune condition checker that reverses the +// decision of the given checker. +func IsNot(cond func(rune) bool) func(rune) bool { + return func(r rune) bool { + return !cond(r) + } +} + +// IsIn generates a rune condition checker that accepts runes +// contained on the provided string +func IsIn(s string) func(rune) bool { + return func(r rune) bool { + return strings.ContainsRune(s, r) + } +} + +// IsOneOf generates a run condition checker that accepts runes +// accepted by any of the given checkers +func IsOneOf(s ...func(rune) bool) func(rune) bool { + return func(r rune) bool { + for _, cond := range s { + if cond(r) { + return true + } + } + return false + } +} // IsSpace reports whether the rune is a space character as // defined by Unicode's White Space property