-
Notifications
You must be signed in to change notification settings - Fork 183
/
utils.jl
104 lines (92 loc) · 3.04 KB
/
utils.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""
escapeHTML(i::String)
Returns a string with special HTML characters escaped: &, <, >, ", '
"""
function escapeHTML(i::String)
# Refer to http://stackoverflow.com/a/7382028/3822752 for spec. links
o = replace(i, "&", "&")
o = replace(o, "\"", """)
o = replace(o, "'", "'")
o = replace(o, "<", "<")
o = replace(o, ">", ">")
return o
end
"""
@timeout secs expr then pollint
Start executing `expr`; if it doesn't finish executing in `secs` seconds,
then execute `then`. `pollint` controls the amount of time to wait in between
checking if `expr` has finished executing (short for polling interval).
"""
macro timeout(t, expr, then, pollint=0.01)
return quote
tm = Float64($(esc(t)))
start = time()
tsk = @async $(esc(expr))
while !istaskdone(tsk) && (time() - start < tm)
sleep($pollint)
end
istaskdone(tsk) || $(esc(then))
tsk.result
end
end
"""
@debug DEBUG expr
@debug DEBUG "message"
A macro to aid when needing to turn on extremely verbose output for debugging.
Set `const DEBUG = true` in HTTP.jl and re-compile the package to see
debug-level output from the package. When `DEBUG = false`, all `@debug` statements
compile to `nothing`.
"""
macro debug(should, line, expr)
if eval(should)
if typeof(expr) == String
return esc(:(println("[DEBUG - ", @__FILE__, ":", $line, "]: ", $(escape_string(expr)))))
else
return esc(:(println("[DEBUG - ", @__FILE__, ":", $line, "]: ", $(sprint(Base.show_unquoted, expr)), " = ", escape_string(string($expr)))))
end
end
end
macro log(verbose, io, stmt)
# "[HTTP]: Connecting to remote host..."
return esc(:($verbose && println($io, "[HTTP]: $($stmt)")))
end
# parsing utils
macro anyeq(var, vals...)
ret = e = Expr(:||)
for (i, v) in enumerate(vals)
x = :($var == $v)
push!(e.args, x)
i >= length(vals) - 1 && continue
ne = Expr(:||)
push!(e.args, ne)
e = ne
end
return esc(ret)
end
@inline lower(c) = Char(UInt32(c) | 0x20)
@inline isurlchar(c) = c > '\u80' ? true : normal_url_char[Int(c) + 1]
@inline ismark(c) = @anyeq(c, '-', '_', '.', '!', '~', '*', '\'', '(', ')')
@inline isalpha(c) = 'a' <= lower(c) <= 'z'
@inline isnum(c) = '0' <= c <= '9'
@inline isalphanum(c) = isalpha(c) || isnum(c)
@inline isuserinfochar(c) = isalphanum(c) || ismark(c) || @anyeq(c, '%', ';', ':', '&', '=', '+', '$', ',')
@inline ishex(c) = isnum(c) || ('a' <= lower(c) <= 'f')
@inline ishostchar(c) = isalphanum(c) || @anyeq(c, '.', '-', '_', '~')
@inline isheaderchar(c) = c == CR || c == LF || c == Char(9) || (c > Char(31) && c != Char(127))
macro shifted(meth, i, char)
return esc(:(Int($meth) << Int(16) | Int($i) << Int(8) | Int($char)))
end
macro errorif(cond, err)
return esc(quote
$cond && @err($err)
end)
end
macro err(e)
return esc(quote
errno = $e
@goto error
end)
end
macro strictcheck(cond)
return esc(:(strict && @errorif($cond, HPE_STRICT)))
end