1if not modules then modules = { } end modules ['l-boolean'] = {
2 version = 1.001,
3 comment = "companion to luat-lib.mkiv",
4 author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5 copyright = "PRAGMA ADE / ConTeXt Development Team",
6 license = "see context related readme files"
7}
8
9local type, tonumber = type, tonumber
10
11boolean = boolean or { }
12local boolean = boolean
13
14function boolean.tonumber(b)
15 if b then return 1 else return 0 end
16end
17
18function toboolean(str,tolerant)
19 if str == nil then
20 return false
21 elseif str == false then
22 return false
23 elseif str == true then
24 return true
25 elseif str == "true" then
26 return true
27 elseif str == "false" then
28 return false
29 elseif not tolerant then
30 return false
31 elseif str == 0 then
32 return false
33 elseif (tonumber(str) or 0) > 0 then
34 return true
35 else
36 return str == "yes" or str == "on" or str == "t"
37 end
38end
39
40string.toboolean = toboolean
41
42function string.booleanstring(str)
43 if str == "0" then
44 return false
45 elseif str == "1" then
46 return true
47 elseif str == "" then
48 return false
49 elseif str == "false" then
50 return false
51 elseif str == "true" then
52 return true
53 elseif (tonumber(str) or 0) > 0 then
54 return true
55 else
56 return str == "yes" or str == "on" or str == "t"
57 end
58end
59
60function string.is_boolean(str,default,strict)
61 if type(str) == "string" then
62 if str == "true" or str == "yes" or str == "on" or str == "t" or (not strict and str == "1") then
63 return true
64 elseif str == "false" or str == "no" or str == "off" or str == "f" or (not strict and str == "0") then
65 return false
66 end
67 end
68 return default
69end
70 |