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
|
// Boolean methods/type info
#include <ctype.h>
#include <err.h>
#include <gc.h>
#include <gc/cord.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/param.h>
#include "bool.h"
#include "text.h"
#include "types.h"
#include "util.h"
public Text_t Bool$as_text(const bool *b, bool colorize, const TypeInfo *type)
{
(void)type;
if (!b) return Text$from_str("Bool");
if (colorize)
return *b ? Text$from_str("\x1b[35myes\x1b[m") : Text$from_str("\x1b[35mno\x1b[m");
else
return *b ? Text$from_str("yes") : Text$from_str("no");
}
public Bool_t Bool$from_text(Text_t text, bool *success)
{
if (Text$equal_ignoring_case(text, Text$from_str("yes"))
|| Text$equal_ignoring_case(text, Text$from_str("on"))
|| Text$equal_ignoring_case(text, Text$from_str("true"))
|| Text$equal_ignoring_case(text, Text$from_str("1"))) {
if (success) *success = yes;
return yes;
} else if (Text$equal_ignoring_case(text, Text$from_str("no"))
|| Text$equal_ignoring_case(text, Text$from_str("off"))
|| Text$equal_ignoring_case(text, Text$from_str("false"))
|| Text$equal_ignoring_case(text, Text$from_str("0"))) {
if (success) *success = yes;
return no;
} else {
if (success) *success = no;
return no;
}
}
public Bool_t Bool$random(double p)
{
return (drand48() < p);
}
public const TypeInfo $Bool = {
.size=sizeof(bool),
.align=__alignof__(bool),
.tag=CustomInfo,
.CustomInfo={.as_text=(void*)Bool$as_text},
};
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0
|