// Package nofmt provides the small subset of fmt used by asyncmachine // libraries, without importing fmt. It keeps error wrapping compatible with // fmt.Errorf (%w, errors.Is, errors.As).
package nofmt import ( ) // Stringer is the local equivalent of fmt.Stringer. type Stringer interface { String() string } // Sprintf formats according to a format specifier and returns the resulting // string. Supported verbs: %s, %d, %v, %+v, %w, and %%. func ( string, ...any) string { , := formatString(, ) return } // Errorf formats according to a format specifier and returns an error. %w // wraps the corresponding error, including multiple %w operands. func ( string, ...any) error { , := formatString(, ) switch len() { case 0: return errors.New() case 1: return &wrapError{msg: , err: [0]} default: return &wrapErrors{msg: , errs: } } } // Sprint formats operands using their default %v representation. Spaces are // added between operands when neither is a string, like fmt.Sprint. func ( ...any) string { var strings.Builder := false for , := range { := isStringOperand() if > 0 && ! && ! { .WriteByte(' ') } .WriteString(formatValue(reflect.ValueOf(), 'v', false)) = } return .String() } // Println writes operands to stdout, separated by spaces and followed by a // newline, like fmt.Println. func ( ...any) { var strings.Builder for , := range { if > 0 { .WriteByte(' ') } .WriteString(formatValue(reflect.ValueOf(), 'v', false)) } .WriteByte('\n') _, _ = os.Stdout.WriteString(.String()) } func isStringOperand( any) bool { if == nil { return false } return reflect.ValueOf().Kind() == reflect.String } type wrapError struct { msg string err error } func ( *wrapError) () string { return .msg } func ( *wrapError) () error { return .err } type wrapErrors struct { msg string errs []error } func ( *wrapErrors) () string { return .msg } func ( *wrapErrors) () []error { return .errs } func formatString( string, []any) (string, []error) { var strings.Builder var []error := 0 for := 0; < len(); { if [] != '%' { .WriteByte([]) ++ continue } if +1 < len() && [+1] == '%' { .WriteByte('%') += 2 continue } := := + 1 := false := false for < len() { switch [] { case '+': = true ++ case '-': = true ++ case '#', '0', ' ': ++ default: goto } } : := 0 := false for < len() && [] >= '0' && [] <= '9' { = true = *10 + int([]-'0') ++ } if < len() && [] == '.' { ++ for < len() && [] >= '0' && [] <= '9' { ++ } } if >= len() { .WriteString([:]) break } := rune([]) ++ if >= len() { .WriteString("%!") .WriteByte(byte()) .WriteString("(MISSING)") = continue } := [] ++ var string if == 'w' { if == nil { = "%!w(<nil>)" } else if , := .(error); { = append(, ) if == nil { = "%!w(<nil>)" } else { = .Error() } } else { = "%!w(" + typeName(reflect.ValueOf()) + "=" + formatValue(reflect.ValueOf(), 'v', ) + ")" } } else { = formatValue(reflect.ValueOf(), , ) } if && ( == 's' || == 'v' || == 'd') { = pad(, , ) } .WriteString() = } if < len() { .WriteString("%!(EXTRA") for ; < len(); ++ { .WriteByte(' ') := reflect.ValueOf([]) .WriteString(typeName()) .WriteByte('=') .WriteString(formatValue(, 'v', false)) } .WriteByte(')') } return .String(), } func formatValue( reflect.Value, rune, bool) string { if !.IsValid() { return "<nil>" } if .Kind() == reflect.Interface { if .IsNil() { return "<nil>" } return (.Elem(), , ) } if ( == 'v' || == 's') && .CanInterface() { if , := .Interface().(error); && != nil { return .Error() } if , := .Interface().(Stringer); { return .String() } } switch .Kind() { case reflect.String: return .String() case reflect.Bool: return strconv.FormatBool(.Bool()) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: return strconv.FormatInt(.Int(), 10) case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: return strconv.FormatUint(.Uint(), 10) case reflect.Float32: return strconv.FormatFloat(.Float(), 'g', -1, 32) case reflect.Float64: return strconv.FormatFloat(.Float(), 'g', -1, 64) case reflect.Complex64, reflect.Complex128: := .Complex() := strconv.FormatFloat(real(), 'g', -1, 64) := strconv.FormatFloat(imag(), 'g', -1, 64) if imag() >= 0 { = "+" + } return "(" + + + "i)" case reflect.Slice, reflect.Array: if == 's' && .Type().Elem().Kind() == reflect.Uint8 { if .Kind() == reflect.Slice { return string(.Bytes()) } := make([]byte, .Len()) for := range { [] = byte(.Index().Uint()) } return string() } := make([]string, .Len()) for := range { [] = (.Index(), , ) } return "[" + strings.Join(, " ") + "]" case reflect.Map: := .MapKeys() sortKeys() := make([]string, len()) for , := range { := .MapIndex() [] = (, , ) + ":" + (, , ) } return "map[" + strings.Join(, " ") + "]" case reflect.Struct: := make([]string, .NumField()) for := range { := .Type().Field().Name [] = (.Field(), , ) if { [] = + ":" + [] } } return "{" + strings.Join(, " ") + "}" case reflect.Pointer: if .IsNil() { return "<nil>" } switch .Elem().Kind() { case reflect.Struct, reflect.Slice, reflect.Array, reflect.Map: return "&" + (.Elem(), , ) } return "0x" + strconv.FormatUint(uint64(.Pointer()), 16) case reflect.Chan, reflect.Func, reflect.UnsafePointer: return "0x" + strconv.FormatUint(uint64(.Pointer()), 16) } if .CanInterface() { return Sprint(.Interface()) } return "<?>" } func sortKeys( []reflect.Value) { sort.SliceStable(, func(, int) bool { , := [], [] switch .Kind() { case reflect.String: return .String() < .String() case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: return .Int() < .Int() case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: return .Uint() < .Uint() case reflect.Float32, reflect.Float64: return .Float() < .Float() } return formatValue(, 'v', false) < formatValue(, 'v', false) }) } func pad( string, int, bool) string { := utf8.RuneCountInString() if >= { return } := strings.Repeat(" ", -) if { return + } return + } func typeName( reflect.Value) string { if !.IsValid() { return "<nil>" } return .Type().String() }