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
7b03f2a9
Commit
7b03f2a9
authored
May 25, 2011
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fmt: make %q work for integers, printing a quoted character literal.
R=rsc CC=golang-dev
https://golang.org/cl/4556060
parent
ddcdbd44
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
32 additions
and
0 deletions
+32
-0
doc.go
src/pkg/fmt/doc.go
+1
-0
fmt_test.go
src/pkg/fmt/fmt_test.go
+11
-0
format.go
src/pkg/fmt/format.go
+7
-0
print.go
src/pkg/fmt/print.go
+13
-0
No files found.
src/pkg/fmt/doc.go
View file @
7b03f2a9
...
...
@@ -25,6 +25,7 @@
%c the character represented by the corresponding Unicode code point
%d base 10
%o base 8
%q a single-quoted character literal safely escaped with Go syntax.
%x base 16, with lower-case letters for a-f
%X base 16, with upper-case letters for A-F
%U Unicode format: U+1234; same as "U+%04X"
...
...
src/pkg/fmt/fmt_test.go
View file @
7b03f2a9
...
...
@@ -135,6 +135,17 @@ var fmttests = []struct {
{
"%q"
,
"
\u263a
"
,
`"\u263a"`
},
{
"%q"
,
"
\U0010ffff
"
,
`"\U0010ffff"`
},
// escaped characters
{
"%q"
,
'x'
,
`'x'`
},
{
"%q"
,
0
,
`'\x00'`
},
{
"%q"
,
'\n'
,
`'\n'`
},
{
"%q"
,
'\u1234'
,
`'\u1234'`
},
{
"%q"
,
'\U00012345'
,
`'\U00012345'`
},
{
"%q"
,
int64
(
0x7FFFFFFF
),
`%!q(int64=2147483647)`
},
{
"%q"
,
uint64
(
0xFFFFFFFF
),
`%!q(uint64=4294967295)`
},
{
"%q"
,
'"'
,
`'"'`
},
{
"%q"
,
'\'
'
,
`'\''`
},
// width
{
"%5s"
,
"abc"
,
" abc"
},
{
"%2s"
,
"
\u263a
"
,
"
\u263a
"
},
...
...
src/pkg/fmt/format.go
View file @
7b03f2a9
...
...
@@ -296,6 +296,13 @@ func (f *fmt) fmt_q(s string) {
f
.
padString
(
quoted
)
}
// fmt_qc formats the integer as a single-quoted, escaped Go character constant.
// If the character is not valid Unicode, it will print '\ufffd'.
func
(
f
*
fmt
)
fmt_qc
(
c
int64
)
{
quoted
:=
strconv
.
QuoteRune
(
int
(
c
))
f
.
padString
(
quoted
)
}
// floating-point
func
doPrec
(
f
*
fmt
,
def
int
)
int
{
...
...
src/pkg/fmt/print.go
View file @
7b03f2a9
...
...
@@ -9,6 +9,7 @@ import (
"io"
"os"
"reflect"
"unicode"
"utf8"
)
...
...
@@ -332,6 +333,12 @@ func (p *pp) fmtInt64(v int64, verb int, value interface{}) {
p
.
fmt
.
integer
(
v
,
10
,
signed
,
ldigits
)
case
'o'
:
p
.
fmt
.
integer
(
v
,
8
,
signed
,
ldigits
)
case
'q'
:
if
0
<=
v
&&
v
<=
unicode
.
MaxRune
{
p
.
fmt
.
fmt_qc
(
v
)
}
else
{
p
.
badVerb
(
verb
,
value
)
}
case
'x'
:
p
.
fmt
.
integer
(
v
,
16
,
signed
,
ldigits
)
case
'U'
:
...
...
@@ -385,6 +392,12 @@ func (p *pp) fmtUint64(v uint64, verb int, goSyntax bool, value interface{}) {
}
case
'o'
:
p
.
fmt
.
integer
(
int64
(
v
),
8
,
unsigned
,
ldigits
)
case
'q'
:
if
0
<=
v
&&
v
<=
unicode
.
MaxRune
{
p
.
fmt
.
fmt_qc
(
int64
(
v
))
}
else
{
p
.
badVerb
(
verb
,
value
)
}
case
'x'
:
p
.
fmt
.
integer
(
int64
(
v
),
16
,
unsigned
,
ldigits
)
case
'X'
:
...
...
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