1
0
mirror of https://github.com/golang/go synced 2024-10-05 11:31:22 -06:00
Commit Graph

62 Commits

Author SHA1 Message Date
Martin Möhrmann
bd6d84282a fmt: refactor and unify float and complex formatting
Removes specialized functions for each verb and float/complex size
and replaces them with generic variants fmtFloat and
fmtComplex similar to other generic fmt functions.

Simplifies the complex formatting by relying on fmtFloat
to handle the verb and default precision selection.

Complex imaginary formatting does not need to clear the f.space flag
because the set f.plus flag will force a sign instead of a space.

Sets default precision for %b to -1 (same as %g and %G)
since precision for %b has no affect in strconv.AppendFloat.

Add more tests and group them a bit better.
Use local copies of +Inf,-Inf and NaN instead
of math package functions for testing.

Saves around 8kb in the go binary.

name              old time/op  new time/op  delta
SprintfFloat-2     200ns ± 4%   196ns ± 4%  -1.55%  (p=0.007 n=20+20)
SprintfComplex-2   569ns ± 4%   570ns ± 3%    ~     (p=0.804 n=20+20)

Change-Id: I36d35dab6f835fc2bd2c042ac97705868eb2446f
Reviewed-on: https://go-review.googlesource.com/20252
Reviewed-by: Rob Pike <r@golang.org>
Run-TryBot: Rob Pike <r@golang.org>
2016-03-06 02:18:05 +00:00
Martin Möhrmann
5763476f73 fmt: use fewer allocations for %q string formatting
Reuse the internal buffer and use append versions of
the strconv quote functions to avoid some allocations.

Add more tests.

name                  old time/op    new time/op    delta
SprintfQuoteString-2     486ns ± 2%     416ns ± 2%  -14.42%  (p=0.000 n=20+20)

name                  old allocs/op  new allocs/op  delta
SprintfQuoteString-2      4.00 ± 0%      2.00 ± 0%  -50.00%  (p=0.000 n=20+20)

Change-Id: I63795b51fd95c53c5993ec8e6e99b659941f9f54
Reviewed-on: https://go-review.googlesource.com/20251
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
2016-03-05 22:04:24 +00:00
Martin Möhrmann
00da3a6ec3 fmt: optimize byte slice and array formatting for %v and %d
Instead of calling printArg in fmtBytes to format each byte call
the byte formatting functions directly since it is known each
element is of type byte.

Add more tests for byte slice and array formatting.

name            old time/op  new time/op  delta
SprintfBytes-2   843ns ±16%   417ns ±11%  -50.58%  (p=0.000 n=20+20)

Change-Id: I5b907dbf52091e3de9710b09d67649c76f4c17e9
Reviewed-on: https://go-review.googlesource.com/20176
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
2016-03-05 11:27:40 +00:00
Martin Möhrmann
783741844b fmt: float formatting should not permanently change width
formatFloat should restore the original f.wid value before
returning. Callers should not have to save and restore f.wid.

Fixes: #14642

Change-Id: I531dae15c7997fe8909e2ad1ef7c376654afb030
Reviewed-on: https://go-review.googlesource.com/20179
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
2016-03-05 03:01:22 +00:00
Martin Möhrmann
033e3e106e fmt: optimize %x and %X formatting for byte slices and strings
No extra buffering is needed to save the encoding
since the left padding can be computed and written out
before the encoding is generated.

Add extra tests to both string and byte slice formatting.

name                old time/op  new time/op  delta
SprintfHexString-2   410ns ± 3%   194ns ± 3%  -52.60%  (p=0.000 n=20+19)
SprintfHexBytes-2    431ns ± 3%   202ns ± 2%  -53.13%  (p=0.000 n=18+20)

Change-Id: Ibca4316427c89f834e4faee61614493c7eedb42b
Reviewed-on: https://go-review.googlesource.com/20097
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
2016-03-03 08:25:28 +00:00
Martin Möhrmann
83765d135f fmt: optimize string truncation
Count only the runes up to the requested precision
to decide where to truncate a string.

Change the loop within truncate to need fewer jumps.

name                     old time/op  new time/op  delta
SprintfTruncateString-2   188ns ± 3%   155ns ± 3%  -17.43%  (p=0.000 n=20+20)

Change-Id: I17ca9fc0bb8bf7648599df48e4785251bbc31e99
Reviewed-on: https://go-review.googlesource.com/20098
Reviewed-by: Rob Pike <r@golang.org>
2016-03-02 20:51:38 +00:00
Brad Fitzpatrick
5fea2ccc77 all: single space after period.
The tree's pretty inconsistent about single space vs double space
after a period in documentation. Make it consistently a single space,
per earlier decisions. This means contributors won't be confused by
misleading precedence.

