diff options
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/map.nim | 21 | ||||
-rw-r--r-- | src/utils/opt.nim | 7 | ||||
-rw-r--r-- | src/utils/twtstr.nim | 14 |
3 files changed, 31 insertions, 11 deletions
diff --git a/src/utils/map.nim b/src/utils/map.nim new file mode 100644 index 00000000..526b9154 --- /dev/null +++ b/src/utils/map.nim @@ -0,0 +1,21 @@ +import algorithm + +func searchInMap*[U, T](a: openarray[(U, T)], u: U): int = + when not (typeof(u) is U): + if c > cast[typeof(c)](high(U)): + return -1 + binarySearch(a, u, proc(x: (U, T), y: U): int = cmp(x[0], y)) + +func isInMap*[U, T](a: openarray[(U, T)], u: U): bool = + a.searchInMap(u) != -1 + +func isInRange*[U](a: openarray[(U, U)], u: U): bool = + let res = binarySearch(a, u, proc(x: (U, U), y: U): int = + if x[0] < y: + -1 + elif x[1] > y: + 1 + else: + 0 + ) + return res != -1 diff --git a/src/utils/opt.nim b/src/utils/opt.nim index bcc4c595..7dd045af 100644 --- a/src/utils/opt.nim +++ b/src/utils/opt.nim @@ -100,10 +100,7 @@ template `?`*[T, E](res: Result[T, E]): auto = else: when typeof(result) is Result[T, E]: return x - elif typeof(result).errType is E: - when E is void: - return err() - else: - return err(x.error) + elif not (E is void) and typeof(result).errType is E: + return err(x.error) else: return err() diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim index 35023290..0c37439a 100644 --- a/src/utils/twtstr.nim +++ b/src/utils/twtstr.nim @@ -3,7 +3,6 @@ import strutils import unicode import os import math -import sugar import sequtils import options import punycode @@ -12,6 +11,7 @@ import bindings/libunicode import data/idna import data/charwidth import utils/opt +import utils/map when defined(posix): import posix @@ -308,7 +308,8 @@ const romanNumbers = [ (40, "XL"), (10, "X"), (9, "IX"), (5, "V"), (4, "IV"), (1, "I") ] -const romanNumbers_lower = romanNumbers.map((x) => (x[0], x[1].tolower())) +const romanNumbers_lower = romanNumbers.map(proc(x: auto): auto = + (x[0], x[1].tolower())) func romanNumber*(i: int): string = return number_additive(i, 1..3999, romanNumbers) @@ -721,6 +722,7 @@ func unicodeToAscii*(s: string, checkhyphens, checkbidi, checkjoiners, transitio return none(string) #error return some(labels.join('.')) + # https://www.w3.org/TR/xml/#NT-Name const NameStartCharRanges = [ (0xC0, 0xD6), @@ -755,7 +757,7 @@ func matchNameProduction*(str: string): bool = inc i else: fastRuneAt(str, i, r) - if binarySearch(NameStartCharRanges, int32(r), (x, y) => cmp(x[0], y)) == -1: + if not isInRange(NameStartCharRanges, int32(r)): return false # NameChar while i < str.len: @@ -765,9 +767,9 @@ func matchNameProduction*(str: string): bool = inc i else: fastRuneAt(str, i, r) - if binarySearch(NameStartCharRanges, int32(r), (x, y) => cmp(x[0], y)) == -1: - if binarySearch(NameCharRanges, int32(r), (x, y) => cmp(x[0], y)) == -1: - return false + if not isInRange(NameStartCharRanges, int32(r)) and + not isInMap(NameCharRanges, int32(r)): + return false return true func matchQNameProduction*(s: string): bool = |