# HG changeset patch # User Adam Chlipala # Date 1220804355 14400 # Node ID 711aad3869d1b6090536f7b886a9f031a04adcdb # Parent 6e665c7c96f6bc3c957db3e2543f517ffa1958f1 Error-parsing floats and bools diff -r 6e665c7c96f6 -r 711aad3869d1 include/urweb.h --- a/include/urweb.h Sun Sep 07 12:15:46 2008 -0400 +++ b/include/urweb.h Sun Sep 07 12:19:15 2008 -0400 @@ -82,3 +82,5 @@ lw_Basis_bool *lw_Basis_stringToBool(lw_context, lw_Basis_string); lw_Basis_int lw_Basis_stringToInt_error(lw_context, lw_Basis_string); +lw_Basis_float lw_Basis_stringToFloat_error(lw_context, lw_Basis_string); +lw_Basis_bool lw_Basis_stringToBool_error(lw_context, lw_Basis_string); diff -r 6e665c7c96f6 -r 711aad3869d1 src/c/urweb.c --- a/src/c/urweb.c Sun Sep 07 12:15:46 2008 -0400 +++ b/src/c/urweb.c Sun Sep 07 12:19:15 2008 -0400 @@ -799,3 +799,22 @@ else lw_error(ctx, FATAL, "Can't parse int: %s", s); } + +lw_Basis_float lw_Basis_stringToFloat_error(lw_context ctx, lw_Basis_string s) { + char *endptr; + lw_Basis_float n = strtod(s, &endptr); + + if (*s != '\0' && *endptr == '\0') + return n; + else + lw_error(ctx, FATAL, "Can't parse float: %s", s); +} + +lw_Basis_bool lw_Basis_stringToBool_error(lw_context ctx, lw_Basis_string s) { + if (!strcasecmp (s, "True")) + return lw_Basis_True; + else if (!strcasecmp (s, "False")) + return lw_Basis_False; + else + lw_error(ctx, FATAL, "Can't parse bool: %s", s); +} diff -r 6e665c7c96f6 -r 711aad3869d1 tests/fromStringErr.ur --- a/tests/fromStringErr.ur Sun Sep 07 12:15:46 2008 -0400 +++ b/tests/fromStringErr.ur Sun Sep 07 12:19:15 2008 -0400 @@ -1,3 +1,5 @@ fun main () : transaction page = return 3 = {cdata (show _ (readError _ "3" : int))}
+ 12.12 = {cdata (show _ (readError _ "12.12" : float))}
+ True = {cdata (show _ (readError _ "True" : bool))}