about summary refs log tree commit diff stats
path: root/src/utils
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-06-27 23:53:44 +0200
committerbptato <nincsnevem662@gmail.com>2022-07-11 21:08:10 +0200
commit62cba694e47a7a1f4bedc7fd48ceac9c26aa3aa1 (patch)
treee20a9f39a293c256f707162c46e117d13f3d5621 /src/utils
parent84882cb8a6f9bca58d178a1f2b8fb5cafa8b3a56 (diff)
downloadchawan-62cba694e47a7a1f4bedc7fd48ceac9c26aa3aa1.tar.gz
Implement HTML5 parsing
Completely replaced the previous HTML2 (?) parser, which was a bad
re-implementation of w3m's parser in the first place. Now we have a
(sort of) compliant HTML5 parser.
Needs tests, badly.
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/eprint.nim35
-rw-r--r--src/utils/twtstr.nim11
2 files changed, 27 insertions, 19 deletions
diff --git a/src/utils/eprint.nim b/src/utils/eprint.nim
index d13fcf91..ac4963fb 100644
--- a/src/utils/eprint.nim
+++ b/src/utils/eprint.nim
@@ -2,24 +2,23 @@
 
 template eprint*(s: varargs[string, `$`]) = {.cast(noSideEffect).}:
   var a = false
-  for x in s:
-    if not a:
-      a = true
-    else:
-      stderr.write(' ')
-    stderr.write(x)
-  stderr.write('\n')
-
-template eecho*(s: varargs[string, `$`]) = {.cast(noSideEffect).}:
-  var a = false
-  var o = ""
-  for x in s:
-    if not a:
-      a = true
-    else:
-      o &= ' '
-    o &= x
-  echo o
+  when nimVm:
+    var o = ""
+    for x in s:
+      if not a:
+        a = true
+      else:
+        o &= ' '
+      o &= x
+    echo o
+  else:
+    for x in s:
+      if not a:
+        a = true
+      else:
+        stderr.write(' ')
+      stderr.write(x)
+    stderr.write('\n')
 
 template print*(s: varargs[string, `$`]) =
   for x in s:
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim
index 04db1d24..a484d3dd 100644
--- a/src/utils/twtstr.nim
+++ b/src/utils/twtstr.nim
@@ -122,6 +122,15 @@ func toAsciiLower*(str: string): string =
   for i in 0..str.high:
     result[i] = str[i].tolower()
 
+func startsWithNoCase*(str, prefix: string): bool =
+  if str.len < prefix.len: return false
+  # prefix.len is always lower
+  var i = 0
+  while true:
+    if i == prefix.len: return true
+    if str[i].tolower() != prefix[i].tolower(): return false
+    inc i
+
 func genHexCharMap(): seq[int] =
   for i in 0..255:
     case chr(i)
@@ -146,7 +155,7 @@ func decValue*(c: char): int =
   return decCharMap[int(c)]
 
 func isAscii*(c: char): bool =
-  return c in Ascii
+  return int(c) < 128
 
 func isAscii*(r: Rune): bool =
   return int(r) < 128