summary refs log tree commit diff stats
path: root/tests/js/t21247.nim
blob: 5b38787b305fe10892c3a66bde1fce7f0adab3b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import std/typetraits

type
  QueryParams* = distinct seq[(string, string)]

converter toBase*(params: var QueryParams): var seq[(string, string)] =
  params.distinctBase

proc foo(): QueryParams =
  # Issue was that the implicit converter call didn't say that it took the
  # address of the parameter it was converting. This led to the parameter not being
  # passed as a fat pointer which toBase expected
  result.add(("hello", "world"))

assert foo().distinctBase() == @[("hello", "world")]
pan class="w"> strutils proc validEmailAddress*(s: string): bool {.noSideEffect, rtl, extern: "nsuValidEmailAddress".} = ## returns true if `s` seems to be a valid e-mail address. ## The checking also uses a domain list. const chars = Letters + Digits + {'!','#','$','%','&', '\'','*','+','/','=','?','^','_','`','{','}','|','~','-','.'} var i = 0 if i >= s.len or s[i] notin chars or s[i] == '.': return false while i < s.len and s[i] in chars: if i+1 < s.len and s[i] == '.' and s[i+1] == '.': return false inc(i) if i >= s.len or s[i] != '@': return false var j = len(s)-1 if j >= 0 and s[j] notin Letters: return false while j >= i and s[j] in Letters: dec(j) inc(i) # skip '@' while i < s.len and s[i] in {'0'..'9', 'a'..'z', '-', '.'}: inc(i) if i != s.len: return false var x = substr(s, j+1) if len(x) == 2 and x[0] in Letters and x[1] in Letters: return true case toLowerAscii(x) of "com", "org", "net", "gov", "mil", "biz", "info", "mobi", "name", "aero", "jobs", "museum": return true else: return false proc parseInt*(s: string, value: var int, validRange: HSlice[int, 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: discard parseutils.parseInt(s, x, 0) except OverflowError: discard if x in validRange: value = x when isMainModule: doAssert "wuseldusel@codehome.com".validEmailAddress {.pop.}