summary refs log tree commit diff stats
path: root/lib/pure/matchers.nim
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pure/matchers.nim')
-rw-r--r--lib/pure/matchers.nim15
1 files changed, 14 insertions, 1 deletions
diff --git a/lib/pure/matchers.nim b/lib/pure/matchers.nim
index f37d4397d..0495f2c30 100644
--- a/lib/pure/matchers.nim
+++ b/lib/pure/matchers.nim
@@ -44,8 +44,21 @@ proc validEmailAddress*(s: string): bool {.noSideEffect,
      "aero", "jobs", "museum": return true
   return false
 
+proc parseInt*(s: string, value: var int, validRange: TSlice[int]) {.
+  noSideEffect, rtl, extern: "nmatchParseInt".} =
+  ## parses `s` into an integer in the range `validRange`. If successful,
+  ## `value` is modified to contain the result. Otherwise no exception is
+  ## raised and `value` is not touched; this way a reasonable default value
+  ## won't be overwritten.
+  var x = value
+  try:
+    x = parseInt(s)
+  except EOverflow:
+    nil
+  if x in validRange: value = x
+
 when isMainModule:
-  assert "wuseldusel@codehome.com".validEmailAddress
+  doAssert "wuseldusel@codehome.com".validEmailAddress
   
 {.pop.}