mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
lib/std/io: support . string format speficier (#970)
Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
This commit is contained in:
@@ -49,13 +49,13 @@ struct Formatter
|
|||||||
|
|
||||||
bitstruct PrintFlags : uint
|
bitstruct PrintFlags : uint
|
||||||
{
|
{
|
||||||
bool zeropad : 0;
|
bool zeropad;
|
||||||
bool left : 1;
|
bool left;
|
||||||
bool plus : 2;
|
bool plus;
|
||||||
bool space : 3;
|
bool space;
|
||||||
bool hash : 4;
|
bool hash;
|
||||||
bool uppercase : 5;
|
bool uppercase;
|
||||||
bool precision : 6;
|
bool precision;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn void Formatter.init(&self, OutputFn out_fn, void* data = null)
|
fn void Formatter.init(&self, OutputFn out_fn, void* data = null)
|
||||||
@@ -349,6 +349,19 @@ fn usz! Formatter.vprintf(&self, String format, any[] anys)
|
|||||||
case 's':
|
case 's':
|
||||||
PrintFlags flags = self.flags;
|
PrintFlags flags = self.flags;
|
||||||
uint width = self.width;
|
uint width = self.width;
|
||||||
|
String str;
|
||||||
|
if (flags.precision)
|
||||||
|
{
|
||||||
|
switch (s = current)
|
||||||
|
{
|
||||||
|
case String:
|
||||||
|
if (s.len > self.prec)
|
||||||
|
{
|
||||||
|
str = (*s)[:self.prec];
|
||||||
|
current = &str;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
defer {
|
defer {
|
||||||
self.flags = flags;
|
self.flags = flags;
|
||||||
self.width = width;
|
self.width = width;
|
||||||
|
|||||||
@@ -46,4 +46,8 @@ fn void printf_a()
|
|||||||
assert(s == "[<12, 23>] ", "got '%s'; want '[<12, 23>] '", s);
|
assert(s == "[<12, 23>] ", "got '%s'; want '[<12, 23>] '", s);
|
||||||
s = string::printf("%20s", vec);
|
s = string::printf("%20s", vec);
|
||||||
assert(s == " [<12, 23>]", "got '%s'; want ' [<12, 23>]'", s);
|
assert(s == " [<12, 23>]", "got '%s'; want ' [<12, 23>]'", s);
|
||||||
|
|
||||||
|
String ss = "hello world";
|
||||||
|
s = string::printf("%.4s %.5s", ss, ss);
|
||||||
|
assert(s == "hell hello", "got '%s'; want 'hell hello'", s);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user