Merge branch 'master' of github.com:Araq/Nimrod into upstream

This commit is contained in:
Zahary Karadjov 2013-12-09 00:26:04 +02:00
commit 95a6e01ea8
108 changed files with 759 additions and 89730 deletions

View file

@ -64,10 +64,10 @@ const
proc newMonitor*(): PFSMonitor =
## Creates a new file system monitor.
new(result)
result.fd = inotifyInit()
result.targets = initTable[cint, string]()
result.fd = inotifyInit()
if result.fd < 0:
OSError()
OSError(OSLastError())
proc add*(monitor: PFSMonitor, target: string,
filters = {MonitorAll}): cint {.discardable.} =
@ -93,7 +93,7 @@ proc add*(monitor: PFSMonitor, target: string,
result = inotifyAddWatch(monitor.fd, target, INFilter.uint32)
if result < 0:
OSError()
OSError(OSLastError())
monitor.targets.add(result, target)
proc del*(monitor: PFSMonitor, wd: cint) =
@ -101,7 +101,7 @@ proc del*(monitor: PFSMonitor, wd: cint) =
##
## If ``wd`` is not a part of ``monitor`` an EOS error is raised.
if inotifyRmWatch(monitor.fd, wd) < 0:
OSError()
OSError(OSLastError())
proc getEvent(m: PFSMonitor, fd: cint): seq[TMonitorEvent] =
result = @[]
@ -184,7 +184,7 @@ proc FSMonitorRead(h: PObject) =
proc toDelegate(m: PFSMonitor): PDelegate =
result = newDelegate()
result.deleVal = m
result.fd = m.fd
result.fd = (type(result.fd))(m.fd)
result.mode = fmRead
result.handleRead = FSMonitorRead
result.open = true

View file

@ -17,11 +17,37 @@
##
## echo loadHtml("mydirty.html")
##
##
## Every tag in the resulting tree is in lower case.
##
## **Note:** The resulting ``PXmlNode`` already uses the ``clientData`` field,
## so it cannot be used by clients of this library.
##
## Example: Transforming hyperlinks
## ================================
##
## This code demonstrates how you can iterate over all the tags in an HTML file
## and write back the modified version. In this case we look for hyperlinks
## ending with the extension ``.rst`` and convert them to ``.html``.
##
## .. code-block:: nimrod
##
## import htmlparser
## import xmltree # To use '$' for PXmlNode
## import strtabs # To access PXmlAttributes
## import os # To use splitFile
## import strutils # To use cmpIgnoreCase
##
## proc transformHyperlinks() =
## let html = loadHTML("input.html")
##
## for a in html.findAll("a"):
## let href = a.attrs["href"]
## if not href.isNil:
## let (dir, filename, ext) = splitFile(href)
## if cmpIgnoreCase(ext, ".rst") == 0:
## a.attrs["href"] = dir / filename & ".html"
##
## writeFile("output.html", $html)
import strutils, streams, parsexml, xmltree, unicode, strtabs
@ -528,7 +554,7 @@ proc parseHtml*(s: PStream, filename: string,
## parses the XML from stream `s` and returns a ``PXmlNode``. Every
## occured parsing error is added to the `errors` sequence.
var x: TXmlParser
open(x, s, filename, {reportComments})
open(x, s, filename, {reportComments, reportWhitespace})
next(x)
# skip the DOCTYPE:
if x.kind == xmlSpecial: next(x)

View file

@ -401,8 +401,9 @@ proc nextAsync(s: PAsyncHTTPServer) =
var value = ""
i = header.parseUntil(key, ':')
inc(i) # skip :
i += header.skipWhiteSpace(i)
i += header.parseUntil(value, {'\c', '\L'}, i)
if i < header.len:
i += header.skipWhiteSpace(i)
i += header.parseUntil(value, {'\c', '\L'}, i)
s.headers[key] = value
else:
s.client.close()

View file

@ -98,6 +98,7 @@ type
params*: seq[string] ## Parameters of the IRC message
origin*: string ## The channel/user that this msg originated from
raw*: string ## Raw IRC message
timestamp*: TTime ## UNIX epoch time the message was received
proc send*(irc: PIRC, message: string, sendImmediately = false) =
## Sends ``message`` as a raw command. It adds ``\c\L`` for you.
@ -160,9 +161,10 @@ proc isNumber(s: string): bool =
result = i == s.len and s.len > 0
proc parseMessage(msg: string): TIRCEvent =
result.typ = EvMsg
result.cmd = MUnknown
result.raw = msg
result.typ = EvMsg
result.cmd = MUnknown
result.raw = msg
result.timestamp = times.getTime()
var i = 0
# Process the prefix
if msg[i] == ':':

View file

@ -154,9 +154,8 @@ proc newTSocket(fd: TSocketHandle, isBuff: bool): TSocket =
proc `==`*(a, b: TPort): bool {.borrow.}
## ``==`` for ports.
proc `$`*(p: TPort): string =
proc `$`*(p: TPort): string {.borrow.}
## returns the port number as a string
result = $ze(int16(p))
proc ntohl*(x: int32): int32 =
## Converts 32-bit integers from network to host byte order.

View file

@ -709,14 +709,6 @@ proc rfind*(s, sub: string, start: int = -1): int {.noSideEffect.} =
if result != -1: return
return -1
proc quoteIfContainsWhite*(s: string): string =
## returns ``'"' & s & '"'`` if `s` contains a space and does not
## start with a quote, else returns `s`
if find(s, {' ', '\t'}) >= 0 and s[0] != '"':
result = '"' & s & '"'
else:
result = s
proc contains*(s: string, c: char): bool {.noSideEffect.} =
## Same as ``find(s, c) >= 0``.
return find(s, c) >= 0
@ -780,6 +772,49 @@ proc replaceWord*(s, sub: string, by = ""): string {.noSideEffect,
# copy the rest:
add result, substr(s, i)
proc quoteIfContainsWhite*(s: string): string {.noSideEffect.} =
## Quote s, so it can be safely passed to shell.
when defined(Windows):
# based on Python's subprocess.list2cmdline
# see http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
let needQuote = {' ', '\t'} in s or s.len == 0
result = ""
var backslashBuff = ""
if needQuote:
result.add("\"")
for c in s:
if c == '\\':
backslashBuff.add(c)
elif c == '\"':
result.add(backslashBuff)
result.add(backslashBuff)
backslashBuff.setLen(0)
result.add("\\\"")
else:
if backslashBuff.len != 0:
result.add(backslashBuff)
backslashBuff.setLen(0)
result.add(c)
if needQuote:
result.add("\"")
else:
# based on Python's pipes.quote
const safeUnixChars = {'%', '+', '-', '.', '/', '_', ':', '=', '@',
'0'..'9', 'A'..'Z', 'a'..'z'}
if s.len == 0:
return "''"
let safe = s.allCharsInSet(safeUnixChars)
if safe:
return s
else:
return "'" & s.replace("'", "'\"'\"'") & "'"
proc delete*(s: var string, first, last: int) {.noSideEffect,
rtl, extern: "nsuDelete".} =
## Deletes in `s` the characters at position `first` .. `last`. This modifies

View file

@ -246,7 +246,8 @@ proc toSeconds(a: TTimeInfo, interval: TTimeInterval): float =
else:
curMonth.inc()
result += float(newinterv.days * 24 * 60 * 60)
result += float(newinterv.minutes * 60 * 60)
result += float(newinterv.hours * 60 * 60)
result += float(newinterv.minutes * 60)
result += float(newinterv.seconds)
result += newinterv.miliseconds / 1000

View file

@ -55,31 +55,31 @@ template fastRuneAt*(s: string, i: int, result: expr, doInc = true) =
result = TRune(ord(s[i]))
when doInc: inc(i)
elif ord(s[i]) shr 5 == 0b110:
assert(ord(s[i+1]) shr 6 == 0b10)
# assert(ord(s[i+1]) shr 6 == 0b10)
result = TRune((ord(s[i]) and (ones(5))) shl 6 or
(ord(s[i+1]) and ones(6)))
when doInc: inc(i, 2)
elif ord(s[i]) shr 4 == 0b1110:
assert(ord(s[i+1]) shr 6 == 0b10)
assert(ord(s[i+2]) shr 6 == 0b10)
# assert(ord(s[i+1]) shr 6 == 0b10)
# assert(ord(s[i+2]) shr 6 == 0b10)
result = TRune((ord(s[i]) and ones(4)) shl 12 or
(ord(s[i+1]) and ones(6)) shl 6 or
(ord(s[i+2]) and ones(6)))
when doInc: inc(i, 3)
elif ord(s[i]) shr 3 == 0b11110:
assert(ord(s[i+1]) shr 6 == 0b10)
assert(ord(s[i+2]) shr 6 == 0b10)
assert(ord(s[i+3]) shr 6 == 0b10)
# assert(ord(s[i+1]) shr 6 == 0b10)
# assert(ord(s[i+2]) shr 6 == 0b10)
# assert(ord(s[i+3]) shr 6 == 0b10)
result = TRune((ord(s[i]) and ones(3)) shl 18 or
(ord(s[i+1]) and ones(6)) shl 12 or
(ord(s[i+2]) and ones(6)) shl 6 or
(ord(s[i+3]) and ones(6)))
when doInc: inc(i, 4)
elif ord(s[i]) shr 2 == 0b111110:
assert(ord(s[i+1]) shr 6 == 0b10)
assert(ord(s[i+2]) shr 6 == 0b10)
assert(ord(s[i+3]) shr 6 == 0b10)
assert(ord(s[i+4]) shr 6 == 0b10)
# assert(ord(s[i+1]) shr 6 == 0b10)
# assert(ord(s[i+2]) shr 6 == 0b10)
# assert(ord(s[i+3]) shr 6 == 0b10)
# assert(ord(s[i+4]) shr 6 == 0b10)
result = TRune((ord(s[i]) and ones(2)) shl 24 or
(ord(s[i+1]) and ones(6)) shl 18 or
(ord(s[i+2]) and ones(6)) shl 12 or
@ -87,11 +87,11 @@ template fastRuneAt*(s: string, i: int, result: expr, doInc = true) =
(ord(s[i+4]) and ones(6)))
when doInc: inc(i, 5)
elif ord(s[i]) shr 1 == 0b1111110:
assert(ord(s[i+1]) shr 6 == 0b10)
assert(ord(s[i+2]) shr 6 == 0b10)
assert(ord(s[i+3]) shr 6 == 0b10)
assert(ord(s[i+4]) shr 6 == 0b10)
assert(ord(s[i+5]) shr 6 == 0b10)
# assert(ord(s[i+1]) shr 6 == 0b10)
# assert(ord(s[i+2]) shr 6 == 0b10)
# assert(ord(s[i+3]) shr 6 == 0b10)
# assert(ord(s[i+4]) shr 6 == 0b10)
# assert(ord(s[i+5]) shr 6 == 0b10)
result = TRune((ord(s[i]) and ones(1)) shl 30 or
(ord(s[i+1]) and ones(6)) shl 24 or
(ord(s[i+2]) and ones(6)) shl 18 or