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

122 Commits

Author SHA1 Message Date
Shawn Smith
7054890715 bytes, strings: improve Title test coverage by adding cases with underscore and unicode line separator
R=golang-codereviews, gobot, r
CC=golang-codereviews
https://golang.org/cl/42310045
2013-12-20 23:19:32 -08:00
Daniel Lidén
4e23b69314 strings: Add example function for IndexAny
R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/42310044
2013-12-16 10:50:56 -08:00
Robin Eklind
ab9b2ae38c strings: Add IndexFunc example
R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/42370043
2013-12-16 10:44:23 -08:00
Robin Eklind
7b53e32e0b strings: Update Trim example.
Use an input which better shows that behaviour of the function. Only leading
and trailing runes are trimed, not intermediate ones.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/42390043
2013-12-16 10:40:28 -08:00
Robin Eklind
a6ebc88bac strings: Add FieldsFunc example.
R=golang-dev, dave
CC=golang-dev
https://golang.org/cl/42360043
2013-12-16 09:43:03 -08:00
Brad Fitzpatrick
2d6a13997a strings: fix Replacer bug with prefix matches
singleStringReplacer had a bug where if a string was replaced
at the beginning and no output had yet been produced into the
temp buffer before matching ended, an invalid nil check (used
as a proxy for having matched anything) meant it always
returned its input.

Fixes #6659

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/16880043
2013-10-24 15:51:19 -07:00
Evan Shaw
f033d988b1 bytes, strings: use copy in Repeat
R=golang-dev, dave, bradfitz, adg
CC=golang-dev
https://golang.org/cl/13249043
2013-08-27 09:21:08 +10:00
Pieter Droogendijk
14903f6598 strings: add test for Count
R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/12541050
2013-08-09 12:51:21 -07:00
Brad Fitzpatrick
1104a2afb1 strings: add IndexByte benchmark
Like existing Index, IndexRune, IndexHardN, etc.

R=golang-dev, khr
CC=golang-dev
https://golang.org/cl/12486044
2013-08-06 14:41:07 -07:00
Brad Fitzpatrick
598c78967f strings: use runtime assembly for IndexByte
Fixes #3751

R=golang-dev, khr
CC=golang-dev
https://golang.org/cl/12483043
2013-08-05 15:04:05 -07:00
Brad Fitzpatrick
9742003ffc strings: add IndexByte, for consistency with bytes package
I always forget which package has it.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/12214044
2013-08-01 11:17:26 -07:00
Rob Pike
1b6b1bc66e bytes,strings: remove user name from BUG in comment
R=golang-dev, dave
CC=golang-dev
https://golang.org/cl/7856048
2013-03-19 15:04:18 -07:00
Rob Pike
464257eeff bytes,string: move the BUG to the comment of the function it's about
Avoids printing it every time we ask a question about the package from
the command line.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/7789048
2013-03-15 17:08:07 -07:00
Ewan Chou
4f43201e51 strings: remove allocations in Split(s, "")
BenchmarkSplit1     77984460     24131380  -69.06%

R=golang-dev, rsc, minux.ma, dave, extemporalgenome
CC=golang-dev
https://golang.org/cl/7458043
2013-03-06 15:21:19 -05:00
Rob Pike
1bf66f081f all: fix a few more printf arg bugs found by go vet
R=golang-dev, iant
CC=golang-dev
https://golang.org/cl/7413045
2013-02-28 11:33:08 -08:00
Rob Pike
55ad7b9bfe bufio: new Scanner interface
Add a new, simple interface for scanning (probably textual) data,
based on a new type called Scanner. It does its own internal buffering,
so should be plausibly efficient even without injecting a bufio.Reader.
The format of the input is defined by a "split function", by default
splitting into lines. Other implemented split functions include single
bytes, single runes, and space-separated words.

Here's the loop to scan stdin as a file of lines:

        s := bufio.NewScanner(os.Stdin)
        for s.Scan() {
                fmt.Printf("%s\n", s.Bytes())
        }
        if s.Err() != nil {
                log.Fatal(s.Err())
        }

While we're dealing with spaces, define what space means to strings.Fields.

Fixes #4802.

