github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/stdlibs/strings/builder.gno (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package strings
     6  
     7  import (
     8  	"unicode/utf8"
     9  )
    10  
    11  // A Builder is used to efficiently build a string using Write methods.
    12  // It minimizes memory copying. The zero value is ready to use.
    13  // Do not copy a non-zero Builder.
    14  type Builder struct {
    15  	addr *Builder // of receiver, to detect copies by value
    16  	buf  []byte
    17  }
    18  
    19  /* XXX Remove unsafe: actually not needed in Gno when not persisting builders across realm boundaries.
    20  // noescape hides a pointer from escape analysis.  noescape is
    21  // the identity function but escape analysis doesn't think the
    22  // output depends on the input. noescape is inlined and currently
    23  // compiles down to zero instructions.
    24  // USE CAREFULLY!
    25  // This was copied from the runtime; see issues 23382 and 7921.
    26  //go:nosplit
    27  //go:nocheckptr
    28  func noescape(p unsafe.Pointer) unsafe.Pointer {
    29  	x := uintptr(p)
    30  	return unsafe.Pointer(x ^ 0)
    31  }
    32  */
    33  
    34  func (b *Builder) copyCheck() {
    35  	if b.addr == nil {
    36  		// This hack works around a failing of Go's escape analysis
    37  		// that was causing b to escape and be heap allocated.
    38  		// See issue 23382.
    39  		// TODO: once issue 7921 is fixed, this should be reverted to
    40  		// just "b.addr = b".
    41  		// XXX removed unsafe.  TODO see if 7921 also helps gno.
    42  		// b.addr = (*Builder)(noescape(unsafe.Pointer(b)))
    43  		b.addr = b
    44  	} else if b.addr != b {
    45  		panic("strings: illegal use of non-zero Builder copied by value")
    46  	}
    47  }
    48  
    49  // String returns the accumulated string.
    50  func (b *Builder) String() string {
    51  	// XXX removed unsafe.
    52  	// return *(*string)(unsafe.Pointer(&b.buf))
    53  	return string(b.buf)
    54  }
    55  
    56  // Len returns the number of accumulated bytes; b.Len() == len(b.String()).
    57  func (b *Builder) Len() int { return len(b.buf) }
    58  
    59  // Cap returns the capacity of the builder's underlying byte slice. It is the
    60  // total space allocated for the string being built and includes any bytes
    61  // already written.
    62  func (b *Builder) Cap() int { return cap(b.buf) }
    63  
    64  // Reset resets the Builder to be empty.
    65  func (b *Builder) Reset() {
    66  	b.addr = nil
    67  	b.buf = nil
    68  }
    69  
    70  // grow copies the buffer to a new, larger buffer so that there are at least n
    71  // bytes of capacity beyond len(b.buf).
    72  func (b *Builder) grow(n int) {
    73  	buf := make([]byte, len(b.buf), 2*cap(b.buf)+n)
    74  	copy(buf, b.buf)
    75  	b.buf = buf
    76  }
    77  
    78  // Grow grows b's capacity, if necessary, to guarantee space for
    79  // another n bytes. After Grow(n), at least n bytes can be written to b
    80  // without another allocation. If n is negative, Grow panics.
    81  func (b *Builder) Grow(n int) {
    82  	b.copyCheck()
    83  	if n < 0 {
    84  		panic("strings.Builder.Grow: negative count")
    85  	}
    86  	if cap(b.buf)-len(b.buf) < n {
    87  		b.grow(n)
    88  	}
    89  }
    90  
    91  // Write appends the contents of p to b's buffer.
    92  // Write always returns len(p), nil.
    93  func (b *Builder) Write(p []byte) (int, error) {
    94  	b.copyCheck()
    95  	b.buf = append(b.buf, p...)
    96  	return len(p), nil
    97  }
    98  
    99  // WriteByte appends the byte c to b's buffer.
   100  // The returned error is always nil.
   101  func (b *Builder) WriteByte(c byte) error {
   102  	b.copyCheck()
   103  	b.buf = append(b.buf, c)
   104  	return nil
   105  }
   106  
   107  // WriteRune appends the UTF-8 encoding of Unicode code point r to b's buffer.
   108  // It returns the length of r and a nil error.
   109  func (b *Builder) WriteRune(r rune) (int, error) {
   110  	b.copyCheck()
   111  	// Compare as uint32 to correctly handle negative runes.
   112  	if uint32(r) < utf8.RuneSelf {
   113  		b.buf = append(b.buf, byte(r))
   114  		return 1, nil
   115  	}
   116  	l := len(b.buf)
   117  	if cap(b.buf)-l < utf8.UTFMax {
   118  		b.grow(utf8.UTFMax)
   119  	}
   120  	n := utf8.EncodeRune(b.buf[l:l+utf8.UTFMax], r)
   121  	b.buf = b.buf[:l+n]
   122  	return n, nil
   123  }
   124  
   125  // WriteString appends the contents of s to b's buffer.
   126  // It returns the length of s and a nil error.
   127  func (b *Builder) WriteString(s string) (int, error) {
   128  	b.copyCheck()
   129  	b.buf = append(b.buf, s...)
   130  	return len(s), nil
   131  }