about summary refs log tree commit diff stats
path: root/src/utils/twtstr.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-01-19 20:58:43 +0100
committerbptato <nincsnevem662@gmail.com>2022-01-19 20:58:43 +0100
commit5d90347a5627346ea95809781f321a0011257b32 (patch)
tree5bb99448bfabefb2c939ccdc0bb9f30b930da89d /src/utils/twtstr.nim
parentd3692eb681e20f8076d2184cd54861ef8d12e31b (diff)
downloadchawan-5d90347a5627346ea95809781f321a0011257b32.tar.gz
Fix percent encoding function and local percent encoded paths
Diffstat (limited to 'src/utils/twtstr.nim')
-rw-r--r--src/utils/twtstr.nim18
1 files changed, 13 insertions, 5 deletions
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim
index 4b414719..43e287fa 100644
--- a/src/utils/twtstr.nim
+++ b/src/utils/twtstr.nim
@@ -173,8 +173,8 @@ func decValue*(r: Rune): int =
 const HexChars = "0123456789ABCDEF"
 func toHex*(c: char): string =
   result = newString(2)
-  result[0] = HexChars[(uint8(c) and 0xF)]
-  result[1] = HexChars[(uint8(c) shr 4)]
+  result[0] = HexChars[(uint8(c) shr 4)]
+  result[1] = HexChars[(uint8(c) and 0xF)]
 
 func equalsIgnoreCase*(s1: seq[Rune], s2: string): bool =
   var i = 0
@@ -432,12 +432,20 @@ func percentEncode*(c: char, set: set[char]): string {.inline.} =
   result.percentEncode(c, set)
 
 func percentDecode*(input: string): string =
-  for i in low(input)..high(input):
+  var i = 0
+  while i < input.len:
     let c = input[i]
-    if c != '%' or i + 2 >= input.len or input[i + 1].hexValue == -1 or input[i + 2].hexValue == -1:
+    if c != '%' or i + 2 >= input.len:
       result &= c
     else:
-      result &= char((input[i + 1].hexValue shr 4) or (input[i + 2].hexValue and 0xF))
+      let h1 = input[i + 1].hexValue
+      let h2 = input[i + 2].hexValue
+      if h1 == -1 or h2 == -1:
+        result &= c
+      else:
+        result &= char((h1 shl 4) or h2)
+        i += 2
+    inc i
 
 #basically std join but with char
 func join*(ss: openarray[string], sep: char): string =