about summary refs log tree commit diff stats
path: root/src/utils
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-04-16 15:08:44 +0200
committerbptato <nincsnevem662@gmail.com>2024-04-17 23:19:09 +0200
commit66b9574b165be62e76c7397cf0eaa8d229d42675 (patch)
treeadb8a9719cc70f2b577706aaa4e30bb0d5d629a6 /src/utils
parentd86f1939204eee771a30f47e4cbe71fd8d9a4f5f (diff)
downloadchawan-66b9574b165be62e76c7397cf0eaa8d229d42675.tar.gz
Update code style
* separate params with ; (semicolon) instead of , (colon)
* reduce screaming snake case use
* wrap long lines
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/luwrap.nim32
-rw-r--r--src/utils/proptable.nim4
-rw-r--r--src/utils/strwidth.nim24
-rw-r--r--src/utils/twtstr.nim58
4 files changed, 68 insertions, 50 deletions
diff --git a/src/utils/luwrap.nim b/src/utils/luwrap.nim
index 41a58701..330a5d1e 100644
--- a/src/utils/luwrap.nim
+++ b/src/utils/luwrap.nim
@@ -4,23 +4,23 @@ import std/unicode
 import bindings/libunicode
 import utils/charcategory
 
-proc passRealloc(opaque: pointer, p: pointer, size: csize_t): pointer
-    {.cdecl.} =
+proc passRealloc(opaque, p: pointer; size: csize_t): pointer {.cdecl.} =
   return realloc(p, size)
 
-proc mnormalize*(rs: var seq[Rune], form = UNICODE_NFC) = {.cast(noSideEffect).}:
-  if rs.len == 0: return
-  var outbuf: ptr uint32
-  let p = cast[ptr uint32](unsafeAddr rs[0])
-  let out_len = unicode_normalize(addr outbuf, p, cint(rs.len), form, nil,
-    passRealloc)
-  if out_len < 0:
-    raise newException(Defect, "Unicode normalization failed")
-  if out_len == 0:
-    return
-  rs = cast[seq[Rune]](newSeqUninitialized[uint32](out_len))
-  copyMem(addr rs[0], outbuf, out_len * sizeof(uint32))
-  dealloc(outbuf)
+proc mnormalize*(rs: var seq[Rune]; form = UNICODE_NFC) =
+  {.cast(noSideEffect).}:
+    if rs.len == 0: return
+    var outbuf: ptr uint32
+    let p = cast[ptr uint32](unsafeAddr rs[0])
+    let out_len = unicode_normalize(addr outbuf, p, cint(rs.len), form, nil,
+      passRealloc)
+    if out_len < 0:
+      raise newException(Defect, "Unicode normalization failed")
+    if out_len == 0:
+      return
+    rs = cast[seq[Rune]](newSeqUninitialized[uint32](out_len))
+    copyMem(addr rs[0], outbuf, out_len * sizeof(uint32))
+    dealloc(outbuf)
 
 #TODO maybe a utf8 normalization procedure?
 proc mnormalize*(s: var string) =
@@ -30,7 +30,7 @@ proc mnormalize*(s: var string) =
   rs.mnormalize()
   s = $rs
 
-func normalize*(rs: seq[Rune], form = UNICODE_NFC): seq[Rune] =
+func normalize*(rs: seq[Rune]; form = UNICODE_NFC): seq[Rune] =
   {.cast(noSideEffect).}:
     if rs.len == 0: return
     var outbuf: ptr uint32
diff --git a/src/utils/proptable.nim b/src/utils/proptable.nim
index 0087dd80..02a342f5 100644
--- a/src/utils/proptable.nim
+++ b/src/utils/proptable.nim
@@ -8,7 +8,7 @@ type
   PropertyTable* = array[0x10000 div (sizeof(ptint) * 8), ptint]
   RangeMap* = openArray[(uint32, uint32)]
 
-func makePropertyTable*(ranges: RangeMap, skip: RangeMap = @[]): PropertyTable =
+func makePropertyTable*(ranges: RangeMap; skip: RangeMap = @[]): PropertyTable =
   var ucs: uint32 = 0
   var j = 0
   var k = 0
@@ -30,7 +30,7 @@ func makePropertyTable*(ranges: RangeMap, skip: RangeMap = @[]): PropertyTable =
     inc ucs
 
 {.push boundChecks:off.}
-func contains*(props: PropertyTable, u: ptint): bool {.inline.} =
+func contains*(props: PropertyTable; u: ptint): bool {.inline.} =
   const isz = sizeof(ptint) * 8
   let i = u div isz
   let m = u mod isz
