fmt

package
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 1, 2020 License: Apache-2.0 Imports: 8 Imported by: 0

README

Overall code structure

This directory imports the "printf" logic as-is from the Go standard library and instruments it for redaction of sensitive data.

Overall, the original Go code is structured as follows:

  • the top-level API functions (Printf Fprintf etc) instantiate a "printer" struct called pp, then call the doPrint*() methods on it.

  • the pp contains a byte slice (pp.buf, type buffer) that accumulates the result of formatting regardless of the final output of the API - i.e. a buffer is used even when using Fprint to an io.Writer. Only after the doPrint() method finishes, is the buffer copied to the final io.Writer in the Fprint* variants.

  • each of the doPrint methods does some analysis on the argument list - quite simple for e.g. doPrintln, more intricate for doPrintf. As part of the analysis it emits "spacing" or no-op bytes directly on the pp.buf. For example doPrint emits spaces between arguments directly, doPrintf emits the non-formatting characters from the format string, as well as certain constant error strings (e.g. %(BADPREC)).

At this point we make the following assumption: there is no data from the printed arguments that is emitted to pp.buf directly by a doPrint() method. This is true of the implementation as of Go 1.14.

From this point, this package contains a custom patch onto the default implementation: the calls to p.printArg performed by the doPrint() methods are redirected to a function callback that is injected by the redact package.

Refreshing the sources

The files in this directory have been imported from

$GOROOT/src/fmt/{format,print}.go

and

$GOROOT/src/internal/fmtsort

And patched using the included .diff files.

To upgrade to a newer Go implementation, import the files anew and re-apply the patches.

See the script refresh.sh for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append(p *InternalPrinter, b []byte)

Append adds bytes to the buffer.

func Buf

func Buf(p *InternalPrinter) []byte

Buf exposes the string buffer to the redact package.

func DoPrint

func DoPrint(p *InternalPrinter, a []interface{})

DoPrint exposes the doPrint() method to the redact package.

func DoPrintf

func DoPrintf(p *InternalPrinter, format string, a []interface{})

DoPrintf exposes the doPrintf() method to the redact package.

func Fprint

func Fprint(w io.Writer, a ...interface{}) (n int, err error)

Fprint formats using the default formats for its operands and writes to w. Spaces are added between operands when neither is a string. It returns the number of bytes written and any write error encountered.

func Fprintf

func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)

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

func Fprintln

func Fprintln(w io.Writer, a ...interface{}) (n int, err error)

Fprintln formats using the default formats for its operands and writes to w. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered.

func Free

func Free(p *InternalPrinter)

Free exposes pp deallocation to the redact package.

func GetState

func GetState(p *InternalPrinter) int

GetState exposes the state to the redact package.

func Print

func Print(a ...interface{}) (n int, err error)

Print formats using the default formats for its operands and writes to standard output. Spaces are added between operands when neither is a string. It returns the number of bytes written and any write error encountered.

func PrintArg

func PrintArg(p *InternalPrinter, a interface{}, verb rune)

PrintArg exposes the printArgOrig() method to the redact package.

func Printf

func Printf(format string, a ...interface{}) (n int, err error)

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

func Println

func Println(a ...interface{}) (n int, err error)

Println formats using the default formats for its operands and writes to standard output. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered.

func SetHook

func SetHook(
	p *InternalPrinter, fn func(p *InternalPrinter, arg interface{}, verb rune) (newState int),
)

SetHook connects an outer printer to the inner printer.

func SetState

func SetState(p *InternalPrinter, b []byte)

SetState exposes the string buffer to the redact package.

func Sprint

func Sprint(a ...interface{}) string

Sprint formats using the default formats for its operands and returns the resulting string. Spaces are added between operands when neither is a string.

func Sprintf

func Sprintf(format string, a ...interface{}) string

Sprintf formats according to a format specifier and returns the resulting string.

func Sprintln

func Sprintln(a ...interface{}) string

Sprintln formats using the default formats for its operands and returns the resulting string. Spaces are always added between operands and a newline is appended.

Types

type Formatter

type Formatter interface {
	// CUSTOM: refer to the original type, not the one defined here.
	Format(f origFmt.State, c rune)
}

Formatter is the interface implemented by values with a custom formatter. The implementation of Format may call Sprint(f) or Fprint(f) etc. to generate its output.

type GoStringer

type GoStringer interface {
	GoString() string
}

GoStringer is implemented by any value that has a GoString method, which defines the Go syntax for that value. The GoString method is used to print values passed as an operand to a %#v format.

type InternalBuffer

type InternalBuffer = buffer

InternalBuffer exposes buffer to the redact package.

type InternalPrinter

type InternalPrinter = pp

InternalPrinter exposes pp to the redact package.

func NewInternalPrinter

func NewInternalPrinter() *InternalPrinter

NewInternalPrinter exposes pp allocation to the redact package.

type State

type State interface {
	// Write is the function to call to emit formatted output to be printed.
	Write(b []byte) (n int, err error)
	// Width returns the value of the width option and whether it has been set.
	Width() (wid int, ok bool)
	// Precision returns the value of the precision option and whether it has been set.
	Precision() (prec int, ok bool)

	// Flag reports whether the flag c, a character, has been set.
	Flag(c int) bool
}

State represents the printer state passed to custom formatters. It provides access to the io.Writer interface plus information about the flags and options for the operand's format specifier.

type Stringer

type Stringer interface {
	String() string
}

Stringer is implemented by any value that has a String method, which defines the “native” format for that value. The String method is used to print values passed as an operand to any format that accepts a string or to an unformatted printer such as Print.

Directories

Path Synopsis
Package fmtsort provides a general stable ordering mechanism for maps, on behalf of the fmt and text/template packages.
Package fmtsort provides a general stable ordering mechanism for maps, on behalf of the fmt and text/template packages.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL