Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
3200b06b
Commit
3200b06b
authored
Nov 04, 2008
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
prepare for recursive printfs
R=rsc DELTA=31 (9 added, 6 deleted, 16 changed) OCL=18470 CL=18472
parent
e2eccf3b
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
22 deletions
+25
-22
print.go
src/lib/fmt/print.go
+25
-22
No files found.
src/lib/fmt/print.go
View file @
3200b06b
...
...
@@ -18,13 +18,13 @@ import (
const
Runeself
=
0x80
const
AllocSize
=
32
export
type
P
struct
{
type
P
struct
{
n
int
;
buf
*
[]
byte
;
fmt
*
Fmt
;
}
export
func
Printer
()
*
P
{
func
Printer
()
*
P
{
p
:=
new
(
P
);
p
.
fmt
=
fmt
.
New
();
return
p
;
...
...
@@ -74,8 +74,11 @@ func (p *P) add(c int) {
}
}
func
(
p
*
P
)
reset
()
{
p
.
n
=
0
;
// 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
;
}
export
type
Writer
interface
{
...
...
@@ -87,46 +90,46 @@ func (p *P) doprint(v reflect.StructValue, addspace, addnewline bool);
// These routines end in 'f' and take a format string.
func
(
p
*
P
)
fprintf
(
w
Writer
,
format
string
,
a
...
)
(
n
int
,
error
*
os
.
Error
)
{
export
func
fprintf
(
w
Writer
,
format
string
,
a
...
)
(
n
int
,
error
*
os
.
Error
)
{
v
:=
reflect
.
NewValue
(
a
)
.
(
reflect
.
PtrValue
)
.
Sub
()
.
(
reflect
.
StructValue
);
p
:=
Printer
();
p
.
doprintf
(
format
,
v
);
n
,
error
=
w
.
Write
(
p
.
buf
[
0
:
p
.
n
]);
p
.
reset
();
return
n
,
error
;
}
func
(
p
*
P
)
printf
(
format
string
,
v
...
)
(
n
int
,
errno
*
os
.
Error
)
{
n
,
errno
=
p
.
fprintf
(
os
.
Stdout
,
format
,
v
);
export
func
printf
(
format
string
,
v
...
)
(
n
int
,
errno
*
os
.
Error
)
{
n
,
errno
=
fprintf
(
os
.
Stdout
,
format
,
v
);
return
n
,
errno
;
}
func
(
p
*
P
)
sprintf
(
format
string
,
v
...
)
string
{
export
func
sprintf
(
format
string
,
v
...
)
string
{
p
:=
Printer
();
p
.
doprintf
(
format
,
reflect
.
NewValue
(
v
)
.
(
reflect
.
StructValue
));
s
:=
string
(
p
.
buf
)[
0
:
p
.
n
];
p
.
reset
();
return
s
;
}
// These routines do not take a format string and add spaces only
// when the operand on neither side is a string.
func
(
p
*
P
)
fprint
(
w
Writer
,
a
...
)
(
n
int
,
error
*
os
.
Error
)
{
export
func
fprint
(
w
Writer
,
a
...
)
(
n
int
,
error
*
os
.
Error
)
{
v
:=
reflect
.
NewValue
(
a
)
.
(
reflect
.
PtrValue
)
.
Sub
()
.
(
reflect
.
StructValue
);
p
:=
Printer
();
p
.
doprint
(
v
,
false
,
false
);
n
,
error
=
w
.
Write
(
p
.
buf
[
0
:
p
.
n
]);
p
.
reset
();
return
n
,
error
;
}
func
(
p
*
P
)
print
(
v
...
)
(
n
int
,
errno
*
os
.
Error
)
{
n
,
errno
=
p
.
fprint
(
os
.
Stdout
,
v
);
export
func
print
(
v
...
)
(
n
int
,
errno
*
os
.
Error
)
{
n
,
errno
=
fprint
(
os
.
Stdout
,
v
);
return
n
,
errno
;
}
func
(
p
*
P
)
sprint
(
v
...
)
string
{
export
func
sprint
(
v
...
)
string
{
p
:=
Printer
();
p
.
doprint
(
reflect
.
NewValue
(
v
)
.
(
reflect
.
StructValue
),
false
,
false
);
s
:=
string
(
p
.
buf
)[
0
:
p
.
n
];
p
.
reset
();
return
s
;
}
...
...
@@ -134,23 +137,23 @@ func (p *P) sprint(v ...) string {
// always add spaces between operands, and add a newline
// after the last operand.
func
(
p
*
P
)
fprintln
(
w
Writer
,
a
...
)
(
n
int
,
error
*
os
.
Error
)
{
export
func
fprintln
(
w
Writer
,
a
...
)
(
n
int
,
error
*
os
.
Error
)
{
v
:=
reflect
.
NewValue
(
a
)
.
(
reflect
.
PtrValue
)
.
Sub
()
.
(
reflect
.
StructValue
);
p
:=
Printer
();
p
.
doprint
(
v
,
true
,
true
);
n
,
error
=
w
.
Write
(
p
.
buf
[
0
:
p
.
n
]);
p
.
reset
();
return
n
,
error
;
}
func
(
p
*
P
)
println
(
v
...
)
(
n
int
,
errno
*
os
.
Error
)
{
n
,
errno
=
p
.
fprintln
(
os
.
Stdout
,
v
);
export
func
println
(
v
...
)
(
n
int
,
errno
*
os
.
Error
)
{
n
,
errno
=
fprintln
(
os
.
Stdout
,
v
);
return
n
,
errno
;
}
func
(
p
*
P
)
sprintln
(
v
...
)
string
{
export
func
sprintln
(
v
...
)
string
{
p
:=
Printer
();
p
.
doprint
(
reflect
.
NewValue
(
v
)
.
(
reflect
.
StructValue
),
true
,
true
);
s
:=
string
(
p
.
buf
)[
0
:
p
.
n
];
p
.
reset
();
return
s
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment