diff --git a/core/String.carp b/core/String.carp index b5605d2e..c9b27c80 100644 --- a/core/String.carp +++ b/core/String.carp @@ -199,7 +199,15 @@ (implements str Int.str) (register format (Fn [&String Int] String)) (implements format Int.format) - (register from-string (λ [&String] Int)) + (private from-string-internal) + (hidden from-string-internal) + (register from-string-internal (λ [&String &Int] Bool)) + + (defn from-string [s] + (let [res 0] + (if (from-string-internal s &res) + (Maybe.Just res) + (Maybe.Nothing)))) (implements from-string Int.from-string) ) @@ -208,7 +216,15 @@ (implements str Byte.str) (register format (Fn [&String Byte] String)) (implements format Byte.format) - (register from-string (λ [&String] Byte)) + (private from-string-internal) + (hidden from-string-internal) + (register from-string-internal (λ [&String &Byte] Bool)) + + (defn from-string [s] + (let [res 0b] + (if (from-string-internal s &res) + (Maybe.Just res) + (Maybe.Nothing)))) (implements from-string Byte.from-string) ) @@ -217,7 +233,15 @@ (implements str Float.str) (register format (Fn [&String Float] String)) (implements format Float.format) - (register from-string (λ [&String] Float)) + (private from-string-internal) + (hidden from-string-internal) + (register from-string-internal (λ [&String &Float] Bool)) + + (defn from-string [s] + (let [res 0.0f] + (if (from-string-internal s &res) + (Maybe.Just res) + (Maybe.Nothing)))) (implements from-string Float.from-string) ) @@ -226,7 +250,15 @@ (implements str Long.str) (register format (Fn [&String Long] String)) (implements format Long.format) - (register from-string (λ [&String] Long)) + (private from-string-internal) + (hidden from-string-internal) + (register from-string-internal (λ [&String &Long] Bool)) + + (defn from-string [s] + (let [res 0l] + (if (from-string-internal s &res) + (Maybe.Just res) + (Maybe.Nothing)))) (implements from-string Long.from-string) ) @@ -235,7 +267,15 @@ (implements str Double.str) (register format (Fn [&String Double] String)) (implements format Double.format) - (register from-string (λ [&String] Double)) + (private from-string-internal) + (hidden from-string-internal) + (register from-string-internal (λ [&String &Double] Bool)) + + (defn from-string [s] + (let [res 0.0] + (if (from-string-internal s &res) + (Maybe.Just res) + (Maybe.Nothing)))) (implements from-string Double.from-string) ) diff --git a/core/carp_pattern.h b/core/carp_pattern.h index cc23f856..e1ba58fa 100644 --- a/core/carp_pattern.h +++ b/core/carp_pattern.h @@ -87,7 +87,9 @@ String Pattern_internal_classend(PatternMatchState *ms, String p) { } while (*p != ']'); return p + 1; } - default: { return p; } + default: { + return p; + } } } diff --git a/core/carp_string.h b/core/carp_string.h index 97d5761d..2bf5aefa 100644 --- a/core/carp_string.h +++ b/core/carp_string.h @@ -228,8 +228,10 @@ String Double_format(const String *s, double x) { return buffer; } -double Double_from_MINUS_string(const String *s) { - return strtod(*s, NULL); +bool Double_from_MINUS_string_MINUS_internal(const String *s, double *target) { + char *err; + *target = strtod(*s, &err); + return *err == 0; } String Float_str(float x) { @@ -246,8 +248,10 @@ String Float_format(const String *str, float x) { return buffer; } -float Float_from_MINUS_string(const String *s) { - return strtof(*s, NULL); +bool Float_from_MINUS_string_MINUS_internal(const String *s, float *target) { + char *err; + *target = strtof(*s, &err); + return *err == 0; } String Int_str(int x) { @@ -264,8 +268,10 @@ String Int_format(const String *str, int x) { return buffer; } -int Int_from_MINUS_string(const String *s) { - return atoi(*s); +bool Int_from_MINUS_string_MINUS_internal(const String *s, int *target) { + char *err; + *target = (int)strtol(*s, &err, 10); + return *err == 0; } String Long_str(Long x) { @@ -282,8 +288,10 @@ String Long_format(const String *str, Long x) { return buffer; } -Long Long_from_MINUS_string(const String *s) { - return atol(*s); +bool Long_from_MINUS_string_MINUS_internal(const String *s, Long *target) { + char *err; + *target = strtol(*s, &err, 10); + return *err == 0; } String Byte_str(uint8_t x) { @@ -300,8 +308,10 @@ String Byte_format(const String *str, uint8_t x) { return buffer; } -uint8_t Byte_from_MINUS_string(const String *s) { - return atoi(*s); +uint8_t Byte_from_MINUS_string_MINUS_internal(const String *s, byte *target) { + char *err; + *target = (uint8_t)strtol(*s, &err, 10); + return *err == 0; } int String_index_MINUS_of_MINUS_from(const String *s, char c, int i) { diff --git a/docs/core/Byte.html b/docs/core/Byte.html index 5931290a..03226c91 100644 --- a/docs/core/Byte.html +++ b/docs/core/Byte.html @@ -543,14 +543,14 @@
- external + defn

- (Fn [(Ref String a)] Byte) + (Fn [(Ref String a)] (Maybe Byte))

- - - +
+                    (from-string s)
+                

diff --git a/docs/core/Double.html b/docs/core/Double.html index dcc045db..d4147702 100644 --- a/docs/core/Double.html +++ b/docs/core/Double.html @@ -697,14 +697,14 @@
- external + defn

- (Fn [(Ref String a)] Double) + (Fn [(Ref String a)] (Maybe Double))

- - - +
+                    (from-string s)
+                

diff --git a/docs/core/Float.html b/docs/core/Float.html index 585c6fdb..f9ed41a8 100644 --- a/docs/core/Float.html +++ b/docs/core/Float.html @@ -659,14 +659,14 @@
- external + defn

- (Fn [(Ref String a)] Float) + (Fn [(Ref String a)] (Maybe Float))

- - - +
+                    (from-string s)
+                

diff --git a/docs/core/Int.html b/docs/core/Int.html index 81b76df2..b7add5d3 100644 --- a/docs/core/Int.html +++ b/docs/core/Int.html @@ -602,14 +602,14 @@
- external + defn

- (Fn [(Ref String a)] Int) + (Fn [(Ref String a)] (Maybe Int))

- - - +
+                    (from-string s)
+                

diff --git a/docs/core/Long.html b/docs/core/Long.html index e3b5e09a..d845f234 100644 --- a/docs/core/Long.html +++ b/docs/core/Long.html @@ -562,14 +562,14 @@
- external + defn

- (Fn [(Ref String a)] Long) + (Fn [(Ref String a)] (Maybe Long))

- - - +
+                    (from-string s)
+                

diff --git a/examples/guessing.carp b/examples/guessing.carp index ea428ae9..54f7d79f 100644 --- a/examples/guessing.carp +++ b/examples/guessing.carp @@ -46,6 +46,9 @@ guessed-num (from-string &user-input)] (if (= &user-input "q\n") (exit!) - (cond (< guessed-num answer) (guess-again "low") - (> guessed-num answer) (guess-again "high") - (correct!))))))) + (match guessed-num + (Maybe.Nothing) (print "Invalid input.\nPlease guess again: ") + (Maybe.Just n) + (cond (< n answer) (guess-again "low") + (> n answer) (guess-again "high") + (correct!)))))))) diff --git a/test/double_math.carp b/test/double_math.carp index 3cbb06c6..ec97fe29 100644 --- a/test/double_math.carp +++ b/test/double_math.carp @@ -103,8 +103,13 @@ "to-bytes works as expected II" ) (assert-equal test - 10.3 - (from-string "10.3") - "from-string works as expected" + &(Maybe.Just 10.3) + &(from-string "10.3") + "from-string works as expected I" + ) + (assert-equal test + &(Maybe.Nothing) + &(Double.from-string "abcd") + "from-string works as expected II" ) ) diff --git a/test/float_math.carp b/test/float_math.carp index 0f1658be..e2b154d4 100644 --- a/test/float_math.carp +++ b/test/float_math.carp @@ -109,8 +109,13 @@ "to-bytes works as expected II" ) (assert-equal test - 10.3f - (from-string "10.3") - "from-string works as expected" + &(Maybe.Just 10.3f) + &(from-string "10.3") + "from-string works as expected I" + ) + (assert-equal test + &(Maybe.Nothing) + &(Float.from-string "abcd") + "from-string works as expected II" ) )