R=adg, rogpeppe, bradfitz, rsc
CC=golang-dev
https://golang.org/cl/7322088
2013-02-20 12:14:31 -08:00
Donovan Hide
937f91e1da strings: faster Count, Index
Slightly better benchmarks for when string and separator are equivalent and also less branching in inner loops.
benchmark                        old ns/op    new ns/op    delta
BenchmarkGenericNoMatch               3430         3442   +0.35%
BenchmarkGenericMatch1               23590        22855   -3.12%
BenchmarkGenericMatch2              108031       105025   -2.78%
BenchmarkSingleMaxSkipping            2969         2704   -8.93%
BenchmarkSingleLongSuffixFail         2826         2572   -8.99%
BenchmarkSingleMatch                205268       197832   -3.62%
BenchmarkByteByteNoMatch               987          921   -6.69%
BenchmarkByteByteMatch                2014         1749  -13.16%
BenchmarkByteStringMatch              3083         3050   -1.07%
BenchmarkHTMLEscapeNew                 922          915   -0.76%
BenchmarkHTMLEscapeOld                1654         1570   -5.08%
BenchmarkByteByteReplaces            11897        11556   -2.87%
BenchmarkByteByteMap                  4485         4255   -5.13%
BenchmarkIndexRune                     174          121  -30.46%
BenchmarkIndexRuneFastPath              41           41   -0.24%
BenchmarkIndex                          45           44   -0.22%
BenchmarkMapNoChanges                  433          431   -0.46%
BenchmarkIndexHard1                4015336      3316490  -17.40%
BenchmarkIndexHard2                3976254      3395627  -14.60%
BenchmarkIndexHard3                3973158      3378329  -14.97%
BenchmarkCountHard1                4403549      3448512  -21.69%
BenchmarkCountHard2                4387437      3413059  -22.21%
BenchmarkCountHard3                4403891      3382661  -23.19%
BenchmarkIndexTorture                28354        25864   -8.78%
BenchmarkCountTorture                29625        27463   -7.30%
BenchmarkFields                   38752040     39169840   +1.08%
BenchmarkFieldsFunc               38797765     38888060   +0.23%

benchmark                         old MB/s     new MB/s  speedup
BenchmarkSingleMaxSkipping         3367.07      3697.62    1.10x
BenchmarkSingleLongSuffixFail       354.51       389.47    1.10x
BenchmarkSingleMatch                 73.07        75.82    1.04x
BenchmarkFields                      27.06        26.77    0.99x
BenchmarkFieldsFunc                  27.03        26.96    1.00x

R=dave, fullung, remyoudompheng, rsc
CC=golang-dev
https://golang.org/cl/7350045
2013-02-19 10:36:15 -05:00
Rémy Oudompheng
23093f86ee strings: better mean complexity for Count and Index.
The O(n+m) complexity is obtained probabilistically
by using Rabin-Karp algorithm, which provides the needed complexity
unless exceptional collisions occur, without memory allocation.

benchmark                 old ns/op    new ns/op    delta
BenchmarkIndexHard1         6532331      4045886  -38.06%
BenchmarkIndexHard2         8178173      4038975  -50.61%
BenchmarkIndexHard3         6973687      4042591  -42.03%
BenchmarkCountHard1         6270864      4071090  -35.08%
BenchmarkCountHard2         7838039      4072853  -48.04%
BenchmarkCountHard3         6697828      4071964  -39.20%
BenchmarkIndexTorture       2730546        28934  -98.94%
BenchmarkCountTorture       2729622        29064  -98.94%

Fixes #4600.

R=rsc, donovanhide, remyoudompheng
CC=golang-dev
https://golang.org/cl/7314095
2013-02-17 13:07:17 +01:00
Brad Fitzpatrick
e515d80d5d bytes, strings: add TrimPrefix and TrimSuffix
Everybody either gets confused and thinks this is
TrimLeft/TrimRight or does this by hand which gets
repetitive looking.

R=rsc, kevlar
CC=golang-dev
https://golang.org/cl/7239044
2013-02-01 08:41:25 -08:00
Brad Fitzpatrick
c8fa7dcc25 bytes, strings: fix Reader WriteTo return value on 0 bytes copied
Fixes #4421

R=golang-dev, dave, minux.ma, mchaten, rsc
CC=golang-dev
https://golang.org/cl/6855083
2012-11-25 09:04:13 -08:00
Robert Griesemer
465b9c35e5 gofmt: apply gofmt -w src misc
Remove trailing whitespace in comments.
No other changes.

R=r
CC=golang-dev
https://golang.org/cl/6815053
2012-10-30 13:38:01 -07:00
Evan Shaw
eae25d430d bytes, strings: add (*Reader).WriteTo
Fixes #4031.

R=golang-dev, bradfitz, remyoudompheng, r, dave
CC=golang-dev
https://golang.org/cl/6632046
2012-10-12 14:43:50 +11:00
Eric Roshan-Eisner
631a0e71c1 strings: implement a faster single-string Replacer
The string searching is implemented separately so other functions
may make use of it in the future.

benchmark                        old ns/op    new ns/op    delta
BenchmarkSingleMaxSkipping          125889         2474  -98.03%
BenchmarkSingleLongSuffixFail        16252         1996  -87.72%
BenchmarkSingleMatch                260793       136266  -47.75%