diff --git a/src/utils/strwidth.nim b/src/utils/strwidth.nim
index 9cb115cc..5bfea156 100644
--- a/src/utils/strwidth.nim
+++ b/src/utils/strwidth.nim
@@ -35,7 +35,7 @@ func width*(r: Rune): int =
 
 # Width, but also works with tabs.
 # Needs the column width of the text so far.
-func twidth*(r: Rune, w: int): int =
+func twidth*(r: Rune; w: int): int =
   if r != Rune('\t'):
     return r.width()
   return ((w div 8) + 1) * 8 - w
@@ -44,7 +44,7 @@ func width*(s: string): int =
   for r in s.runes():
     result += r.twidth(result)
 
-func width*(s: string, start, len: int): int =
+func width*(s: string; start, len: int): int =
   var i = start
   var m = len
   if m > s.len: m = s.len
@@ -57,13 +57,13 @@ func notwidth*(s: string): int =
   for r in s.runes:
     result += r.width()
 
-func twidth*(s: string, w: int): int =
+func twidth*(s: string; w: int): int =
   var i = w
   for r in s.runes():
     i += r.twidth(w)
   return i - w
 
-func padToWidth*(str: string, size: int, schar = '$'): string =
+func padToWidth*(str: string; size: int; schar = '$'): string =
   if str.width() < size:
     return str & ' '.repeat(size - str.width())
   else:
@@ -83,24 +83,24 @@ func isDigitAscii(r: Rune): bool =
   return uint32(r) < 128 and char(r) in AsciiDigit
 
 type BreakCategory* = enum
-  BREAK_ALPHA, BREAK_SPACE, BREAK_SYMBOL
+  bcAlpha, bcSpace, bcSymbol
 
 func breaksWord*(r: Rune): bool =
   return not (r.isDigitAscii() or r.width() == 0 or r.isAlpha())
 
 func breaksViWordCat*(r: Rune): BreakCategory =
   if r.isWhiteSpace():
-    return BREAK_SPACE
+    return bcSpace
   elif r.breaksWord():
-    return BREAK_SYMBOL
-  return BREAK_ALPHA
+    return bcSymbol
+  return bcAlpha
 
 func breaksWordCat*(r: Rune): BreakCategory =
   if not r.breaksWord():
-    return BREAK_ALPHA
-  return BREAK_SPACE
+    return bcAlpha
+  return bcSpace
 
 func breaksBigWordCat*(r: Rune): BreakCategory =
   if not r.isWhiteSpace():
-    return BREAK_ALPHA
-  return BREAK_SPACE
+    return bcAlpha
+  return bcSpace
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim
index 91f7a4ab..31ceeec2 100644
--- a/src/utils/twtstr.nim
+++ b/src/utils/twtstr.nim
@@ -86,11 +86,11 @@ func decValue*(c: char): int =
 
 const HexCharsUpper = "0123456789ABCDEF"
 const HexCharsLower = "0123456789abcdef"
-func pushHex*(buf: var string, u: uint8) =
+func pushHex*(buf: var string; u: uint8) =
   buf &= HexCharsUpper[u shr 4]
   buf &= HexCharsUpper[u and 0xF]
 
-func pushHex*(buf: var string, c: char) =
+func pushHex*(buf: var string; c: char) =
   buf.pushHex(cast[uint8](c))
 
 func toHexLower*(u: uint16): string =
@@ -143,36 +143,38 @@ func stripAndCollapse*(s: string): string =
       result &= ' '
     inc i
 
-func skipBlanks*(buf: string, at: int): int =
+func skipBlanks*(buf: string; at: int): int =
   result = at
   while result < buf.len and buf[result] in AsciiWhitespace:
     inc result
 
-func until*(s: string, c: set[char], starti = 0): string =
+func until*(s: string; c: set[char]; starti = 0): string =
+  result = ""
   for i in starti ..< s.len:
     if s[i] in c:
       break
     result.add(s[i])
 
-func untilLower*(s: string, c: set[char], starti = 0): string =
+func untilLower*(s: string; c: set[char]; starti = 0): string =
+  result = ""
   for i in starti ..< s.len:
     if s[i] in c:
       break
     result.add(s[i].toLowerAscii())
 
-func until*(s: string, c: char, starti = 0): string =
+func until*(s: string; c: char; starti = 0): string =
   s.until({c}, starti)
 
