about summary refs log tree commit diff stats
path: root/src/utils/twtstr.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-07-07 14:12:40 +0200
committerbptato <nincsnevem662@gmail.com>2023-07-07 14:13:19 +0200
commita84193b0af85cc1e4c298d66a6fd951e0e46b480 (patch)
tree87ba336c353eaa3ce6bf75b43422b02082403b03 /src/utils/twtstr.nim
parentb2514e94bac6f7b3462649834ad36287b0b29ab2 (diff)
downloadchawan-a84193b0af85cc1e4c298d66a6fd951e0e46b480.tar.gz
Fixes in ipv6 parser and serializer
Diffstat (limited to 'src/utils/twtstr.nim')
-rw-r--r--src/utils/twtstr.nim23
1 files changed, 20 insertions, 3 deletions
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim
index 77d86998..a32cc8da 100644
--- a/src/utils/twtstr.nim
+++ b/src/utils/twtstr.nim
@@ -172,11 +172,28 @@ func isAscii*(s: string): bool =
       return false
   return true
 
-const HexChars = "0123456789ABCDEF"
+const HexCharsUpper = "0123456789ABCDEF"
+const HexCharsLower = "0123456789abcdef"
 func toHex*(c: char): string =
   result = newString(2)
-  result[0] = HexChars[(uint8(c) shr 4)]
-  result[1] = HexChars[(uint8(c) and 0xF)]
+  result[0] = HexCharsUpper[(uint8(c) shr 4)]
+  result[1] = HexCharsLower[(uint8(c) and 0xF)]
+
+func toHexLower*(u: uint16): string =
+  var x = u
+  let len = if (u and 0xF000) != 0:
+    4
+  elif (u and 0x0F00) != 0:
+    3
+  elif (u and 0xF0) != 0:
+    2
+  else:
+    1
+  var s = newString(len)
+  for i in countdown(len - 1, 0):
+    s[i] = HexCharsLower[x and 0xF]
+    x = x shr 4
+  return s
 
 func toHex*(i: uint8): string =
   return toHex(cast[char](i))