This CL doesn't use go/doc to parse. It only addresses // comments.
It was generated with:

$ perl -i -npe 's,^(\s*// .+[a-z]\.)  +([A-Z]),$1 $2,' $(git grep -l -E '^\s*//(.+\.)  +([A-Z])')
$ go test go/doc -update

Change-Id: Iccdb99c37c797ef1f804a94b22ba5ee4b500c4f7
Reviewed-on: https://go-review.googlesource.com/20022
Reviewed-by: Rob Pike <r@golang.org>
Reviewed-by: Dave Day <djd@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-03-02 00:13:47 +00:00
Brad Fitzpatrick
519474451a all: make copyright headers consistent with one space after period
This is a subset of https://golang.org/cl/20022 with only the copyright
header lines, so the next CL will be smaller and more reviewable.

Go policy has been single space after periods in comments for some time.

The copyright header template at:

    https://golang.org/doc/contribute.html#copyright

also uses a single space.

Make them all consistent.

Change-Id: Icc26c6b8495c3820da6b171ca96a74701b4a01b0
Reviewed-on: https://go-review.googlesource.com/20111
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-03-01 23:34:33 +00:00
Martin Möhrmann
33c0ef2de3 fmt: make identification of string arguments consistent
Use only reflect.TypeOf to detect if argument is a string.

The wasString return is only needed in doPrint with the 'v' verb.
This type of string detection is handled correctly by reflect.TypeOf
which is used already in doPrint for identifying a string argument.

Remove now obsolete wasString computations and return values.

Change-Id: Iea2de7ac0f5c536a53eec63f7e679d628f5af8dc
Reviewed-on: https://go-review.googlesource.com/19976
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
2016-03-01 07:36:06 +00:00
Brad Fitzpatrick
351c15f1ce all: remove public named return values when useless
Named returned values should only be used on public funcs and methods
when it contributes to the documentation.

Named return values should not be used if they're only saving the
programmer a few lines of code inside the body of the function,
especially if that means there's stutter in the documentation or it
was only there so the programmer could use a naked return
statement. (Naked returns should not be used except in very small
functions)

This change is a manual audit & cleanup of public func signatures.

Signatures were not changed if:

* the func was private (wouldn't be in public godoc)
* the documentation referenced it
* the named return value was an interesting name. (i.e. it wasn't
  simply stutter, repeating the name of the type)

There should be no changes in behavior. (At least: none intended)

Change-Id: I3472ef49619678fe786e5e0994bdf2d9de76d109
Reviewed-on: https://go-review.googlesource.com/20024
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Andrew Gerrand <adg@golang.org>
2016-02-29 03:31:19 +00:00
Martin Möhrmann
75cc05fa55 fmt: fix formatting of numbers with f.space and f.plus specified
Do not replace the sign in front of a number with a space if both
f.space and f.plus are both specified for number formatting.
This was already the case for integers but not for floats
and complex numbers.

Updates: #14543.

Change-Id: I07ddeb505003db84a8a7d2c743dc19fc427a00bd
Reviewed-on: https://go-review.googlesource.com/19974
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
2016-02-29 00:17:07 +00:00
Martin Möhrmann
7da4ceddd0 fmt: simplify buffer write methods and adjust calls to them
Once upon a time fmt did use bytes.Buffer for its buffer.
The buffer write methods still mimic the bytes.Buffer signatures.
The current code depends on manipulating the buffer []bytes array directly
which makes going back to bytes.Buffer by only changing the type of buffer
impossible. Since type buffer is not exported the methods can be simplified
to the needs of fmt. This saves space and avoids unnecessary overhead.

Use WriteString instead of Write for known inputs since
WriteString is faster than Write to append the same data.
This also saves space in the binary.

Remove the add method from Printer and depending on the data to be written
use WriteRune or WriteByte directly instead.

In total makes the go binary around 4 kilobyte smaller.

name                  old time/op  new time/op  delta
SprintfEmpty-2        24.1ns ± 3%  23.8ns ± 1%  -1.14%  (p=0.000 n=20+20)
SprintfString-2        114ns ± 2%   114ns ± 4%    ~     (p=0.558 n=20+19)
SprintfInt-2           116ns ± 9%   118ns ± 7%    ~     (p=0.086 n=20+20)
SprintfIntInt-2        195ns ± 6%   193ns ± 5%    ~     (p=0.345 n=20+19)
SprintfPrefixedInt-2   251ns ±16%   241ns ± 9%  -3.69%  (p=0.024 n=20+19)
SprintfFloat-2         203ns ± 4%   205ns ± 5%    ~     (p=0.153 n=20+20)
SprintfBoolean-2       101ns ± 7%    96ns ±11%  -5.23%  (p=0.005 n=19+20)
ManyArgs-2             651ns ± 7%   628ns ± 7%  -3.44%  (p=0.002 n=20+20)
FprintInt-2            164ns ± 2%   158ns ± 2%  -3.62%  (p=0.000 n=20+18)
FprintfBytes-2         215ns ± 1%   216ns ± 1%  +0.58%  (p=0.000 n=20+20)
FprintIntNoAlloc-2     115ns ± 0%   112ns ± 0%  -2.61%  (p=0.000 n=20+20)
ScanInts-2             700µs ± 0%   702µs ± 1%  +0.38%  (p=0.000 n=18+20)
ScanRecursiveInt-2    82.7ms ± 0%  82.7ms ± 0%    ~     (p=0.820 n=20+20)

Change-Id: I0409eb170b8a26d9f4eb271f6292e5d39faf2d8b
Reviewed-on: https://go-review.googlesource.com/19955
Reviewed-by: Rob Pike <r@golang.org>
2016-02-27 21:12:19 +00:00
Martin Möhrmann
abcad1e59d fmt: change padding functions to avoid package init
Move the decision if zero padding is allowed to doPrintf
where the other formatting decisions are made.

Removes some dead code for negative f.wid that was never used
due to f.wid always being positive and f.minus deciding if left
or right padding should be used.

New padding code writes directly into the buffer and is as fast
as the old version but avoids the cost of needing package init.

name              old time/op  new time/op  delta
SprintfPadding-2   246ns ± 5%   245ns ± 4%   ~     (p=0.345 n=50+47)

Change-Id: I7dfddbac8e328f4ef0cdee8fafc0d06c784b2711
Reviewed-on: https://go-review.googlesource.com/19957
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
2016-02-27 06:59:39 +00:00
Martin Möhrmann
6520da6ed5 fmt: use public io.RuneScanner interface for ScanState reader
All io.Reader that are passed to newScanState in all the standard
library tests that implement io.RuneReader also implement io.RuneScanner.

Do not check on each call ScanState's UnreadRune that the used RuneReader
also implements the UnreadRune method by using a private interface.
Instead require the used Reader to implement the public RuneScanner
interface.

The extra implementation logic for UnreadRune is removed from ScanState.
Instead the readRune wrapper is extended to implement UnreadRune for the
RuneScanner interface. If the Reader passed to newScanstate does not
implement RuneScanner the readRune wrapper is used to implement the
missing functionality.

Note that a RuneReader that does not implement RuneScanner will also
be wrapped by runeRead which was not the case before.
Performance with the readRune wrapper is better than without before.

Add benchmark to compare performance with and without using the
readRune wrapper.

name                             old time/op  new time/op  delta
ScanInts-2                        704µs ± 0%   615µs ± 1%  -12.73%  (p=0.000 n=20+20)
ScanRecursiveInt-2               82.6ms ± 0%  51.4ms ± 0%  -37.71%  (p=0.000 n=20+20)
ScanRecursiveIntReaderWrapper-2  85.1ms ± 0%  52.4ms ± 0%  -38.36%  (p=0.000 n=20+20)

Change-Id: I8c6e85db9b87a8171caab12f020b6e256b498e81
Reviewed-on: https://go-review.googlesource.com/19895
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
2016-02-26 23:46:51 +00:00
Martin Möhrmann
5dc053b9de fmt: fix zero padding for NaN
Makes zero padding of NaN and infinities consistent
by using spaces instead of zeroes to pad NaN.
Adds more tests for NaN formatting.

Fixes #14421

Change-Id: Ia20f8e878cc81ac72a744ec10d65e84b94e09c6a
Reviewed-on: https://go-review.googlesource.com/19723
Reviewed-by: Rob Pike <r@golang.org>
2016-02-21 12:04:21 +00:00
Martin Möhrmann
5a9c128a03 fmt: remove math package dependency and avoid float operations
Remove floating point comparisons and rely only on the information
directly provided by appendFloat.
Make restoring the zero padding flag explicit instead of using a defer.
Rearrange some case distinctions to remove duplicated code.
Add more test cases for zero padded floating point numbers with sign.

benchmark                   old ns/op     new ns/op     delta
BenchmarkSprintfFloat-4     187           180           -3.74%

Change-Id: Ifa2ae85257909f40b1b18118c92b516933271729
Reviewed-on: https://go-review.googlesource.com/19721
Reviewed-by: Rob Pike <r@golang.org>
2016-02-19 22:55:30 +00:00
Nathan VanBenschoten
b04f3b06ec all: replace strings.Index with strings.Contains where possible
Change-Id: Ia613f1c37bfce800ece0533a5326fca91d99a66a
Reviewed-on: https://go-review.googlesource.com/18120
Reviewed-by: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
2016-02-19 01:06:05 +00:00
Russ Cox
dbaf5010b3 Revert "fmt: check newline in the end of input"
This change can break real code. There are other newline-related bugs in this code, and fixing them will also break real code. If we're going to break real code, let's fix all the bugs together and just break things once.

This reverts commit 8331f19d97.

Change-Id: Ie4b3022f3a305c3e1f78cc208e50beed212608e6
Reviewed-on: https://go-review.googlesource.com/17724
Reviewed-by: Rob Pike <r@golang.org>
2015-12-11 06:35:20 +00:00
Dmitry Vyukov
7b767f4e52 internal/race: add package
Factor out duplicated race thunks from sync, syscall net
and fmt packages into a separate package and use it.

Fixes #8593

Change-Id: I156869c50946277809f6b509463752e7f7d28cdb
Reviewed-on: https://go-review.googlesource.com/14870
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Dmitry Vyukov <dvyukov@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-11-26 16:50:31 +00:00
Nodir Turakulov
8331f19d97 fmt: check newline in the end of input
Sscanf doc says:
Newlines in the input must match newlines in the format.

However Sscanf didn't check newline in the end of input (EOF).
A test for the case is broken.

* check newline in EOF
* fix the test
* slightly simplify ss.doScanf

Fixes #12788

Change-Id: Iaf6b7d81324a72e557543ac22ecea5cecb72e0d6
Reviewed-on: https://go-review.googlesource.com/16165
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
2015-11-23 21:11:37 +00:00
Rob Pike
29499858bf fmt: give correct error for % at end of string when scanning
Previously it said, "bad verb %% for ...", which is not only wrong,
it's ironic as the fix is to use %% rather than % at the end of the
string. Diagnose the case where a simple % is at EOF.

If there's anything after the percent, the error is already good
but this CL also puts quotes around the verb designation ('%d' etc.)
to make it even clearer, especially when there is a space involved.

Fixes #12315.

Change-Id: I31d30659965e940d0bd9ce92a475aab3e2369ef0
Reviewed-on: https://go-review.googlesource.com/17150
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-11-20 21:50:54 +00:00
Thorben Krueger
26fe24cbe2 fmt: Add support for capital '%X' format verb for scanning
For printing, the format verb '%X' results in a capitalized
hex-representation of the formatted value. Conversely, using
'%X' in a Scanf function should scan a hex-representation
into the given interface{}. The existing implementation
however only supports '%X' for scanning hex values into
integers; strings or byte slices remain empty. On the other
hand, lower-case '%x' supports strings and byte slices just
fine. This is merely an oversight, which this commit fixes.
(Additional tests also included.)

    Fixes #12940

Change-Id: I178a7f615bae950dfc014ca8c0a038448cf0452a
Reviewed-on: https://go-review.googlesource.com/15689
Reviewed-by: Andrew Gerrand <adg@golang.org>
2015-10-20 01:08:54 +00:00
Nodir Turakulov
1da6b2ea19 fmt: clarify reflect.Value printing
fmt docs say:

  If the operand is a reflect.Value, the concrete value it
  holds is printed as if it was the operand.

It implies recursive application of this rule, which is not the case.
Clarify the docs.

Change-Id: I019277c7c6439095bab83e5536aa06403638aa51
Reviewed-on: https://go-review.googlesource.com/15952
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-10-19 18:29:36 +00:00
Rob Pike
a00cec90ca fmt: allow any type in a format's width argument
The construction
	fmt.Printf("%*d", n, 4)
reads the argument n as a width specifier to use when printing 4.
Until now, only strict int type was accepted here and it couldn't
be fixed because the fix, using reflection, broke escape analysis
and added an extra allocation in every Printf call, even those that
do not use this feature.

The compiler has been fixed, although I am not sure when exactly,
so let's fix Printf and then write

Fixes #10732.

Change-Id: I79cf0c4fadd876265aa39d3cb62867247b36ab65
Reviewed-on: https://go-review.googlesource.com/14491
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-09-10 20:53:22 +00:00
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