From 3ff2f7fbbc48b1de3f8e7031dfb271862a8878fa Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz Date: Tue, 11 Dec 2012 21:39:05 +0100 Subject: [PATCH 1/5] Adds to split() a code example using a set of separators. --- lib/pure/strutils.nim | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 0f11b4d896..8a5061037a 100755 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -186,7 +186,24 @@ iterator split*(s: string, seps: set[char] = Whitespace): string = ## for word in split(";;this;is;an;;example;;;", {';'}): ## writeln(stdout, word) ## - ## produces the same output. + ## produces the same output. The code: + ## + ## .. code-block:: nimrod + ## let date = "2012-11-20T22:08:08.398990" + ## let separators = {' ', '-', ':', 'T'} + ## for number in split(date, separators): + ## writeln(stdout, number) + ## + ## Results in: + ## + ## .. code-block:: nimrod + ## "2012" + ## "11" + ## "20" + ## "22" + ## "08" + ## "08.398990" + ## var last = 0 assert(not ('\0' in seps)) while last < len(s): From d9dab30c1499dec081f553b887905009cd8f3e1a Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz Date: Tue, 11 Dec 2012 21:51:03 +0100 Subject: [PATCH 2/5] Documents json [] accesors, raises explicit exception. --- lib/pure/json.nim | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/pure/json.nim b/lib/pure/json.nim index 941d88dfc2..e7945eac33 100755 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -607,14 +607,14 @@ proc len*(n: PJsonNode): int = else: nil proc `[]`*(node: PJsonNode, name: String): PJsonNode = - ## Gets a field from a `JObject`. + ## Gets a field from a `JObject`. Returns nil if the key is not found. assert(node.kind == JObject) for key, item in items(node.fields): if key == name: return item return nil -proc `[]`*(node: PJsonNode, index: Int): PJsonNode = +proc `[]`*(node: PJsonNode, index: Int): PJsonNode {.raises: [EInvalidIndex].} = ## Gets the node at `index` in an Array. assert(node.kind == JArray) return node.elems[index] @@ -893,6 +893,10 @@ when isMainModule: echo(parsed["keyÄÖöoßß"]) echo() echo(pretty(parsed2)) + try: + echo(parsed["key2"][12123]) + raise newException(EInvalidValue, "That line was expected to fail") + except EInvalidIndex: echo() discard """ while true: From d3eaddc961092d50bc02c2f5cdd3e940c876a3fc Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz Date: Tue, 11 Dec 2012 21:59:41 +0100 Subject: [PATCH 3/5] Transforms httpclient into hyperlink for ease of use. --- lib/impure/web.nim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/impure/web.nim b/lib/impure/web.nim index 417fe9746a..5f04422d1e 100755 --- a/lib/impure/web.nim +++ b/lib/impure/web.nim @@ -17,7 +17,8 @@ ## Currently only requesting URLs is implemented. The implementation depends ## on the libcurl library! ## -## **Deprecated since version 0.8.8:** Use the ``httpclient`` module instead. +## **Deprecated since version 0.8.8:** Use the +## `httpclient `_ module instead. ## {.deprecated.} From 545a7577e55597595a15c98a092f5e5da847c33e Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz Date: Tue, 11 Dec 2012 22:15:50 +0100 Subject: [PATCH 4/5] Adds json usage example. --- lib/pure/json.nim | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/pure/json.nim b/lib/pure/json.nim index e7945eac33..cee135f08f 100755 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -13,6 +13,23 @@ ## (unlike XML). It is easy for machines to parse and generate. ## JSON is based on a subset of the JavaScript Programming Language, ## Standard ECMA-262 3rd Edition - December 1999. +## +## Usage example: +## +## .. code-block:: nimrod +## let +## small_json = """{"test": 1.3, "key2": true}""" +## jobj = parseJson(small_json) +## assert (jobj.kind == JObject) +## echo($jobj["test"].fnum) +## echo($jobj["key2"].bval) +## +## Results in: +## +## .. code-block:: nimrod +## +## 1.3000000000000000e+00 +## true import hashes, strutils, lexbase, streams, unicode From afff026dbd0466cabbce17a1026f7927598cceea Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz Date: Tue, 11 Dec 2012 22:24:07 +0100 Subject: [PATCH 5/5] Adds hyperlink to system module to explain TFileMode type. --- lib/pure/streams.nim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/pure/streams.nim b/lib/pure/streams.nim index 5db21d76a2..212aab493d 100755 --- a/lib/pure/streams.nim +++ b/lib/pure/streams.nim @@ -263,7 +263,8 @@ proc newFileStream*(f: TFile): PFileStream = proc newFileStream*(filename: string, mode: TFileMode): PFileStream = ## creates a new stream from the file named `filename` with the mode `mode`. - ## If the file cannot be opened, nil is returned. + ## If the file cannot be opened, nil is returned. See the `system + ## `_ module for a list of available TFileMode enums. var f: TFile if Open(f, filename, mode): result = newFileStream(f)