diff options
author | Dominik Picheta <dominikpicheta@googlemail.com> | 2017-08-01 19:19:05 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-01 19:19:05 +0100 |
commit | 28737e9a403655293d2d7ece716892df99e356a6 (patch) | |
tree | 173e8e921f755983c874ea0d179af7667fe2fa4c /lib/pure | |
parent | f3b3af5f3f75718c887792f4b30bd977945744cb (diff) | |
parent | ff835d56a3f3f33c7f2bfa57faa0c3d333896148 (diff) | |
download | Nim-28737e9a403655293d2d7ece716892df99e356a6.tar.gz |
Merge pull request #6168 from konqoro/patch-10
Docs: add one more example in strscans module
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/strscans.nim | 25 |
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 + ]## |