1
0
mirror of https://github.com/golang/go synced 2024-10-04 13:21:22 -06:00
Commit Graph

88 Commits

Author SHA1 Message Date
Shenghou Ma
24e4cd9eb6 fmt: fix scientific notation in docs
Fixes #12340.

Change-Id: I17a8b3711a8593ec60882a0dcadb38f0cc138f4b
Reviewed-on: https://go-review.googlesource.com/13949
Reviewed-by: Rob Pike <r@golang.org>
2015-08-31 18:43:00 +00:00
Rob Pike
b1eec186f2 fmt: in Scanf, %c can scan a space, so don't skip spaces at %c
In short, %c should just give you the next rune, period.
Apparently this is the design. I use the term loosely.

Fixes #12275

Change-Id: I6f30bed442c0e88eac2244d465c7d151b29cf393
Reviewed-on: https://go-review.googlesource.com/13821
Reviewed-by: Andrew Gerrand <adg@golang.org>
2015-08-24 20:25:07 +00:00
Rob Pike
fe3d8d4db0 fmt: don't unread eof scanning %x
When scanning a hex byte at EOF, the code was ungetting the eof,
which backed up the input and caused double-scanning of a byte.

Delete the call to UnreadRune.

This line appeared in 1.5 for some reason; it was not in 1.4 and
should be removed again for 1.5

Fixes #12090.

Change-Id: Iad1ce8e7db8ec26615c5271310f4b0228cca7d78
Reviewed-on: https://go-review.googlesource.com/13461
Reviewed-by: Andrew Gerrand <adg@golang.org>
2015-08-10 03:34:48 +00:00
Rob Pike
a76c1a5c7f fmt: restore padding for %x on byte slices and strings
Also improve the documentation. A prior fix in this release
changed the properties for empty strings and slices, incorrectly.
Previous behavior is now restored and better documented.

Add lots of tests.

The behavior is that when using a string-like format (%s %q %x %X)
a byte slice is equivalent to a string, and printed as a unit. The padding
applies to the entire object. (The space and sharp flags apply
elementwise.)

Fixes #11422.
Fixes #10430.

Change-Id: I758f0521caf71630437e43990ec6d6c9a92655e3
Reviewed-on: https://go-review.googlesource.com/11600
Reviewed-by: Russ Cox <rsc@golang.org>
2015-06-29 07:17:23 +00:00
Todd Neal
4e834cff4f fmt: handle negative width/prec when supplied as an argument
Negative width arguments now left align the way a minus-width in the
format string aligns. The minus in the format string overrides the sign
of the argument as in C.

Precision behavior is modified to include an error if the argument is
negative.  This differs from a negative precision in a format string
which just terminates the format.

Additional checks for large magnitude widths and precisions are added to
make the runtime behavior (failure, but with different error messages),
more consistent between format string specified width/precision and
argument specified width/precision.

Fixes #11376

Change-Id: I8c7ed21088e9c18128a45d4c487c5ab9fafd13ef
Reviewed-on: https://go-review.googlesource.com/11405
Reviewed-by: Rob Pike <r@golang.org>
Run-TryBot: Rob Pike <r@golang.org>
2015-06-25 05:34:02 +00:00
Rob Pike
a1fe3b5046 fmt: scanning widths apply after leading spaces
When scanning with a width, as in %5s, C skips leading spaces
brefore counting the 5 characters. We should do the same.

Reword the documentation about widths to make this clear.

Fixes #9444

Change-Id: I443a6441adcf1c834057ef3977f9116a987a79cd
Reviewed-on: https://go-review.googlesource.com/10997
Reviewed-by: Andrew Gerrand <adg@golang.org>
2015-06-13 04:33:26 +00:00
Rob Pike
57f4b43078 fmt: require newlines to match when scanning with a format
The documentation says that newlines behave like this:

Scan etc.: newlines are spaces.
Scanln etc.: newlines terminate the scan.
Scanf etc.: newlines must match in input and format.

The code did not implement this behavior in all cases,
especially for Scanf. Make it behave:

