Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

v0 source pure

v0 - Unaudited: This is an initial version that has not yet been formally audited. A fully audited version will be pu...

Readme View source

v0 - Unaudited This is an initial version of this package that has not yet been formally audited. A fully audited version will be published as a subsequent release. Use in production at your own risk.

ufmt - String formatting

Gno port of a subset of Go's fmt package (micro-fmt). Provides Printf, Sprintf, Errorf and friends for formatting strings with verb-based templates.

Usage

 1import "gno.land/p/nt/ufmt/v0"
 2
 3s := ufmt.Sprintf("hello %s, you are %d years old", "alice", 30)
 4// "hello alice, you are 30 years old"
 5
 6err := ufmt.Errorf("invalid id: %q", input)
 7
 8var buf bytes.Buffer
 9ufmt.Fprintf(&buf, "balance: %d", amount)
10
11line := ufmt.Sprintln("token", symbol, "transferred") // adds spaces + newline

API

 1// Format and return a string.
 2func Sprint(a ...any) string
 3func Sprintf(format string, a ...any) string
 4func Sprintln(a ...any) string
 5
 6// Format and write to an io.Writer.
 7func Fprint(w io.Writer, a ...any) (n int, err error)
 8func Fprintf(w io.Writer, format string, a ...any) (n int, err error)
 9func Fprintln(w io.Writer, a ...any) (n int, err error)
10
11// Format and print to standard output.
12func Print(a ...any) (n int, err error)
13func Printf(format string, a ...any) (n int, err error)
14func Println(a ...any) (n int, err error)
15
16// Format and append to a byte slice.
17func Append(b []byte, a ...any) []byte
18func Appendf(b []byte, format string, a ...any) []byte
19func Appendln(b []byte, a ...any) []byte
20
21// Format and return an error.
22func Errorf(format string, args ...any) error

Supported verbs

Verb Meaning
%s String. Uses String() or Error() if implemented.
%d Integer (signed and unsigned, all widths).
%c Unicode character from rune/int code point.
%t Boolean: true or false.
%q Double-quoted, escaped string.
%x Hexadecimal (uint8 only).
%f / %F Decimal float; default precision 6.
%e / %E Scientific notation float; default precision 2.
%g / %G Float, compact representation.
%T Type name of the argument (basic types only).
%v Default representation appropriate for the value's type.
%% Literal %.

Width (%5s) and precision (%.2f) are supported for the relevant verbs.

Notes

  • Verb/type mismatches produce %!verb(type=value) strings, matching Go's fmt behaviour.
  • Missing or extra arguments panic.
  • Not supported: %b, %o, %U, %p, %+v, %#v, argument indexing, flags like -, +, #, 0.
  • Print* writes via the built-in print (stdout substitute) until os.Stdout is available.

Overview

v0 - Unaudited: This is an initial version that has not yet been formally audited. A fully audited version will be published as a subsequent release. Use in production at your own risk.

Package ufmt provides utility functions for formatting strings, similarly to the Go package "fmt", of which only a subset is currently supported.

Package ufmt provides utility functions for formatting strings, similarly to the Go package "fmt", of which only a subset is currently supported (hence the name µfmt - micro fmt). It includes functions like Printf, Sprintf, Fprintf, and Errorf. Supported formatting verbs are documented in the Sprintf function.

Functions 13

func Append

1func Append(b []byte, a ...any) []byte
source

Append formats using default formats, appends to b, and returns the updated slice. Spaces are added between arguments.

func Appendf

1func Appendf(b []byte, format string, a ...any) []byte
source

Appendf formats according to a format specifier, appends the result to the byte slice, and returns the updated slice.

func Appendln

1func Appendln(b []byte, a ...any) []byte
source

Appendln formats using default formats, appends to b, and returns the updated slice. Appends a newline after the last argument.

func Errorf

1func Errorf(format string, args ...any) error
source

Errorf formats according to a format specifier and returns an error value. Supports the same verbs as Sprintf. See Sprintf documentation for details.

func Fprint

1func Fprint(w io.Writer, a ...any) (n int, err error)
source

Fprint formats using default formats and writes to w. Spaces are added between arguments. Returns the number of bytes written and any write error encountered.

func Fprintf

1func Fprintf(w io.Writer, format string, a ...any) (n int, err error)
source

Fprintf formats according to a format specifier and writes to w. Returns the number of bytes written and any write error encountered.

func Fprintln

1func Fprintln(w io.Writer, a ...any) (n int, err error)
source

Fprintln formats using default formats and writes to w with newline. Returns the number of bytes written and any write error encountered.

func Print

1func Print(a ...any) (n int, err error)
source

Print formats using default formats and writes to standard output. Spaces are added between arguments. Returns the number of bytes written and any write error encountered.

XXX: Replace with os.Stdout handling when available.

func Printf

1func Printf(format string, a ...any) (n int, err error)
source

Printf formats according to a format specifier and writes to standard output. Returns the number of bytes written and any write error encountered.

XXX: Replace with os.Stdout handling when available.

func Println

1func Println(a ...any) (n int, err error)
source

Println formats using default formats and writes to standard output with newline. Returns the number of bytes written and any write error encountered.

XXX: Replace with os.Stdout handling when available.

func Sprint

1func Sprint(a ...any) string
source

Sprint formats using the default formats for its operands and returns the resulting string. Sprint writes the given arguments with spaces between arguments.

func Sprintf

1func Sprintf(format string, a ...any) string
source

Sprintf offers similar functionality to Go's fmt.Sprintf, or the sprintf equivalent available in many languages, including C/C++. The number of args passed must exactly match the arguments consumed by the format. A limited number of formatting verbs and features are currently supported.

Supported verbs:

Example
 1%s: Places a string value directly.
 2    If the value implements the interface interface{ String() string },
 3    the String() method is called to retrieve the value. Same about Error()
 4    string.
 5%c: Formats the character represented by Unicode code point
 6%d: Formats an integer value using package "strconv".
 7    Currently supports only uint, uint64, int, int64.
 8%f: Formats a float value, with a default precision of 6.
 9%e: Formats a float with scientific notation; 1.23456e+78
10%E: Formats a float with scientific notation; 1.23456E+78
11%F: The same as %f
12%g: Formats a float value with %e for large exponents, and %f with full precision for smaller numbers
13%G: Formats a float value with %G for large exponents, and %F with full precision for smaller numbers
14%t: Formats a boolean value to "true" or "false".
15%x: Formats an integer value as a hexadecimal string.
16    Currently supports only uint8, []uint8, [32]uint8.
17%c: Formats a rune value as a string.
18    Currently supports only rune, int.
19%q: Formats a string value as a quoted string.
20%T: Formats the type of the value.
21%v: Formats the value with a default representation appropriate for the value's type
22    - nil: <nil>
23    - bool: true/false
24    - integers: base 10
25    - float64: %g format
26    - string: verbatim
27    - types with String()/Error(): method result
28    - others: (unhandled)
29%%: Outputs a literal %. Does not consume an argument.

Unsupported verbs or type mismatches produce error strings like "%!d(string=foo)".

func Sprintln

1func Sprintln(a ...any) string
source

Sprintln formats using default formats and returns the string with newline. Spaces are always added between arguments.

Imports 5

  • errors stdlib
  • io stdlib
  • strconv stdlib
  • strings stdlib
  • unicode/utf8 stdlib

Source Files 5