github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/sqlparse/bytes2/buffer.go (about)

     1  /*
     2  Copyright 2017 Google Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package bytes2
    18  
    19  // Buffer implements a subset of the write portion of
    20  // bytes.Buffer, but more efficiently. This is meant to
    21  // be used in very high QPS operations, especially for
    22  // WriteByte, and without abstracting it as a Writer.
    23  // Function signatures contain errors for compatibility,
    24  // but they do not return errors.
    25  type Buffer struct {
    26  	bytes []byte
    27  }
    28  
    29  // NewBuffer is equivalent to bytes.NewBuffer.
    30  func NewBuffer(b []byte) *Buffer {
    31  	return &Buffer{bytes: b}
    32  }
    33  
    34  // Write is equivalent to bytes.Buffer.Write.
    35  func (buf *Buffer) Write(b []byte) (int, error) {
    36  	buf.bytes = append(buf.bytes, b...)
    37  	return len(b), nil
    38  }
    39  
    40  // WriteString is equivalent to bytes.Buffer.WriteString.
    41  func (buf *Buffer) WriteString(s string) (int, error) {
    42  	buf.bytes = append(buf.bytes, s...)
    43  	return len(s), nil
    44  }
    45  
    46  // WriteByte is equivalent to bytes.Buffer.WriteByte.
    47  func (buf *Buffer) WriteByte(b byte) error {
    48  	buf.bytes = append(buf.bytes, b)
    49  	return nil
    50  }
    51  
    52  // Bytes is equivalent to bytes.Buffer.Bytes.
    53  func (buf *Buffer) Bytes() []byte {
    54  	return buf.bytes
    55  }
    56  
    57  // Strings is equivalent to bytes.Buffer.Strings.
    58  func (buf *Buffer) String() string {
    59  	return string(buf.bytes)
    60  }
    61  
    62  // Len is equivalent to bytes.Buffer.Len.
    63  func (buf *Buffer) Len() int {
    64  	return len(buf.bytes)
    65  }