- Fix the handling of spaces and newlines in ss.Advance.
The code is longer but now behaves as it should.

- Delete the reuse of the current ss in newScanState.
There is really no need, since it's only used in recursive
calls to Scan etc., and the flags are likely wrong. Simpler
just to allocate a new one every time, and likelier to
be correct.

Fixes #10862.

Change-Id: If060ac021017346723b0d62de4e5a305da898f68
Reviewed-on: https://go-review.googlesource.com/10991
Reviewed-by: Andrew Gerrand <adg@golang.org>
2015-06-12 20:14:14 +00:00
Håvard Haugen
70cf7352b4 fmt: fix buffer underflow for negative integers
Allow room for the initial minus sign of negative integers when
computing widths.

Fixes #10945.

Change-Id: I04d80203aaff64611992725d613ec13ed2ae721f
Reviewed-on: https://go-review.googlesource.com/10393
Reviewed-by: Rob Pike <r@golang.org>
2015-06-02 13:55:40 +00:00
Rob Pike
e3a9a08a0b fmt: allow for space and plus flags when computing widths
Fixes #10770.
Fixes #10771.

This time maybe for sure?

Change-Id: I43d6e5fd6846cf58427fec183832d500a932df59
Reviewed-on: https://go-review.googlesource.com/9896
Reviewed-by: Russ Cox <rsc@golang.org>
2015-05-11 18:34:19 +00:00
Josh Bleecher Snyder
e92a7247fa fmt: skip malloc test under race detector
Fixes #10778.

Change-Id: I09aab55dec429ec4a023e5ad591b929563cef0d9
Reviewed-on: https://go-review.googlesource.com/9855
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2015-05-11 17:45:26 +00:00
Rob Pike
c6688b7b1f fmt: fix panic with large precision
The code already handled high widths but not high precisions.
Also make sure it handles the harder cases of %U.

Fixes #10745.

Change-Id: Ib4d394d49a9941eeeaff866dc59d80483e312a98
Reviewed-on: https://go-review.googlesource.com/9769
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-05-07 20:12:39 +00:00
Fabrizio (Misto) Milo
325642ee06 fmt: prevent panic from %.[]
Fixes #10675

Change-Id: Ia057427ce3e81d35f1ba6c354868a0ad6cc9abf2
Reviewed-on: https://go-review.googlesource.com/9636
Reviewed-by: Rob Pike <r@golang.org>
2015-05-05 21:07:38 +00:00
Rob Pike
a63ba85a9f fmt: document that Scanf returns an error the same as Scan
No semantic change.

Fixes #8708.

Change-Id: Ieda04a86a19bb69bfc2519d381a2f025e7cb8279
Reviewed-on: https://go-review.googlesource.com/9740
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-05-05 19:48:50 +00:00
Rob Pike
92715d7780 fmt: change the overflow test for large numbers in verbs
The old one was inferior.

Fixes #10695.

Change-Id: Ia7fb88c9ceb1b10197b77a54f729865385288d98
Reviewed-on: https://go-review.googlesource.com/9709
Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
2015-05-05 18:17:59 +00:00
Rob Pike
660a6825ea fmt: catch overflow in width and prec calculations
Fixes #10674.

Change-Id: If3fae3244d87aeaa70815f499105c264394aa7ad
Reviewed-on: https://go-review.googlesource.com/9657
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-05-04 19:17:05 +00:00
Russ Cox
a2ef54b51e fmt: adjust formatting of invalid reflect.Value, add more tests
Repeat of CL 8951.

Change-Id: I5430e4a9eb5d8b7d0e3963657092bede67439056
Reviewed-on: https://go-review.googlesource.com/9003
Reviewed-by: Rob Pike <r@golang.org>
2015-04-17 14:14:39 +00:00
Brad Fitzpatrick
db1f9cdd3f Revert "fmt: add a few more reflect.Value tests"
This reverts commit 3e6b7f35de.

