Merge pull request #273 from gradha/pr_misc_documentation_improvements

Misc documentation improvements
This commit is contained in:
Araq 2012-12-12 11:27:12 -08:00
commit 77492158ac
4 changed files with 45 additions and 5 deletions

View file

@ -17,7 +17,8 @@
## Currently only requesting URLs is implemented. The implementation depends ## Currently only requesting URLs is implemented. The implementation depends
## on the libcurl library! ## on the libcurl library!
## ##
## **Deprecated since version 0.8.8:** Use the ``httpclient`` module instead. ## **Deprecated since version 0.8.8:** Use the
## `httpclient <httpclient.html>`_ module instead.
## ##
{.deprecated.} {.deprecated.}

View file

@ -13,6 +13,23 @@
## (unlike XML). It is easy for machines to parse and generate. ## (unlike XML). It is easy for machines to parse and generate.
## JSON is based on a subset of the JavaScript Programming Language, ## JSON is based on a subset of the JavaScript Programming Language,
## Standard ECMA-262 3rd Edition - December 1999. ## 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 import
hashes, strutils, lexbase, streams, unicode hashes, strutils, lexbase, streams, unicode
@ -607,14 +624,14 @@ proc len*(n: PJsonNode): int =
else: nil else: nil
proc `[]`*(node: PJsonNode, name: String): PJsonNode = 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) assert(node.kind == JObject)
for key, item in items(node.fields): for key, item in items(node.fields):
if key == name: if key == name:
return item return item
return nil return nil
proc `[]`*(node: PJsonNode, index: Int): PJsonNode = proc `[]`*(node: PJsonNode, index: Int): PJsonNode {.raises: [EInvalidIndex].} =
## Gets the node at `index` in an Array. ## Gets the node at `index` in an Array.
assert(node.kind == JArray) assert(node.kind == JArray)
return node.elems[index] return node.elems[index]
@ -893,6 +910,10 @@ when isMainModule:
echo(parsed["keyÄÖöoßß"]) echo(parsed["keyÄÖöoßß"])
echo() echo()
echo(pretty(parsed2)) echo(pretty(parsed2))
try:
echo(parsed["key2"][12123])
raise newException(EInvalidValue, "That line was expected to fail")
except EInvalidIndex: echo()
discard """ discard """
while true: while true:

View file

@ -263,7 +263,8 @@ proc newFileStream*(f: TFile): PFileStream =
proc newFileStream*(filename: string, mode: TFileMode): PFileStream = proc newFileStream*(filename: string, mode: TFileMode): PFileStream =
## creates a new stream from the file named `filename` with the mode `mode`. ## 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
## <system.html>`_ module for a list of available TFileMode enums.
var f: TFile var f: TFile
if Open(f, filename, mode): result = newFileStream(f) if Open(f, filename, mode): result = newFileStream(f)

View file

@ -186,7 +186,24 @@ iterator split*(s: string, seps: set[char] = Whitespace): string =
## for word in split(";;this;is;an;;example;;;", {';'}): ## for word in split(";;this;is;an;;example;;;", {';'}):
## writeln(stdout, word) ## 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 var last = 0
assert(not ('\0' in seps)) assert(not ('\0' in seps))
while last < len(s): while last < len(s):