summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authorkonqoro <capoiosct@gmail.com>2017-08-01 18:22:20 +0300
committerGitHub <noreply@github.com>2017-08-01 18:22:20 +0300
commitff835d56a3f3f33c7f2bfa57faa0c3d333896148 (patch)
treefa72e3ab3b49954e0f409389abd7165d3ac680ca /lib/pure
parent3d543b1539f8d5dde5746c90c5de5fa3df1cadfd (diff)
downloadNim-ff835d56a3f3f33c7f2bfa57faa0c3d333896148.tar.gz
Docs: add one more example in strscans module
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/strscans.nim25
1 files changed, 24 insertions, 1 deletions
diff --git a/lib/pure/strscans.nim b/lib/pure/strscans.nim
index 83b86fd54..bf26d2e59 100644
--- a/lib/pure/strscans.nim
+++ b/lib/pure/strscans.nim
@@ -187,7 +187,6 @@ overloaded to handle both single characters and sets of character.
   if scanp(content, idx, +( ~{'\L', '\0'} -> entry.add(peekChar($input))), '\L'):
     result.add entry
 
-
 Calling ordinary Nim procs inside the macro is possible:
 
 .. code-block:: nim
@@ -253,6 +252,30 @@ is performed.
 
   for r in collectLinks(body):
     echo r
+
+In this example both macros are combined seamlessly in order to maximise 
+efficiency and perform different checks.
+
+.. code-block:: nim
+
+  iterator parseIps*(soup: string): string =
+    ## ipv4 only!
+    const digits = {'0'..'9'}
+    var a, b, c, d: int
+    var buf = ""
+    var idx = 0
+    while idx < soup.len:
+      if scanp(soup, idx, (`digits`{1,3}, '.', `digits`{1,3}, '.',
+               `digits`{1,3}, '.', `digits`{1,3}) -> buf.add($_)):
+        discard buf.scanf("$i.$i.$i.$i", a, b, c, d)
+        if (a >= 0 and a <= 254) and
+           (b >= 0 and b <= 254) and
+           (c >= 0 and c <= 254) and
+           (d >= 0 and d <= 254):
+          yield buf
+      buf.setLen(0) # need to clear `buf` each time, cause it might contain garbage
+      idx.inc
+
 ]##