diff options
author | Ian Lance Taylor <iant@golang.org> | 2020-12-23 09:57:37 -0800 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2020-12-30 15:13:24 -0800 |
commit | cfcbb4227fb20191e04eb8d7766ae6202f526afd (patch) | |
tree | e2effea96f6f204451779f044415c2385e45042b /libgo/go/strings | |
parent | 0696141107d61483f38482b941549959a0d7f613 (diff) | |
download | gcc-cfcbb4227fb20191e04eb8d7766ae6202f526afd.zip gcc-cfcbb4227fb20191e04eb8d7766ae6202f526afd.tar.gz gcc-cfcbb4227fb20191e04eb8d7766ae6202f526afd.tar.bz2 |
libgo: update to Go1.16beta1 release
This does not yet include support for the //go:embed directive added
in this release.
* Makefile.am (check-runtime): Don't create check-runtime-dir.
(mostlyclean-local): Don't remove check-runtime-dir.
(check-go-tool, check-vet): Copy in go.mod and modules.txt.
(check-cgo-test, check-carchive-test): Add go.mod file.
* Makefile.in: Regenerate.
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/280172
Diffstat (limited to 'libgo/go/strings')
-rw-r--r-- | libgo/go/strings/reader.go | 10 | ||||
-rw-r--r-- | libgo/go/strings/reader_test.go | 7 | ||||
-rw-r--r-- | libgo/go/strings/strings.go | 12 | ||||
-rw-r--r-- | libgo/go/strings/strings_test.go | 9 |
4 files changed, 26 insertions, 12 deletions
diff --git a/libgo/go/strings/reader.go b/libgo/go/strings/reader.go index eb2fa11..6f069a6 100644 --- a/libgo/go/strings/reader.go +++ b/libgo/go/strings/reader.go @@ -10,8 +10,8 @@ import ( "unicode/utf8" ) -// A Reader implements the io.Reader, io.ReaderAt, io.Seeker, io.WriterTo, -// io.ByteScanner, and io.RuneScanner interfaces by reading +// A Reader implements the io.Reader, io.ReaderAt, io.ByteReader, io.ByteScanner, +// io.RuneReader, io.RuneScanner, io.Seeker, and io.WriterTo interfaces by reading // from a string. // The zero value for Reader operates like a Reader of an empty string. type Reader struct { @@ -35,6 +35,7 @@ func (r *Reader) Len() int { // to any other method. func (r *Reader) Size() int64 { return int64(len(r.s)) } +// Read implements the io.Reader interface. func (r *Reader) Read(b []byte) (n int, err error) { if r.i >= int64(len(r.s)) { return 0, io.EOF @@ -45,6 +46,7 @@ func (r *Reader) Read(b []byte) (n int, err error) { return } +// ReadAt implements the io.ReaderAt interface. func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) { // cannot modify state - see io.ReaderAt if off < 0 { @@ -60,6 +62,7 @@ func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) { return } +// ReadByte implements the io.ByteReader interface. func (r *Reader) ReadByte() (byte, error) { r.prevRune = -1 if r.i >= int64(len(r.s)) { @@ -70,6 +73,7 @@ func (r *Reader) ReadByte() (byte, error) { return b, nil } +// UnreadByte implements the io.ByteScanner interface. func (r *Reader) UnreadByte() error { if r.i <= 0 { return errors.New("strings.Reader.UnreadByte: at beginning of string") @@ -79,6 +83,7 @@ func (r *Reader) UnreadByte() error { return nil } +// ReadRune implements the io.RuneReader interface. func (r *Reader) ReadRune() (ch rune, size int, err error) { if r.i >= int64(len(r.s)) { r.prevRune = -1 @@ -94,6 +99,7 @@ func (r *Reader) ReadRune() (ch rune, size int, err error) { return } +// UnreadRune implements the io.RuneScanner interface. func (r *Reader) UnreadRune() error { if r.i <= 0 { return errors.New("strings.Reader.UnreadRune: at beginning of string") diff --git a/libgo/go/strings/reader_test.go b/libgo/go/strings/reader_test.go index a4c211d..5adea6f 100644 --- a/libgo/go/strings/reader_test.go +++ b/libgo/go/strings/reader_test.go @@ -8,7 +8,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "strings" "sync" "testing" @@ -162,7 +161,7 @@ func TestWriteTo(t *testing.T) { // tests that Len is affected by reads, but Size is not. func TestReaderLenSize(t *testing.T) { r := strings.NewReader("abc") - io.CopyN(ioutil.Discard, r, 1) + io.CopyN(io.Discard, r, 1) if r.Len() != 2 { t.Errorf("Len = %d; want 2", r.Len()) } @@ -182,7 +181,7 @@ func TestReaderReset(t *testing.T) { if err := r.UnreadRune(); err == nil { t.Errorf("UnreadRune: expected error, got nil") } - buf, err := ioutil.ReadAll(r) + buf, err := io.ReadAll(r) if err != nil { t.Errorf("ReadAll: unexpected error: %v", err) } @@ -228,7 +227,7 @@ func TestReaderZero(t *testing.T) { t.Errorf("UnreadRune: got nil, want error") } - if n, err := (&strings.Reader{}).WriteTo(ioutil.Discard); n != 0 || err != nil { + if n, err := (&strings.Reader{}).WriteTo(io.Discard); n != 0 || err != nil { t.Errorf("WriteTo: got %d, %v; want 0, nil", n, err) } } diff --git a/libgo/go/strings/strings.go b/libgo/go/strings/strings.go index d6f5cea..b429735 100644 --- a/libgo/go/strings/strings.go +++ b/libgo/go/strings/strings.go @@ -934,8 +934,8 @@ func Replace(s, old, new string, n int) string { } // Apply replacements to buffer. - t := make([]byte, len(s)+n*(len(new)-len(old))) - w := 0 + var b Builder + b.Grow(len(s) + n*(len(new)-len(old))) start := 0 for i := 0; i < n; i++ { j := start @@ -947,12 +947,12 @@ func Replace(s, old, new string, n int) string { } else { j += Index(s[start:], old) } - w += copy(t[w:], s[start:j]) - w += copy(t[w:], new) + b.WriteString(s[start:j]) + b.WriteString(new) start = j + len(old) } - w += copy(t[w:], s[start:]) - return string(t[0:w]) + b.WriteString(s[start:]) + return b.String() } // ReplaceAll returns a copy of the string s with all diff --git a/libgo/go/strings/strings_test.go b/libgo/go/strings/strings_test.go index 095e482..c15aca3 100644 --- a/libgo/go/strings/strings_test.go +++ b/libgo/go/strings/strings_test.go @@ -1904,3 +1904,12 @@ func BenchmarkTrimSpace(b *testing.B) { }) } } + +var stringSink string + +func BenchmarkReplaceAll(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + stringSink = ReplaceAll("banana", "a", "<>") + } +} |