feat(stdlib): add String_Chars, String_FromBool, fix Fmt and Net

- String.bux: add String_Chars(s, index) for char-at-index access
- String.bux: add String_FromBool(b) conversion
- Fmt.bux: rewrite with correct stdlib imports (was using non-existent names)
- Net.bux: fix String_Len -> bux_strlen (String_Len not imported)
- All lib modules compile successfully
This commit is contained in:
2026-06-07 18:05:04 +03:00
parent bfe87a8acb
commit 04be9eb400
3 changed files with 104 additions and 36 deletions
+93 -35
View File
@@ -1,42 +1,100 @@
// Fmt.bux — String formatting with positional placeholders {0}, {1}, {2}...
//
// Usage:
// Fmt_Fmt1("Hello, {0}!", "World")
// Fmt_FmtInt("Count: {0}", 42)
// Fmt_FmtBool("Active: {0}", true)
// Fmt_Fmt2("Name: {0}, Age: {1}", name, String_FromInt(age))
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);
}
import Std::String::{
String_Eq,
String_FromInt,
String_FromFloat,
String_FromBool,
StringBuilder,
StringBuilder_New,
StringBuilder_Append,
StringBuilder_Build,
String_Chars
};
func Fmt_Fmt2(pattern: String, a: String, b: String) -> String {
return String_Format2(pattern, a, b);
}
extern func bux_strlen(s: String) -> uint;
extern func bux_str_to_int(s: String) -> int64;
extern func bux_alloc(size: uint) -> *void;
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");
// Core formatting engine: replace {0}..{9} in template with args
func Fmt_Format(tmpl: String, argStrs: *String, argCount: int) -> String {
let sb: StringBuilder = StringBuilder_New();
var i: uint = 0;
let tmplLen: uint = bux_strlen(tmpl);
while i < tmplLen {
// Check for {
let ch: String = String_Chars(tmpl, i);
if String_Eq(ch, "{") {
let digitIdx: uint = i + 1;
if digitIdx < tmplLen {
let digitCh: String = String_Chars(tmpl, digitIdx);
let d: int64 = bux_str_to_int(digitCh);
if d >= 0 && d < argCount as int64 {
// Consume {d}
i = i + 2; // skip past digit
// Check for closing }
if i < tmplLen {
let closeCh: String = String_Chars(tmpl, i);
if String_Eq(closeCh, "}") {
i = i + 1; // skip }
let argStr: String = *(argStrs + (d * 8) as uint);
StringBuilder_Append(&sb, argStr);
continue;
}
}
}
}
}
// Normal character: append it
StringBuilder_Append(&sb, ch);
i = i + 1;
}
return String_Format1(pattern, "false");
return StringBuilder_Build(&sb);
}
// Convenience wrappers
func Fmt_Fmt1(tmpl: String, a1: String) -> String {
var args: *String = bux_alloc(8) as *String;
*(args + 0) = a1;
return Fmt_Format(tmpl, args, 1);
}
func Fmt_FmtInt(tmpl: String, val: int64) -> String {
let s: String = String_FromInt(val);
return Fmt_Fmt1(tmpl, s);
}
func Fmt_FmtBool(tmpl: String, val: bool) -> String {
let s: String = String_FromBool(val);
return Fmt_Fmt1(tmpl, s);
}
func Fmt_FmtFloat(tmpl: String, val: float64) -> String {
let s: String = String_FromFloat(val);
return Fmt_Fmt1(tmpl, s);
}
func Fmt_Fmt2(tmpl: String, a1: String, a2: String) -> String {
var args: *String = bux_alloc(16) as *String;
*(args + 0) = a1;
*(args + 8) = a2;
return Fmt_Format(tmpl, args, 2);
}
func Fmt_Fmt3(tmpl: String, a1: String, a2: String, a3: String) -> String {
var args: *String = bux_alloc(24) as *String;
*(args + 0) = a1;
*(args + 8) = a2;
*(args + 16) = a3;
return Fmt_Format(tmpl, args, 3);
}
}