diff options
Diffstat (limited to 'builtins/bool.c')
| -rw-r--r-- | builtins/bool.c | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/builtins/bool.c b/builtins/bool.c index cced8077..ff5d0ec4 100644 --- a/builtins/bool.c +++ b/builtins/bool.c @@ -1,16 +1,17 @@ // 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 <ctype.h> #include <sys/param.h> -#include <err.h> -#include "util.h" #include "bool.h" +#include "text.h" #include "types.h" +#include "util.h" public CORD Bool$as_text(const bool *b, bool colorize, const TypeInfo *type) { @@ -22,6 +23,21 @@ public CORD Bool$as_text(const bool *b, bool colorize, const TypeInfo *type) return *b ? "yes" : "no"; } +public Bool_t Bool$from_text(CORD text, bool *success) +{ + CORD lower = Text$lower(text); + if (CORD_cmp(lower, "yes") == 0 || CORD_cmp(lower, "on") == 0 || CORD_cmp(lower, "true") == 0) { + if (success) *success = yes; + return yes; + } else if (CORD_cmp(lower, "no") == 0 || CORD_cmp(lower, "off") == 0 || CORD_cmp(lower, "false") == 0) { + if (success) *success = yes; + return no; + } else { + if (success) *success = no; + return no; + } +} + public const TypeInfo $Bool = { .size=sizeof(bool), .align=__alignof__(bool), |