Change-Id: Icad2b235b9880729fbdf80d7dfd71e56df4b1231
Reviewed-on: https://go-review.googlesource.com/8944
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2015-04-16 16:58:41 +00:00
Russ Cox
3e6b7f35de fmt: add a few more reflect.Value tests
Change-Id: I86530a4fd240f3e056e3277355d2965f6863b852
Reviewed-on: https://go-review.googlesource.com/8951
Reviewed-by: Rob Pike <r@golang.org>
2015-04-16 16:29:58 +00:00
Rob Pike
049b89dc6f fmt: treat reflect.Value specially - as the value it holds
When a reflect.Value is passed to Printf (etc.), fmt called the
String method, which does not disclose its contents. To get the
contents, one could call Value.Interface(), but that is illegal
if the Value is not exported or otherwise forbidden.

This CL improves the situation with a trivial change to the
fmt package: when we see a reflect.Value as an argument,
we treat it exactly as we treat a reflect.Value we make inside
the package. This means that we always print the
contents of the Value as if _that_ was the argument to Printf.

This is arguably a breaking change but I think it is a genuine
improvement and no greater a break than many other tweaks
we have made to formatted output from this package.

Fixes #8965.

Change-Id: Ifc2a4ce3c1134ad5160e101d2196c22f1542faab
Reviewed-on: https://go-review.googlesource.com/8731
Reviewed-by: roger peppe <rogpeppe@gmail.com>
Reviewed-by: Russ Cox <rsc@golang.org>
2015-04-15 15:59:39 +00:00
Rob Pike
57058327c8 fmt: empty byte slices should print nothing in hex
The documentation is clear that formats like %02x applied to a
byte slice are per-element, so the result should be nothing if the
slice is empty. It's not, because the top-level padding routine is called.
It shouldn't be: the loop does the padding for us.

Fixes #10430.

Change-Id: I04ea0e804c0f2e70fff3701e5bf22acc90e890da
Reviewed-on: https://go-review.googlesource.com/8864
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-04-13 20:50:13 +00:00
Martin Möhrmann
15e66f9d01 fmt: improve test coverage of %x and %X format variations for strings
The tests in the basic string section are now covering more code paths
for encoding a string into the hexadecimal representation of its bytes.

Changed the basic string and basic bytes tests so that they mirror each other.

Change-Id: Ib5dc7b33876769965f9aba2ac270040abc4b2451
Reviewed-on: https://go-review.googlesource.com/2611
Reviewed-by: Robert Griesemer <gri@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
2015-04-01 00:55:30 +00:00
Rob Pike
3ee9df799f fmt: document existing behavior of nil receivers
If a method called by fmt triggers a panic, the output usually says
so. However, there is heretofore undocumented special treatment for
a panic caused by formatting a nil value with an Error or String
method: the output is simply "<nil>". Document that behavior.

Change-Id: Id0f79dd0b3487f9d1c74a0856727bba5cc342be4
Reviewed-on: https://go-review.googlesource.com/6410
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-01 18:17:53 +00:00
Dmitry Vyukov
e6fac08146 cmd/gc: allocate buffers for non-escaped strings on stack
Currently we always allocate string buffers in heap.
For example, in the following code we allocate a temp string
just for comparison:

	if string(byteSlice) == "abc" { ... }

This change extends escape analysis to cover []byte->string
conversions and string concatenation. If the result of operations
does not escape, compiler allocates a small buffer
on stack and passes it to slicebytetostring and concatstrings.
Then runtime uses the buffer if the result fits into it.

Size of the buffer is 32 bytes. There is no fundamental theory
behind this number. Just an observation that on std lib
tests/benchmarks frequency of string allocation is inversely
proportional to string length; and there is significant number
of allocations up to length 32.

benchmark                                    old allocs     new allocs     delta
BenchmarkFprintfBytes                        2              1              -50.00%
BenchmarkDecodeComplex128Slice               318            316            -0.63%
BenchmarkDecodeFloat64Slice                  318            316            -0.63%
BenchmarkDecodeInt32Slice                    318            316            -0.63%
BenchmarkDecodeStringSlice                   2318           2316           -0.09%
BenchmarkStripTags                           11             5              -54.55%
BenchmarkDecodeGray                          111            102            -8.11%
BenchmarkDecodeNRGBAGradient                 200            188            -6.00%
BenchmarkDecodeNRGBAOpaque                   165            152            -7.88%
BenchmarkDecodePaletted                      319            309            -3.13%
BenchmarkDecodeRGB                           166            157            -5.42%
BenchmarkDecodeInterlacing                   279            268            -3.94%
BenchmarkGoLookupIP                          153            135            -11.76%
BenchmarkGoLookupIPNoSuchHost                508            466            -8.27%
BenchmarkGoLookupIPWithBrokenNameServer      245            226            -7.76%
BenchmarkClientServerParallel4               62             61             -1.61%
BenchmarkClientServerParallel64              62             61             -1.61%
BenchmarkClientServerParallelTLS4            79             78             -1.27%
BenchmarkClientServerParallelTLS64           112            111            -0.89%

benchmark                                    old ns/op      new ns/op      delta
BenchmarkFprintfBytes                        381            311            -18.37%
BenchmarkStripTags                           2615           2351           -10.10%
BenchmarkDecodeNRGBAGradient                 3715887        3635096        -2.17%
BenchmarkDecodeNRGBAOpaque                   3047645        2928644        -3.90%
BenchmarkGoLookupIP                          153            135            -11.76%
BenchmarkGoLookupIPNoSuchHost                508            466            -8.27%

Change-Id: I9ec01da816945c3329d7be3c7794b520418c3f99
Reviewed-on: https://go-review.googlesource.com/3120
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
2015-01-28 20:12:38 +00:00
Shenghou Ma
e12b1ddc99 fmt: reword the document for [n].
Fixes #9632.

Change-Id: Ic4d7cad8ff62023c1beecd2d62e48eb9258f5306
Reviewed-on: https://go-review.googlesource.com/3013
Reviewed-by: Rob Pike <r@golang.org>
2015-01-19 06:35:21 +00:00
Shenghou Ma
9a5789d499 fmt: fix two typos
Change-Id: I7b65cf3b67bef8950115066d6d12b25cd0a5edfc
Reviewed-on: https://go-review.googlesource.com/2272
Reviewed-by: Rob Pike <r@golang.org>
2015-01-04 23:07:43 +00:00
Rob Pike
f3c85c507b fmt: fix bug in scanning of hex strings
Couldn't handle a hex string terminated by anything
other than spaces. Easy to fix.

Fixes #9124.

Change-Id: I18f89a0bd99a105c9110e1ede641873bf9daf3af
Reviewed-on: https://go-review.googlesource.com/1538
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2014-12-22 04:08:32 +00:00
Rob Pike
3bbc8638d5 fmt: fix one-letter typo in doc.go
Stupid mistake in previous CL.

TBR=rsc
R=rsc
CC=golang-codereviews
https://golang.org/cl/166880043
2014-10-29 06:53:05 -07:00
Rob Pike
c88ba199e2 fmt: fix documentation for %g and %G
It now echoes what strconv.FormatFloat says.

Fixes #9012.

LGTM=rsc
R=golang-codereviews, rsc
CC=golang-codereviews
https://golang.org/cl/169730043
2014-10-28 20:19:03 -07:00
Rob Pike
a0c5adc35c fmt: print &map like &slice and &struct
It was inconsistent.
Also test these better.
Also document the default format for types.
This wasn't written down.

Fixes #8470.

LGTM=iant
R=golang-codereviews, iant
CC=golang-codereviews
https://golang.org/cl/154870043
2014-10-03 20:27:08 -07:00
Rob Pike
a22424567c fmt: part 2 of the great flag rebuild: make %+v work in formatters
Apply a similar transformation to %+v that we did to %#v, making it
a top-level setting separate from the + flag itself. This fixes the
appearance of flags in Formatters and cleans up the code too,
probably making it a little faster.

