mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
format: added simple versions of format (work on issue #108)
This commit is contained in:
parent
38d7bcd32a
commit
eccddd00fe
@ -39,6 +39,7 @@
|
||||
(register mod (Fn [Double Double] Double))
|
||||
(register copy (Fn [(Ref Double)] Double))
|
||||
(register abs (Fn [Double] Double))
|
||||
(register format (Fn [String Double] String))
|
||||
|
||||
(defn clamp [min, max, val]
|
||||
(if (> val max)
|
||||
|
@ -11,6 +11,7 @@
|
||||
(register random-between (λ [Float Float] Float))
|
||||
(register str (Fn [Float] String))
|
||||
(register copy (Fn [(Ref Float)] Float))
|
||||
(register format (Fn [String Float] String))
|
||||
|
||||
(register = (Fn [Float Float] Bool))
|
||||
(defn /= [x y]
|
||||
|
@ -22,6 +22,7 @@
|
||||
(register inc (λ [Int] Int))
|
||||
(register dec (λ [Int] Int))
|
||||
(register copy (λ [&Int] Int)) ;; TODO: Should not be needed when refs to value types are auto-converted to non-refs.
|
||||
(register format (Fn [String Int] String))
|
||||
|
||||
(register safe-add (λ [Int Int (Ref Int)] Bool))
|
||||
(register safe-sub (λ [Int Int (Ref Int)] Bool))
|
||||
|
@ -1,5 +1,6 @@
|
||||
(definterface = (λ [a a] Bool))
|
||||
(definterface /= (λ [a a] Bool))
|
||||
(definterface format (λ [String a] String))
|
||||
|
||||
(definterface zero (λ [] a))
|
||||
(definterface add-ref (λ [&a &a] a))
|
||||
|
@ -23,6 +23,7 @@
|
||||
(register to-int (λ [Long] Int))
|
||||
(register from-int (λ [Int] Long))
|
||||
(register copy (λ [&Long] Long)) ;; TODO: Should not be needed when refs to value types are auto-converted to non-refs.
|
||||
(register format (Fn [String Long] String))
|
||||
|
||||
(defn /= [x y]
|
||||
(not (Long.= x y)))
|
||||
|
@ -12,6 +12,7 @@
|
||||
(register chars (Fn [&String] (Array Char)))
|
||||
(register from-chars (Fn [(Array Char)] String))
|
||||
(register tail (λ [(Ref String)] String))
|
||||
(register format (Fn [String String] String))
|
||||
|
||||
(defn /= [a b]
|
||||
(not (= (the (Ref String) a) b)))
|
||||
|
39
core/core.h
39
core/core.h
@ -186,6 +186,16 @@ string Int_str(int x) {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
string Int_format(string str, int x) {
|
||||
char *buffer = CARP_MALLOC(64);
|
||||
snprintf(buffer, 64, str, x);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
bool Int_mask(int a, int b) {
|
||||
return a & b;
|
||||
}
|
||||
|
||||
long Long_from_MINUS_string(string *s) {
|
||||
return atol(*s);
|
||||
}
|
||||
@ -213,6 +223,16 @@ string Long_str(long x) {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
string Long_format(string str, long x) {
|
||||
char *buffer = CARP_MALLOC(64);
|
||||
snprintf(buffer, 64, str, x);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
bool Long_mask(long a, long b) {
|
||||
return a & b;
|
||||
}
|
||||
|
||||
int Long_to_MINUS_int(long a) {
|
||||
return (int) a;
|
||||
}
|
||||
@ -275,6 +295,13 @@ char String_char_MINUS_at(string* s, int i) {
|
||||
return (*s)[i];
|
||||
}
|
||||
|
||||
string String_format(string str, string s) {
|
||||
int n = strlen(s);
|
||||
string buffer = CARP_MALLOC(n);
|
||||
snprintf(buffer, n, str, *s);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
Array String_chars(string *s) {
|
||||
Array chars;
|
||||
chars.len = strlen(*s);
|
||||
@ -441,6 +468,12 @@ double Double_random_MINUS_between(double lower, double upper) {
|
||||
return lower + diff * r;
|
||||
}
|
||||
|
||||
string Double_format(string s, double x) {
|
||||
char *buffer = CARP_MALLOC(32);
|
||||
snprintf(buffer, 32, s, x);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
int Float_to_MINUS_int(float x) {
|
||||
return (int)x;
|
||||
}
|
||||
@ -549,6 +582,12 @@ string Float_str(float x) {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
string Float_format(string str, float x) {
|
||||
char *buffer = CARP_MALLOC(32);
|
||||
snprintf(buffer, 32, str, x);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
// Bool
|
||||
bool Bool_copy(bool* b) {
|
||||
return *b;
|
||||
|
Loading…
Reference in New Issue
Block a user