]> Cypherpunks repositories - gostls13.git/commitdiff
Removed bytes.Add and bytes.AddByte; we now have 'append'.
authorKyle Consalus <consalus@gmail.com>
Wed, 1 Dec 2010 19:59:13 +0000 (11:59 -0800)
committerRob Pike <r@golang.org>
Wed, 1 Dec 2010 19:59:13 +0000 (11:59 -0800)
Changed all uses of bytes.Add (aside from those testing bytes.Add) to append(a, b...).
Also ran "gofmt -s" and made use of copy([]byte, string) in the fasta benchmark.

R=golang-dev, r, r2
CC=golang-dev
https://golang.org/cl/3302042

doc/effective_go.html
src/pkg/bytes/bytes.go
src/pkg/bytes/bytes_test.go
src/pkg/crypto/tls/conn.go
src/pkg/json/scanner_test.go
src/pkg/json/stream.go
src/pkg/net/textproto/reader.go
src/pkg/regexp/regexp.go
src/pkg/xml/read.go
test/bench/fasta.go

index 8bb04e917637666f95e538800851d62d123d731b..ab21edfbba712ffa23cfa4884d09043ced20a80b 100644 (file)
@@ -794,7 +794,7 @@ func Contents(filename string) (string, os.Error) {
     buf := make([]byte, 100)
     for {
         n, err := f.Read(buf[0:])
-        result = bytes.Add(result, buf[0:n])
+        result = append(result, buf[0:n]...) // append is discussed later.
         if err != nil {
             if err == os.EOF {
                 break
index d0749870ebe2c77b7e97e3af547934bd1157f768..c0937ca3004418fe827aa86c620d81885d5d31ed 100644 (file)
@@ -552,48 +552,6 @@ func TrimSpace(s []byte) []byte {
        return TrimFunc(s, unicode.IsSpace)
 }
 
-// How big to make a byte array when growing.
-// Heuristic: Scale by 50% to give n log n time.
-func resize(n int) int {
-       if n < 16 {
-               n = 16
-       }
-       return n + n/2
-}
-
-// Add appends the contents of t to the end of s and returns the result.
-// If s has enough capacity, it is extended in place; otherwise a
-// new array is allocated and returned.
-func Add(s, t []byte) []byte { // TODO
-       lens := len(s)
-       lent := len(t)
-       if lens+lent <= cap(s) {
-               s = s[0 : lens+lent]
-       } else {
-               news := make([]byte, lens+lent, resize(lens+lent))
-               copy(news, s)
-               s = news
-       }
-       copy(s[lens:lens+lent], t)
-       return s
-}
-
-// AddByte appends byte t to the end of s and returns the result.
-// If s has enough capacity, it is extended in place; otherwise a
-// new array is allocated and returned.
-func AddByte(s []byte, t byte) []byte { // TODO
-       lens := len(s)
-       if lens+1 <= cap(s) {
-               s = s[0 : lens+1]
-       } else {
-               news := make([]byte, lens+1, resize(lens+1))
-               copy(news, s)
-               s = news
-       }
-       s[lens] = t
-       return s
-}
-
 // Runes returns a slice of runes (Unicode code points) equivalent to s.
 func Runes(s []byte) []int {
        t := make([]int, utf8.RuneCount(s))
index 28e70865291e687598801872c142199220cac99b..063686ec5d6e7d01cc28c44b4053bf1da8ef7f8e 100644 (file)
@@ -573,45 +573,6 @@ func TestToLower(t *testing.T) { runStringTests(t, ToLower, "ToLower", lowerTest
 
 func TestTrimSpace(t *testing.T) { runStringTests(t, TrimSpace, "TrimSpace", trimSpaceTests) }
 
-type AddTest struct {
-       s, t string
-       cap  int
-}
-
-var addtests = []AddTest{
-       {"", "", 0},
-       {"a", "", 1},
-       {"a", "b", 1},
-       {"abc", "def", 100},
-}
-
-func TestAdd(t *testing.T) {
-       for _, test := range addtests {
-               b := make([]byte, len(test.s), test.cap)
-               copy(b, test.s)
-               b = Add(b, []byte(test.t))
-               if string(b) != test.s+test.t {
-                       t.Errorf("Add(%q,%q) = %q", test.s, test.t, string(b))
-               }
-       }
-}
-
-func TestAddByte(t *testing.T) {
-       const N = 2e5
-       b := make([]byte, 0)
-       for i := 0; i < N; i++ {
-               b = AddByte(b, byte(i))
-       }
-       if len(b) != N {
-               t.Errorf("AddByte: too small; expected %d got %d", N, len(b))
-       }
-       for i, c := range b {
-               if c != byte(i) {
-                       t.Fatalf("AddByte: b[%d] should be %d is %d", i, c, byte(i))
-               }
-       }
-}
-
 type RepeatTest struct {
        in, out string
        count   int
index b18cda7bbaa7c49818b4cde40be4db1a5578494c..125d0a90866ee2c56f06cc59dc5ef6527f2b0106 100644 (file)
@@ -560,7 +560,7 @@ func (c *Conn) readHandshake() (interface{}, os.Error) {
        // The handshake message unmarshallers
        // expect to be able to keep references to data,
        // so pass in a fresh copy that won't be overwritten.
-       data = bytes.Add(nil, data)
+       data = append([]byte(nil), data...)
 
        if !m.unmarshal(data) {
                c.sendAlert(alertUnexpectedMessage)
index 82d520b63303c856dd4acb3bb7ec328537ae28ed..b90f5811b7ea67dcf9bb1f2198de3513de558f28 100644 (file)
@@ -147,7 +147,7 @@ func TestNextValueBig(t *testing.T) {
                t.Errorf("invalid rest: %d", len(rest))
        }
 
-       item, rest, err = nextValue(bytes.Add(jsonBig, []byte("HELLO WORLD")), &scan)
+       item, rest, err = nextValue(append(jsonBig, []byte("HELLO WORLD")...), &scan)
        if err != nil {
                t.Fatalf("nextValue extra: ", err)
        }
index d4fb346607999c5419bbb1304db541910b0ee729..cb9b16559ed2bb8d2760cf5250a03829fa291fa5 100644 (file)
@@ -5,7 +5,6 @@
 package json
 
 import (
-       "bytes"
        "io"
        "os"
 )
@@ -177,7 +176,7 @@ func (m *RawMessage) UnmarshalJSON(data []byte) os.Error {
        if m == nil {
                return os.NewError("json.RawMessage: UnmarshalJSON on nil pointer")
        }
-       *m = bytes.Add((*m)[0:0], data)
+       *m = append((*m)[0:0], data...)
        return nil
 }
 
index aad25539d4e70aa31dc5f5fdc5d97f3a8db7fb4a..c8e34b7589d1f82d795176c279e1582b846effcd 100644 (file)
@@ -51,8 +51,6 @@ func (r *Reader) ReadLineBytes() ([]byte, os.Error) {
        return line[0:n], err
 }
 
-var space = []byte{' '}
-
 // ReadContinuedLine reads a possibly continued line from r,
 // eliding the final trailing ASCII white space.
 // Lines after the first are considered continuations if they
@@ -132,8 +130,8 @@ func (r *Reader) ReadContinuedLineBytes() ([]byte, os.Error) {
                var cont []byte
                cont, err = r.ReadLineBytes()
                cont = trim(cont)
-               line = bytes.Add(line, space)
-               line = bytes.Add(line, cont)
+               line = append(line, ' ')
+               line = append(line, cont...)
                if err != nil {
                        break
                }
index 80bcb46a9f8db98bd0ad31eb5cca2ee51a953c9e..2d43437783efe9539e5c09445c8315ec7d1324c2 100644 (file)
@@ -675,7 +675,7 @@ Loop:
                        break Loop
                }
                n := utf8.EncodeRune(utf, inst.(*_Char).char)
-               b = bytes.Add(b, utf[0:n])
+               b = append(b, utf[0:n]...)
                i = inst.next().index()
        }
        // point prefixStart instruction to first non-CHAR after prefix
index bbceda6b49b44dc1dd32ea125f8a910633b111b2..1999ebcb884293e4c568f7affcc579936a559b47 100644 (file)
@@ -389,12 +389,12 @@ Loop:
 
                case CharData:
                        if saveData != nil {
-                               data = bytes.Add(data, t)
+                               data = append(data, t...)
                        }
 
                case Comment:
                        if saveComment != nil {
-                               comment = bytes.Add(comment, t)
+                               comment = append(comment, t...)
                        }
                }
        }
index 470bdb3285aec9f12b845b9f9896390b3b3a6677..d13edd5dcfa039fc3cc7bf0ef7c843a578c3f220 100644 (file)
@@ -37,7 +37,6 @@ POSSIBILITY OF SUCH DAMAGE.
 package main
 
 import (
-       "bytes"
        "flag"
        "os"
 )
@@ -49,7 +48,7 @@ var n = flag.Int("n", 1000, "length of result")
 const Line = 60
 
 func Repeat(alu []byte, n int) {
-       buf := bytes.Add(alu, alu)
+       buf := append(alu, alu...)
        off := 0
        for n > 0 {
                m := n
@@ -138,28 +137,28 @@ func main() {
        flag.Parse()
 
        iub := []Acid{
-               Acid{prob: 0.27, sym: 'a'},
-               Acid{prob: 0.12, sym: 'c'},
-               Acid{prob: 0.12, sym: 'g'},
-               Acid{prob: 0.27, sym: 't'},
-               Acid{prob: 0.02, sym: 'B'},
-               Acid{prob: 0.02, sym: 'D'},
-               Acid{prob: 0.02, sym: 'H'},
-               Acid{prob: 0.02, sym: 'K'},
-               Acid{prob: 0.02, sym: 'M'},
-               Acid{prob: 0.02, sym: 'N'},
-               Acid{prob: 0.02, sym: 'R'},
-               Acid{prob: 0.02, sym: 'S'},
-               Acid{prob: 0.02, sym: 'V'},
-               Acid{prob: 0.02, sym: 'W'},
-               Acid{prob: 0.02, sym: 'Y'},
+               {prob: 0.27, sym: 'a'},
+               {prob: 0.12, sym: 'c'},
+               {prob: 0.12, sym: 'g'},
+               {prob: 0.27, sym: 't'},
+               {prob: 0.02, sym: 'B'},
+               {prob: 0.02, sym: 'D'},
+               {prob: 0.02, sym: 'H'},
+               {prob: 0.02, sym: 'K'},
+               {prob: 0.02, sym: 'M'},
+               {prob: 0.02, sym: 'N'},
+               {prob: 0.02, sym: 'R'},
+               {prob: 0.02, sym: 'S'},
+               {prob: 0.02, sym: 'V'},
+               {prob: 0.02, sym: 'W'},
+               {prob: 0.02, sym: 'Y'},
        }
 
        homosapiens := []Acid{
-               Acid{prob: 0.3029549426680, sym: 'a'},
-               Acid{prob: 0.1979883004921, sym: 'c'},
-               Acid{prob: 0.1975473066391, sym: 'g'},
-               Acid{prob: 0.3015094502008, sym: 't'},
+               {prob: 0.3029549426680, sym: 'a'},
+               {prob: 0.1979883004921, sym: 'c'},
+               {prob: 0.1975473066391, sym: 'g'},
+               {prob: 0.3015094502008, sym: 't'},
        }
 
        alu := []byte(
@@ -192,9 +191,7 @@ func (b *buffer) Flush() {
 
 func (b *buffer) WriteString(s string) {
        p := b.NextWrite(len(s))
-       for i := 0; i < len(s); i++ {
-               p[i] = s[i]
-       }
+       copy(p, s)
 }
 
 func (b *buffer) NextWrite(n int) []byte {
@@ -204,6 +201,6 @@ func (b *buffer) NextWrite(n int) []byte {
                p = *b
        }
        out := p[len(p) : len(p)+n]
-       *b = p[0 : len(p)+n]
+       *b = p[:len(p)+n]
        return out
 }