Use ^ instead of - in slices

This commit is contained in:
def 2015-03-27 11:49:28 +01:00
commit 64903e7050
10 changed files with 93 additions and 93 deletions

View file

@ -61,8 +61,8 @@ proc pasv(ftp: AsyncFtpClient) {.async.} =
assertReply(pasvMsg, "227") assertReply(pasvMsg, "227")
var betweenParens = captureBetween(pasvMsg.string, '(', ')') var betweenParens = captureBetween(pasvMsg.string, '(', ')')
var nums = betweenParens.split(',') var nums = betweenParens.split(',')
var ip = nums[0.. -3] var ip = nums[0.. ^3]
var port = nums[-2.. -1] var port = nums[^2.. ^1]
var properPort = port[0].parseInt()*256+port[1].parseInt() var properPort = port[0].parseInt()*256+port[1].parseInt()
await ftp.dsock.connect(ip.join("."), Port(properPort.toU16)) await ftp.dsock.connect(ip.join("."), Port(properPort.toU16))
ftp.dsockConnected = true ftp.dsockConnected = true

View file

@ -15,8 +15,8 @@ from rawsockets import nil
from asyncdispatch import PFuture from asyncdispatch import PFuture
## This module **partially** implements an FTP client as specified ## This module **partially** implements an FTP client as specified
## by `RFC 959 <http://tools.ietf.org/html/rfc959>`_. ## by `RFC 959 <http://tools.ietf.org/html/rfc959>`_.
## ##
## This module provides both a synchronous and asynchronous implementation. ## This module provides both a synchronous and asynchronous implementation.
## The asynchronous implementation requires you to use the ``asyncFTPClient`` ## The asynchronous implementation requires you to use the ``asyncFTPClient``
## function. You are then required to register the ``AsyncFTPClient`` with a ## function. You are then required to register the ``AsyncFTPClient`` with a
@ -27,7 +27,7 @@ from asyncdispatch import PFuture
## file transfers, calls to functions which use the command socket will block. ## file transfers, calls to functions which use the command socket will block.
## ##
## Here is some example usage of this module: ## Here is some example usage of this module:
## ##
## .. code-block:: Nim ## .. code-block:: Nim
## var ftp = ftpClient("example.org", user = "user", pass = "pass") ## var ftp = ftpClient("example.org", user = "user", pass = "pass")
## ftp.connect() ## ftp.connect()
@ -51,7 +51,7 @@ type
port*: rawsockets.Port port*: rawsockets.Port
else: else:
port*: Port port*: Port
jobInProgress*: bool jobInProgress*: bool
job*: FTPJob[SockType] job*: FTPJob[SockType]
@ -91,7 +91,7 @@ type
of EvLines: of EvLines:
lines*: string ## Lines that have been transferred. lines*: string ## Lines that have been transferred.
of EvRetr, EvStore: ## Retr/Store operation finished. of EvRetr, EvStore: ## Retr/Store operation finished.
nil nil
of EvTransferProgress: of EvTransferProgress:
bytesTotal*: BiggestInt ## Bytes total. bytesTotal*: BiggestInt ## Bytes total.
bytesFinished*: BiggestInt ## Bytes transferred. bytesFinished*: BiggestInt ## Bytes transferred.
@ -213,7 +213,7 @@ proc handleConnect(s: AsyncSocket, ftp: AsyncFTPClient) =
proc handleRead(s: AsyncSocket, ftp: AsyncFTPClient) = proc handleRead(s: AsyncSocket, ftp: AsyncFTPClient) =
assert ftp.jobInProgress assert ftp.jobInProgress
assert ftp.job.typ != JStore assert ftp.job.typ != JStore
# This can never return true, because it shouldn't check for code # This can never return true, because it shouldn't check for code
# 226 from csock. # 226 from csock.
assert(not ftp.job.prc(ftp, true)) assert(not ftp.job.prc(ftp, true))
@ -236,13 +236,13 @@ proc pasv[T](ftp: FtpBase[T]) =
ftp.disp.register(ftp.dsock) ftp.disp.register(ftp.dsock)
else: else:
{.fatal: "Incorrect socket instantiation".} {.fatal: "Incorrect socket instantiation".}
var pasvMsg = ftp.send("PASV").string.strip.TaintedString var pasvMsg = ftp.send("PASV").string.strip.TaintedString
assertReply(pasvMsg, "227") assertReply(pasvMsg, "227")
var betweenParens = captureBetween(pasvMsg.string, '(', ')') var betweenParens = captureBetween(pasvMsg.string, '(', ')')
var nums = betweenParens.split(',') var nums = betweenParens.split(',')
var ip = nums[0.. -3] var ip = nums[0.. ^3]
var port = nums[-2.. -1] var port = nums[^2.. ^1]
var properPort = port[0].parseInt()*256+port[1].parseInt() var properPort = port[0].parseInt()*256+port[1].parseInt()
ftp.dsock.connect(ip.join("."), Port(properPort.toU16)) ftp.dsock.connect(ip.join("."), Port(properPort.toU16))
when T is AsyncSocket: when T is AsyncSocket:
@ -307,7 +307,7 @@ proc getLines[T](ftp: FtpBase[T], async: bool = false): bool =
ftp.job.lines.add(r.string & "\n") ftp.job.lines.add(r.string & "\n")
else: else:
{.fatal: "Incorrect socket instantiation".} {.fatal: "Incorrect socket instantiation".}
if not async: if not async:
var readSocks: seq[Socket] = @[ftp.csock] var readSocks: seq[Socket] = @[ftp.csock]
# This is only needed here. Asyncio gets this socket... # This is only needed here. Asyncio gets this socket...
@ -396,7 +396,7 @@ proc chmod*[T](ftp: FtpBase[T], path: string,
proc list*[T](ftp: FtpBase[T], dir: string = "", async = false): string = proc list*[T](ftp: FtpBase[T], dir: string = "", async = false): string =
## Lists all files in ``dir``. If ``dir`` is ``""``, uses the current ## Lists all files in ``dir``. If ``dir`` is ``""``, uses the current
## working directory. If ``async`` is true, this function will return ## working directory. If ``async`` is true, this function will return
## immediately and it will be your job to call asyncio's ## immediately and it will be your job to call asyncio's
## ``poll`` to progress this operation. ## ``poll`` to progress this operation.
ftp.createJob(getLines[T], JRetrText) ftp.createJob(getLines[T], JRetrText)
ftp.pasv() ftp.pasv()
@ -417,7 +417,7 @@ proc retrText*[T](ftp: FtpBase[T], file: string, async = false): string =
ftp.createJob(getLines[T], JRetrText) ftp.createJob(getLines[T], JRetrText)
ftp.pasv() ftp.pasv()
assertReply ftp.send("RETR " & file.normalizePathSep), ["125", "150"] assertReply ftp.send("RETR " & file.normalizePathSep), ["125", "150"]
if not async: if not async:
while not ftp.job.prc(ftp, false): discard while not ftp.job.prc(ftp, false): discard
result = ftp.job.lines result = ftp.job.lines
@ -436,7 +436,7 @@ proc getFile[T](ftp: FtpBase[T], async = false): bool =
else: else:
bytesRead = ftp.dsock.recvAsync(r, BufferSize) bytesRead = ftp.dsock.recvAsync(r, BufferSize)
returned = bytesRead != -1 returned = bytesRead != -1
else: else:
bytesRead = ftp.dsock.recv(r, BufferSize) bytesRead = ftp.dsock.recv(r, BufferSize)
returned = true returned = true
let r2 = r.string let r2 = r.string
@ -458,7 +458,7 @@ proc getFile[T](ftp: FtpBase[T], async = false): bool =
proc retrFile*[T](ftp: FtpBase[T], file, dest: string, async = false) = proc retrFile*[T](ftp: FtpBase[T], file, dest: string, async = false) =
## Downloads ``file`` and saves it to ``dest``. Usage of this function ## Downloads ``file`` and saves it to ``dest``. Usage of this function
## asynchronously is recommended to view the progress of the download. ## asynchronously is recommended to view the progress of the download.
## The ``EvRetr`` event is passed to the specified ``handleEvent`` function ## The ``EvRetr`` event is passed to the specified ``handleEvent`` function
## when the download is finished, and the ``filename`` field will be equal ## when the download is finished, and the ``filename`` field will be equal
## to ``file``. ## to ``file``.
ftp.createJob(getFile[T], JRetr) ftp.createJob(getFile[T], JRetr)
@ -471,7 +471,7 @@ proc retrFile*[T](ftp: FtpBase[T], file, dest: string, async = false) =
var fileSize: BiggestInt var fileSize: BiggestInt
if reply.string.captureBetween('(', ')').parseBiggestInt(fileSize) == 0: if reply.string.captureBetween('(', ')').parseBiggestInt(fileSize) == 0:
raise newException(ReplyError, "Reply has no file size.") raise newException(ReplyError, "Reply has no file size.")
ftp.job.total = fileSize ftp.job.total = fileSize
ftp.job.lastProgressReport = epochTime() ftp.job.lastProgressReport = epochTime()
ftp.job.filename = file.normalizePathSep ftp.job.filename = file.normalizePathSep
@ -488,7 +488,7 @@ proc doUpload[T](ftp: FtpBase[T], async = false): bool =
if bytesSent == ftp.job.toStore.len: if bytesSent == ftp.job.toStore.len:
ftp.job.toStore = "" ftp.job.toStore = ""
elif bytesSent != ftp.job.toStore.len and bytesSent != 0: elif bytesSent != ftp.job.toStore.len and bytesSent != 0:
ftp.job.toStore = ftp.job.toStore[bytesSent .. -1] ftp.job.toStore = ftp.job.toStore[bytesSent .. ^1]
ftp.job.progress.inc(bytesSent) ftp.job.progress.inc(bytesSent)
ftp.job.oneSecond.inc(bytesSent) ftp.job.oneSecond.inc(bytesSent)
else: else:
@ -499,12 +499,12 @@ proc doUpload[T](ftp: FtpBase[T], async = false): bool =
# File finished uploading. # File finished uploading.
ftp.dsock.close() ftp.dsock.close()
ftp.dsockConnected = false ftp.dsockConnected = false
if not async: if not async:
assertReply ftp.expectReply(), "226" assertReply ftp.expectReply(), "226"
return true return true
return false return false
if not async: if not async:
ftp.dsock.send(s) ftp.dsock.send(s)
else: else:
@ -512,9 +512,9 @@ proc doUpload[T](ftp: FtpBase[T], async = false): bool =
if bytesSent == 0: if bytesSent == 0:
ftp.job.toStore.add(s) ftp.job.toStore.add(s)
elif bytesSent != s.len: elif bytesSent != s.len:
ftp.job.toStore.add(s[bytesSent .. -1]) ftp.job.toStore.add(s[bytesSent .. ^1])
len = bytesSent len = bytesSent
ftp.job.progress.inc(len) ftp.job.progress.inc(len)
ftp.job.oneSecond.inc(len) ftp.job.oneSecond.inc(len)
@ -522,8 +522,8 @@ proc store*[T](ftp: FtpBase[T], file, dest: string, async = false) =
## Uploads ``file`` to ``dest`` on the remote FTP server. Usage of this ## Uploads ``file`` to ``dest`` on the remote FTP server. Usage of this
## function asynchronously is recommended to view the progress of ## function asynchronously is recommended to view the progress of
## the download. ## the download.
## The ``EvStore`` event is passed to the specified ``handleEvent`` function ## The ``EvStore`` event is passed to the specified ``handleEvent`` function
## when the upload is finished, and the ``filename`` field will be ## when the upload is finished, and the ``filename`` field will be
## equal to ``file``. ## equal to ``file``.
ftp.createJob(doUpload[T], JStore) ftp.createJob(doUpload[T], JStore)
ftp.job.file = open(file) ftp.job.file = open(file)
@ -531,7 +531,7 @@ proc store*[T](ftp: FtpBase[T], file, dest: string, async = false) =
ftp.job.lastProgressReport = epochTime() ftp.job.lastProgressReport = epochTime()
ftp.job.filename = file ftp.job.filename = file
ftp.pasv() ftp.pasv()
assertReply ftp.send("STOR " & dest.normalizePathSep), ["125", "150"] assertReply ftp.send("STOR " & dest.normalizePathSep), ["125", "150"]
if not async: if not async:
@ -564,12 +564,12 @@ proc csockHandleRead(s: AsyncSocket, ftp: AsyncFTPClient) =
if ftp.job.progress != ftp.job.total: if ftp.job.progress != ftp.job.total:
raise newException(FTPError, "Didn't upload full file.") raise newException(FTPError, "Didn't upload full file.")
ftp.deleteJob() ftp.deleteJob()
ftp.handleEvent(ftp, r) ftp.handleEvent(ftp, r)
proc asyncFTPClient*(address: string, port = Port(21), proc asyncFTPClient*(address: string, port = Port(21),
user, pass = "", user, pass = "",
handleEvent: proc (ftp: AsyncFTPClient, ev: FTPEvent) {.closure,gcsafe.} = handleEvent: proc (ftp: AsyncFTPClient, ev: FTPEvent) {.closure,gcsafe.} =
(proc (ftp: AsyncFTPClient, ev: FTPEvent) = discard)): AsyncFTPClient = (proc (ftp: AsyncFTPClient, ev: FTPEvent) = discard)): AsyncFTPClient =
## Create a ``AsyncFTPClient`` object. ## Create a ``AsyncFTPClient`` object.
## ##
@ -617,7 +617,7 @@ when isMainModule:
echo d.len echo d.len
else: assert(false) else: assert(false)
var ftp = asyncFTPClient("example.com", user = "foo", pass = "bar", handleEvent = hev) var ftp = asyncFTPClient("example.com", user = "foo", pass = "bar", handleEvent = hev)
d.register(ftp) d.register(ftp)
d.len.echo() d.len.echo()
ftp.connect() ftp.connect()

View file

@ -227,7 +227,7 @@ proc parseResponse(s: Socket, getBody: bool, timeout: int): Response =
inc(linei, le) inc(linei, le)
# Status code # Status code
linei.inc skipWhitespace(line, linei) linei.inc skipWhitespace(line, linei)
result.status = line[linei .. -1] result.status = line[linei .. ^1]
parsedStatus = true parsedStatus = true
else: else:
# Parse headers # Parse headers
@ -238,7 +238,7 @@ proc parseResponse(s: Socket, getBody: bool, timeout: int): Response =
if line[linei] != ':': httpError("invalid headers") if line[linei] != ':': httpError("invalid headers")
inc(linei) # Skip : inc(linei) # Skip :
result.headers[name] = line[linei.. -1].strip() result.headers[name] = line[linei.. ^1].strip()
if not fullyRead: if not fullyRead:
httpError("Connection was closed before full request has been made") httpError("Connection was closed before full request has been made")
if getBody: if getBody:
@ -442,7 +442,7 @@ proc request*(url: string, httpMethod = httpGET, extraHeaders = "",
## | Extra headers can be specified and must be separated by ``\c\L`` ## | Extra headers can be specified and must be separated by ``\c\L``
## | An optional timeout can be specified in miliseconds, if reading from the ## | An optional timeout can be specified in miliseconds, if reading from the
## server takes longer than specified an ETimeout exception will be raised. ## server takes longer than specified an ETimeout exception will be raised.
result = request(url, $httpMethod, extraHeaders, body, sslContext, timeout, result = request(url, $httpMethod, extraHeaders, body, sslContext, timeout,
userAgent, proxy) userAgent, proxy)
proc redirection(status: string): bool = proc redirection(status: string): bool =
@ -725,7 +725,7 @@ proc parseResponse(client: AsyncHttpClient,
inc(linei, le) inc(linei, le)
# Status code # Status code
linei.inc skipWhitespace(line, linei) linei.inc skipWhitespace(line, linei)
result.status = line[linei .. -1] result.status = line[linei .. ^1]
parsedStatus = true parsedStatus = true
else: else:
# Parse headers # Parse headers
@ -736,7 +736,7 @@ proc parseResponse(client: AsyncHttpClient,
if line[linei] != ':': httpError("invalid headers") if line[linei] != ':': httpError("invalid headers")
inc(linei) # Skip : inc(linei) # Skip :
result.headers[name] = line[linei.. -1].strip() result.headers[name] = line[linei.. ^1].strip()
if not fullyRead: if not fullyRead:
httpError("Connection was closed before full request has been made") httpError("Connection was closed before full request has been made")
if getBody: if getBody:

View file

@ -10,7 +10,7 @@
## This module implements a simple logger. It has been designed to be as simple ## This module implements a simple logger. It has been designed to be as simple
## as possible to avoid bloat, if this library does not fulfill your needs, ## as possible to avoid bloat, if this library does not fulfill your needs,
## write your own. ## write your own.
## ##
## Format strings support the following variables which must be prefixed with ## Format strings support the following variables which must be prefixed with
## the dollar operator (``$``): ## the dollar operator (``$``):
## ##
@ -21,13 +21,13 @@
## $time Current time ## $time Current time
## $app ``os.getAppFilename()`` ## $app ``os.getAppFilename()``
## ============ ======================= ## ============ =======================
## ##
## ##
## The following example demonstrates logging to three different handlers ## The following example demonstrates logging to three different handlers
## simultaneously: ## simultaneously:
## ##
## .. code-block:: nim ## .. code-block:: nim
## ##
## var L = newConsoleLogger() ## var L = newConsoleLogger()
## var fL = newFileLogger("test.log", fmtStr = verboseFmtStr) ## var fL = newFileLogger("test.log", fmtStr = verboseFmtStr)
## var rL = newRollingFileLogger("rolling.log", fmtStr = verboseFmtStr) ## var rL = newRollingFileLogger("rolling.log", fmtStr = verboseFmtStr)
@ -64,20 +64,20 @@ const
type type
Logger* = ref object of RootObj ## abstract logger; the base type of all loggers Logger* = ref object of RootObj ## abstract logger; the base type of all loggers
levelThreshold*: Level ## only messages of level >= levelThreshold levelThreshold*: Level ## only messages of level >= levelThreshold
## should be processed ## should be processed
fmtStr: string ## = defaultFmtStr by default, see substituteLog for $date etc. fmtStr: string ## = defaultFmtStr by default, see substituteLog for $date etc.
ConsoleLogger* = ref object of Logger ## logger that writes the messages to the ConsoleLogger* = ref object of Logger ## logger that writes the messages to the
## console ## console
FileLogger* = ref object of Logger ## logger that writes the messages to a file FileLogger* = ref object of Logger ## logger that writes the messages to a file
f: File f: File
RollingFileLogger* = ref object of FileLogger ## logger that writes the RollingFileLogger* = ref object of FileLogger ## logger that writes the
## messages to a file and ## messages to a file and
## performs log rotation ## performs log rotation
maxLines: int # maximum number of lines maxLines: int # maximum number of lines
curLine : int curLine : int
baseName: string # initial filename baseName: string # initial filename
baseMode: FileMode # initial file mode baseMode: FileMode # initial file mode
@ -86,22 +86,22 @@ type
{.deprecated: [TLevel: Level, PLogger: Logger, PConsoleLogger: ConsoleLogger, {.deprecated: [TLevel: Level, PLogger: Logger, PConsoleLogger: ConsoleLogger,
PFileLogger: FileLogger, PRollingFileLogger: RollingFileLogger].} PFileLogger: FileLogger, PRollingFileLogger: RollingFileLogger].}
proc substituteLog(frmt: string): string = proc substituteLog(frmt: string): string =
## converts $date to the current date ## converts $date to the current date
## converts $time to the current time ## converts $time to the current time
## converts $app to getAppFilename() ## converts $app to getAppFilename()
## converts ## converts
result = newStringOfCap(frmt.len + 20) result = newStringOfCap(frmt.len + 20)
var i = 0 var i = 0
while i < frmt.len: while i < frmt.len:
if frmt[i] != '$': if frmt[i] != '$':
result.add(frmt[i]) result.add(frmt[i])
inc(i) inc(i)
else: else:
inc(i) inc(i)
var v = "" var v = ""
var app = getAppFilename() var app = getAppFilename()
while frmt[i] in IdentChars: while frmt[i] in IdentChars:
v.add(toLower(frmt[i])) v.add(toLower(frmt[i]))
inc(i) inc(i)
case v case v
@ -114,12 +114,12 @@ proc substituteLog(frmt: string): string =
method log*(logger: Logger, level: Level, method log*(logger: Logger, level: Level,
frmt: string, args: varargs[string, `$`]) {. frmt: string, args: varargs[string, `$`]) {.
raises: [Exception], raises: [Exception],
tags: [TimeEffect, WriteIOEffect, ReadIOEffect].} = tags: [TimeEffect, WriteIOEffect, ReadIOEffect].} =
## Override this method in custom loggers. Default implementation does ## Override this method in custom loggers. Default implementation does
## nothing. ## nothing.
discard discard
method log*(logger: ConsoleLogger, level: Level, method log*(logger: ConsoleLogger, level: Level,
frmt: string, args: varargs[string, `$`]) = frmt: string, args: varargs[string, `$`]) =
## Logs to the console using ``logger`` only. ## Logs to the console using ``logger`` only.
@ -127,14 +127,14 @@ method log*(logger: ConsoleLogger, level: Level,
writeln(stdout, LevelNames[level], " ", substituteLog(logger.fmtStr), writeln(stdout, LevelNames[level], " ", substituteLog(logger.fmtStr),
frmt % args) frmt % args)
method log*(logger: FileLogger, level: Level, method log*(logger: FileLogger, level: Level,
frmt: string, args: varargs[string, `$`]) = frmt: string, args: varargs[string, `$`]) =
## Logs to a file using ``logger`` only. ## Logs to a file using ``logger`` only.
if level >= logger.levelThreshold: if level >= logger.levelThreshold:
writeln(logger.f, LevelNames[level], " ", writeln(logger.f, LevelNames[level], " ",
substituteLog(logger.fmtStr), frmt % args) substituteLog(logger.fmtStr), frmt % args)
proc defaultFilename*(): string = proc defaultFilename*(): string =
## Returns the default filename for a logger. ## Returns the default filename for a logger.
var (path, name, ext) = splitFile(getAppFilename()) var (path, name, ext) = splitFile(getAppFilename())
result = changeFileExt(path / name, "log") result = changeFileExt(path / name, "log")
@ -145,10 +145,10 @@ proc newConsoleLogger*(levelThreshold = lvlAll, fmtStr = defaultFmtStr): Console
result.fmtStr = fmtStr result.fmtStr = fmtStr
result.levelThreshold = levelThreshold result.levelThreshold = levelThreshold
proc newFileLogger*(filename = defaultFilename(), proc newFileLogger*(filename = defaultFilename(),
mode: FileMode = fmAppend, mode: FileMode = fmAppend,
levelThreshold = lvlAll, levelThreshold = lvlAll,
fmtStr = defaultFmtStr): FileLogger = fmtStr = defaultFmtStr): FileLogger =
## Creates a new file logger. This logger logs to a file. ## Creates a new file logger. This logger logs to a file.
new(result) new(result)
result.levelThreshold = levelThreshold result.levelThreshold = levelThreshold
@ -170,14 +170,14 @@ proc countFiles(filename: string): int =
if kind == pcFile: if kind == pcFile:
let llfn = name & ext & ExtSep let llfn = name & ext & ExtSep
if path.extractFilename.startsWith(llfn): if path.extractFilename.startsWith(llfn):
let numS = path.extractFilename[llfn.len .. -1] let numS = path.extractFilename[llfn.len .. ^1]
try: try:
let num = parseInt(numS) let num = parseInt(numS)
if num > result: if num > result:
result = num result = num
except ValueError: discard except ValueError: discard
proc newRollingFileLogger*(filename = defaultFilename(), proc newRollingFileLogger*(filename = defaultFilename(),
mode: FileMode = fmReadWrite, mode: FileMode = fmReadWrite,
levelThreshold = lvlAll, levelThreshold = lvlAll,
fmtStr = defaultFmtStr, fmtStr = defaultFmtStr,
@ -192,9 +192,9 @@ proc newRollingFileLogger*(filename = defaultFilename(),
result.curLine = 0 result.curLine = 0
result.baseName = filename result.baseName = filename
result.baseMode = mode result.baseMode = mode
result.logFiles = countFiles(filename) result.logFiles = countFiles(filename)
if mode == fmAppend: if mode == fmAppend:
# We need to get a line count because we will be appending to the file. # We need to get a line count because we will be appending to the file.
result.curLine = countLogLines(result) result.curLine = countLogLines(result)
@ -206,7 +206,7 @@ proc rotate(logger: RollingFileLogger) =
moveFile(dir / (name & ext & srcSuff), moveFile(dir / (name & ext & srcSuff),
dir / (name & ext & ExtSep & $(i+1))) dir / (name & ext & ExtSep & $(i+1)))
method log*(logger: RollingFileLogger, level: Level, method log*(logger: RollingFileLogger, level: Level,
frmt: string, args: varargs[string, `$`]) = frmt: string, args: varargs[string, `$`]) =
## Logs to a file using rolling ``logger`` only. ## Logs to a file using rolling ``logger`` only.
if level >= logger.levelThreshold: if level >= logger.levelThreshold:
@ -216,7 +216,7 @@ method log*(logger: RollingFileLogger, level: Level,
logger.logFiles.inc logger.logFiles.inc
logger.curLine = 0 logger.curLine = 0
logger.f = open(logger.baseName, logger.baseMode) logger.f = open(logger.baseName, logger.baseMode)
writeln(logger.f, LevelNames[level], " ",substituteLog(logger.fmtStr), frmt % args) writeln(logger.f, LevelNames[level], " ",substituteLog(logger.fmtStr), frmt % args)
logger.curLine.inc logger.curLine.inc
@ -226,7 +226,7 @@ var level {.threadvar.}: Level ## global log filter
var handlers {.threadvar.}: seq[Logger] ## handlers with their own log levels var handlers {.threadvar.}: seq[Logger] ## handlers with their own log levels
proc logLoop(level: Level, frmt: string, args: varargs[string, `$`]) = proc logLoop(level: Level, frmt: string, args: varargs[string, `$`]) =
for logger in items(handlers): for logger in items(handlers):
if level >= logger.levelThreshold: if level >= logger.levelThreshold:
log(logger, level, frmt, args) log(logger, level, frmt, args)
@ -235,7 +235,7 @@ template log*(level: Level, frmt: string, args: varargs[string, `$`]) =
bind logLoop bind logLoop
bind `%` bind `%`
bind logging.level bind logging.level
if level >= logging.level: if level >= logging.level:
logLoop(level, frmt, args) logLoop(level, frmt, args)
@ -243,19 +243,19 @@ template debug*(frmt: string, args: varargs[string, `$`]) =
## Logs a debug message to all registered handlers. ## Logs a debug message to all registered handlers.
log(lvlDebug, frmt, args) log(lvlDebug, frmt, args)
template info*(frmt: string, args: varargs[string, `$`]) = template info*(frmt: string, args: varargs[string, `$`]) =
## Logs an info message to all registered handlers. ## Logs an info message to all registered handlers.
log(lvlInfo, frmt, args) log(lvlInfo, frmt, args)
template warn*(frmt: string, args: varargs[string, `$`]) = template warn*(frmt: string, args: varargs[string, `$`]) =
## Logs a warning message to all registered handlers. ## Logs a warning message to all registered handlers.
log(lvlWarn, frmt, args) log(lvlWarn, frmt, args)
template error*(frmt: string, args: varargs[string, `$`]) = template error*(frmt: string, args: varargs[string, `$`]) =
## Logs an error message to all registered handlers. ## Logs an error message to all registered handlers.
log(lvlError, frmt, args) log(lvlError, frmt, args)
template fatal*(frmt: string, args: varargs[string, `$`]) = template fatal*(frmt: string, args: varargs[string, `$`]) =
## Logs a fatal error message to all registered handlers. ## Logs a fatal error message to all registered handlers.
log(lvlFatal, frmt, args) log(lvlFatal, frmt, args)
@ -287,5 +287,5 @@ when isMainModule:
addHandler(rL) addHandler(rL)
for i in 0 .. 25: for i in 0 .. 25:
info("hello" & $i, []) info("hello" & $i, [])

View file

@ -2874,7 +2874,7 @@ when hostOS != "standalone":
## ##
## .. code-block:: nim ## .. code-block:: nim
## var s = "abcdef" ## var s = "abcdef"
## s[1 .. -2] = "xyz" ## s[1 .. ^2] = "xyz"
## assert s == "axyzf" ## assert s == "axyzf"
var a = x.a var a = x.a
var L = x.b - a + 1 var L = x.b - a + 1

View file

@ -7,6 +7,6 @@ discard """
proc test: string = proc test: string =
result = "blah" result = "blah"
result[1 .. -1] result[1 .. ^1]
echo test() echo test()

View file

@ -27,7 +27,7 @@ proc refExpr(exprNode: NimNode): string {.compileTime.} =
"expr" & $(exprNodes.len - 1) "expr" & $(exprNodes.len - 1)
proc derefExpr(exprRef: string): NimNode {.compileTime.} = proc derefExpr(exprRef: string): NimNode {.compileTime.} =
exprNodes[parseInt(exprRef[4 .. -1])] exprNodes[parseInt(exprRef[4 .. ^1])]
#=============================================================================== #===============================================================================
# Define a type that allows a callable expression to be mapped onto elements # Define a type that allows a callable expression to be mapped onto elements

View file

@ -36,7 +36,7 @@ echo()
var myseq = @[1, 2, 3, 4, 5, 6] var myseq = @[1, 2, 3, 4, 5, 6]
myseq[0..2] = myseq[-3.. -1] myseq[0..2] = myseq[^3 .. ^1]
for x in items(myseq): stdout.write(x) for x in items(myseq): stdout.write(x)
echo() echo()
@ -46,7 +46,7 @@ echo mystr
mystr[4..4] = "u" mystr[4..4] = "u"
# test full replacement # test full replacement
mystr[.. -2] = "egerichtet" mystr[.. ^2] = "egerichtet"
echo mystr echo mystr
@ -54,6 +54,6 @@ mystr[0..2] = "ve"
echo mystr echo mystr
var s = "abcdef" var s = "abcdef"
s[1 .. -2] = "xyz" s[1 .. ^2] = "xyz"
assert s == "axyzf" assert s == "axyzf"

View file

@ -22,35 +22,35 @@ proc delNimCache() =
removeDir(nimcacheDir) removeDir(nimcacheDir)
except OSError: except OSError:
echo "[Warning] could not delete: ", nimcacheDir echo "[Warning] could not delete: ", nimcacheDir
proc runRodFiles(r: var TResults, cat: Category, options: string) = proc runRodFiles(r: var TResults, cat: Category, options: string) =
template test(filename: expr): stmt = template test(filename: expr): stmt =
testSpec r, makeTest(rodfilesDir / filename, options, cat, actionRun) testSpec r, makeTest(rodfilesDir / filename, options, cat, actionRun)
delNimCache() delNimCache()
# test basic recompilation scheme: # test basic recompilation scheme:
test "hallo" test "hallo"
test "hallo" test "hallo"
# test incremental type information: # test incremental type information:
test "hallo2" test "hallo2"
delNimCache() delNimCache()
# test type converters: # test type converters:
test "aconv" test "aconv"
test "bconv" test "bconv"
delNimCache() delNimCache()
# test G, A, B example from the documentation; test init sections: # test G, A, B example from the documentation; test init sections:
test "deada" test "deada"
test "deada2" test "deada2"
delNimCache() delNimCache()
# test method generation: # test method generation:
test "bmethods" test "bmethods"
test "bmethods2" test "bmethods2"
delNimCache() delNimCache()
# test generics: # test generics:
test "tgeneric1" test "tgeneric1"
test "tgeneric2" test "tgeneric2"
@ -79,8 +79,8 @@ proc runBasicDLLTest(c, r: var TResults, cat: Category, options: string) =
options & " --app:lib -d:createNimRtl", cat) options & " --app:lib -d:createNimRtl", cat)
testSpec c, makeTest("tests/dll/server.nim", testSpec c, makeTest("tests/dll/server.nim",
options & " --app:lib -d:useNimRtl", cat) options & " --app:lib -d:useNimRtl", cat)
when defined(Windows): when defined(Windows):
# windows looks in the dir of the exe (yay!): # windows looks in the dir of the exe (yay!):
var nimrtlDll = DynlibFormat % "nimrtl" var nimrtlDll = DynlibFormat % "nimrtl"
safeCopyFile("lib" / nimrtlDll, "tests/dll" / nimrtlDll) safeCopyFile("lib" / nimrtlDll, "tests/dll" / nimrtlDll)
@ -91,14 +91,14 @@ proc runBasicDLLTest(c, r: var TResults, cat: Category, options: string) =
putEnv("LD_LIBRARY_PATH", "lib:" & libpath) putEnv("LD_LIBRARY_PATH", "lib:" & libpath)
var serverDll = DynlibFormat % "server" var serverDll = DynlibFormat % "server"
safeCopyFile("tests/dll" / serverDll, "lib" / serverDll) safeCopyFile("tests/dll" / serverDll, "lib" / serverDll)
testSpec r, makeTest("tests/dll/client.nim", options & " -d:useNimRtl", testSpec r, makeTest("tests/dll/client.nim", options & " -d:useNimRtl",
cat, actionRun) cat, actionRun)
proc dllTests(r: var TResults, cat: Category, options: string) = proc dllTests(r: var TResults, cat: Category, options: string) =
# dummy compile result: # dummy compile result:
var c = initResults() var c = initResults()
runBasicDLLTest c, r, cat, options runBasicDLLTest c, r, cat, options
runBasicDLLTest c, r, cat, options & " -d:release" runBasicDLLTest c, r, cat, options & " -d:release"
runBasicDLLTest c, r, cat, options & " --gc:boehm" runBasicDLLTest c, r, cat, options & " --gc:boehm"
@ -134,7 +134,7 @@ proc gcTests(r: var TResults, cat: Category, options: string) =
test "cycleleak" test "cycleleak"
test "closureleak" test "closureleak"
testWithoutMs "refarrayleak" testWithoutMs "refarrayleak"
test "stackrefleak" test "stackrefleak"
test "cyclecollector" test "cyclecollector"
@ -147,7 +147,7 @@ proc threadTests(r: var TResults, cat: Category, options: string) =
" -d:release", cat, actionRun) " -d:release", cat, actionRun)
testSpec r, makeTest("tests/threads" / filename, options & testSpec r, makeTest("tests/threads" / filename, options &
" --tlsEmulation:on", cat, actionRun) " --tlsEmulation:on", cat, actionRun)
test "tactors" test "tactors"
test "tactors2" test "tactors2"
test "threadex" test "threadex"
@ -182,7 +182,7 @@ proc jsTests(r: var TResults, cat: Category, options: string) =
actionRun, targetJS) actionRun, targetJS)
testSpec r, makeTest(filename, options & " -d:nodejs -d:release", cat, testSpec r, makeTest(filename, options & " -d:nodejs -d:release", cat,
actionRun, targetJS) actionRun, targetJS)
for t in os.walkFiles("tests/js/t*.nim"): for t in os.walkFiles("tests/js/t*.nim"):
test(t) test(t)
for testfile in ["exception/texceptions", "exception/texcpt1", for testfile in ["exception/texceptions", "exception/texcpt1",
@ -199,13 +199,13 @@ proc jsTests(r: var TResults, cat: Category, options: string) =
proc findMainFile(dir: string): string = proc findMainFile(dir: string): string =
# finds the file belonging to ".nim.cfg"; if there is no such file # finds the file belonging to ".nim.cfg"; if there is no such file
# it returns the some ".nim" file if there is only one: # it returns the some ".nim" file if there is only one:
const cfgExt = ".nim.cfg" const cfgExt = ".nim.cfg"
result = "" result = ""
var nimFiles = 0 var nimFiles = 0
for kind, file in os.walkDir(dir): for kind, file in os.walkDir(dir):
if kind == pcFile: if kind == pcFile:
if file.endsWith(cfgExt): return file[.. -(cfgExt.len+1)] & ".nim" if file.endsWith(cfgExt): return file[.. ^(cfgExt.len+1)] & ".nim"
elif file.endsWith(".nim"): elif file.endsWith(".nim"):
if result.len == 0: result = file if result.len == 0: result = file
inc nimFiles inc nimFiles
@ -236,7 +236,7 @@ type PackageFilter = enum
pfExtraOnly pfExtraOnly
pfAll pfAll
let let
nimbleExe = findExe("nimble") nimbleExe = findExe("nimble")
nimbleDir = getHomeDir() / ".nimble" nimbleDir = getHomeDir() / ".nimble"
packageDir = nimbleDir / "pkgs" packageDir = nimbleDir / "pkgs"

View file

@ -59,7 +59,7 @@ proc callCompiler(cmdTemplate, filename, options: string,
target: TTarget): TSpec = target: TTarget): TSpec =
let c = parseCmdLine(cmdTemplate % ["target", targetToCmd[target], let c = parseCmdLine(cmdTemplate % ["target", targetToCmd[target],
"options", options, "file", filename.quoteShell]) "options", options, "file", filename.quoteShell])
var p = startProcess(command=c[0], args=c[1.. -1], var p = startProcess(command=c[0], args=c[1.. ^1],
options={poStdErrToStdOut, poUseShell}) options={poStdErrToStdOut, poUseShell})
let outp = p.outputStream let outp = p.outputStream
var suc = "" var suc = ""
@ -284,7 +284,7 @@ proc main() =
let testsDir = "tests" & DirSep let testsDir = "tests" & DirSep
for kind, dir in walkDir(testsDir): for kind, dir in walkDir(testsDir):
assert testsDir.startsWith(testsDir) assert testsDir.startsWith(testsDir)
let cat = dir[testsDir.len .. -1] let cat = dir[testsDir.len .. ^1]
if kind == pcDir and cat notin ["testament", "testdata", "nimcache"]: if kind == pcDir and cat notin ["testament", "testdata", "nimcache"]:
processCategory(r, Category(cat), p.cmdLineRest.string) processCategory(r, Category(cat), p.cmdLineRest.string)
for a in AdditionalCategories: for a in AdditionalCategories: