Previously fmt would first obtain a list of map keys
and then look up the value for each key. Since NaNs can
be map keys but cannot be fetched directly, the lookup would
fail and return a zero reflect.Value, which formats as <nil>.
golang.org/cl/33572 added a map iterator to the reflect package
that is used in this CL to retrieve the key and value from
the map and prints the correct value even for keys that are not
equal to themselves.
Fixes#14427
Change-Id: I9e1522959760b3de8b7ecf7a6e67cd603339632a
Reviewed-on: https://go-review.googlesource.com/129777
Reviewed-by: Alan Donovan <adonovan@google.com>
Fix the wording in "strconv" and "fmt" to make explicit
that the "g" and "G" formats remove trailing zeroes.
Fixes#25082
Change-Id: I2e2ad0a98d2ea27a3a8a006a0563b366f7a3b71b
Reviewed-on: https://go-review.googlesource.com/127135
Reviewed-by: Rob Pike <r@golang.org>
The documentation for %v behavior for compound objects uses an ellipsis
to indicate indefinite lenght of elements. This is done for struct
fields as well as elements of arrays and slices. This adds the missing
ellipsis for maps.
Change-Id: Ia433387fe189d2daf5095df32085a541458f00a1
Reviewed-on: https://go-review.googlesource.com/107623
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
In the Scan functions documentation, clarify that for float/complex
literals in scientific notation both decimal (e) and binary (p)
exponents are accepted.
Fixes#24453
Change-Id: Ic6dcdb0c36e088ffb65177038aff7a57ab56b805
Reviewed-on: https://go-review.googlesource.com/107416
Reviewed-by: Rob Pike <r@golang.org>
Hide in the source code instead of in the separate whitelist.
Removes the only printf false positive in the standard library.
Change-Id: I99285e67588c7c93bd56d59ee768a03be7c301e7
Reviewed-on: https://go-review.googlesource.com/74590
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
The complainant is confused by the ambiguity of 'next' in the
phrase 'next operand'. It seems clear enough to me that things
are always read left to right when formatting, but to calm the
waters we add a clarifying parenthetical.
Fixes#22275
Change-Id: I82418c1e987db736f4bee0faa53fe715c9cde8f5
Reviewed-on: https://go-review.googlesource.com/71010
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
This allows io.WriteString to make use of WriteString method
implemented by pp when writing a string to fmt.State.
Fixes#20786
Change-Id: Ice7a92bf303127ad87f05562217fa076f5c589ad
Reviewed-on: https://go-review.googlesource.com/61430
Reviewed-by: Rob Pike <r@golang.org>
This has been supported since Go 1 and there's even a test for it.
The documentation was missing.
Fixes#21409.
Change-Id: I5813488f6a98c1b4506c239e968d43344b91be12
Reviewed-on: https://go-review.googlesource.com/59412
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Went mainly for the ones that make no sense, such as the ones
mid-sentence or after commas.
Change-Id: Ie245d2c19cc7428a06295635cf6a9482ade25ff0
Reviewed-on: https://go-review.googlesource.com/57293
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This parameter is always false. The last occurrence of s.skipSpace(true)
was removed in mid-2015.
While at it, merge skipSpace into SkipSpace, since the latter was just a
wrapper without the parameter.
Found with github.com/mvdan/unparam.
Change-Id: I884ea4036f41234a898d6aeee515211c49b0b435
Reviewed-on: https://go-review.googlesource.com/52890
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Avelino <t@avelino.xxx>
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Change list https://golang.org/cl/20686/ removed the last use
of the variable byteType.
Change-Id: I4ea79095136a49a9d22767b37f48f3404da05056
Reviewed-on: https://go-review.googlesource.com/37197
Run-TryBot: Martin Möhrmann <moehrmann@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Added an alternate form of printing floats and complex values
by specifying the sharp flag.
Output formatted using the the verbs v, e, E, f, F, g and G in
combination with the sharp flag will always include a decimal point.
The alternate form specified by the sharp flag for %g and %G verbs
will not truncate trailing zeros and assume a default precision of 6.
Fixes#18857.
Change-Id: I4d776239e06d7a6a90f2d8556240a359888cb7c3
Reviewed-on: https://go-review.googlesource.com/37051
Reviewed-by: Rob Pike <r@golang.org>
Check for and call the special printing and format methods such as String
at printing depth 0 when printing the concrete value of a reflect.Value.
Fixes: #16015
Change-Id: I23bd2927255b60924e5558321e98dd4a95e11c4c
Reviewed-on: https://go-review.googlesource.com/30753
Reviewed-by: Russ Cox <rsc@golang.org>
Run-TryBot: Martin Möhrmann <martisch@uos.de>
TryBot-Result: Gobot Gobot <gobot@golang.org>
It's the same as %#x not %x.
Just a documentation change; tests already cover it.
Fixes#17322
Change-Id: Ia9db229f781f9042ac5c0bb824e3d7a26fb74ec5
Reviewed-on: https://go-review.googlesource.com/31254
Reviewed-by: Russ Cox <rsc@golang.org>
This CL makes minor adjustment to the handling of spaces and newlines
and then documents the result.
The semantic adjustment mainly concerns the handling of a run of
spaces following a newline in the format, like in "\n ".
Previously, that run of spaces was ignored entirely, leading to paradoxes
like the format "1 \n 2" not matching itself as input.
Now, spaces following a newline in the format match zero or more
spaces following the corresponding newline in the input.
The changes to the test suite show how minor the semantic adjustments are
and how they make the behavior more regular than previously.
This CL also updates the documentation to explain the handling of
spaces more precisely, incorporating the draft from CL 17723 but
describing the newly introduced behavior.
Fixes#13565.
Change-Id: I129666e9ba42de3c28b67f75cb47488e9a4c1867
Reviewed-on: https://go-review.googlesource.com/30611
Reviewed-by: Rob Pike <r@golang.org>
There are no semantic changes here, just tests to establish
the status quo. A followup CL will make some semantic changes,
the (limited) scope of which should be clear from the number of
tests that change.
For #13565.
Change-Id: I960749cf59d4dfe39c324875bcc575096654f883
Reviewed-on: https://go-review.googlesource.com/30610
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
It is better to document what golang does, rather than how it differs
from languages which readers may or may not know.
That the output format is based on the type is basically self-evident
if you consider this just in go terms.
Change-Id: I0223e9b4cb67cc83a9ebe4d424e6c151d7ed600f
Reviewed-on: https://go-review.googlesource.com/28393
Reviewed-by: Rob Pike <r@golang.org>
Change https://golang.org/cl/19895 caused a regression
where the last character in a string would be dropped if it was
accompanied by an io.EOF.
This change fixes the logic so that the last byte is still returned
without a problem.
Fixes#16393
Change-Id: I7a4d0abf761c2c15454136a79e065fe002d736ea
Reviewed-on: https://go-review.googlesource.com/24981
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Ignore the f.zero flag and use spaces for padding instead
when precision is set.
Fixes#15331
Change-Id: I3ac485df24b7bdf4fddf69e3cc17c213c737b5ff
Reviewed-on: https://go-review.googlesource.com/22131
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
This code made sense before fmt switched to using sync.Pool, but a
sync.Pool clears all items on GC, so not reusing something based on
size is just a waste of memory.
Change-Id: I201312b0ee6c572ff3c0ffaf71e42623a160d23f
Reviewed-on: https://go-review.googlesource.com/21480
Reviewed-by: Rob Pike <r@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Simplify the handling of zero padding in fmt_integer and
fmt_float to not require any adjustment of the format flags.
Note that f.zero can only be true when padding to the left
and f.wid is always greater than or equal to 0.
Change-Id: I204b57d103c0eac13d86995992f2b26209196925
Reviewed-on: https://go-review.googlesource.com/21185
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Make verbs b,c,o and U work for any array and slice of integer
type including byte and uint8.
Fix a bug that triggers badverb for []uint8 and []byte type
on the slice/array level instead of on each element like for
any other slice or array type.
Add tests that make sure we do not accidentally alter the
behavior of printing []byte for []byte and []uint8 type
if they are used at the top level when formatting with %#v.
name old time/op new time/op delta
SprintfHexBytes-2 177ns ± 2% 176ns ± 2% ~ (p=0.066 n=48+49)
SprintfBytes-2 330ns ± 1% 329ns ± 1% ~ (p=0.118 n=45+47)
Fixes#13478
Change-Id: I99328a184973ae219bcc0f69c3978cb1ff462888
Reviewed-on: https://go-review.googlesource.com/20686
Run-TryBot: Rob Pike <r@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Merge printReflectValue into printValue. Determine if handleMethods
was already called in printArg by checking if depth is 0. Do not
call handleMethods on depth 0 again in printValue to not introduce
a performance regression. handleMethods is called already in printArg
to not introduce a performance penalty for top-level Stringer,
GoStringer, Errors and Formatters by using reflect.ValueOf on them
just to retrieve them again as interface{} values in printValue.
Clear p.arg in printValue after handleMethods to print the type
of the value inside the reflect.Value when a bad verb is encountered
on the top level instead of printing "reflect.Value=" as the type of
the argument. This also fixes a bug that incorrectly prints the
whole map instead of just the value for a key if the returned value
by the map for the key is an invalid reflect value.
name old time/op new time/op delta
SprintfPadding-2 229ns ± 2% 227ns ± 1% -0.50% (p=0.013 n=20+20)
SprintfEmpty-2 36.4ns ± 6% 37.2ns ±14% ~ (p=0.091 n=18+20)
SprintfString-2 102ns ± 1% 102ns ± 0% ~ (p=0.751 n=20+20)
SprintfTruncateString-2 142ns ± 0% 141ns ± 1% -0.95% (p=0.000 n=16+20)
SprintfQuoteString-2 389ns ± 0% 388ns ± 0% -0.12% (p=0.019 n=20+20)
SprintfInt-2 100ns ± 2% 100ns ± 1% ~ (p=0.188 n=20+15)
SprintfIntInt-2 155ns ± 3% 154ns ± 2% ~ (p=0.092 n=20+20)
SprintfPrefixedInt-2 250ns ± 2% 251ns ± 3% ~ (p=0.559 n=20+20)
SprintfFloat-2 177ns ± 2% 175ns ± 1% -1.30% (p=0.000 n=20+20)
SprintfComplex-2 516ns ± 1% 510ns ± 1% -1.13% (p=0.000 n=19+16)
SprintfBoolean-2 90.9ns ± 3% 90.6ns ± 1% ~ (p=0.193 n=19+19)
SprintfHexString-2 171ns ± 1% 169ns ± 1% -1.44% (p=0.000 n=19+20)
SprintfHexBytes-2 180ns ± 1% 180ns ± 1% ~ (p=0.060 n=19+18)
SprintfBytes-2 330ns ± 1% 329ns ± 1% -0.42% (p=0.003 n=20+20)
SprintfStringer-2 354ns ± 3% 352ns ± 3% ~ (p=0.525 n=20+19)
SprintfStructure-2 804ns ± 3% 776ns ± 2% -3.56% (p=0.000 n=20+20)
FprintInt-2 155ns ± 0% 151ns ± 1% -2.35% (p=0.000 n=19+20)
FprintfBytes-2 169ns ± 0% 170ns ± 1% +0.81% (p=0.000 n=18+19)
FprintIntNoAlloc-2 112ns ± 0% 109ns ± 1% -2.28% (p=0.000 n=20+20)
Change-Id: Ib9a39082ed1be0f1f7499ee6fb6c9530f043e43a
Reviewed-on: https://go-review.googlesource.com/20923
Run-TryBot: Rob Pike <r@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Remove format flag reset from doPrint. Flags will not be set in
doPrint and printArg will not return with flags modified.
Remove the extra arguments addspace and addnewline and split up
doPrint into two simpler and specialized functions.
Change-Id: Ib884d027abfbb31c6f01b008f51d6d76fc0c1a17
Reviewed-on: https://go-review.googlesource.com/21181
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Deduplicate the verb switch for signed and unsigned integer formatting.
Make names of integer related functions consistent
with names of other fmt functions.
Consolidate basic integer tests.
Change-Id: I0c19c24f1c2c06a3b1a4d7d377dcdac3b36bb0f5
Reviewed-on: https://go-review.googlesource.com/20831
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Changes the integer function to restore the original f.zero value
and therefore padding type before returning.
Change-Id: I456449259a3d39bd6d62e110553120c31ec63f23
Reviewed-on: https://go-review.googlesource.com/20512
Reviewed-by: Rob Pike <r@golang.org>
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
handleMethods can format Error() and String() directly as its known
these return strings that can be directly printed using fmtString.
Remove the obsolete depth argument from handleMethods.
Remove the depth argument from printArg since it is only ever
called with depth set to 0. Recursion for formatting complex
arguments is handled only by printValue which keeps track of depth.
Change-Id: I4c4be588751de12ed999e7561a51bc168eb9eb2d
Reviewed-on: https://go-review.googlesource.com/20911
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Remove rewriting of flags before calling formatters.
Change Flag method to directly take plusV and sharpV flags
into account when reporting if plus or sharp flag is set.
Change-Id: Ic3423881ad89e5a5f9fff5ab59e842062394ef6d
Reviewed-on: https://go-review.googlesource.com/20859
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Separate unicode formatting into its own fmt_unicode function.
Remove the fmtUnicode wrapper and the f.unicode and f.uniQuote
flags that are not needed anymore. Remove mangling and restoring
of the precision and sharp flags.
Removes the buffer copy needed for %#U by moving
the character encoding before the number encoding.
Changes the behavior of plus and space flag to have
no effect instead of printing a plus or space before "U+".
Always print at least four digits after "U+"
even if precision is set to less than 4.
Change-Id: If9a0ee79e9eca2c76f06a4e0fdd75d98393899ac
Reviewed-on: https://go-review.googlesource.com/20574
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Use The fmt internal buffer for character formatting instead of
the pp Printer rune decoding buffer.
Uses an uint64 instead of int64 argument to fmt_c and fmt_qc for easier
range checks since no valid runes are represented by negative numbers or
are above 0x10ffff.
Add range checks to fmt_c and fmt_qc to guarantee that a RuneError
character is returned by the functions for any invalid code point
in range uint64. For invalid code points in range utf8.MaxRune
the used utf8 and strconv functions already return a RuneError.
Change-Id: I9772f804dfcd79c3826fa7f6c5ebfbf4b5304a51
Reviewed-on: https://go-review.googlesource.com/20373
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Remove check for %p and %T in printValue.
These verbs are not recursive and are handled already in
printArg which is called on any argument before printValue.
Format the type string for %T directly instead of invoking
the more complex printArg with %s on the type string.
Decouple the %T tests from variables declared in scan_test.go.
Change-Id: Ibd51566bd4cc1a260ce6d052f36382ed05020b48
Reviewed-on: https://go-review.googlesource.com/20622
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>