core: make from-string better

This commit is contained in:
hellerve 2020-05-12 18:02:30 +02:00
parent aae743fb35
commit c569666a0a
11 changed files with 115 additions and 50 deletions

View File

@ -199,7 +199,15 @@
(implements str Int.str) (implements str Int.str)
(register format (Fn [&String Int] String)) (register format (Fn [&String Int] String))
(implements format Int.format) (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) (implements from-string Int.from-string)
) )
@ -208,7 +216,15 @@
(implements str Byte.str) (implements str Byte.str)
(register format (Fn [&String Byte] String)) (register format (Fn [&String Byte] String))
(implements format Byte.format) (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) (implements from-string Byte.from-string)
) )
@ -217,7 +233,15 @@
(implements str Float.str) (implements str Float.str)
(register format (Fn [&String Float] String)) (register format (Fn [&String Float] String))
(implements format Float.format) (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) (implements from-string Float.from-string)
) )
@ -226,7 +250,15 @@
(implements str Long.str) (implements str Long.str)
(register format (Fn [&String Long] String)) (register format (Fn [&String Long] String))
(implements format Long.format) (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) (implements from-string Long.from-string)
) )
@ -235,7 +267,15 @@
(implements str Double.str) (implements str Double.str)
(register format (Fn [&String Double] String)) (register format (Fn [&String Double] String))
(implements format Double.format) (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) (implements from-string Double.from-string)
) )

View File

@ -87,7 +87,9 @@ String Pattern_internal_classend(PatternMatchState *ms, String p) {
} while (*p != ']'); } while (*p != ']');
return p + 1; return p + 1;
} }
default: { return p; } default: {
return p;
}
} }
} }

View File

@ -228,8 +228,10 @@ String Double_format(const String *s, double x) {
return buffer; return buffer;
} }
double Double_from_MINUS_string(const String *s) { bool Double_from_MINUS_string_MINUS_internal(const String *s, double *target) {
return strtod(*s, NULL); char *err;
*target = strtod(*s, &err);
return *err == 0;
} }
String Float_str(float x) { String Float_str(float x) {
@ -246,8 +248,10 @@ String Float_format(const String *str, float x) {
return buffer; return buffer;
} }
float Float_from_MINUS_string(const String *s) { bool Float_from_MINUS_string_MINUS_internal(const String *s, float *target) {
return strtof(*s, NULL); char *err;
*target = strtof(*s, &err);
return *err == 0;
} }
String Int_str(int x) { String Int_str(int x) {
@ -264,8 +268,10 @@ String Int_format(const String *str, int x) {
return buffer; return buffer;
} }
int Int_from_MINUS_string(const String *s) { bool Int_from_MINUS_string_MINUS_internal(const String *s, int *target) {
return atoi(*s); char *err;
*target = (int)strtol(*s, &err, 10);
return *err == 0;
} }
String Long_str(Long x) { String Long_str(Long x) {
@ -282,8 +288,10 @@ String Long_format(const String *str, Long x) {
return buffer; return buffer;
} }
Long Long_from_MINUS_string(const String *s) { bool Long_from_MINUS_string_MINUS_internal(const String *s, Long *target) {
return atol(*s); char *err;
*target = strtol(*s, &err, 10);
return *err == 0;
} }
String Byte_str(uint8_t x) { String Byte_str(uint8_t x) {
@ -300,8 +308,10 @@ String Byte_format(const String *str, uint8_t x) {
return buffer; return buffer;
} }
uint8_t Byte_from_MINUS_string(const String *s) { uint8_t Byte_from_MINUS_string_MINUS_internal(const String *s, byte *target) {
return atoi(*s); 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) { int String_index_MINUS_of_MINUS_from(const String *s, char c, int i) {

View File

@ -543,14 +543,14 @@
</h3> </h3>
</a> </a>
<div class="description"> <div class="description">
external defn
</div> </div>
<p class="sig"> <p class="sig">
(Fn [(Ref String a)] Byte) (Fn [(Ref String a)] (Maybe Byte))
</p> </p>
<span> <pre class="args">
(from-string s)
</span> </pre>
<p class="doc"> <p class="doc">
</p> </p>

View File

@ -697,14 +697,14 @@
</h3> </h3>
</a> </a>
<div class="description"> <div class="description">
external defn
</div> </div>
<p class="sig"> <p class="sig">
(Fn [(Ref String a)] Double) (Fn [(Ref String a)] (Maybe Double))
</p> </p>
<span> <pre class="args">
(from-string s)
</span> </pre>
<p class="doc"> <p class="doc">
</p> </p>

View File

@ -659,14 +659,14 @@
</h3> </h3>
</a> </a>
<div class="description"> <div class="description">
external defn
</div> </div>
<p class="sig"> <p class="sig">
(Fn [(Ref String a)] Float) (Fn [(Ref String a)] (Maybe Float))
</p> </p>
<span> <pre class="args">
(from-string s)
</span> </pre>
<p class="doc"> <p class="doc">
</p> </p>

View File

@ -602,14 +602,14 @@
</h3> </h3>
</a> </a>
<div class="description"> <div class="description">
external defn
</div> </div>
<p class="sig"> <p class="sig">
(Fn [(Ref String a)] Int) (Fn [(Ref String a)] (Maybe Int))
</p> </p>
<span> <pre class="args">
(from-string s)
</span> </pre>
<p class="doc"> <p class="doc">
</p> </p>

View File

@ -562,14 +562,14 @@
</h3> </h3>
</a> </a>
<div class="description"> <div class="description">
external defn
</div> </div>
<p class="sig"> <p class="sig">
(Fn [(Ref String a)] Long) (Fn [(Ref String a)] (Maybe Long))
</p> </p>
<span> <pre class="args">
(from-string s)
</span> </pre>
<p class="doc"> <p class="doc">
</p> </p>

View File

@ -46,6 +46,9 @@
guessed-num (from-string &user-input)] guessed-num (from-string &user-input)]
(if (= &user-input "q\n") (if (= &user-input "q\n")
(exit!) (exit!)
(cond (< guessed-num answer) (guess-again "low") (match guessed-num
(> guessed-num answer) (guess-again "high") (Maybe.Nothing) (print "Invalid input.\nPlease guess again: ")
(correct!))))))) (Maybe.Just n)
(cond (< n answer) (guess-again "low")
(> n answer) (guess-again "high")
(correct!))))))))

View File

@ -103,8 +103,13 @@
"to-bytes works as expected II" "to-bytes works as expected II"
) )
(assert-equal test (assert-equal test
10.3 &(Maybe.Just 10.3)
(from-string "10.3") &(from-string "10.3")
"from-string works as expected" "from-string works as expected I"
)
(assert-equal test
&(Maybe.Nothing)
&(Double.from-string "abcd")
"from-string works as expected II"
) )
) )

View File

@ -109,8 +109,13 @@
"to-bytes works as expected II" "to-bytes works as expected II"
) )
(assert-equal test (assert-equal test
10.3f &(Maybe.Just 10.3f)
(from-string "10.3") &(from-string "10.3")
"from-string works as expected" "from-string works as expected I"
)
(assert-equal test
&(Maybe.Nothing)
&(Float.from-string "abcd")
"from-string works as expected II"
) )
) )