Fixes #8835.

LGTM=iant
R=golang-codereviews, iant
CC=golang-codereviews
https://golang.org/cl/154820043
2014-10-03 13:23:35 -07:00
Rob Pike
7c8e057ad3 fmt: make the %#v verb a special flag
The %#v verb is special: it says all values below need to print as %#v.
However, for some situations the # flag has other meanings and this
causes some issues, particularly in how Formatters work. Since %#v
dominates all formatting, translate it into actual state of the formatter
and decouple it from the # flag itself within the calculations (although
it must be restored when methods are doing the work.)
The result is cleaner code and correct handling of # for Formatters.
TODO: Apply the same thinking to the + flag in a followup CL.

Also, the wasString return value in handleMethods is always false,
so eliminate it.

Update #8835

LGTM=iant
R=golang-codereviews, iant
CC=golang-codereviews
https://golang.org/cl/146650043
2014-10-02 14:16:58 -07:00
Rob Pike
9f4084278f fmt: fix internal unknownType function
This thing should never be called, but before
151960044 it was being called, incorrectly.
This is now just a precaution but let's pretend it
Fixes #8843
even though that was fixed by 151960044.
The test case was already there and ran, another mystery.

LGTM=rsc
R=rsc
CC=golang-codereviews
https://golang.org/cl/151970043
2014-10-01 21:35:12 +00:00
Rob Pike
54111a5893 fmt: document and fix the handling of precision for strings and byte slices
Previous behavior was undocumented and inconsistent. Now it is documented
and consistent and measures the input size, since that makes more sense
when talking about %q and %x. For %s the change has no effect.

Fixes #8151.

LGTM=iant
R=golang-codereviews, iant
CC=golang-codereviews
https://golang.org/cl/144540044
2014-09-24 14:33:30 -07:00
Rob Pike
892b5074f5 fmt: document that self-recursive data structures can be fatal
Fixes #8241.

LGTM=iant
R=golang-codereviews, iant
CC=golang-codereviews
https://golang.org/cl/144420043
2014-09-22 15:35:25 -07:00
Rob Pike
78b5321e82 fmt: make printing of ints 25-35% faster
Inspired by a remark by Leonard Holz, use constants for division

BenchmarkSprintfEmpty           130           132           +1.54%
BenchmarkSprintfString          438           437           -0.23%
BenchmarkSprintfInt             417           414           -0.72%
BenchmarkSprintfIntInt          663           691           +4.22%
BenchmarkSprintfPrefixedInt     791           774           -2.15%
BenchmarkSprintfFloat           701           686           -2.14%
BenchmarkManyArgs               2584          2469          -4.45%
BenchmarkFprintInt              488           357           -26.84%
BenchmarkFprintIntNoAlloc       402           265           -34.08%
BenchmarkScanInts               1244346       1267574       +1.87%
BenchmarkScanRecursiveInt       1748741       1724138       -1.41%

Update #3463

LGTM=josharian, rsc
R=golang-codereviews, josharian, rsc
CC=golang-codereviews
https://golang.org/cl/144250043
2014-09-22 11:58:15 -07:00
Josh Bleecher Snyder
73a82db1c8 fmt: fix allocation tests
Converting an integer to an interface{} allocates as of CL 130240043.

Fixes #8617.

LGTM=r
R=r
CC=golang-codereviews, khr
https://golang.org/cl/141700043
2014-09-18 09:45:58 -07:00
Rob Pike
eafa4fff52 fmt: fix allocation test
With new interface allocation rules, the old counts were wrong and
so was the commentary.

LGTM=rsc
R=rsc
CC=golang-codereviews
https://golang.org/cl/142760044
2014-09-09 11:45:46 -07:00
Russ Cox
c007ce824d build: move package sources from src/pkg to src
Preparation was in CL 134570043.
This CL contains only the effect of 'hg mv src/pkg/* src'.
For more about the move, see golang.org/s/go14nopkg.
2014-09-08 00:08:51 -04:00