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";
|
|
|
|
"reflect";
|
|
|
|
"os";
|
|
|
|
)
|
|
|
|
|
2008-11-06 11:40:57 -07:00
|
|
|
export type Writer interface {
|
|
|
|
Write(b *[]byte) (ret int, err *os.Error);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Representation of printer state passed to custom formatters.
|
|
|
|
// Provides access to the Writer interface plus information about
|
|
|
|
// the active formatting verb.
|
|
|
|
export type FormatHelper interface {
|
|
|
|
Write(b *[]byte) (ret int, err *os.Error);
|
|
|
|
Width() (wid int, ok bool);
|
|
|
|
Precision() (prec int, ok bool);
|
|
|
|
}
|
|
|
|
|
|
|
|
export type Formatter interface {
|
|
|
|
Format(f FormatHelper, c int);
|
|
|
|
}
|
|
|
|
|
2008-10-24 17:33:29 -06:00
|
|
|
const Runeself = 0x80
|
|
|
|
const AllocSize = 32
|
|
|
|
|
2008-11-04 14:57:21 -07:00
|
|
|
type P struct {
|
2008-10-24 17:33:29 -06:00
|
|
|
n int;
|
|
|
|
buf *[]byte;
|
|
|
|
fmt *Fmt;
|
2008-11-06 11:40:57 -07:00
|
|
|
wid int;
|
|
|
|
wid_ok bool;
|
|
|
|
prec int;
|
|
|
|
prec_ok bool;
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
|
2008-11-04 14:57:21 -07:00
|
|
|
func Printer() *P {
|
2008-10-24 17:33:29 -06:00
|
|
|
p := new(P);
|
|
|
|
p.fmt = fmt.New();
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2008-11-06 11:40:57 -07:00
|
|
|
func (p *P) Width() (wid int, ok bool) {
|
|
|
|
return p.wid, p.wid_ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *P) Precision() (prec int, ok bool) {
|
|
|
|
return p.prec, p.prec_ok
|
|
|
|
}
|
|
|
|
|
2008-10-24 17:33:29 -06:00
|
|
|
func (p *P) ensure(n int) {
|
|
|
|
if p.buf == nil || len(p.buf) < n {
|
|
|
|
newn := AllocSize;
|
|
|
|
if p.buf != nil {
|
|
|
|
newn += len(p.buf);
|
|
|
|
}
|
|
|
|
if newn < n {
|
|
|
|
newn = n + AllocSize
|
|
|
|
}
|
|
|
|
b := new([]byte, newn);
|
|
|
|
for i := 0; i < p.n; i++ {
|
|
|
|
b[i] = p.buf[i];
|
|
|
|
}
|
|
|
|
p.buf = b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *P) addstr(s string) {
|
|
|
|
n := len(s);
|
|
|
|
p.ensure(p.n + n);
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
p.buf[p.n] = s[i];
|
|
|
|
p.n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *P) addbytes(b *[]byte, start, end int) {
|
|
|
|
p.ensure(p.n + end-start);
|
|
|
|
for i := start; i < end; i++ {
|
|
|
|
p.buf[p.n] = b[i];
|
|
|
|
p.n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *P) add(c int) {
|
|
|
|
p.ensure(p.n + 1);
|
|
|
|
if c < Runeself {
|
|
|
|
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.
|
|
|
|
func (p *P) Write(b *[]byte) (ret int, err *os.Error) {
|
|
|
|
p.addbytes(b, 0, len(b));
|
|
|
|
return len(b), nil;
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *P) doprintf(format string, v reflect.StructValue);
|
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
|
|
|
func (p *P) 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.
|
|
|
|
|
2008-11-04 14:57:21 -07:00
|
|
|
export func fprintf(w Writer, format string, a ...) (n int, error *os.Error) {
|
2008-10-24 17:33:29 -06:00
|
|
|
v := reflect.NewValue(a).(reflect.PtrValue).Sub().(reflect.StructValue);
|
2008-11-04 14:57:21 -07:00
|
|
|
p := Printer();
|
2008-10-24 17:33:29 -06:00
|
|
|
p.doprintf(format, v);
|
|
|
|
n, error = w.Write(p.buf[0:p.n]);
|
|
|
|
return n, error;
|
|
|
|
}
|
|
|
|
|
2008-11-04 14:57:21 -07:00
|
|
|
export func printf(format string, v ...) (n int, errno *os.Error) {
|
|
|
|
n, errno = fprintf(os.Stdout, format, v);
|
2008-10-24 17:33:29 -06:00
|
|
|
return n, errno;
|
|
|
|
}
|
|
|
|
|
2008-11-04 14:57:21 -07:00
|
|
|
export func sprintf(format string, v ...) string {
|
|
|
|
p := Printer();
|
2008-10-24 17:33:29 -06:00
|
|
|
p.doprintf(format, reflect.NewValue(v).(reflect.StructValue));
|
|
|
|
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.
|
|
|
|
|
2008-11-04 14:57:21 -07:00
|
|
|
export func fprint(w Writer, a ...) (n int, error *os.Error) {
|
2008-10-24 17:33:29 -06:00
|
|
|
v := reflect.NewValue(a).(reflect.PtrValue).Sub().(reflect.StructValue);
|
2008-11-04 14:57:21 -07:00
|
|
|
p := Printer();
|
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;
|
|
|
|
}
|
|
|
|
|
2008-11-04 14:57:21 -07:00
|
|
|
export func print(v ...) (n int, errno *os.Error) {
|
|
|
|
n, errno = fprint(os.Stdout, v);
|
2008-10-24 17:33:29 -06:00
|
|
|
return n, errno;
|
|
|
|
}
|
|
|
|
|
2008-11-04 14:57:21 -07:00
|
|
|
export func sprint(v ...) string {
|
|
|
|
p := Printer();
|
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(reflect.NewValue(v).(reflect.StructValue), 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.
|
|
|
|
|
2008-11-04 14:57:21 -07:00
|
|
|
export func fprintln(w Writer, a ...) (n int, error *os.Error) {
|
2008-10-24 17:33:29 -06:00
|
|
|
v := reflect.NewValue(a).(reflect.PtrValue).Sub().(reflect.StructValue);
|
2008-11-04 14:57:21 -07:00
|
|
|
p := Printer();
|
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;
|
|
|
|
}
|
|
|
|
|
2008-11-04 14:57:21 -07:00
|
|
|
export func println(v ...) (n int, errno *os.Error) {
|
|
|
|
n, errno = fprintln(os.Stdout, v);
|
2008-10-24 17:33:29 -06:00
|
|
|
return n, errno;
|
|
|
|
}
|
|
|
|
|
2008-11-04 14:57:21 -07:00
|
|
|
export func sprintln(v ...) string {
|
|
|
|
p := Printer();
|
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(reflect.NewValue(v).(reflect.StructValue), true, true);
|
2008-10-24 17:33:29 -06:00
|
|
|
s := string(p.buf)[0 : p.n];
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
return "", false;
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFloat(v reflect.Value) (val float64, ok bool) {
|
|
|
|
switch v.Kind() {
|
2008-10-29 17:05:10 -06:00
|
|
|
case reflect.FloatKind:
|
|
|
|
return float64(v.(reflect.FloatValue).Get()), true;
|
2008-10-24 17:33:29 -06:00
|
|
|
case reflect.Float32Kind:
|
|
|
|
return float64(v.(reflect.Float32Value).Get()), true;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPtr(v reflect.Value) (val uint64, ok bool) {
|
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.PtrKind:
|
|
|
|
return v.(reflect.PtrValue).Get(), true;
|
|
|
|
}
|
|
|
|
return 0, 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
|
|
|
|
}
|
|
|
|
if s[start] == '-' {
|
|
|
|
a, b, c := parsenum(s, start+1, end);
|
|
|
|
if b {
|
|
|
|
return -a, b, c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *P) doprintf(format string, v reflect.StructValue) {
|
|
|
|
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; {
|
|
|
|
c, w := sys.stringtorune(format, i);
|
|
|
|
if c != '%' || i == end {
|
|
|
|
p.add(c);
|
|
|
|
i += w;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// saw % - do we have %20 (width)?
|
2008-11-06 11:40:57 -07:00
|
|
|
p.wid, p.wid_ok, i = parsenum(format, i+1, end);
|
|
|
|
p.prec_ok = false;
|
2008-10-24 17:33:29 -06:00
|
|
|
// do we have %.20 (precision)?
|
|
|
|
if i < end && format[i] == '.' {
|
2008-11-06 11:40:57 -07:00
|
|
|
p.prec, p.prec_ok, i = parsenum(format, i+1, end);
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
c, w = sys.stringtorune(format, i);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
field := v.Field(fieldnum);
|
|
|
|
fieldnum++;
|
2008-11-06 11:40:57 -07:00
|
|
|
if formatter, ok := field.Interface().(Formatter); ok {
|
|
|
|
formatter.Format(p, c);
|
|
|
|
continue;
|
|
|
|
}
|
2008-10-24 17:33:29 -06:00
|
|
|
s := "";
|
2008-11-06 11:40:57 -07:00
|
|
|
if p.wid_ok {
|
|
|
|
p.fmt.w(p.wid);
|
|
|
|
}
|
|
|
|
if p.prec_ok {
|
|
|
|
p.fmt.p(p.prec);
|
|
|
|
}
|
2008-10-24 17:33:29 -06:00
|
|
|
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 {
|
2008-10-29 15:28:19 -06:00
|
|
|
s = p.fmt.b64(uint64(v)).str() // always unsigned
|
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 {
|
|
|
|
s = p.fmt.c(int(v)).str()
|
|
|
|
} else {
|
|
|
|
goto badtype
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
case 'd':
|
|
|
|
if v, signed, ok := getInt(field); ok {
|
|
|
|
if signed {
|
2008-10-29 15:28:19 -06:00
|
|
|
s = p.fmt.d64(v).str()
|
2008-10-24 17:33:29 -06:00
|
|
|
} else {
|
2008-10-29 15:28:19 -06:00
|
|
|
s = p.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 {
|
2008-10-29 15:28:19 -06:00
|
|
|
s = p.fmt.o64(v).str()
|
2008-10-24 17:33:29 -06:00
|
|
|
} else {
|
2008-10-29 15:28:19 -06:00
|
|
|
s = p.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 {
|
2008-10-29 15:28:19 -06:00
|
|
|
s = p.fmt.x64(v).str()
|
2008-10-24 17:33:29 -06:00
|
|
|
} else {
|
2008-10-29 15:28:19 -06:00
|
|
|
s = p.fmt.ux64(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
|
|
|
}
|
|
|
|
|
|
|
|
// float
|
|
|
|
case 'e':
|
|
|
|
if v, ok := getFloat(field); ok {
|
2008-10-29 15:28:19 -06:00
|
|
|
s = p.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':
|
|
|
|
if v, ok := getFloat(field); ok {
|
2008-10-29 15:28:19 -06:00
|
|
|
s = p.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':
|
|
|
|
if v, ok := getFloat(field); ok {
|
2008-10-29 15:28:19 -06:00
|
|
|
s = p.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 {
|
|
|
|
s = p.fmt.s(v).str()
|
|
|
|
} else {
|
2008-11-06 11:40:57 -07:00
|
|
|
goto badtype
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// pointer
|
|
|
|
case 'p':
|
|
|
|
if v, ok := getPtr(field); ok {
|
2008-10-29 15:28:19 -06:00
|
|
|
s = "0x" + p.fmt.uX64(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
|
|
|
}
|
|
|
|
|
|
|
|
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++ {
|
|
|
|
p.addstr(v.Field(fieldnum).Type().String());
|
|
|
|
if fieldnum + 1 < v.Len() {
|
|
|
|
p.addstr(", ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.addstr(")");
|
|
|
|
}
|
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
|
|
|
func (p *P) 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
|
|
|
|
field := v.Field(fieldnum);
|
|
|
|
s := "";
|
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(' ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch field.Kind() {
|
2008-11-01 17:37:53 -06:00
|
|
|
case reflect.BoolKind:
|
|
|
|
s = p.fmt.boolean(field.(reflect.BoolValue).Get()).str();
|
2008-10-29 17:05:10 -06:00
|
|
|
case reflect.IntKind, reflect.Int8Kind, reflect.Int16Kind, reflect.Int32Kind, reflect.Int64Kind:
|
2008-10-24 17:33:29 -06:00
|
|
|
v, signed, ok := getInt(field);
|
2008-10-29 15:28:19 -06:00
|
|
|
s = p.fmt.d64(v).str();
|
2008-10-29 17:05:10 -06:00
|
|
|
case reflect.UintKind, reflect.Uint8Kind, reflect.Uint16Kind, reflect.Uint32Kind, reflect.Uint64Kind:
|
2008-10-24 17:33:29 -06:00
|
|
|
v, signed, ok := getInt(field);
|
2008-10-29 15:28:19 -06:00
|
|
|
s = p.fmt.ud64(uint64(v)).str();
|
2008-10-29 17:05:10 -06:00
|
|
|
case reflect.FloatKind, reflect.Float32Kind, reflect.Float64Kind, reflect.Float80Kind:
|
2008-10-24 17:33:29 -06:00
|
|
|
v, ok := getFloat(field);
|
2008-10-29 15:28:19 -06:00
|
|
|
s = p.fmt.g64(v).str();
|
2008-10-24 17:33:29 -06:00
|
|
|
case reflect.StringKind:
|
|
|
|
v, ok := getString(field);
|
|
|
|
s = p.fmt.s(v).str();
|
|
|
|
case reflect.PtrKind:
|
|
|
|
v, ok := getPtr(field);
|
|
|
|
p.add('0');
|
|
|
|
p.add('x');
|
2008-10-29 15:28:19 -06:00
|
|
|
s = p.fmt.uX64(v).str();
|
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
|
|
|
case reflect.StructKind:
|
|
|
|
p.add('{');
|
|
|
|
p.doprint(field, true, false);
|
|
|
|
p.add('}');
|
2008-10-24 17:33:29 -06:00
|
|
|
default:
|
2008-11-06 11:40:57 -07:00
|
|
|
s = "?" + field.Type().String() + "?";
|
2008-10-24 17:33:29 -06:00
|
|
|
}
|
|
|
|
p.addstr(s);
|
|
|
|
prev_string = field.Kind() == reflect.StringKind;
|
|
|
|
}
|
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')
|
|
|
|
}
|
|
|
|
}
|