2008-10-24 17:33:29 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package fmt
|
|
|
|
|
2008-10-26 09:27:50 -06:00
|
|
|
/*
|
|
|
|
C-like printf, but because of reflection knowledge does not need
|
|
|
|
to be told about sizes and signedness (no %llud etc. - just %d).
|
|
|
|
*/
|
|
|
|
|
2008-10-24 17:33:29 -06:00
|
|
|
import (
|
|
|
|
"fmt";
|
2008-11-14 11:42:45 -07:00
|
|
|
"io";
|
2008-10-24 17:33:29 -06:00
|
|
|
"reflect";
|
|
|
|
"os";
|
2009-01-16 15:58:14 -07:00
|
|
|
"utf8";
|
2008-10-24 17:33:29 -06:00
|
|
|
)
|
|
|
|
|
2008-11-06 11:40:57 -07:00
|
|
|
// Representation of printer state passed to custom formatters.
|
2008-11-14 11:42:45 -07:00
|
|
|
// Provides access to the io.Write interface plus information about
|
2008-11-06 11:40:57 -07:00
|
|
|
// the active formatting verb.
|
2009-01-20 15:40:40 -07:00
|
|
|
type Formatter interface {
|
2008-12-18 23:37:22 -07:00
|
|
|
Write(b []byte) (ret int, err *os.Error);
|
2008-11-06 11:40:57 -07:00
|
|
|
Width() (wid int, ok bool);
|
|
|
|
Precision() (prec int, ok bool);
|
2008-12-11 17:53:33 -07:00
|
|
|
|
|
|
|
// flags
|
|
|
|
Flag(int) bool;
|
2008-11-06 11:40:57 -07:00
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type Format interface {
|
2008-11-06 12:38:44 -07:00
|
|
|
Format(f Formatter, c int);
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type String interface {
|
2008-11-06 12:38:44 -07:00
|
|
|
String() string
|
2008-11-06 11:40:57 -07:00
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
const runeSelf = 0x80
|
|
|
|
const allocSize = 32
|
2008-10-24 17:33:29 -06:00
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
type pp struct {
|
2008-10-24 17:33:29 -06:00
|
|
|
n int;
|
2008-12-18 23:37:22 -07:00
|
|
|
buf []byte;
|
2008-10-24 17:33:29 -06:00
|
|
|
fmt *Fmt;
|
|
|
|
}
|
|
|
|
|
2009-01-16 14:29:43 -07:00
|
|
|
func newPrinter() *pp {
|
2009-01-15 16:40:27 -07:00
|
|
|
p := new(pp);
|
2008-10-24 17:33:29 -06:00
|
|
|
p.fmt = fmt.New();
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (p *pp) Width() (wid int, ok bool) {
|
2008-12-11 17:53:33 -07:00
|
|
|
return p.fmt.wid, p.fmt.wid_present
|
2008-11-06 11:40:57 -07:00
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (p *pp) Precision() (prec int, ok bool) {
|
2008-12-11 17:53:33 -07:00
|
|
|
return p.fmt.prec, p.fmt.prec_present
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (p *pp) Flag(b int) bool {
|
2008-12-11 17:53:33 -07:00
|
|
|
switch b {
|
|
|
|
case '-':
|
|
|
|
return p.fmt.minus;
|
|
|
|
case '+':
|
|
|
|
return p.fmt.plus;
|
|
|
|
case '#':
|
|
|
|
return p.fmt.sharp;
|
|
|
|
case ' ':
|
|
|
|
return p.fmt.space;
|
|
|
|
case '0':
|
|
|
|
return p.fmt.zero;
|
|
|
|
}
|
|
|
|
return false
|
2008-11-06 11:40:57 -07:00
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (p *pp) ensure(n int) {
|
2008-12-18 23:37:22 -07:00
|
|
|
if len(p.buf) < n {
|
2009-01-15 16:40:27 -07:00
|
|
|
newn := allocSize + len(p.buf);
|
2008-10-24 17:33:29 -06:00
|
|
|
if newn < n {
|
2009-01-15 16:40:27 -07:00
|
|
|
newn = n + allocSize
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
2009-01-06 16:19:02 -07:00
|
|
|
b := make([]byte, newn);
|
2008-10-24 17:33:29 -06:00
|
|
|
for i := 0; i < p.n; i++ {
|
|
|
|
b[i] = p.buf[i];
|
|
|
|
}
|
|
|
|
p.buf = b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (p *pp) addstr(s string) {
|
2008-10-24 17:33:29 -06:00
|
|
|
n := len(s);
|
|
|
|
p.ensure(p.n + n);
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
p.buf[p.n] = s[i];
|
|
|
|
p.n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (p *pp) addbytes(b []byte, start, end int) {
|
2008-10-24 17:33:29 -06:00
|
|
|
p.ensure(p.n + end-start);
|
|
|
|
for i := start; i < end; i++ {
|
|
|
|
p.buf[p.n] = b[i];
|
|
|
|
p.n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (p *pp) add(c int) {
|
2008-10-24 17:33:29 -06:00
|
|
|
p.ensure(p.n + 1);
|
2009-01-15 16:40:27 -07:00
|
|
|
if c < runeSelf {
|
2008-10-24 17:33:29 -06:00
|
|
|
p.buf[p.n] = byte(c);
|
|
|
|
p.n++;
|
|
|
|
} else {
|
|
|
|
p.addstr(string(c));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-04 14:57:21 -07:00
|
|
|
// Implement Write so we can call fprintf on a P, for
|
|
|
|
// recursive use in custom verbs.
|
2009-01-15 16:40:27 -07:00
|
|
|
func (p *pp) Write(b []byte) (ret int, err *os.Error) {
|
2008-11-04 14:57:21 -07:00
|
|
|
p.addbytes(b, 0, len(b));
|
|
|
|
return len(b), nil;
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (p *pp) doprintf(format string, v reflect.StructValue);
|
|
|
|
func (p *pp) doprint(v reflect.StructValue, addspace, addnewline bool);
|
2008-10-24 17:33:29 -06:00
|
|
|
|
|
|
|
// These routines end in 'f' and take a format string.
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func Fprintf(w io.Write, format string, a ...) (n int, error *os.Error) {
|
2009-01-27 16:05:25 -07:00
|
|
|
v := reflect.NewValue(a).(reflect.StructValue);
|
2009-01-16 14:29:43 -07:00
|
|
|
p := newPrinter();
|
2008-10-24 17:33:29 -06:00
|
|
|
p.doprintf(format, v);
|
|
|
|
n, error = w.Write(p.buf[0:p.n]);
|
|
|
|
return n, error;
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func Printf(format string, v ...) (n int, errno *os.Error) {
|
2009-01-15 14:48:11 -07:00
|
|
|
n, errno = Fprintf(os.Stdout, format, v);
|
2008-10-24 17:33:29 -06:00
|
|
|
return n, errno;
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func Sprintf(format string, a ...) string {
|
2009-01-27 16:05:25 -07:00
|
|
|
v := reflect.NewValue(a).(reflect.StructValue);
|
2009-01-16 14:29:43 -07:00
|
|
|
p := newPrinter();
|
2008-11-06 12:56:08 -07:00
|
|
|
p.doprintf(format, v);
|
2008-10-24 17:33:29 -06:00
|
|
|
s := string(p.buf)[0 : p.n];
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
// These routines do not take a format string and add spaces only
|
|
|
|
// when the operand on neither side is a string.
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func Fprint(w io.Write, a ...) (n int, error *os.Error) {
|
2009-01-27 16:05:25 -07:00
|
|
|
v := reflect.NewValue(a).(reflect.StructValue);
|
2009-01-16 14:29:43 -07:00
|
|
|
p := newPrinter();
|
printf as we know and love it.
Plus print[ln] with the ability to print struct values.
Note for language mavens: if a "..." function passes its argument
to another "..." function, the argument is not wrapped again. This
allows printf to call fprintf without extra manipulation. It's good
but needs to go in the spec.
This code works:
///
package main
import fmt "fmt"
import os "os"
type T struct { s string; a, b int }
func main() {
P := fmt.Printer();
P.printf("%s = %d with float value %.4f\n", "hi there", 7, 123.456);
P.println("hi there", 7, 123.456);
P.fprintf(os.Stdout, "%s = %d with float value %.4f\n", "hi there", 7, 123.456);
P.println(T{"x", 7, 234}, "end of struct", 8, 9);
}
R=rsc
DELTA=28 (7 added, 3 deleted, 18 changed)
OCL=18321
CL=18324
2008-11-02 13:33:02 -07:00
|
|
|
p.doprint(v, false, false);
|
2008-10-24 17:33:29 -06:00
|
|
|
n, error = w.Write(p.buf[0:p.n]);
|
|
|
|
return n, error;
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func Print(v ...) (n int, errno *os.Error) {
|
2009-01-15 14:48:11 -07:00
|
|
|
n, errno = Fprint(os.Stdout, v);
|
2008-10-24 17:33:29 -06:00
|
|
|
return n, errno;
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func Sprint(a ...) string {
|
2009-01-27 16:05:25 -07:00
|
|
|
v := reflect.NewValue(a).(reflect.StructValue);
|
2009-01-16 14:29:43 -07:00
|
|
|
p := newPrinter();
|
2008-11-06 12:56:08 -07:00
|
|
|
p.doprint(v, false, false);
|
2008-10-24 17:33:29 -06:00
|
|
|
s := string(p.buf)[0 : p.n];
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
// These routines end in 'ln', do not take a format string,
|
|
|
|
// always add spaces between operands, and add a newline
|
|
|
|
// after the last operand.
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func Fprintln(w io.Write, a ...) (n int, error *os.Error) {
|
2009-01-27 16:05:25 -07:00
|
|
|
v := reflect.NewValue(a).(reflect.StructValue);
|
2009-01-16 14:29:43 -07:00
|
|
|
p := newPrinter();
|
printf as we know and love it.
Plus print[ln] with the ability to print struct values.
Note for language mavens: if a "..." function passes its argument
to another "..." function, the argument is not wrapped again. This
allows printf to call fprintf without extra manipulation. It's good
but needs to go in the spec.
This code works:
///
package main
import fmt "fmt"
import os "os"
type T struct { s string; a, b int }
func main() {
P := fmt.Printer();
P.printf("%s = %d with float value %.4f\n", "hi there", 7, 123.456);
P.println("hi there", 7, 123.456);
P.fprintf(os.Stdout, "%s = %d with float value %.4f\n", "hi there", 7, 123.456);
P.println(T{"x", 7, 234}, "end of struct", 8, 9);
}
R=rsc
DELTA=28 (7 added, 3 deleted, 18 changed)
OCL=18321
CL=18324
2008-11-02 13:33:02 -07:00
|
|
|
p.doprint(v, true, true);
|
2008-10-24 17:33:29 -06:00
|
|
|
n, error = w.Write(p.buf[0:p.n]);
|
|
|
|
return n, error;
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func Println(v ...) (n int, errno *os.Error) {
|
2009-01-15 14:48:11 -07:00
|
|
|
n, errno = Fprintln(os.Stdout, v);
|
2008-10-24 17:33:29 -06:00
|
|
|
return n, errno;
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func Sprintln(a ...) string {
|
2009-01-27 16:05:25 -07:00
|
|
|
v := reflect.NewValue(a).(reflect.StructValue);
|
2009-01-16 14:29:43 -07:00
|
|
|
p := newPrinter();
|
2008-11-06 12:56:08 -07:00
|
|
|
p.doprint(v, true, true);
|
2008-10-24 17:33:29 -06:00
|
|
|
s := string(p.buf)[0 : p.n];
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2008-11-24 15:51:33 -07:00
|
|
|
|
|
|
|
// Get the i'th arg of the struct value.
|
|
|
|
// If the arg itself is an interface, return a value for
|
|
|
|
// the thing inside the interface, not the interface itself.
|
|
|
|
func getField(v reflect.StructValue, i int) reflect.Value {
|
|
|
|
val := v.Field(i);
|
|
|
|
if val.Kind() == reflect.InterfaceKind {
|
|
|
|
inter := val.(reflect.InterfaceValue).Get();
|
|
|
|
return reflect.NewValue(inter);
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2008-10-24 17:33:29 -06:00
|
|
|
// Getters for the fields of the argument structure.
|
|
|
|
|
2008-11-06 11:40:57 -07:00
|
|
|
func getBool(v reflect.Value) (val bool, ok bool) {
|
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.BoolKind:
|
|
|
|
return v.(reflect.BoolValue).Get(), true;
|
|
|
|
}
|
|
|
|
return false, false
|
|
|
|
}
|
|
|
|
|
2008-10-24 17:33:29 -06:00
|
|
|
func getInt(v reflect.Value) (val int64, signed, ok bool) {
|
|
|
|
switch v.Kind() {
|
2008-10-29 17:05:10 -06:00
|
|
|
case reflect.IntKind:
|
|
|
|
return int64(v.(reflect.IntValue).Get()), true, true;
|
2008-10-24 17:33:29 -06:00
|
|
|
case reflect.Int8Kind:
|
|
|
|
return int64(v.(reflect.Int8Value).Get()), true, true;
|
|
|
|
case reflect.Int16Kind:
|
|
|
|
return int64(v.(reflect.Int16Value).Get()), true, true;
|
|
|
|
case reflect.Int32Kind:
|
|
|
|
return int64(v.(reflect.Int32Value).Get()), true, true;
|
|
|
|
case reflect.Int64Kind:
|
|
|
|
return int64(v.(reflect.Int64Value).Get()), true, true;
|
2008-10-29 17:05:10 -06:00
|
|
|
case reflect.UintKind:
|
|
|
|
return int64(v.(reflect.UintValue).Get()), false, true;
|
2008-10-24 17:33:29 -06:00
|
|
|
case reflect.Uint8Kind:
|
|
|
|
return int64(v.(reflect.Uint8Value).Get()), false, true;
|
|
|
|
case reflect.Uint16Kind:
|
|
|
|
return int64(v.(reflect.Uint16Value).Get()), false, true;
|
|
|
|
case reflect.Uint32Kind:
|
|
|
|
return int64(v.(reflect.Uint32Value).Get()), false, true;
|
|
|
|
case reflect.Uint64Kind:
|
|
|
|
return int64(v.(reflect.Uint64Value).Get()), false, true;
|
2008-12-11 15:41:12 -07:00
|
|
|
case reflect.UintptrKind:
|
|
|
|
return int64(v.(reflect.UintptrValue).Get()), false, true;
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
return 0, false, false;
|
|
|
|
}
|
|
|
|
|
|
|
|
func getString(v reflect.Value) (val string, ok bool) {
|
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.StringKind:
|
|
|
|
return v.(reflect.StringValue).Get(), true;
|
2008-12-19 18:11:54 -07:00
|
|
|
case reflect.ArrayKind:
|
|
|
|
if val, ok := v.Interface().([]byte); ok {
|
|
|
|
return string(val), true;
|
2008-12-19 04:06:19 -07:00
|
|
|
}
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
return "", false;
|
|
|
|
}
|
|
|
|
|
2008-11-17 13:34:03 -07:00
|
|
|
func getFloat32(v reflect.Value) (val float32, ok bool) {
|
2008-10-24 17:33:29 -06:00
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.Float32Kind:
|
2008-11-17 13:34:03 -07:00
|
|
|
return float32(v.(reflect.Float32Value).Get()), true;
|
|
|
|
case reflect.FloatKind:
|
|
|
|
if v.Type().Size()*8 == 32 {
|
|
|
|
return float32(v.(reflect.FloatValue).Get()), true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0.0, false;
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFloat64(v reflect.Value) (val float64, ok bool) {
|
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.FloatKind:
|
|
|
|
if v.Type().Size()*8 == 64 {
|
|
|
|
return float64(v.(reflect.FloatValue).Get()), true;
|
|
|
|
}
|
2008-10-24 17:33:29 -06:00
|
|
|
case reflect.Float64Kind:
|
2008-11-01 17:37:53 -06:00
|
|
|
return float64(v.(reflect.Float64Value).Get()), true;
|
2008-10-24 17:33:29 -06:00
|
|
|
case reflect.Float80Kind:
|
|
|
|
break; // TODO: what to do here?
|
|
|
|
}
|
|
|
|
return 0.0, false;
|
|
|
|
}
|
|
|
|
|
2008-12-09 16:41:21 -07:00
|
|
|
func getPtr(v reflect.Value) (val uintptr, ok bool) {
|
2008-10-24 17:33:29 -06:00
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.PtrKind:
|
2008-12-10 16:55:59 -07:00
|
|
|
return uintptr(v.(reflect.PtrValue).Get()), true;
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
return 0, false;
|
|
|
|
}
|
|
|
|
|
2008-11-13 16:20:52 -07:00
|
|
|
func getArrayPtr(v reflect.Value) (val reflect.ArrayValue, ok bool) {
|
|
|
|
if v.Kind() == reflect.PtrKind {
|
|
|
|
v = v.(reflect.PtrValue).Sub();
|
|
|
|
if v.Kind() == reflect.ArrayKind {
|
|
|
|
return v.(reflect.ArrayValue), true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false;
|
|
|
|
}
|
|
|
|
|
2008-12-22 12:04:17 -07:00
|
|
|
func getArray(v reflect.Value) (val reflect.ArrayValue, ok bool) {
|
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.ArrayKind:
|
|
|
|
return v.(reflect.ArrayValue), true;
|
|
|
|
}
|
|
|
|
return nil, false;
|
|
|
|
}
|
|
|
|
|
2008-11-06 11:40:57 -07:00
|
|
|
// Convert ASCII to integer. n is 0 (and got is false) if no number present.
|
2008-10-24 17:33:29 -06:00
|
|
|
|
|
|
|
func parsenum(s string, start, end int) (n int, got bool, newi int) {
|
|
|
|
if start >= end {
|
|
|
|
return 0, false, end
|
|
|
|
}
|
|
|
|
isnum := false;
|
|
|
|
num := 0;
|
|
|
|
for '0' <= s[start] && s[start] <= '9' {
|
|
|
|
num = num*10 + int(s[start] - '0');
|
|
|
|
start++;
|
|
|
|
isnum = true;
|
|
|
|
}
|
|
|
|
return num, isnum, start;
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (p *pp) printField(field reflect.Value) (was_string bool) {
|
2008-12-11 13:59:49 -07:00
|
|
|
inter := field.Interface();
|
|
|
|
if inter != nil {
|
|
|
|
if stringer, ok := inter.(String); ok {
|
|
|
|
p.addstr(stringer.String());
|
|
|
|
return false; // this value is not a string
|
|
|
|
}
|
2008-11-13 16:20:52 -07:00
|
|
|
}
|
|
|
|
s := "";
|
|
|
|
switch field.Kind() {
|
|
|
|
case reflect.BoolKind:
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_boolean(field.(reflect.BoolValue).Get()).Str();
|
2008-11-13 16:20:52 -07:00
|
|
|
case reflect.IntKind, reflect.Int8Kind, reflect.Int16Kind, reflect.Int32Kind, reflect.Int64Kind:
|
|
|
|
v, signed, ok := getInt(field);
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_d64(v).Str();
|
2008-11-13 16:20:52 -07:00
|
|
|
case reflect.UintKind, reflect.Uint8Kind, reflect.Uint16Kind, reflect.Uint32Kind, reflect.Uint64Kind:
|
|
|
|
v, signed, ok := getInt(field);
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_ud64(uint64(v)).Str();
|
2008-12-11 15:41:12 -07:00
|
|
|
case reflect.UintptrKind:
|
|
|
|
v, signed, ok := getInt(field);
|
|
|
|
p.fmt.sharp = !p.fmt.sharp; // turn 0x on by default
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_ux64(uint64(v)).Str();
|
2008-11-17 13:34:03 -07:00
|
|
|
case reflect.Float32Kind:
|
|
|
|
v, ok := getFloat32(field);
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_g32(v).Str();
|
2008-11-17 13:34:03 -07:00
|
|
|
case reflect.Float64Kind, reflect.Float80Kind:
|
|
|
|
v, ok := getFloat64(field);
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_g64(v).Str();
|
2008-11-17 13:34:03 -07:00
|
|
|
case reflect.FloatKind:
|
|
|
|
if field.Type().Size()*8 == 32 {
|
|
|
|
v, ok := getFloat32(field);
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_g32(v).Str();
|
2008-11-17 13:34:03 -07:00
|
|
|
} else {
|
|
|
|
v, ok := getFloat64(field);
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_g64(v).Str();
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2008-11-13 16:20:52 -07:00
|
|
|
case reflect.StringKind:
|
|
|
|
v, ok := getString(field);
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_s(v).Str();
|
2008-11-13 16:20:52 -07:00
|
|
|
was_string = true;
|
|
|
|
case reflect.PtrKind:
|
2008-11-14 11:42:45 -07:00
|
|
|
if v, ok := getPtr(field); v == 0 {
|
|
|
|
s = "<nil>"
|
|
|
|
} else {
|
2008-12-22 12:04:17 -07:00
|
|
|
// pointer to array? (TODO(r): holdover; delete?)
|
2008-11-14 11:42:45 -07:00
|
|
|
if a, ok := getArrayPtr(field); ok {
|
|
|
|
p.addstr("&[");
|
|
|
|
for i := 0; i < a.Len(); i++ {
|
|
|
|
if i > 0 {
|
|
|
|
p.addstr(" ");
|
|
|
|
}
|
|
|
|
p.printField(a.Elem(i));
|
2008-11-13 16:20:52 -07:00
|
|
|
}
|
2008-11-14 11:42:45 -07:00
|
|
|
p.addstr("]");
|
|
|
|
} else {
|
2008-12-11 15:41:12 -07:00
|
|
|
p.fmt.sharp = !p.fmt.sharp; // turn 0x on by default
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_uX64(uint64(v)).Str();
|
2008-11-13 16:20:52 -07:00
|
|
|
}
|
|
|
|
}
|
2008-12-22 12:04:17 -07:00
|
|
|
case reflect.ArrayKind:
|
|
|
|
if a, ok := getArray(field); ok {
|
|
|
|
p.addstr("[");
|
|
|
|
for i := 0; i < a.Len(); i++ {
|
|
|
|
if i > 0 {
|
|
|
|
p.addstr(" ");
|
|
|
|
}
|
|
|
|
p.printField(a.Elem(i));
|
|
|
|
}
|
|
|
|
p.addstr("]");
|
|
|
|
}
|
2008-11-13 16:20:52 -07:00
|
|
|
case reflect.StructKind:
|
|
|
|
p.add('{');
|
2008-12-11 17:53:33 -07:00
|
|
|
v := field.(reflect.StructValue);
|
|
|
|
t := v.Type().(reflect.StructType);
|
2008-12-16 15:39:29 -07:00
|
|
|
donames := p.fmt.plus;
|
|
|
|
p.fmt.clearflags(); // clear flags for p.printField
|
2008-12-11 17:53:33 -07:00
|
|
|
for i := 0; i < v.Len(); i++ {
|
|
|
|
if i > 0 {
|
|
|
|
p.add(' ')
|
|
|
|
}
|
|
|
|
if donames {
|
|
|
|
if name, typ, tag, off := t.Field(i); name != "" {
|
|
|
|
p.addstr(name);
|
|
|
|
p.add('=');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.printField(getField(v, i));
|
|
|
|
}
|
2008-11-13 16:20:52 -07:00
|
|
|
p.add('}');
|
2008-12-11 13:59:49 -07:00
|
|
|
case reflect.InterfaceKind:
|
|
|
|
inter := field.(reflect.InterfaceValue).Get();
|
|
|
|
if inter == nil {
|
|
|
|
s = "<nil>"
|
|
|
|
} else {
|
|
|
|
// should never happen since a non-nil interface always has a type
|
|
|
|
s = "<non-nil interface>";
|
|
|
|
}
|
2008-11-13 16:20:52 -07:00
|
|
|
default:
|
|
|
|
s = "?" + field.Type().String() + "?";
|
|
|
|
}
|
|
|
|
p.addstr(s);
|
|
|
|
return was_string;
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (p *pp) doprintf(format string, v reflect.StructValue) {
|
2008-10-24 17:33:29 -06:00
|
|
|
p.ensure(len(format)); // a good starting size
|
|
|
|
end := len(format) - 1;
|
|
|
|
fieldnum := 0; // we process one field per non-trivial format
|
|
|
|
for i := 0; i <= end; {
|
2009-01-16 15:58:14 -07:00
|
|
|
c, w := utf8.DecodeRuneInString(format, i);
|
2008-10-24 17:33:29 -06:00
|
|
|
if c != '%' || i == end {
|
|
|
|
p.add(c);
|
|
|
|
i += w;
|
|
|
|
continue;
|
|
|
|
}
|
2008-11-24 15:51:33 -07:00
|
|
|
i++;
|
2008-12-11 17:53:33 -07:00
|
|
|
// flags and widths
|
|
|
|
p.fmt.clearflags();
|
2008-11-24 15:51:33 -07:00
|
|
|
F: for ; i < end; i++ {
|
|
|
|
switch format[i] {
|
|
|
|
case '#':
|
|
|
|
p.fmt.sharp = true;
|
|
|
|
case '0':
|
|
|
|
p.fmt.zero = true;
|
|
|
|
case '+':
|
|
|
|
p.fmt.plus = true;
|
|
|
|
case '-':
|
|
|
|
p.fmt.minus = true;
|
|
|
|
case ' ':
|
|
|
|
p.fmt.space = true;
|
|
|
|
default:
|
|
|
|
break F;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// do we have 20 (width)?
|
2008-12-11 17:53:33 -07:00
|
|
|
p.fmt.wid, p.fmt.wid_present, i = parsenum(format, i, end);
|
2008-11-24 15:51:33 -07:00
|
|
|
// do we have .20 (precision)?
|
2008-10-24 17:33:29 -06:00
|
|
|
if i < end && format[i] == '.' {
|
2008-12-11 17:53:33 -07:00
|
|
|
p.fmt.prec, p.fmt.prec_present, i = parsenum(format, i+1, end);
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
2009-01-16 15:58:14 -07:00
|
|
|
c, w = utf8.DecodeRuneInString(format, i);
|
2008-10-24 17:33:29 -06:00
|
|
|
i += w;
|
|
|
|
// percent is special - absorbs no operand
|
|
|
|
if c == '%' {
|
|
|
|
p.add('%'); // TODO: should we bother with width & prec?
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if fieldnum >= v.Len() { // out of operands
|
2008-11-06 11:40:57 -07:00
|
|
|
p.add('%');
|
|
|
|
p.add(c);
|
|
|
|
p.addstr("(missing)");
|
2008-10-24 17:33:29 -06:00
|
|
|
continue;
|
|
|
|
}
|
2008-11-24 15:51:33 -07:00
|
|
|
field := getField(v, fieldnum);
|
2008-10-24 17:33:29 -06:00
|
|
|
fieldnum++;
|
2008-12-11 13:59:49 -07:00
|
|
|
inter := field.Interface();
|
|
|
|
if inter != nil && c != 'T' { // don't want thing to describe itself if we're asking for its type
|
|
|
|
if formatter, ok := inter.(Format); ok {
|
2008-11-13 16:20:52 -07:00
|
|
|
formatter.Format(p, c);
|
|
|
|
continue;
|
|
|
|
}
|
2008-11-06 11:40:57 -07:00
|
|
|
}
|
2008-10-24 17:33:29 -06:00
|
|
|
s := "";
|
|
|
|
switch c {
|
2008-11-01 17:37:53 -06:00
|
|
|
// bool
|
|
|
|
case 't':
|
2008-11-06 11:40:57 -07:00
|
|
|
if v, ok := getBool(field); ok {
|
|
|
|
if v {
|
|
|
|
s = "true";
|
|
|
|
} else {
|
|
|
|
s = "false";
|
|
|
|
}
|
2008-11-01 17:37:53 -06:00
|
|
|
} else {
|
2008-11-06 11:40:57 -07:00
|
|
|
goto badtype;
|
2008-11-01 17:37:53 -06:00
|
|
|
}
|
|
|
|
|
2008-10-24 17:33:29 -06:00
|
|
|
// int
|
|
|
|
case 'b':
|
|
|
|
if v, signed, ok := getInt(field); ok {
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_b64(uint64(v)).Str() // always unsigned
|
2008-11-17 13:34:03 -07:00
|
|
|
} else if v, ok := getFloat32(field); ok {
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_fb32(v).Str()
|
2008-11-17 13:34:03 -07:00
|
|
|
} else if v, ok := getFloat64(field); ok {
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_fb64(v).Str()
|
2008-10-24 17:33:29 -06:00
|
|
|
} else {
|
2008-11-06 11:40:57 -07:00
|
|
|
goto badtype
|
|
|
|
}
|
|
|
|
case 'c':
|
|
|
|
if v, signed, ok := getInt(field); ok {
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_c(int(v)).Str()
|
2008-11-06 11:40:57 -07:00
|
|
|
} else {
|
|
|
|
goto badtype
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
case 'd':
|
|
|
|
if v, signed, ok := getInt(field); ok {
|
|
|
|
if signed {
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_d64(v).Str()
|
2008-10-24 17:33:29 -06:00
|
|
|
} else {
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_ud64(uint64(v)).Str()
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
} else {
|
2008-11-06 11:40:57 -07:00
|
|
|
goto badtype
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
case 'o':
|
|
|
|
if v, signed, ok := getInt(field); ok {
|
|
|
|
if signed {
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_o64(v).Str()
|
2008-10-24 17:33:29 -06:00
|
|
|
} else {
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_uo64(uint64(v)).Str()
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
} else {
|
2008-11-06 11:40:57 -07:00
|
|
|
goto badtype
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
case 'x':
|
|
|
|
if v, signed, ok := getInt(field); ok {
|
|
|
|
if signed {
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_x64(v).Str()
|
2008-10-24 17:33:29 -06:00
|
|
|
} else {
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_ux64(uint64(v)).Str()
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
2008-11-24 15:51:33 -07:00
|
|
|
} else if v, ok := getString(field); ok {
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_sx(v).Str();
|
2008-11-24 15:51:33 -07:00
|
|
|
} else {
|
|
|
|
goto badtype
|
|
|
|
}
|
|
|
|
case 'X':
|
|
|
|
if v, signed, ok := getInt(field); ok {
|
|
|
|
if signed {
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_X64(v).Str()
|
2008-11-24 15:51:33 -07:00
|
|
|
} else {
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_uX64(uint64(v)).Str()
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
|
|
|
} else if v, ok := getString(field); ok {
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_sX(v).Str();
|
2008-10-24 17:33:29 -06:00
|
|
|
} else {
|
2008-11-06 11:40:57 -07:00
|
|
|
goto badtype
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// float
|
|
|
|
case 'e':
|
2008-11-17 13:34:03 -07:00
|
|
|
if v, ok := getFloat32(field); ok {
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_e32(v).Str()
|
2008-11-17 13:34:03 -07:00
|
|
|
} else if v, ok := getFloat64(field); ok {
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_e64(v).Str()
|
2008-10-24 17:33:29 -06:00
|
|
|
} else {
|
2008-11-06 11:40:57 -07:00
|
|
|
goto badtype
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
case 'f':
|
2008-11-17 13:34:03 -07:00
|
|
|
if v, ok := getFloat32(field); ok {
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_f32(v).Str()
|
2008-11-17 13:34:03 -07:00
|
|
|
} else if v, ok := getFloat64(field); ok {
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_f64(v).Str()
|
2008-10-24 17:33:29 -06:00
|
|
|
} else {
|
2008-11-06 11:40:57 -07:00
|
|
|
goto badtype
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
case 'g':
|
2008-11-17 13:34:03 -07:00
|
|
|
if v, ok := getFloat32(field); ok {
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_g32(v).Str()
|
2008-11-17 13:34:03 -07:00
|
|
|
} else if v, ok := getFloat64(field); ok {
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_g64(v).Str()
|
2008-10-24 17:33:29 -06:00
|
|
|
} else {
|
2008-11-06 11:40:57 -07:00
|
|
|
goto badtype
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// string
|
|
|
|
case 's':
|
|
|
|
if v, ok := getString(field); ok {
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_s(v).Str()
|
2008-10-24 17:33:29 -06:00
|
|
|
} else {
|
2008-11-06 11:40:57 -07:00
|
|
|
goto badtype
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
2008-11-24 15:51:33 -07:00
|
|
|
case 'q':
|
|
|
|
if v, ok := getString(field); ok {
|
2009-01-15 16:40:27 -07:00
|
|
|
s = p.fmt.Fmt_q(v).Str()
|
2008-11-24 15:51:33 -07:00
|
|
|
} else {
|
|
|
|
goto badtype
|
|
|
|
}
|
2008-10-24 17:33:29 -06:00
|
|
|
|
|
|
|
// pointer
|
|
|
|
case 'p':
|
|
|
|
if v, ok := getPtr(field); ok {
|
2008-12-09 18:43:11 -07:00
|
|
|
if v == 0 {
|
2008-11-14 11:42:45 -07:00
|
|
|
s = "<nil>"
|
|
|
|
} else {
|
2009-01-15 16:40:27 -07:00
|
|
|
s = "0x" + p.fmt.Fmt_uX64(uint64(v)).Str()
|
2008-11-14 11:42:45 -07:00
|
|
|
}
|
2008-10-24 17:33:29 -06:00
|
|
|
} else {
|
2008-11-06 11:40:57 -07:00
|
|
|
goto badtype
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
|
2008-11-13 16:20:52 -07:00
|
|
|
// arbitrary value; do your best
|
|
|
|
case 'v':
|
|
|
|
p.printField(field);
|
|
|
|
|
|
|
|
// the value's type
|
|
|
|
case 'T':
|
|
|
|
s = field.Type().String();
|
|
|
|
|
2008-10-24 17:33:29 -06:00
|
|
|
default:
|
2008-11-06 11:40:57 -07:00
|
|
|
badtype:
|
|
|
|
s = "%" + string(c) + "(" + field.Type().String() + ")%";
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
p.addstr(s);
|
|
|
|
}
|
2008-11-06 11:40:57 -07:00
|
|
|
if fieldnum < v.Len() {
|
|
|
|
p.addstr("?(extra ");
|
|
|
|
for ; fieldnum < v.Len(); fieldnum++ {
|
2008-11-24 15:51:33 -07:00
|
|
|
p.addstr(getField(v, fieldnum).Type().String());
|
2008-11-06 11:40:57 -07:00
|
|
|
if fieldnum + 1 < v.Len() {
|
|
|
|
p.addstr(", ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.addstr(")");
|
|
|
|
}
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
|
2009-01-15 16:40:27 -07:00
|
|
|
func (p *pp) doprint(v reflect.StructValue, addspace, addnewline bool) {
|
2008-10-24 17:33:29 -06:00
|
|
|
prev_string := false;
|
|
|
|
for fieldnum := 0; fieldnum < v.Len(); fieldnum++ {
|
|
|
|
// always add spaces if we're doing println
|
2008-11-24 15:51:33 -07:00
|
|
|
field := getField(v, fieldnum);
|
printf as we know and love it.
Plus print[ln] with the ability to print struct values.
Note for language mavens: if a "..." function passes its argument
to another "..." function, the argument is not wrapped again. This
allows printf to call fprintf without extra manipulation. It's good
but needs to go in the spec.
This code works:
///
package main
import fmt "fmt"
import os "os"
type T struct { s string; a, b int }
func main() {
P := fmt.Printer();
P.printf("%s = %d with float value %.4f\n", "hi there", 7, 123.456);
P.println("hi there", 7, 123.456);
P.fprintf(os.Stdout, "%s = %d with float value %.4f\n", "hi there", 7, 123.456);
P.println(T{"x", 7, 234}, "end of struct", 8, 9);
}
R=rsc
DELTA=28 (7 added, 3 deleted, 18 changed)
OCL=18321
CL=18324
2008-11-02 13:33:02 -07:00
|
|
|
if fieldnum > 0 {
|
|
|
|
if addspace {
|
|
|
|
p.add(' ')
|
|
|
|
} else if field.Kind() != reflect.StringKind && !prev_string{
|
|
|
|
// if not doing println, add spaces if neither side is a string
|
2008-10-24 17:33:29 -06:00
|
|
|
p.add(' ')
|
|
|
|
}
|
|
|
|
}
|
2008-11-13 16:20:52 -07:00
|
|
|
was_string := p.printField(field);
|
|
|
|
prev_string = was_string;
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
printf as we know and love it.
Plus print[ln] with the ability to print struct values.
Note for language mavens: if a "..." function passes its argument
to another "..." function, the argument is not wrapped again. This
allows printf to call fprintf without extra manipulation. It's good
but needs to go in the spec.
This code works:
///
package main
import fmt "fmt"
import os "os"
type T struct { s string; a, b int }
func main() {
P := fmt.Printer();
P.printf("%s = %d with float value %.4f\n", "hi there", 7, 123.456);
P.println("hi there", 7, 123.456);
P.fprintf(os.Stdout, "%s = %d with float value %.4f\n", "hi there", 7, 123.456);
P.println(T{"x", 7, 234}, "end of struct", 8, 9);
}
R=rsc
DELTA=28 (7 added, 3 deleted, 18 changed)
OCL=18321
CL=18324
2008-11-02 13:33:02 -07:00
|
|
|
if addnewline {
|
2008-10-24 17:33:29 -06:00
|
|
|
p.add('\n')
|
|
|
|
}
|
|
|
|
}
|