summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2016-12-06 10:08:38 +0100
committerAndreas Rumpf <rumpf_a@web.de>2016-12-06 10:08:38 +0100
commit36a97038665c3fa21ef97b2cc00d381e15b0d95c (patch)
tree309c86af6f24bac43443a86f0c8cde74aa3a5f61 /lib/pure
parent0d6f4f1bb17e72b6bc0a7d781b2ca9fc1a6c4adb (diff)
parent14b9eaee06894f5bf00548117984381d37f16ec7 (diff)
downloadNim-36a97038665c3fa21ef97b2cc00d381e15b0d95c.tar.gz
Merge branch 'devel' into sighashes
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/collections/tableimpl.nim7
-rw-r--r--lib/pure/httpcore.nim4
-rw-r--r--lib/pure/terminal.nim48
-rw-r--r--lib/pure/xmltree.nim23
4 files changed, 72 insertions, 10 deletions
diff --git a/lib/pure/collections/tableimpl.nim b/lib/pure/collections/tableimpl.nim
index 674fdddd2..5e871f08b 100644
--- a/lib/pure/collections/tableimpl.nim
+++ b/lib/pure/collections/tableimpl.nim
@@ -116,16 +116,15 @@ template hasKeyOrPutImpl(enlarge) {.dirty.} =
     maybeRehashPutImpl(enlarge)
   else: result = true
 
-proc default[T](t: typedesc[T]): T {.inline.} = discard
+template default[T](t: typedesc[T]): T =
+  var v: T
+  v
 
 template delImpl() {.dirty.} =
   var hc: Hash
   var i = rawGet(t, key, hc)
   let msk = maxHash(t)
   if i >= 0:
-    t.data[i].hcode = 0
-    t.data[i].key = default(type(t.data[i].key))
-    t.data[i].val = default(type(t.data[i].val))
     dec(t.counter)
     block outer:
       while true:         # KnuthV3 Algo6.4R adapted for i=i+1 instead of i=i-1
diff --git a/lib/pure/httpcore.nim b/lib/pure/httpcore.nim
index 8147f1c50..48001ccaa 100644
--- a/lib/pure/httpcore.nim
+++ b/lib/pure/httpcore.nim
@@ -154,6 +154,10 @@ proc add*(headers: HttpHeaders, key, value: string) =
   else:
     headers.table[key.toLowerAscii].add(value)
 
+proc del*(headers: HttpHeaders, key: string) =
+  ## Delete the header entries associated with ``key``
+  headers.table.del(key.toLowerAscii)
+
 iterator pairs*(headers: HttpHeaders): tuple[key, value: string] =
   ## Yields each key, value pair.
   for k, v in headers.table:
diff --git a/lib/pure/terminal.nim b/lib/pure/terminal.nim
index 16cf91d40..7a8113b2a 100644
--- a/lib/pure/terminal.nim
+++ b/lib/pure/terminal.nim
@@ -83,6 +83,13 @@ when defined(windows):
         return int(csbi.srWindow.Right - csbi.srWindow.Left + 1)
     return 0
 
