mirror of
https://github.com/ziglang/zig.git
synced 2025-12-15 18:53:07 +00:00
std: replace print_u64/i64 with printInt
This commit is contained in:
parent
37d167f6e0
commit
a2ac06dcd5
@ -10,7 +10,7 @@ pub fn printStackTrace() {
|
||||
while (true) {
|
||||
const fp = maybe_fp ?? break;
|
||||
const return_address = *(&const usize)(usize(fp) + @sizeOf(usize));
|
||||
%%io.stderr.print_u64(return_address);
|
||||
%%io.stderr.printInt(usize, return_address);
|
||||
%%io.stderr.printf("\n");
|
||||
maybe_fp = *(&const ?&const u8)(fp);
|
||||
}
|
||||
|
||||
24
std/io.zig
24
std/io.zig
@ -94,23 +94,13 @@ pub struct OutStream {
|
||||
return byte_count;
|
||||
}
|
||||
|
||||
pub fn print_u64(os: &OutStream, x: u64) -> %usize {
|
||||
pub fn printInt(os: &OutStream, inline T: type, x: T) -> %usize {
|
||||
// TODO replace max_u64_base10_digits with math.log10(math.pow(2, @sizeOf(T)))
|
||||
if (os.index + max_u64_base10_digits >= os.buffer.len) {
|
||||
%return os.flush();
|
||||
}
|
||||
const amt_printed = bufPrintUnsigned(u64, os.buffer[os.index...], x);
|
||||
const amt_printed = bufPrintInt(T, os.buffer[os.index...], x);
|
||||
os.index += amt_printed;
|
||||
|
||||
return amt_printed;
|
||||
}
|
||||
|
||||
pub fn print_i64(os: &OutStream, x: i64) -> %usize {
|
||||
if (os.index + max_u64_base10_digits >= os.buffer.len) {
|
||||
%return os.flush();
|
||||
}
|
||||
const amt_printed = bufPrintSigned(i64, os.buffer[os.index...], x);
|
||||
os.index += amt_printed;
|
||||
|
||||
return amt_printed;
|
||||
}
|
||||
|
||||
@ -234,7 +224,11 @@ fn charToDigit(c: u8, radix: u8) -> %u8 {
|
||||
return if (value >= radix) error.InvalidChar else value;
|
||||
}
|
||||
|
||||
pub fn bufPrintSigned(inline T: type, out_buf: []u8, x: T) -> usize {
|
||||
pub fn bufPrintInt(inline T: type, out_buf: []u8, x: T) -> usize {
|
||||
if (T.is_signed) bufPrintSigned(T, out_buf, x) else bufPrintUnsigned(T, out_buf, x)
|
||||
}
|
||||
|
||||
fn bufPrintSigned(inline T: type, out_buf: []u8, x: T) -> usize {
|
||||
const uint = @intType(false, T.bit_count);
|
||||
if (x < 0) {
|
||||
out_buf[0] = '-';
|
||||
@ -244,7 +238,7 @@ pub fn bufPrintSigned(inline T: type, out_buf: []u8, x: T) -> usize {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn bufPrintUnsigned(inline T: type, out_buf: []u8, x: T) -> usize {
|
||||
fn bufPrintUnsigned(inline T: type, out_buf: []u8, x: T) -> usize {
|
||||
var buf: [max_u64_base10_digits]u8 = undefined;
|
||||
var a = x;
|
||||
var index: usize = buf.len;
|
||||
|
||||
@ -11,9 +11,9 @@ pub fn runTests() -> %void {
|
||||
for (zig_test_fn_list) |testFn, i| {
|
||||
// TODO: print var args
|
||||
%%io.stderr.write("Test ");
|
||||
%%io.stderr.print_u64(i + 1);
|
||||
%%io.stderr.printInt(@typeOf(i), i + 1);
|
||||
%%io.stderr.write("/");
|
||||
%%io.stderr.print_u64(zig_test_fn_list.len);
|
||||
%%io.stderr.printInt(@typeOf(zig_test_fn_list.len), zig_test_fn_list.len);
|
||||
%%io.stderr.write(" ");
|
||||
%%io.stderr.write(testFn.name);
|
||||
%%io.stderr.write("...");
|
||||
|
||||
@ -462,20 +462,20 @@ const io = @import("std").io;
|
||||
pub fn main(args: [][]u8) -> %void {
|
||||
const array = []u8 {9, 8, 7, 6};
|
||||
for (array) |item| {
|
||||
%%io.stdout.print_u64(item);
|
||||
%%io.stdout.printInt(@typeOf(item), item);
|
||||
%%io.stdout.printf("\n");
|
||||
}
|
||||
for (array) |item, index| {
|
||||
%%io.stdout.print_u64(index);
|
||||
%%io.stdout.printInt(@typeOf(index), index);
|
||||
%%io.stdout.printf("\n");
|
||||
}
|
||||
const unknown_size: []u8 = array;
|
||||
for (unknown_size) |item| {
|
||||
%%io.stdout.print_u64(item);
|
||||
%%io.stdout.printInt(@typeOf(item), item);
|
||||
%%io.stdout.printf("\n");
|
||||
}
|
||||
for (unknown_size) |item, index| {
|
||||
%%io.stdout.print_u64(index);
|
||||
%%io.stdout.printInt(@typeOf(index), index);
|
||||
%%io.stdout.printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user