benchmark                         old MB/s     new MB/s  speedup
BenchmarkSingleMaxSkipping           79.43      4041.57   50.88x
BenchmarkSingleLongSuffixFail        61.65       501.81    8.14x
BenchmarkSingleMatch                 57.52       110.08    1.91x

R=nigeltao
CC=golang-dev
https://golang.org/cl/6545049
2012-09-28 12:34:18 +10:00
Russ Cox
0e60019a42 bytes, strings: add Fields benchmarks
The performance changes will be a few different CLs.
Start with benchmarks as a baseline.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/6537043
2012-09-18 15:02:08 -04:00
Eric Eisner
0aad3cdc59 strings: implement a faster generic Replacer
This also fixes the semantics of some corner cases with the empty
match. TODOs for genericReplacer in the tests are fixed.

benchmark                  old ns/op    new ns/op    delta
BenchmarkGenericNoMatch        71395         3132  -95.61%
BenchmarkGenericMatch1         75610        20280  -73.18%
BenchmarkGenericMatch2        837995        86725  -89.65%

R=nigeltao, rsc
CC=golang-dev
https://golang.org/cl/6492076
2012-09-17 11:50:15 +10:00
Nigel Tao
6910356ea8 strings: fix NewReplacer(old0, new0, old1, new1, ...) to be consistent
when oldi == oldj.

Benchmark numbers show no substantial change.

R=eric.d.eisner, rogpeppe
CC=golang-dev
https://golang.org/cl/6496104
2012-09-12 10:40:39 +10:00
Nigel Tao
b19c32acce strings: more thorough Replacer tests.
This verifies existing behavior. Some replacements are arguably wrong
(these are marked with TODO) but changing behavior is left for a
follow-up CL.

Also fix that BenchmarkGenericMatch wasn't actually matching anything.

R=rsc, eric.d.eisner
CC=bradfitz, golang-dev
https://golang.org/cl/6488110
2012-09-11 14:40:08 +10:00
Robert Griesemer
f26d61731d go/parser: fix comment grouping (day 1 bug)
Comment groups must end at the end of a line (or the
next non-comment token) if the group started on a line
with non-comment tokens.

This is important for correct computation of "lead"
and "line" comments (Doc and Comment fields in AST nodes).

Without this fix, the "line" comment for F1 in the
following example:

type T struct {
     F1 int // comment1
     // comment2
     F2 int
}

is "// comment1// comment2" rather than just "// comment1".

This bug was present from Day 1 but only visible when
looking at export-filtered ASTs where only comments
associated with AST nodes are printed, and only in rare
cases (e.g, in the case above, if F2 where not exported,
godoc would show "// comment2" anyway because it was
considered part of the "line" comment for F1).

The bug fix is very small (parser.go). The bulk of the
changes are additional test cases (parser_test.go).

The fix exposed a caching bug in go/printer via one of the
existing tests, hence the changes to printer.go.

As an aside, the fix removes the the need for empty lines
before an "// Output" comment for some special cases of
code examples (e.g.: src/pkg/strings/example_test.go, Count
example).

No impact on gofmt formatting of src, misc.

Fixes #3139.

R=rsc
CC=golang-dev
https://golang.org/cl/6209080
2012-05-22 10:04:34 -07:00
Volker Dobler
06e18ca5a3 strings: Rename example to match function name.
R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/5729065
2012-03-05 22:19:51 +11:00
Robert Griesemer
b495e5c538 strings: make Count example show results
Thanks to dr.volker.dobler for tracking this down.

Filed a long-term issue (3142) which may eventually
resolve this problem w/o the need for a manual fix.

R=iant
CC=golang-dev
https://golang.org/cl/5698078
2012-02-27 12:22:10 -08:00
Andrew Gerrand
11e113db57 godoc: make example code more readable with new comment convention
go/doc: move Examples to go/ast
cmd/go: use go/doc to read examples
src/pkg: update examples to use new convention

This is to make whole file examples more readable. When presented as a
complete function, preceding an Example with its output is confusing.
The new convention is to put the expected output in the final comment
of the example, preceded by the string "output:" (case insensitive).

An idiomatic example looks like this:

// This example demonstrates Foo by doing bar and quux.
func ExampleFoo() {
        // example body that does bar and quux

        // Output:
        // example output
}

