feat: add Std::Fmt — string formatting module
- C runtime: bux_float_to_string() for float64 → String conversion
- String.bux: add String_FromFloat, extern decl for bux_float_to_string
- library/std/Fmt.bux: Fmt_Fmt1, Fmt_Fmt2, Fmt_Fmt3, Fmt_FmtInt, Fmt_FmtInt2,
Fmt_FmtFloat, Fmt_FmtBool — positional {0}, {1}, {2} placeholders
- Refactor examples to use Fmt:
factorial.bux, fibonacci.bux, process.bux, os_time.bux
This commit is contained in:
@@ -430,6 +430,12 @@ char* bux_str_join2(const char* a, const char* b, const char* sep) {
|
||||
|
||||
/* Simple string format: replace {0}, {1}, ... with string arguments.
|
||||
Returns formatted string. Supports up to 8 arguments. */
|
||||
char* bux_float_to_string(double f) {
|
||||
char* buf = (char*)bux_alloc(64);
|
||||
snprintf(buf, 64, "%g", f);
|
||||
return buf;
|
||||
}
|
||||
|
||||
char* bux_str_format(const char* pattern,
|
||||
const char* a0, const char* a1, const char* a2, const char* a3,
|
||||
const char* a4, const char* a5, const char* a6, const char* a7) {
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
module Std::Fmt {
|
||||
/* String formatting with positional placeholders {0}, {1}, {2}...
|
||||
*
|
||||
* Usage:
|
||||
* Fmt_Fmt1("Hello, {0}!", "World")
|
||||
* Fmt_FmtInt("Count: {0}", 42)
|
||||
* Fmt_FmtFloat("Pi: {0}", 3.14159)
|
||||
* Fmt_FmtBool("Active: {0}", true)
|
||||
* Fmt_Fmt2("Name: {0}, Age: {1}", name, String_FromInt(age))
|
||||
*/
|
||||
|
||||
func Fmt_Fmt1(pattern: String, a: String) -> String {
|
||||
return String_Format1(pattern, a);
|
||||
}
|
||||
|
||||
func Fmt_Fmt2(pattern: String, a: String, b: String) -> String {
|
||||
return String_Format2(pattern, a, b);
|
||||
}
|
||||
|
||||
func Fmt_Fmt3(pattern: String, a: String, b: String, c: String) -> String {
|
||||
return String_Format3(pattern, a, b, c);
|
||||
}
|
||||
|
||||
func Fmt_FmtInt(pattern: String, n: int64) -> String {
|
||||
return String_Format1(pattern, String_FromInt(n));
|
||||
}
|
||||
|
||||
func Fmt_FmtInt2(pattern: String, n1: int64, n2: int64) -> String {
|
||||
return String_Format2(pattern, String_FromInt(n1), String_FromInt(n2));
|
||||
}
|
||||
|
||||
func Fmt_FmtFloat(pattern: String, f: float64) -> String {
|
||||
return String_Format1(pattern, String_FromFloat(f));
|
||||
}
|
||||
|
||||
func Fmt_FmtBool(pattern: String, b: bool) -> String {
|
||||
if b {
|
||||
return String_Format1(pattern, "true");
|
||||
}
|
||||
return String_Format1(pattern, "false");
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@ extern func bux_sb_free(sb: *void);
|
||||
extern func bux_str_split_count(s: String, delim: String) -> uint;
|
||||
extern func bux_str_split_part(s: String, delim: String, index: uint) -> String;
|
||||
extern func bux_str_join2(a: String, b: String, sep: String) -> String;
|
||||
extern func bux_float_to_string(f: float64) -> String;
|
||||
extern func bux_str_format(pattern: String, a0: String, a1: String, a2: String, a3: String, a4: String, a5: String, a6: String, a7: String) -> String;
|
||||
|
||||
|
||||
@@ -178,6 +179,10 @@ func String_Replace(s: String, old: String, new: String) -> String {
|
||||
return result;
|
||||
}
|
||||
|
||||
func String_FromFloat(f: float64) -> String {
|
||||
return bux_float_to_string(f);
|
||||
}
|
||||
|
||||
func String_Format1(pattern: String, a0: String) -> String {
|
||||
return bux_str_format(pattern, a0, "", "", "", "", "", "", "");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user