about summary refs log tree commit diff stats
path: root/src/utils/twtstr.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-02-17 22:03:07 +0100
committerbptato <nincsnevem662@gmail.com>2024-02-17 22:07:11 +0100
commit390772358cedc9ed541a27b3cac1f8d97beef0ef (patch)
tree621e56ad7b1b4957b1ba686a8902381ce270619c /src/utils/twtstr.nim
parente98d0ad1dc51050eb17120f835847d55950c2a0b (diff)
downloadchawan-390772358cedc9ed541a27b3cac1f8d97beef0ef.tar.gz
regex: re-work compileSearchRegex
I've gotten tired of not being able to search for forward slashes.

Now it works like in vim, and you can also set default ignore case in
the config.
Diffstat (limited to 'src/utils/twtstr.nim')
-rw-r--r--src/utils/twtstr.nim16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim
index 80b6be65..1916fff5 100644
--- a/src/utils/twtstr.nim
+++ b/src/utils/twtstr.nim
@@ -713,3 +713,19 @@ proc makeCRLF*(s: string): string =
       result &= '\n'
     else:
       result &= s[i]
+
+func strictParseEnum*[T: enum](s: string): Opt[T] =
+  # cmp when len is small enough, otherwise hashmap
+  when {T.low..T.high}.len <= 4:
+    for e in T.low .. T.high:
+      if $e == s:
+        return ok(e)
+  else:
+    const tab = (func(): Table[string, T] =
+      result = initTable[string, T]()
+      for e in T.low .. T.high:
+        result[$e] = e
+    )()
+    if s in tab:
+      return ok(tab[s])
+  return err()