+  proc terminalHeightIoctl*(handles: openArray[Handle]): int =
+    var csbi: CONSOLE_SCREEN_BUFFER_INFO
+    for h in handles:
+      if getConsoleScreenBufferInfo(h, addr csbi) != 0:
+        return int(csbi.srWindow.Bottom - csbi.srWindow.Top + 1)
+    return 0
+
   proc terminalWidth*(): int =
     var w: int = 0
     w = terminalWidthIoctl([ getStdHandle(STD_INPUT_HANDLE),
@@ -91,6 +98,14 @@ when defined(windows):
     if w > 0: return w
     return 80
 
+  proc terminalHeight*(): int =
+    var h: int = 0
+    h = terminalHeightIoctl([ getStdHandle(STD_INPUT_HANDLE),
+                              getStdHandle(STD_OUTPUT_HANDLE),
+                              getStdHandle(STD_ERROR_HANDLE) ] )
+    if h > 0: return h
+    return 0
+
   proc setConsoleCursorPosition(hConsoleOutput: HANDLE,
                                 dwCursorPosition: COORD): WINBOOL{.
       stdcall, dynlib: "kernel32", importc: "SetConsoleCursorPosition".}
@@ -177,7 +192,17 @@ else:
         return int(win.ws_col)
     return 0
 
+  proc terminalHeightIoctl*(fds: openArray[int]): int =
+    ## Returns terminal height from first fd that supports the ioctl.
+
+    var win: IOctl_WinSize
+    for fd in fds:
+      if ioctl(cint(fd), TIOCGWINSZ, addr win) != -1:
+        return int(win.ws_row)
+    return 0
+
   var L_ctermid{.importc, header: "<stdio.h>".}: cint
+
   proc terminalWidth*(): int =
     ## Returns some reasonable terminal width from either standard file
     ## descriptors, controlling terminal, environment variables or tradition.
@@ -195,6 +220,29 @@ else:
       return w
     return 80                               #Finally default to venerable value
 
+  proc terminalHeight*(): int =
+    ## Returns some reasonable terminal height from either standard file
+    ## descriptors, controlling terminal, environment variables or tradition.
+    ## Zero is returned if the height could not be determined.
+
+    var h = terminalHeightIoctl([0, 1, 2])  # Try standard file descriptors
+    if h > 0: return h
+    var cterm = newString(L_ctermid)        # Try controlling tty
+    var fd = open(ctermid(cstring(cterm)), O_RDONLY)
+    if fd != -1:
+      h = terminalHeightIoctl([ int(fd) ])
+    discard close(fd)
+    if h > 0: return h
+    var s = getEnv("LINES")                 # Try standard env var
+    if len(s) > 0 and parseInt(string(s), h) > 0 and h > 0:
+      return h
+    return 0                                # Could not determine height
+
+proc terminalSize*(): tuple[w, h: int] =
+  ## Returns the terminal width and height as a tuple. Internally calls
+  ## `terminalWidth` and `terminalHeight`, so the same assumptions apply.
+  result = (terminalWidth(), terminalHeight())
+
 when defined(windows):
   proc setCursorVisibility(f: File, visible: bool) =
     var ccsi: CONSOLE_CURSOR_INFO
diff --git a/lib/pure/xmltree.nim b/lib/pure/xmltree.nim
index 3c6eb14e3..7cfb62157 100644
--- a/lib/pure/xmltree.nim
+++ b/lib/pure/xmltree.nim
@@ -91,13 +91,24 @@ proc rawTag*(n: XmlNode): string {.inline.} =
   shallowCopy(result, n.fTag)
 
 proc innerText*(n: XmlNode): string =
-  ## gets the inner text of `n`. `n` has to be an ``xnElement`` node. Only
-  ## ``xnText`` and ``xnEntity`` nodes are considered part of `n`'s inner text,
-  ## other child nodes are silently ignored.
+  ## gets the inner text of `n`:
+  ##
+  ## - If `n` is `xnText` or `xnEntity`, returns its content.
+  ## - If `n` is `xnElement`, runs recursively on each child node and
+  ##   concatenates the results.
+  ## - Otherwise returns an empty string.
+  proc worker(res: var string, n: XmlNode) =
+    case n.k
+    of xnText, xnEntity:
+      res.add(n.fText)
+    of xnElement:
+      for sub in n.s:
+        worker(res, sub)
+    else:
+      discard
+
   result = ""
-  assert n.k == xnElement
-  for i in 0 .. n.s.len-1:
-    if n.s[i].k in {xnText, xnEntity}: result.add(n.s[i].fText)
+  worker(result, n)
 
 proc tag*(n: XmlNode): string {.inline.} =
   ## gets the tag name of `n`. `n` has to be an ``xnElement`` node.