-func after*(s: string, c: set[char]): string =
+func after*(s: string; c: set[char]): string =
   var i = 0
   while i < s.len:
     if s[i] in c:
       return s.substr(i + 1)
     inc i
 
-func after*(s: string, c: char): string = s.after({c})
+func after*(s: string; c: char): string = s.after({c})
 
-func afterLast*(s: string, c: set[char], n = 1): string =
+func afterLast*(s: string; c: set[char]; n = 1): string =
   var j = 0
   for i in countdown(s.high, 0):
     if s[i] in c:
@@ -181,9 +183,9 @@ func afterLast*(s: string, c: set[char], n = 1): string =
         return s.substr(i + 1)
   return s
 
-func afterLast*(s: string, c: char, n = 1): string = s.afterLast({c}, n)
+func afterLast*(s: string; c: char; n = 1): string = s.afterLast({c}, n)
 
-func beforeLast*(s: string, c: set[char], n = 1): string =
+func beforeLast*(s: string; c: set[char]; n = 1): string =
   var j = 0
   for i in countdown(s.high, 0):
     if s[i] in c:
@@ -192,7 +194,7 @@ func beforeLast*(s: string, c: set[char], n = 1): string =
         return s.substr(0, i)
   return s
 
-func beforeLast*(s: string, c: char, n = 1): string = s.beforeLast({c}, n)
+func beforeLast*(s: string; c: char; n = 1): string = s.beforeLast({c}, n)
 
 proc c_sprintf(buf, fm: cstring): cint
   {.header: "<stdio.h>", importc: "sprintf", varargs}
@@ -213,7 +215,7 @@ func convertSize*(size: int): string =
   discard c_sprintf(cstring(result), cstring("%.3g%s"), f, SizeUnit[sizepos])
   result.setLen(cstring(result).len)
 
-func numberAdditive*(i: int, range: HSlice[int, int],
+func numberAdditive*(i: int; range: HSlice[int, int];
     symbols: openArray[(int, string)]): string =
   if i notin range:
     return $i
@@ -432,7 +434,7 @@ else:
   const LocalPathPercentEncodeSet* = Ascii - AsciiAlpha - AsciiDigit -
     {'.', '/'}
 
-proc percentEncode*(append: var string, c: char, set: set[char],
+proc percentEncode*(append: var string; c: char; set: set[char];
     spaceAsPlus = false) {.inline.} =
   if spaceAsPlus and c == ' ':
     append &= '+'
@@ -442,19 +444,19 @@ proc percentEncode*(append: var string, c: char, set: set[char],
     append &= '%'
     append.pushHex(c)
 
-proc percentEncode*(append: var string, s: string, set: set[char],
+proc percentEncode*(append: var string; s: string; set: set[char];
     spaceAsPlus = false) {.inline.} =
   for c in s:
     append.percentEncode(c, set, spaceAsPlus)
 
-func percentEncode*(c: char, set: set[char], spaceAsPlus = false): string
+func percentEncode*(c: char; set: set[char]; spaceAsPlus = false): string
     {.inline.} =
   result.percentEncode(c, set, spaceAsPlus)
 
-func percentEncode*(s: string, set: set[char], spaceAsPlus = false): string =
+func percentEncode*(s: string; set: set[char]; spaceAsPlus = false): string =
   result.percentEncode(s, set, spaceAsPlus)
 
-func percentDecode*(input: string, si = 0): string =
+func percentDecode*(input: string; si = 0): string =
   var i = si
   while i < input.len:
     let c = input[i]
@@ -489,7 +491,7 @@ func dqEscape*(s: string): string =
     result &= c
 
 #basically std join but with char
-func join*(ss: openArray[string], sep: char): string =
+func join*(ss: openArray[string]; sep: char): string =
   if ss.len == 0:
     return ""
   var n = ss.high - 1
@@ -501,7 +503,7 @@ func join*(ss: openArray[string], sep: char): string =
     result &= sep
     result &= ss[i]
 
-proc passRealloc*(opaque, p: pointer, size: csize_t): pointer {.cdecl.} =
+proc passRealloc*(opaque, p: pointer; size: csize_t): pointer {.cdecl.} =
   return realloc(p, size)
 
 # https://www.w3.org/TR/xml/#NT-Name
@@ -657,6 +659,22 @@ func strictParseEnum*[T: enum](s: string): Opt[T] =
       return ok(tab[s])
   return err()
 
+func parseEnumNoCase*[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.equalsIgnoreCase(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()
+
 proc getContentTypeAttr*(contentType, attrname: string): string =
   var i = contentType.find(';')
   if i == -1: