summary refs log tree commit diff stats
path: root/lib/impure
diff options
context:
space:
mode:
Diffstat (limited to 'lib/impure')
-rw-r--r--lib/impure/db_odbc.nim45
-rw-r--r--lib/impure/nre.nim98
-rw-r--r--lib/impure/rdstdin.nim7
-rw-r--r--lib/impure/re.nim13
-rw-r--r--lib/impure/ssl.nim3
5 files changed, 85 insertions, 81 deletions
diff --git a/lib/impure/db_odbc.nim b/lib/impure/db_odbc.nim
index 3a14e6304..d6343acc7 100644
--- a/lib/impure/db_odbc.nim
+++ b/lib/impure/db_odbc.nim
@@ -210,7 +210,7 @@ proc dbFormat(formatstr: SqlQuery, args: varargs[string]): string {.
       add(result, c)
 
 proc prepareFetch(db: var DbConn, query: SqlQuery,
-                args: varargs[string, `$`]) : TSqlSmallInt {.
+                args: varargs[string, `$`]): TSqlSmallInt {.
                 tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
   # Prepare a statement, execute it and fetch the data to the driver
   # ready for retrieval of the data
@@ -222,9 +222,8 @@ proc prepareFetch(db: var DbConn, query: SqlQuery,
   var q = dbFormat(query, args)
   db.sqlCheck(SQLPrepare(db.stmt, q.PSQLCHAR, q.len.TSqlSmallInt))
   db.sqlCheck(SQLExecute(db.stmt))
-  var retcode = SQLFetch(db.stmt)
-  db.sqlCheck(retcode)
-  result=retcode
+  result = SQLFetch(db.stmt)
+  db.sqlCheck(result)
 
 proc prepareFetchDirect(db: var DbConn, query: SqlQuery,
                 args: varargs[string, `$`]) {.
@@ -250,8 +249,8 @@ proc tryExec*(db: var DbConn, query: SqlQuery, args: varargs[string, `$`]): bool
     var
       rCnt = -1
     res = SQLRowCount(db.stmt, rCnt)
-    if res != SQL_SUCCESS: dbError(db)
     properFreeResult(SQL_HANDLE_STMT, db.stmt)
+    if res != SQL_SUCCESS: dbError(db)
   except: discard
   return res == SQL_SUCCESS
 
@@ -286,10 +285,10 @@ iterator fastRows*(db: var DbConn, query: SqlQuery,
     sz: TSqlSmallInt = 0
     cCnt: TSqlSmallInt = 0.TSqlSmallInt
     res: TSqlSmallInt = 0.TSqlSmallInt
-    tempcCnt:TSqlSmallInt # temporary cCnt,Fix the field values to be null when the release schema is compiled.
-    # tempcCnt,A field to store the number of temporary variables, for unknown reasons, 
+    tempcCnt: TSqlSmallInt # temporary cCnt,Fix the field values to be null when the release schema is compiled.
+    # tempcCnt,A field to store the number of temporary variables, for unknown reasons,
     # after performing a sqlgetdata function and circulating variables cCnt value will be changed to 0,
-    # so the values of the temporary variable to store the cCnt. 
+    # so the values of the temporary variable to store the cCnt.
     # After every cycle and specified to cCnt. To ensure the traversal of all fields.
   res = db.prepareFetch(query, args)
   if res == SQL_NO_DATA:
@@ -308,8 +307,8 @@ iterator fastRows*(db: var DbConn, query: SqlQuery,
         cCnt = tempcCnt
       yield rowRes
       res = SQLFetch(db.stmt)
-  db.sqlCheck(res)
   properFreeResult(SQL_HANDLE_STMT, db.stmt)
+  db.sqlCheck(res)
 
 iterator instantRows*(db: var DbConn, query: SqlQuery,
                       args: varargs[string, `$`]): InstantRow
@@ -317,14 +316,14 @@ iterator instantRows*(db: var DbConn, query: SqlQuery,
   ## Same as fastRows but returns a handle that can be used to get column text
   ## on demand using []. Returned handle is valid only within the interator body.
   var
-    rowRes: Row
+    rowRes: Row = @[]
     sz: TSqlSmallInt = 0
     cCnt: TSqlSmallInt = 0.TSqlSmallInt
     res: TSqlSmallInt = 0.TSqlSmallInt
-    tempcCnt:TSqlSmallInt # temporary cCnt,Fix the field values to be null when the release schema is compiled.
-    # tempcCnt,A field to store the number of temporary variables, for unknown reasons, 
+    tempcCnt: TSqlSmallInt # temporary cCnt,Fix the field values to be null when the release schema is compiled.
+    # tempcCnt,A field to store the number of temporary variables, for unknown reasons,
     # after performing a sqlgetdata function and circulating variables cCnt value will be changed to 0,
-    # so the values of the temporary variable to store the cCnt. 
+    # so the values of the temporary variable to store the cCnt.
     # After every cycle and specified to cCnt. To ensure the traversal of all fields.
   res = db.prepareFetch(query, args)
   if res == SQL_NO_DATA:
@@ -343,8 +342,8 @@ iterator instantRows*(db: var DbConn, query: SqlQuery,
         cCnt = tempcCnt
       yield (row: rowRes, len: cCnt.int)
       res = SQLFetch(db.stmt)
-  db.sqlCheck(res)
   properFreeResult(SQL_HANDLE_STMT, db.stmt)
+  db.sqlCheck(res)
 
 proc `[]`*(row: InstantRow, col: int): string {.inline.} =
   ## Returns text for given column of the row
@@ -364,10 +363,10 @@ proc getRow*(db: var DbConn, query: SqlQuery,
     sz: TSqlSmallInt = 0.TSqlSmallInt
     cCnt: TSqlSmallInt = 0.TSqlSmallInt
     res: TSqlSmallInt = 0.TSqlSmallInt
-    tempcCnt:TSqlSmallInt # temporary cCnt,Fix the field values to be null when the release schema is compiled.
-    ## tempcCnt,A field to store the number of temporary variables, for unknown reasons, 
+    tempcCnt: TSqlSmallInt # temporary cCnt,Fix the field values to be null when the release schema is compiled.
+    ## tempcCnt,A field to store the number of temporary variables, for unknown reasons,
     ## after performing a sqlgetdata function and circulating variables cCnt value will be changed to 0,
-    ## so the values of the temporary variable to store the cCnt. 
+    ## so the values of the temporary variable to store the cCnt.
     ## After every cycle and specified to cCnt. To ensure the traversal of all fields.
   res = db.prepareFetch(query, args)
   if res == SQL_NO_DATA:
@@ -385,8 +384,8 @@ proc getRow*(db: var DbConn, query: SqlQuery,
       cCnt = tempcCnt
     res = SQLFetch(db.stmt)
     result = rowRes
-  db.sqlCheck(res)
   properFreeResult(SQL_HANDLE_STMT, db.stmt)
+  db.sqlCheck(res)
 
 proc getAllRows*(db: var DbConn, query: SqlQuery,
                  args: varargs[string, `$`]): seq[Row] {.
@@ -398,10 +397,10 @@ proc getAllRows*(db: var DbConn, query: SqlQuery,
     sz: TSqlSmallInt = 0
     cCnt: TSqlSmallInt = 0.TSqlSmallInt
     res: TSqlSmallInt = 0.TSqlSmallInt
-    tempcCnt:TSqlSmallInt # temporary cCnt,Fix the field values to be null when the release schema is compiled.
-    ## tempcCnt,A field to store the number of temporary variables, for unknown reasons, 
+    tempcCnt: TSqlSmallInt # temporary cCnt,Fix the field values to be null when the release schema is compiled.
+    ## tempcCnt,A field to store the number of temporary variables, for unknown reasons,
     ## after performing a sqlgetdata function and circulating variables cCnt value will be changed to 0,
-    ## so the values of the temporary variable to store the cCnt. 
+    ## so the values of the temporary variable to store the cCnt.
     ## After every cycle and specified to cCnt. To ensure the traversal of all fields.
   res = db.prepareFetch(query, args)
   if res == SQL_NO_DATA:
@@ -421,8 +420,8 @@ proc getAllRows*(db: var DbConn, query: SqlQuery,
       rows.add(rowRes)
       res = SQLFetch(db.stmt)
     result = rows
-    db.sqlCheck(res)
   properFreeResult(SQL_HANDLE_STMT, db.stmt)
+  db.sqlCheck(res)
 
 iterator rows*(db: var DbConn, query: SqlQuery,
                args: varargs[string, `$`]): Row {.
@@ -544,4 +543,4 @@ proc setEncoding*(connection: DbConn, encoding: string): bool {.
   ## Sets the encoding of a database connection, returns true for
   ## success, false for failure.
   ##result = set_character_set(connection, encoding) == 0
-  dbError("setEncoding() is currently not implemented by the db_odbc module")
\ No newline at end of file
+  dbError("setEncoding() is currently not implemented by the db_odbc module")
diff --git a/lib/impure/nre.nim b/lib/impure/nre.nim
index c8f690461..557bb0549 100644
--- a/lib/impure/nre.nim
+++ b/lib/impure/nre.nim
@@ -15,6 +15,8 @@ from math import ceil
 import options
 from unicode import runeLenAt
 
+export options
+
 
 ## What is NRE?
 ## ============
@@ -24,46 +26,34 @@ from unicode import runeLenAt
 ## Licencing
 ## ---------
 ##
-## PCRE has some additional terms that you must comply with if you use this module.::
+## PCRE has `some additional terms`_ that you must agree to in order to use
+## this module.
+##
+## .. _`some additional terms`: http://pcre.sourceforge.net/license.txt
+##
+## Example
+## -------
+##
+## .. code-block:: nim
+##
+##     import nre
 ##
-## > Copyright (c) 1997-2001 University of Cambridge
-## >
-## > Permission is granted to anyone to use this software for any purpose on any
-## > computer system, and to redistribute it freely, subject to the following
-## > restrictions:
-## >
-## > 1. This software is distributed in the hope that it will be useful,
-## >    but WITHOUT ANY WARRANTY; without even the implied warranty of
-## >    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-## >
-## > 2. The origin of this software must not be misrepresented, either by
-## >    explicit claim or by omission. In practice, this means that if you use
-## >    PCRE in software that you distribute to others, commercially or
-## >    otherwise, you must put a sentence like this
-## >
-## >      Regular expression support is provided by the PCRE library package,
-## >      which is open source software, written by Philip Hazel, and copyright
-## >      by the University of Cambridge, England.
-## >
-## >    somewhere reasonably visible in your documentation and in any relevant
-## >    files or online help data or similar. A reference to the ftp site for
-## >    the source, that is, to
-## >
-## >      ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
-## >
-## >    should also be given in the documentation. However, this condition is not
-## >    intended to apply to whole chains of software. If package A includes PCRE,
-## >    it must acknowledge it, but if package B is software that includes package
-## >    A, the condition is not imposed on package B (unless it uses PCRE
-## >    independently).
-## >
-## > 3. Altered versions must be plainly marked as such, and must not be
-## >    misrepresented as being the original software.
-## >
-## > 4. If PCRE is embedded in any software that is released under the GNU
-## >    General Purpose Licence (GPL), or Lesser General Purpose Licence (LGPL),
-## >    then the terms of that licence shall supersede any condition above with
-## >    which it is incompatible.
+##     let vowels = re"[aeoui]"
+##
+##     for match in "moigagoo".findIter(vowels):
+##       echo match.matchBounds
+##     # (a: 1, b: 1)
+##     # (a: 2, b: 2)
+##     # (a: 4, b: 4)
+##     # (a: 6, b: 6)
+##     # (a: 7, b: 7)
+##
+##     let firstVowel = "foo".find(vowels)
+##     let hasVowel = firstVowel.isSome()
+##     if hasVowel:
+##       let matchBounds = firstVowel.get().captureBounds[-1]
+##       echo "first vowel @", matchBounds.get().a
+##       # first vowel @1
 
 
 # Type definitions {{{
@@ -125,11 +115,11 @@ type
     ## -  ``(*NO_STUDY)`` - turn off studying; study is enabled by default
     ##
     ## For more details on the leading option groups, see the `Option
-    ## Setting <http://man7.org/linux/man-pages/man3/pcresyntax.3.html#OPTION_SETTING>`__
+    ## Setting <http://man7.org/linux/man-pages/man3/pcresyntax.3.html#OPTION_SETTING>`_
     ## and the `Newline
-    ## Convention <http://man7.org/linux/man-pages/man3/pcresyntax.3.html#NEWLINE_CONVENTION>`__
+    ## Convention <http://man7.org/linux/man-pages/man3/pcresyntax.3.html#NEWLINE_CONVENTION>`_
     ## sections of the `PCRE syntax
-    ## manual <http://man7.org/linux/man-pages/man3/pcresyntax.3.html>`__.
+    ## manual <http://man7.org/linux/man-pages/man3/pcresyntax.3.html>`_.
     pattern*: string  ## not nil
     pcreObj: ptr pcre.Pcre  ## not nil
     pcreExtra: ptr pcre.ExtraData  ## nil
@@ -284,7 +274,7 @@ proc `[]`*(pattern: Captures, name: string): string =
   let pattern = RegexMatch(pattern)
   return pattern.captures[pattern.pattern.captureNameToId.fget(name)]
 
-template toTableImpl(cond: bool): stmt {.immediate, dirty.} =
+template toTableImpl(cond: untyped) {.dirty.} =
   for key in RegexMatch(pattern).pattern.captureNameId.keys:
     let nextVal = pattern[key]
     if cond:
@@ -301,7 +291,7 @@ proc toTable*(pattern: CaptureBounds, default = none(Slice[int])):
   result = initTable[string, Option[Slice[int]]]()
   toTableImpl(nextVal.isNone)
 
-template itemsImpl(cond: bool): stmt {.immediate, dirty.} =
+template itemsImpl(cond: untyped) {.dirty.} =
   for i in 0 .. <RegexMatch(pattern).pattern.captureCount:
     let nextVal = pattern[i]
     # done in this roundabout way to avoid multiple yields (potential code
@@ -493,17 +483,17 @@ proc matchImpl(str: string, pattern: Regex, start, endpos: int, flags: int): Opt
       raise RegexInternalError(msg : "Unknown internal error: " & $execRet)
 
 proc match*(str: string, pattern: Regex, start = 0, endpos = int.high): Option[RegexMatch] =
-  ## Like ```find(...)`` <#proc-find>`__, but anchored to the start of the
+  ## Like ```find(...)`` <#proc-find>`_, but anchored to the start of the
   ## string. This means that ``"foo".match(re"f") == true``, but
   ## ``"foo".match(re"o") == false``.
   return str.matchImpl(pattern, start, endpos, pcre.ANCHORED)
 
 iterator findIter*(str: string, pattern: Regex, start = 0, endpos = int.high): RegexMatch =
-  ## Works the same as ```find(...)`` <#proc-find>`__, but finds every
+  ## Works the same as ```find(...)`` <#proc-find>`_, but finds every
   ## non-overlapping match. ``"2222".find(re"22")`` is ``"22", "22"``, not
   ## ``"22", "22", "22"``.
   ##
-  ## Arguments are the same as ```find(...)`` <#proc-find>`__
+  ## Arguments are the same as ```find(...)`` <#proc-find>`_
   ##
   ## Variants:
   ##
@@ -566,6 +556,16 @@ proc findAll*(str: string, pattern: Regex, start = 0, endpos = int.high): seq[st
   for match in str.findIter(pattern, start, endpos):
     result.add(match.match)
 
+proc contains*(str: string, pattern: Regex, start = 0, endpos = int.high): bool =
+  ## Determine if the string contains the given pattern between the end and
+  ## start positions:
+  ## -  "abc".contains(re"bc") == true
+  ## -  "abc".contains(re"cd") == false
+  ## -  "abc".contains(re"a", start = 1) == false
+  ##
+  ## Same as ``isSome(str.find(pattern, start, endpos))``.
+  return isSome(str.find(pattern, start, endpos))
+
 proc split*(str: string, pattern: Regex, maxSplit = -1, start = 0): seq[string] =
   ## Splits the string with the given regex. This works according to the
   ## rules that Perl and Javascript use:
@@ -581,7 +581,7 @@ proc split*(str: string, pattern: Regex, maxSplit = -1, start = 0): seq[string]
   ##    strings in the output seq.
   ##    ``"1.2.3".split(re"\.", maxsplit = 2) == @["1", "2.3"]``
   ##
-  ## ``start`` behaves the same as in ```find(...)`` <#proc-find>`__.
+  ## ``start`` behaves the same as in ```find(...)`` <#proc-find>`_.
   result = @[]
   var lastIdx = start
   var splits = 0
@@ -625,7 +625,7 @@ proc split*(str: string, pattern: Regex, maxSplit = -1, start = 0): seq[string]
     result.add(str.substr(bounds.b + 1, str.high))
 
 template replaceImpl(str: string, pattern: Regex,
-                     replacement: expr): stmt {.immediate, dirty.} =
+                     replacement: untyped) {.dirty.} =
   # XXX seems very similar to split, maybe I can reduce code duplication
   # somehow?
   result = ""
diff --git a/lib/impure/rdstdin.nim b/lib/impure/rdstdin.nim
index 469bb69c5..f722a6b39 100644
--- a/lib/impure/rdstdin.nim
+++ b/lib/impure/rdstdin.nim
@@ -105,7 +105,8 @@ else:
   proc readLineFromStdin*(prompt: string): TaintedString {.
                           tags: [ReadIOEffect, WriteIOEffect].} =
     var buffer = linenoise.readLine(prompt)
-    if isNil(buffer): quit(0)
+    if isNil(buffer):
+      raise newException(IOError, "Linenoise returned nil")
     result = TaintedString($buffer)
     if result.string.len > 0:
       historyAdd(buffer)
@@ -114,12 +115,12 @@ else:
   proc readLineFromStdin*(prompt: string, line: var TaintedString): bool {.
                           tags: [ReadIOEffect, WriteIOEffect].} =
     var buffer = linenoise.readLine(prompt)
-    if isNil(buffer): quit(0)
+    if isNil(buffer):
+      raise newException(IOError, "Linenoise returned nil")
     line = TaintedString($buffer)
     if line.string.len > 0:
       historyAdd(buffer)
     linenoise.free(buffer)
-    # XXX how to determine CTRL+D?
     result = true
 
   proc readPasswordFromStdin*(prompt: string, password: var TaintedString):
diff --git a/lib/impure/re.nim b/lib/impure/re.nim
index 60bb6c77f..bf397550a 100644
--- a/lib/impure/re.nim
+++ b/lib/impure/re.nim
@@ -7,8 +7,11 @@
 #    distribution, for details about the copyright.
 #
 
-## Regular expression support for Nim. Deprecated. Consider using the ``nre``
-## or ``pegs`` modules instead.
+## Regular expression support for Nim. This module still has some
+## obscure bugs and limitations,
+## consider using the ``nre`` or ``pegs`` modules instead.
+## We had to de-deprecate this module since too much code relies on it
+## and many people prefer its API over ``nre``'s.
 ##
 ## **Note:** The 're' proc defaults to the **extended regular expression
 ## syntax** which lets you use whitespace freely to make your regexes readable.
@@ -22,14 +25,12 @@
 ## though.
 ## PRCE's licence follows:
 ##
-## .. include:: ../doc/regexprs.txt
+## .. include:: ../../doc/regexprs.txt
 ##
 
 import
   pcre, strutils, rtarrays
 
-{.deprecated.}
-
 const
   MaxSubpatterns* = 20
     ## defines the maximum number of subpatterns that can be captured.
@@ -78,7 +79,7 @@ proc finalizeRegEx(x: Regex) =
   if not isNil(x.e):
     pcre.free_substring(cast[cstring](x.e))
 
-proc re*(s: string, flags = {reExtended, reStudy}): Regex {.deprecated.} =
+proc re*(s: string, flags = {reExtended, reStudy}): Regex =
   ## Constructor of regular expressions. Note that Nim's
   ## extended raw string literals support this syntax ``re"[abc]"`` as
   ## a short form for ``re(r"[abc]")``.
diff --git a/lib/impure/ssl.nim b/lib/impure/ssl.nim
index 721e5ce51..e3312d792 100644
--- a/lib/impure/ssl.nim
+++ b/lib/impure/ssl.nim
@@ -9,6 +9,9 @@
 
 ## This module provides an easy to use sockets-style
 ## nim interface to the OpenSSL library.
+##
+## **Warning:** This module is deprecated, use the SSL procedures defined in
+## the ``net`` module instead.
 
 {.deprecated.}