aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-12-12 19:42:04 +0100
committerbptato <nincsnevem662@gmail.com>2023-12-12 19:42:04 +0100
commit820f0f0f039252533133c3bd1037a73036815a45 (patch)
tree305dd3cd0416123c27701c199164fa54e822a7fb
parent5efdc54db2d2c766d7ce7c6545207ce2ce805620 (diff)
downloadchawan-820f0f0f039252533133c3bd1037a73036815a45.tar.gz
twtstr: import functions from gopher2html
-rw-r--r--adapter/gopher/gopher2html.nim19
-rw-r--r--src/utils/twtstr.nim25
2 files changed, 12 insertions, 32 deletions
diff --git a/adapter/gopher/gopher2html.nim b/adapter/gopher/gopher2html.nim
index 4821814f..cfdeab1e 100644
--- a/adapter/gopher/gopher2html.nim
+++ b/adapter/gopher/gopher2html.nim
@@ -5,6 +5,8 @@ import std/os
import std/streams
import std/strutils
+import utils/twtstr
+
type GopherType = enum
UNKNOWN = "unsupported"
TEXT_FILE = "text file"
@@ -38,23 +40,6 @@ func gopherType(c: char): GopherType =
of 'p': PNG
else: UNKNOWN
-func htmlEscape(s: string): string =
- result = ""
- for c in s:
- case c
- of '<': result &= "&lt;"
- of '>': result &= "&gt;"
- of '&': result &= "&amp;"
- of '"': result &= "&quot;"
- of '\'': result &= "&apos;"
- else: result &= c
-
-func until(s: string, c: char, starti = 0): string =
- for i in starti ..< s.len:
- if s[i] == c:
- break
- result &= s[i]
-
const ControlPercentEncodeSet = {char(0x00)..char(0x1F), char(0x7F)..char(0xFF)}
const QueryPercentEncodeSet = (ControlPercentEncodeSet + {' ', '"', '#', '<', '>'})
const PathPercentEncodeSet = (QueryPercentEncodeSet + {'?', '`', '{', '}'})
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim
index 13a432bf..c5078568 100644
--- a/src/utils/twtstr.nim
+++ b/src/utils/twtstr.nim
@@ -222,7 +222,8 @@ func until*(s: string, c: set[char], starti = 0): string =
break
result.add(s[i])
-func until*(s: string, c: char): string = s.until({c})
+func until*(s: string, c: char, starti = 0): string =
+ s.until({c}, starti)
func after*(s: string, c: set[char]): string =
var i = 0
@@ -557,21 +558,15 @@ func percentDecode*(input: string, si = 0): string =
inc i
func htmlEscape*(s: string): string =
- var res = ""
+ result = ""
for c in s:
- if c == '<':
- res &= "&lt;"
- elif c == '>':
- res &= "&gt;"
- elif c == '&':
- res &= "&amp;"
- elif c == '"':
- res &= "&quot;"
- elif c == '\'':
- res &= "&apos;"
- else:
- res &= c
- return res
+ case c
+ of '<': result &= "&lt;"
+ of '>': result &= "&gt;"
+ of '&': result &= "&amp;"
+ of '"': result &= "&quot;"
+ of '\'': result &= "&apos;"
+ else: result &= c
func dqEscape*(s: string): string =
result = newStringOfCap(s.len)