Skip to content

Commit

Permalink
std/fmt: use io::Writer instead of os::File for the Fprint, Fprintln,…
Browse files Browse the repository at this point in the history
… and Fprintf functions
  • Loading branch information
mertcandav committed Dec 31, 2024
1 parent 0fcff48 commit ebe930d
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions std/fmt/print.jule
Original file line number Diff line number Diff line change
@@ -4,17 +4,18 @@

use "std/internal/fmt"
use "std/internal/stringslite"
use "std/io"
use "std/os"
use "std/unsafe"

// Prints arguments to file by default formatting.
// Prints arguments to w by default formatting.
// See documentation of format function for formatting.
fn Fprint(mut f: &os::File, args: ...any) {
fn Fprint(mut w: io::Writer, args: ...any) {
mut sb := stringslite::Builder{}
sb.Grow(20)
for _, arg in args {
fmt::FmtByDefault(sb, arg)
f.Write(unsafe { sb.Buf() }) else {
w.Write(unsafe { sb.Buf() }) else {
panic("fmt: Fprint: error occurs when printing")
}
// Do not use the [Clear] method to avoid making new allocations.
@@ -23,38 +24,38 @@ fn Fprint(mut f: &os::File, args: ...any) {
}
}

// Prints arguments to file by default formatting.
// Prints arguments to w by default formatting.
// Prints new-line after arguments.
// See documentation of format function for formatting.
fn Fprintln(mut f: &os::File, args: ...any) {
Fprint(f, args...)
Fprintf(f, "\n")
fn Fprintln(mut w: io::Writer, args: ...any) {
Fprint(w, args...)
Fprintf(w, "\n")
}

// Prints result of formatting to file.
// Prints result of formatting to w.
// See documentation of format function for formatting.
fn Fprintf(mut f: &os::File, fmt: str, args: ...any) {
fn Fprintf(mut w: io::Writer, fmt: str, args: ...any) {
format := fmt::Format(fmt, args...)
f.Write(format) else {
w.Write(format) else {
panic("fmt: Fprintf: error occurs when printing")
}
}

// Prints result of formatting to stdout.
// See documentation of format function for formatting.
fn Printf(fmt: str, args: ...any) {
Fprintf(unsafe { os::Stdout().File() }, fmt, args...)
Fprintf(os::Stdout(), fmt, args...)
}

// Prints arguments by default formatting to stdout.
fn Print(args: ...any) {
Fprint(unsafe { os::Stdout().File() }, args...)
Fprint(os::Stdout(), args...)
}

// Prints arguments by default formatting to stdout.
// Prints new-line after arguments.
fn Println(args: ...any) {
Fprintln(unsafe { os::Stdout().File() }, args...)
Fprintln(os::Stdout(), args...)
}

// Returns string result of arguments by default formatting.

0 comments on commit ebe930d

Please sign in to comment.