Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/fmt: add nprint family of procs to get required buffer sizes #4935

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions core/fmt/fmt.odin
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,81 @@ sbprintf :: proc(buf: ^strings.Builder, fmt: string, args: ..any, newline := fal
sbprintfln :: proc(buf: ^strings.Builder, format: string, args: ..any) -> string {
return sbprintf(buf, format, ..args, newline=true)
}
// This proc creates a helper stream for the nprint family of procs that just helps with counting the written bytes via the wprint procs.
@(private)
_nprint_stream_create :: proc "contextless"() -> io.Stream {
return io.Stream {procedure =
proc(
stream_data: rawptr,
mode: io.Stream_Mode,
p: []byte,
offset: i64,
whence: io.Seek_From,
) -> (
n: i64,
err: io.Error,
) {
#partial switch mode {
case .Query:
return io.query_utility({.Write})
}
return i64(len(p)), nil
},
}
}
// Returns the number of bytes required to write the arguments using the default print settings.
//
// Inputs:
// - args: A variadic list of arguments to be formatted
// - sep: An optional separator string (default is a single space)
//
// Returns: The number of bytes required
//
@(require_results)
nprint :: proc(args: ..any, sep := " ") -> int {
stream := _nprint_stream_create()
return wprint(stream, ..args, sep = sep, flush = false)
}
// Returns the number of bytes required to write the arguments using the default print settings with a newline character at the end.
//
// Inputs:
// - args: A variadic list of arguments to be formatted
// - sep: An optional separator string (default is a single space)
//
// Returns: The number of bytes required
//
@(require_results)
nprintln :: proc(args: ..any, sep := " ") -> int {
stream := _nprint_stream_create()
return wprintln(stream, ..args, sep = sep, flush = false)
}
// Returns the number of bytes required to write the arguments according to the specified format string.
//
// Inputs:
// - fmt: The format string
// - args: A variadic list of arguments to be formatted
// - newline: Whether a trailing newline should be written. (See `nprintfln`)
//
// Returns: The number of bytes required
//
@(require_results)
nprintf :: proc(fmt: string, args: ..any, newline := false) -> int {
stream := _nprint_stream_create()
return wprintf(stream, fmt, ..args, flush = false)
}
// Returns the number of bytes required to write the arguments according to the specified format string, followed by a newline.
//
// Inputs:
// - fmt: The format string
// - args: A variadic list of arguments to be formatted
//
// Returns: The number of bytes required
//
@(require_results)
nprintfln :: proc(fmt: string, args: ..any) -> int {
stream := _nprint_stream_create()
return wprintfln(stream, fmt, ..args, flush = false)
}
// Formats and writes to an io.Writer using the default print settings
//
// Inputs:
Expand Down