R=rsc, gri
CC=golang-dev
https://golang.org/cl/5673053
2012-02-16 11:50:28 +11:00
Andrew Gerrand
d6a3650ad7 strings: add Bernardo O'Higgins example
R=r, bradfitz
CC=golang-dev, rogpeppe
https://golang.org/cl/5673049
2012-02-15 13:38:28 +11:00
Brad Fitzpatrick
7127b6fddc bytes,strings: make *Reader implement io.ReaderAt
R=golang-dev, adg, bradfitz, r
CC=golang-dev
https://golang.org/cl/5675053
2012-02-15 12:58:00 +11:00
Brad Fitzpatrick
71c8b82dd1 strings: more examples
R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/5645092
2012-02-13 14:05:57 +11:00
Rob Pike
4c7695126d strings: delete method comments implied by interface satisfaction
Fixes #2957.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/5653053
2012-02-10 14:45:11 +11:00
Brad Fitzpatrick
396170da9b strings: add Seek method to Reader
strings.Reader is already stateful and read-only.

This permits a *Reader with http.ServeContent.

R=golang-dev, r, rsc, rsc
CC=golang-dev
https://golang.org/cl/5639068
2012-02-09 17:28:41 +11:00
Brad Fitzpatrick
10f1b6a074 strings: add Fields example
R=golang-dev, rsc, r
CC=golang-dev
https://golang.org/cl/5629043
2012-02-03 11:17:55 -08:00
Russ Cox
2050a9e478 build: remove Make.pkg, Make.tool
Consequently, remove many package Makefiles,
and shorten the few that remain.

gomake becomes 'go tool make'.

Turn off test phases of run.bash that do not work,
flagged with $BROKEN.  Future CLs will restore these,
but this seemed like a big enough CL already.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/5601057
2012-01-30 23:43:46 -05:00
Rob Pike
6b772462e4 panics: use the new facilities of testing.B instead
Lots of panics go away.
Also fix a name error in html/template.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/5498045
2011-12-20 10:36:25 -08:00
Russ Cox
a250f37cbc update tree for new default type rule
R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/5448091
2011-12-08 22:08:03 -05:00
Christopher Wedgwood
356b8ee26f strings: fix test output
R=rsc, gri
CC=golang-dev
https://golang.org/cl/5445044
2011-11-28 09:51:03 -08:00
Scott Lawrence
0f0c25dccc strings: Add ContainsAny and ContainsRune to correspond to IndexAny etc.
R=golang-dev, r
CC=golang-dev
https://golang.org/cl/5430046
2011-11-23 20:20:14 -08:00
Russ Cox
558e7fc332 various: avoid func compare
R=gri, r, bradfitz
CC=golang-dev
https://golang.org/cl/5371074
2011-11-13 22:57:19 -05:00
Rob Pike
45e3bcb343 renaming_3: gofix -r go1pkgrename src/pkg/[m-z]*
R=rsc
CC=golang-dev
https://golang.org/cl/5345045
2011-11-08 15:41:54 -08:00
Vincent Vanackere
eb1717e035 all: rename os.EOF to io.EOF in various non-code contexts
R=golang-dev, r
CC=golang-dev
https://golang.org/cl/5334050
2011-11-03 14:01:30 -07:00
Russ Cox
eb6929299b src/pkg/[n-z]*: gofix -r error -force=error
R=golang-dev, bsiegert, iant
CC=golang-dev
https://golang.org/cl/5294074
2011-11-01 22:05:34 -04:00
Russ Cox
8f5718176f bytes, strings: use rune
Various rune-based APIs change.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/5306044
2011-10-25 22:22:09 -07:00
Brad Fitzpatrick
bba7396fbd strings: implement a faster byte->string Replacer
This implements a replacer for when all old strings are single
bytes, but new values are not.

BenchmarkHTMLEscapeNew   1000000   1090 ns/op
BenchmarkHTMLEscapeOld   1000000   2049 ns/op

R=rsc
CC=golang-dev
https://golang.org/cl/5176043
2011-10-03 15:19:04 -07:00
Brad Fitzpatrick
f75ff01f44 strings: implement a faster byte->byte Replacer
When all old & new string values are single bytes,
byteReplacer is now used, instead of the generic
algorithm.

BenchmarkGenericMatch       10000  102519 ns/op
BenchmarkByteByteMatch    1000000    2178 ns/op

fast path, when nothing matches:
BenchmarkByteByteNoMatch  1000000    1109 ns/op

comparisons to multiple Replace calls:
BenchmarkByteByteReplaces  100000   16164 ns/op

comparison to strings.Map:
BenchmarkByteByteMap       500000    5454 ns/op

R=rsc
CC=golang-dev
https://golang.org/cl/5175050
2011-10-03 13:12:01 -07:00
Brad Fitzpatrick
7b0f3caa26 strings: add Replacer, NewReplacer
This is just a new API to do many replacements at once.

While the point of this API is to be faster than doing replacements one
at a time, the implementation in this CL has the optimizations removed
and may actually be slower.

Future CLs will bring back & add optimizations.

R=r, rsc, rogpeppe
CC=golang-dev
https://golang.org/cl/5081042
2011-09-28 09:34:26 -07:00