|
12 | 12 | ##
|
13 | 13 | ## To unpack raw bytes look at the `streams <streams.html>`_ module.
|
14 | 14 | ##
|
15 |
| -## |
16 |
| -## .. code-block:: |
17 |
| -## import parseutils |
| 15 | +## .. code-block:: nim |
| 16 | +## :test: |
18 | 17 | ##
|
19 | 18 | ## let logs = @["2019-01-10: OK_", "2019-01-11: FAIL_", "2019-01: aaaa"]
|
| 19 | +## var outp: seq[string] |
20 | 20 | ##
|
21 | 21 | ## for log in logs:
|
22 | 22 | ## var res: string
|
23 | 23 | ## if parseUntil(log, res, ':') == 10: # YYYY-MM-DD == 10
|
24 |
| -## echo res & " - " & captureBetween(log, ' ', '_') |
25 |
| -## # => 2019-01-10 - OK |
26 |
| -## |
| 24 | +## outp.add(res & " - " & captureBetween(log, ' ', '_')) |
| 25 | +## doAssert outp == @["2019-01-10 - OK", "2019-01-11 - FAIL"] |
27 | 26 | ##
|
28 |
| -## .. code-block:: |
29 |
| -## import parseutils |
| 27 | +## .. code-block:: nim |
| 28 | +## :test: |
30 | 29 | ## from strutils import Digits, parseInt
|
31 | 30 | ##
|
32 |
| -## let input1 = "2019 school start" |
33 |
| -## let input2 = "3 years back" |
34 |
| -## |
35 |
| -## let startYear = input1[0..skipWhile(input1, Digits)-1] # 2019 |
36 |
| -## let yearsBack = input2[0..skipWhile(input2, Digits)-1] # 3 |
37 |
| -## |
38 |
| -## echo "Examination is in " & $(parseInt(startYear) + parseInt(yearsBack)) |
39 |
| -## # => Examination is in 2022 |
40 |
| -## |
| 31 | +## let |
| 32 | +## input1 = "2019 school start" |
| 33 | +## input2 = "3 years back" |
| 34 | +## startYear = input1[0 .. skipWhile(input1, Digits)-1] # 2019 |
| 35 | +## yearsBack = input2[0 .. skipWhile(input2, Digits)-1] # 3 |
| 36 | +## examYear = parseInt(startYear) + parseInt(yearsBack) |
| 37 | +## doAssert "Examination is in " & $examYear == "Examination is in 2022" |
41 | 38 | ##
|
42 | 39 | ## **See also:**
|
43 | 40 | ## * `strutils module<strutils.html>`_ for combined and identical parsing proc's
|
@@ -586,21 +583,17 @@ iterator interpolatedFragments*(s: string): tuple[kind: InterpolatedKind,
|
586 | 583 | value: string] =
|
587 | 584 | ## Tokenizes the string `s` into substrings for interpolation purposes.
|
588 | 585 | ##
|
589 |
| - ## Example: |
590 |
| - ## |
591 |
| - ## .. code-block:: nim |
592 |
| - ## for k, v in interpolatedFragments(" $this is ${an example} $$"): |
593 |
| - ## echo "(", k, ", \"", v, "\")" |
594 |
| - ## |
595 |
| - ## Results in: |
596 |
| - ## |
597 |
| - ## .. code-block:: nim |
598 |
| - ## (ikString, " ") |
599 |
| - ## (ikExpr, "this") |
600 |
| - ## (ikString, " is ") |
601 |
| - ## (ikExpr, "an example") |
602 |
| - ## (ikString, " ") |
603 |
| - ## (ikDollar, "$") |
| 586 | + runnableExamples: |
| 587 | + var outp: seq[tuple[kind: InterpolatedKind, value: string]] |
| 588 | + for k, v in interpolatedFragments(" $this is ${an example} $$"): |
| 589 | + outp.add (k, v) |
| 590 | + doAssert outp == @[(ikStr, " "), |
| 591 | + (ikVar, "this"), |
| 592 | + (ikStr, " is "), |
| 593 | + (ikExpr, "an example"), |
| 594 | + (ikStr, " "), |
| 595 | + (ikDollar, "$")] |
| 596 | + |
604 | 597 | var i = 0
|
605 | 598 | var kind: InterpolatedKind
|
606 | 599 | while true:
|
|
0 commit comments