about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-11-28 17:44:40 +0100
committerbptato <nincsnevem662@gmail.com>2024-11-28 17:44:40 +0100
commitbd38cbbc7068737214129cc5365ee8d4bfecda9c (patch)
treeda9d6f772eea3e8def9d58c1a5afd568375b2d7a /src
parent46abcd79d5422261cc349cc0b4981357d5ae2fcd (diff)
downloadchawan-bd38cbbc7068737214129cc5365ee8d4bfecda9c.tar.gz
twtstr: add mypairs
This couldn't get into system.nim for technical reasons, but it's still
pretty useful when iterating over non-mutable openArrays.
Diffstat (limited to 'src')
-rw-r--r--src/config/mailcap.nim2
-rw-r--r--src/css/cssvalues.nim10
-rw-r--r--src/css/layout.nim4
-rw-r--r--src/html/dom.nim16
-rw-r--r--src/html/script.nim3
-rw-r--r--src/io/console.nim3
-rw-r--r--src/local/container.nim4
-rw-r--r--src/local/pager.nim2
-rw-r--r--src/server/buffer.nim2
-rw-r--r--src/server/loader.nim4
-rw-r--r--src/utils/twtstr.nim9
11 files changed, 35 insertions, 24 deletions
diff --git a/src/config/mailcap.nim b/src/config/mailcap.nim
index 8700323b..1c200cfa 100644
--- a/src/config/mailcap.nim
+++ b/src/config/mailcap.nim
@@ -311,7 +311,7 @@ proc findMailcapEntry*(mailcap: var Mailcap; contentType, outpath: string;
     url: URL): int =
   let mt = contentType.until('/')
   let st = contentType.until(AsciiWhitespace + {';'}, mt.len + 1)
-  for i, entry in mailcap.mpairs:
+  for i, entry in mailcap.mypairs:
     if entry.mt != "*" and not entry.mt.equalsIgnoreCase(mt):
       continue
     if entry.subt != "*" and not entry.subt.equalsIgnoreCase(st):
diff --git a/src/css/cssvalues.nim b/src/css/cssvalues.nim
index b89e1dad..7b4e4abf 100644
--- a/src/css/cssvalues.nim
+++ b/src/css/cssvalues.nim
@@ -908,7 +908,7 @@ func parseANSI(value: openArray[CSSComponentValue]): Opt[CSSColor] =
       "cyan",
       "white"
     ]
-    for i, it in NameTable:
+    for i, it in NameTable.mypairs:
       if it.equalsIgnoreCase(name):
         var i = int(i)
         if bright:
@@ -1316,13 +1316,13 @@ func lengthShorthand(cvals: openArray[CSSComponentValue];
     inc i
   case lengths.len
   of 1: # top, bottom, left, right
-    for i, t in props:
+    for i, t in props.mypairs:
       res.add((t, lengths[0], cgtNone))
   of 2: # top, bottom | left, right
-    for i, t in props:
+    for i, t in props.mypairs:
       res.add((t, lengths[i mod 2], cgtNone))
   of 3: # top | left, right | bottom
-    for i, t in props:
+    for i, t in props.mypairs:
       let j = if i == 0:
         0 # top
       elif i == 3:
@@ -1331,7 +1331,7 @@ func lengthShorthand(cvals: openArray[CSSComponentValue];
         1 # left, right
       res.add((t, lengths[j], cgtNone))
   of 4: # top | right | bottom | left
-    for i, t in props:
+    for i, t in props.mypairs:
       res.add((t, lengths[i], cgtNone))
   else:
     return err()
diff --git a/src/css/layout.nim b/src/css/layout.nim
index 8dd54c24..def840c6 100644
--- a/src/css/layout.nim
+++ b/src/css/layout.nim
@@ -1993,9 +1993,9 @@ proc layoutTableRow(tctx: TableContext; ctx: RowContext;
     row.applyOverflowDimensions(cell)
   row.state.size.w = x
 
-proc preLayoutTableRows(tctx: var TableContext; rows: seq[BlockBox];
+proc preLayoutTableRows(tctx: var TableContext; rows: openArray[BlockBox];
     table: BlockBox) =
-  for i, row in rows:
+  for i, row in rows.mypairs:
     let rctx = tctx.preLayoutTableRow(row, table, i, rows.len)
     tctx.rows.add(rctx)
     tctx.maxwidth = max(rctx.width, tctx.maxwidth)
diff --git a/src/html/dom.nim b/src/html/dom.nim
index 759988eb..3c148952 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -1077,7 +1077,7 @@ func tagType*(element: Element): TagType =
   return element.tagTypeNoNS
 
 func findAttr(element: Element; qualifiedName: CAtom): int =
-  for i, attr in element.attrs.mpairs:
+  for i, attr in element.attrs.mypairs:
     if attr.qualifiedName == qualifiedName:
       return i
   return -1
@@ -1086,7 +1086,7 @@ func findAttr(element: Element; qualifiedName: StaticAtom): int =
   return element.findAttr(element.document.toAtom(qualifiedName))
 
 func findAttrNS(element: Element; namespace, qualifiedName: CAtom): int =
-  for i, attr in element.attrs.mpairs:
+  for i, attr in element.attrs.mypairs:
     if attr.namespace == namespace and attr.qualifiedName == qualifiedName:
       return i
   return -1
@@ -1902,7 +1902,7 @@ func name(attr: Attr): CAtom {.jsfget.} =
   return attr.data.qualifiedName
 
 func findAttr(map: NamedNodeMap; dataIdx: int): int =
-  for i, attr in map.attrlist:
+  for i, attr in map.attrlist.mypairs:
     if attr.dataIdx == dataIdx:
       return i
   return -1
@@ -1932,7 +1932,7 @@ func attributes(element: Element): NamedNodeMap {.jsfget.} =
   if element.cachedAttributes != nil:
     return element.cachedAttributes
   element.cachedAttributes = NamedNodeMap(element: element)
-  for i, attr in element.attrs.mpairs:
+  for i, attr in element.attrs.mypairs:
     element.cachedAttributes.attrlist.add(Attr(
       internalDocument: element.document,
       index: -1,
@@ -3182,7 +3182,7 @@ proc delAttr(element: Element; i: int; keep = false) =
   if map != nil:
     # delete from attrlist + adjust indices invalidated
     var j = -1
-    for i, attr in map.attrlist.mpairs:
+    for i, attr in map.attrlist.mypairs:
       if attr.dataIdx == i:
         j = i
       elif attr.dataIdx > i:
@@ -3583,7 +3583,7 @@ func cmpAttrName(a: AttrData; b: CAtom): int =
 # Returns the attr index if found, or the negation - 1 of an upper bound
 # (where a new attr with the passed name may be inserted).
 func findAttrOrNext(element: Element; qualName: CAtom): int =
-  for i, data in element.attrs.mpairs:
+  for i, data in element.attrs.mypairs:
     if data.qualifiedName == qualName:
       return i
     if int(data.qualifiedName) > int(qualName):
@@ -4617,7 +4617,7 @@ func isEqualNode(node, other: Node): bool {.jsfunc.} =
         node.localName != other.localName or
         node.attrs.len != other.attrs.len:
       return false
-    for i, attr in node.attrs.mpairs:
+    for i, attr in node.attrs.mypairs:
       if not attr.equals(other.attrs[i]):
         return false
   elif node of Attr:
@@ -4637,7 +4637,7 @@ func isEqualNode(node, other: Node): bool {.jsfunc.} =
         node of Comment and not (other of Comment):
       return false
     return CharacterData(node).data == CharacterData(other).data
-  for i, child in node.childList:
+  for i, child in node.childList.mypairs:
     if not child.isEqualNode(other.childList[i]):
       return false
   true
diff --git a/src/html/script.nim b/src/html/script.nim
index 2e009503..2d77c312 100644
--- a/src/html/script.nim
+++ b/src/html/script.nim
@@ -2,6 +2,7 @@ import monoucha/javascript
 import monoucha/quickjs
 import types/referrer
 import types/url
+import utils/twtstr
 
 type
   ParserMetadata* = enum
@@ -84,7 +85,7 @@ var errorImpl*: proc(ctx: JSContext; ss: varargs[string]) {.nimcall.}
 
 proc find*(moduleMap: ModuleMap; url: URL; moduleType: string): int =
   let surl = $url
-  for i, entry in moduleMap:
+  for i, entry in moduleMap.mypairs:
     if entry.key.moduleType == moduleType and entry.key.url == surl:
       return i
   return -1
diff --git a/src/io/console.nim b/src/io/console.nim
index 755910a5..2e61fea3 100644
--- a/src/io/console.nim
+++ b/src/io/console.nim
@@ -2,6 +2,7 @@ import io/dynstream
 import monoucha/fromjs
 import monoucha/javascript
 import types/opt
+import utils/twtstr
 
 type Console* = ref object
   err*: DynStream
@@ -22,7 +23,7 @@ proc newConsole*(err: DynStream; clearFun: proc() = nil; showFun: proc() = nil;
 
 proc log*(console: Console; ss: varargs[string]) =
   var buf = ""
-  for i, s in ss:
+  for i, s in ss.mypairs:
     buf &= s
     if i != ss.high:
       buf &= ' '
diff --git a/src/local/container.nim b/src/local/container.nim
index 4b41ed74..59c0a492 100644
--- a/src/local/container.nim
+++ b/src/local/container.nim
@@ -1419,14 +1419,14 @@ proc getSelectionText(container: Container; hl: Highlight = nil):
         let ei = res.lines[^1].str.findColBytes(endx + 1) - 1
         s &= res.lines[^1].str.substr(0, ei)
     of stBlock:
-      for i, line in res.lines:
+      for i, line in res.lines.mypairs:
         let si = line.str.findColBytes(startx)
         let ei = line.str.findColBytes(endx + 1, startx, si) - 1
         if i > 0:
           s &= '\n'
         s &= line.str.substr(si, ei)
     of stLine:
-      for i, line in res.lines:
+      for i, line in res.lines.mypairs:
         if i > 0:
           s &= '\n'
         s &= line.str
diff --git a/src/local/pager.nim b/src/local/pager.nim
index 8bf6b848..5aa1b82f 100644
--- a/src/local/pager.nim
+++ b/src/local/pager.nim
@@ -870,7 +870,7 @@ func findConnectingContainer*(pager: Pager; container: Container):
   return nil
 
 func findProcMapItem*(pager: Pager; pid: int): int =
-  for i, item in pager.procmap:
+  for i, item in pager.procmap.mypairs:
     if item.container.process == pid:
       return i
   -1
diff --git a/src/server/buffer.nim b/src/server/buffer.nim
index 41b1ec1e..bc7f0286 100644
--- a/src/server/buffer.nim
+++ b/src/server/buffer.nim
@@ -516,7 +516,7 @@ proc findNextLink*(buffer: Buffer; cursorx, cursory, n: int):
   if i >= 0:
     link = buffer.lines[cursory].formats[i].node.getClickable()
   inc i
-  for j, line in buffer.lines.toOpenArray(cursory, buffer.lines.high).mpairs:
+  for j, line in buffer.lines.toOpenArray(cursory, buffer.lines.high).mypairs:
     while i < line.formats.len:
       let format = line.formats[i]
       let fl = format.node.getClickable()
diff --git a/src/server/loader.nim b/src/server/loader.nim
index 2339408c..6a54ebcc 100644
--- a/src/server/loader.nim
+++ b/src/server/loader.nim
@@ -315,8 +315,8 @@ func findCachedHandle(ctx: LoaderContext; cacheId: int): InputHandle =
       return it
   return nil
 
-func find(cacheMap: seq[CachedItem]; id: int): int =
-  for i, it in cacheMap:
+func find(cacheMap: openArray[CachedItem]; id: int): int =
+  for i, it in cacheMap.mypairs:
     if it.id == id:
       return i
   -1
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim
index 9cb7e8e2..78dc982d 100644
--- a/src/utils/twtstr.nim
+++ b/src/utils/twtstr.nim
@@ -825,3 +825,12 @@ proc getEnvEmpty*(name: string; fallback = ""): string =
   if res != "":
     return res
   return fallback
+
+iterator mypairs*[T](a: openArray[T]): tuple[key: int; val: lent T] {.inline.} =
+  var i = 0
+  let L = a.len
+  while i < L:
+    yield (i, a[i])
+    {.push overflowChecks: off.}
+    inc i
+    {.pop.}