2022-09-09 22:55:34 +01:00
|
|
|
const stack = @import("./stack.zig");
|
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Opaque interface to a "writable" resource, such as a block device, memory buffer, or network
|
|
|
|
/// socket.
|
|
|
|
///
|
|
|
|
pub const Writer = struct {
|
|
|
|
context: *anyopaque,
|
2022-10-02 23:57:41 +01:00
|
|
|
writeContext: fn (*anyopaque, []const u8) usize,
|
|
|
|
|
|
|
|
///
|
|
|
|
///
|
|
|
|
///
|
|
|
|
pub const Radix = enum {
|
|
|
|
binary,
|
|
|
|
tinary,
|
|
|
|
quaternary,
|
|
|
|
quinary,
|
|
|
|
senary,
|
|
|
|
septenary,
|
|
|
|
octal,
|
|
|
|
nonary,
|
|
|
|
decimal,
|
|
|
|
undecimal,
|
|
|
|
duodecimal,
|
|
|
|
tridecimal,
|
|
|
|
tetradecimal,
|
|
|
|
pentadecimal,
|
|
|
|
hexadecimal,
|
|
|
|
};
|
2022-09-09 22:55:34 +01:00
|
|
|
|
|
|
|
///
|
|
|
|
/// Wraps and returns a reference to `write_context` of type `WriteContext` and its associated
|
|
|
|
/// `writeContext` writing operation in a [Writer].
|
|
|
|
///
|
|
|
|
pub fn wrap(
|
|
|
|
comptime WriteContext: type,
|
|
|
|
write_context: *WriteContext,
|
|
|
|
comptime writeContext: fn (*WriteContext, []const u8) usize
|
|
|
|
) Writer {
|
|
|
|
return .{
|
|
|
|
.context = write_context,
|
|
|
|
|
2022-10-02 23:57:41 +01:00
|
|
|
.writeContext = struct {
|
2022-09-09 22:55:34 +01:00
|
|
|
fn write(context: *anyopaque, buffer: []const u8) usize {
|
|
|
|
return writeContext(@ptrCast(*WriteContext,
|
|
|
|
@alignCast(@alignOf(WriteContext), context)), buffer);
|
|
|
|
}
|
|
|
|
}.write,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Attempts to write `buffer` to `writer`, returning the number of bytes from `buffer` that
|
|
|
|
/// were successfully written.
|
|
|
|
///
|
|
|
|
pub fn write(writer: Writer, buffer: []const u8) usize {
|
2022-10-02 23:57:41 +01:00
|
|
|
return writer.writeContext(writer.context, buffer);
|
2022-09-09 22:55:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Writes the singular `byte` to `writer`, returning `true` if it was successfully written,
|
|
|
|
/// otherwise `false`.
|
|
|
|
///
|
|
|
|
pub fn writeByte(writer: Writer, byte: u8) bool {
|
2022-10-02 23:57:41 +01:00
|
|
|
return (writer.writeContext(writer.context,
|
2022-09-09 22:55:34 +01:00
|
|
|
@ptrCast([*]const u8, &byte)[0 .. 1]) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Writes `value` as a ASCII / UTF-8 encoded integer to `writer`, returning `true` if the full
|
|
|
|
/// sequence was successfully written, otherwise `false`.
|
|
|
|
///
|
2022-10-02 23:57:41 +01:00
|
|
|
/// The `radix` argument identifies which base system to encode `value` as, with `10` being
|
2022-09-09 22:55:34 +01:00
|
|
|
/// decimal, `16` being hexadecimal, `8` being octal`, so on and so forth.
|
|
|
|
///
|
2022-10-02 23:57:41 +01:00
|
|
|
pub fn writeInt(writer: Writer, radix: Radix, value: anytype) bool {
|
2022-09-09 22:55:34 +01:00
|
|
|
const Int = @TypeOf(value);
|
|
|
|
const type_info = @typeInfo(Int);
|
|
|
|
|
|
|
|
if (type_info != .Int) @compileError("value must be of type int");
|
|
|
|
|
|
|
|
if (value == 0) return writer.writeByte('0');
|
|
|
|
|
2022-10-02 23:57:41 +01:00
|
|
|
// TODO: Unhardcode this as it will break with large ints.
|
2022-09-09 22:55:34 +01:00
|
|
|
var buffer = std.mem.zeroes([28]u8);
|
|
|
|
var buffer_count = @as(usize, 0);
|
|
|
|
var n1 = value;
|
|
|
|
|
|
|
|
if ((type_info.Int.signedness == .signed) and (value < 0)) {
|
|
|
|
// Negative value.
|
|
|
|
n1 = -value;
|
|
|
|
buffer[0] = '-';
|
|
|
|
buffer_count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (n1 != 0) {
|
2022-10-02 23:57:41 +01:00
|
|
|
const base = @enumToInt(radix);
|
|
|
|
|
2022-09-09 22:55:34 +01:00
|
|
|
buffer[buffer_count] = @intCast(u8, (n1 % base) + '0');
|
|
|
|
n1 = (n1 / base);
|
|
|
|
buffer_count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (buffer[0 .. (buffer_count / 2)]) |_, i|
|
|
|
|
std.mem.swap(u8, &buffer[i], &buffer[buffer_count - i - 1]);
|
|
|
|
|
|
|
|
return (writer.write(buffer[0 .. buffer_count]) == buffer_count);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var null_context = @as(usize, 0);
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Writer that silently throws consumed data away and never fails.
|
|
|
|
///
|
|
|
|
/// This is commonly used for testing or redirected otherwise unwanted output data that can't not be
|
|
|
|
/// sent somewhere for whatever reason.
|
|
|
|
///
|
|
|
|
pub const null_writer = Writer{
|
|
|
|
.context = (&null_context),
|
|
|
|
|
|
|
|
.operation = struct {
|
|
|
|
fn write(context: *anyopaque, buffer: []const u8) usize {
|
|
|
|
// Validate context canary value.
|
|
|
|
std.debug.assert(@ptrCast(*usize, @alignCast(@alignOf(usize), context)).* == 0);
|
|
|
|
|
|
|
|
return buffer.len;
|
|
|
|
}
|
|
|
|
}.write,
|
|
|
|
};
|
2022-10-02 23:57:41 +01:00
|
|
|
|
|
|
|
test {
|
|
|
|
const testing = std.testing;
|
|
|
|
|
|
|
|
{
|
|
|
|
const sequence = "foo";
|
|
|
|
|
|
|
|
testing.expectEqual(null_writer.write(sequence), sequence.len);
|
|
|
|
}
|
|
|
|
|
|
|
|
testing.expect(null_writer.writeByte(0));
|
|
|
|
testing.expect(null_writer.writeInt(.decimal, 420));
|
|
|
|
}
|