2024-03-18 09:57:49 -07:00
|
|
|
// Boolean methods/type info
|
2024-04-10 10:33:40 -07:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
2024-02-04 18:13:50 -08:00
|
|
|
#include <gc.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
2024-09-13 17:08:20 -07:00
|
|
|
#include "bools.h"
|
2024-09-11 11:53:48 -07:00
|
|
|
#include "optionals.h"
|
2024-04-10 10:33:40 -07:00
|
|
|
#include "text.h"
|
|
|
|
#include "util.h"
|
2024-02-04 18:13:50 -08:00
|
|
|
|
2024-11-29 09:55:14 -08:00
|
|
|
PUREFUNC public Text_t Bool$as_text(const void *b, bool colorize, const TypeInfo_t*)
|
2024-02-04 18:13:50 -08:00
|
|
|
{
|
2024-09-03 12:00:28 -07:00
|
|
|
if (!b) return Text("Bool");
|
2024-02-04 18:13:50 -08:00
|
|
|
if (colorize)
|
2024-11-29 09:55:14 -08:00
|
|
|
return *(Bool_t*)b ? Text("\x1b[35myes\x1b[m") : Text("\x1b[35mno\x1b[m");
|
2024-02-04 18:13:50 -08:00
|
|
|
else
|
2024-11-29 09:55:14 -08:00
|
|
|
return *(Bool_t*)b ? Text("yes") : Text("no");
|
2024-02-04 18:13:50 -08:00
|
|
|
}
|
|
|
|
|
2024-11-09 12:11:11 -08:00
|
|
|
PUREFUNC public OptionalBool_t Bool$parse(Text_t text)
|
2024-04-10 10:33:40 -07:00
|
|
|
{
|
2025-03-07 13:19:12 -08:00
|
|
|
if (Text$equal_ignoring_case(text, Text("yes"), NONE_TEXT)
|
|
|
|
|| Text$equal_ignoring_case(text, Text("on"), NONE_TEXT)
|
|
|
|
|| Text$equal_ignoring_case(text, Text("true"), NONE_TEXT)
|
|
|
|
|| Text$equal_ignoring_case(text, Text("1"), NONE_TEXT)) {
|
2024-04-10 10:33:40 -07:00
|
|
|
return yes;
|
2025-03-07 13:19:12 -08:00
|
|
|
} else if (Text$equal_ignoring_case(text, Text("no"), NONE_TEXT)
|
|
|
|
|| Text$equal_ignoring_case(text, Text("off"), NONE_TEXT)
|
|
|
|
|| Text$equal_ignoring_case(text, Text("false"), NONE_TEXT)
|
|
|
|
|| Text$equal_ignoring_case(text, Text("0"), NONE_TEXT)) {
|
2024-04-10 10:33:40 -07:00
|
|
|
return no;
|
|
|
|
} else {
|
2024-11-24 13:18:21 -08:00
|
|
|
return NONE_BOOL;
|
2024-04-10 10:33:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-29 09:55:14 -08:00
|
|
|
static bool Bool$is_none(const void *b, const TypeInfo_t*)
|
|
|
|
{
|
|
|
|
return *(OptionalBool_t*)b == NONE_BOOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const metamethods_t Bool$metamethods = {
|
|
|
|
.as_text=Bool$as_text,
|
|
|
|
.is_none=Bool$is_none,
|
|
|
|
};
|
|
|
|
|
2024-09-30 11:39:30 -07:00
|
|
|
public const TypeInfo_t Bool$info = {
|
2024-02-27 10:39:12 -08:00
|
|
|
.size=sizeof(bool),
|
|
|
|
.align=__alignof__(bool),
|
2024-11-29 09:55:14 -08:00
|
|
|
.metamethods=Bool$metamethods,
|
2024-02-04 18:13:50 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0
|