stringbuilder

package
v2.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 20, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package stringbuilder provides a minimal, mutable string buffer used internally by the libphonenumber port. It is internal because it only exists to mirror the slice of java.lang.StringBuilder the port relies on and is not part of the public API.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidIndex = errors.New("stringbuilder: invalid index")

ErrInvalidIndex is returned by Insert/InsertString/ByteAt when the given index falls outside the buffer.

Functions

This section is empty.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder is a minimal, mutable string buffer that mirrors the parts of java.lang.StringBuilder the libphonenumber port relies on — chiefly the ability to insert at an arbitrary index, which neither strings.Builder nor bytes.Buffer provide. It is backed by a plain []byte and leans on the standard library (slices.Insert, utf8.AppendRune) for the heavy lifting.

func New

func New(buf []byte) *Builder

New creates a Builder using buf as its initial contents.

func NewString

func NewString(s string) *Builder

NewString creates a Builder using s as its initial contents.

func (*Builder) ByteAt

func (b *Builder) ByteAt(i int) (byte, error)

ByteAt returns the byte at index i, or ErrInvalidIndex if i is out of range.

func (*Builder) Bytes

func (b *Builder) Bytes() []byte

Bytes returns the underlying contents. The slice is valid until the next mutating call; callers that retain it should copy.

func (*Builder) CharAt

func (b *Builder) CharAt(i int) byte

CharAt returns the byte at index i, mirroring StringBuilder.charAt. Callers in the port only ever index ASCII content, so a byte is an exact analogue of Java's char here.

func (*Builder) Delete

func (b *Builder) Delete(start, end int)

Delete removes the bytes in [start, end), mirroring StringBuilder.delete.

func (*Builder) Insert

func (b *Builder) Insert(i int, p []byte) (int, error)

Insert inserts p at index i, growing the buffer as needed. It returns ErrInvalidIndex if i is negative or greater than the buffer length.

func (*Builder) InsertString

func (b *Builder) InsertString(i int, s string) (int, error)

InsertString inserts s at index i, growing the buffer as needed. It returns ErrInvalidIndex if i is negative or greater than the buffer length.

func (*Builder) LastIndexOf

func (b *Builder) LastIndexOf(s string) int

LastIndexOf returns the byte index of the last occurrence of s, or -1 if absent, mirroring StringBuilder.lastIndexOf.

func (*Builder) Len

func (b *Builder) Len() int

Len returns the number of bytes in the buffer.

func (*Builder) Reset

func (b *Builder) Reset()

Reset empties the buffer while retaining its capacity.

func (*Builder) ResetWith

func (b *Builder) ResetWith(buf []byte) (int, error)

ResetWith replaces the buffer's contents with buf.

func (*Builder) ResetWithString

func (b *Builder) ResetWithString(s string) (int, error)

ResetWithString replaces the buffer's contents with s.

func (*Builder) SetLength

func (b *Builder) SetLength(n int)

SetLength truncates the buffer to n bytes, or pads it with NUL bytes if n is greater than the current length, mirroring StringBuilder.setLength.

func (*Builder) String

func (b *Builder) String() string

String returns the contents as a string. A nil receiver returns "<nil>", which is handy when debugging.

func (*Builder) Substring

func (b *Builder) Substring(start, end int) string

Substring returns the bytes in [start, end) as a string, mirroring StringBuilder.substring(start, end).

func (*Builder) Write

func (b *Builder) Write(p []byte) (int, error)

Write appends p to the buffer. The error is always nil; it exists to satisfy io.Writer.

func (*Builder) WriteByte

func (b *Builder) WriteByte(c byte) error

WriteByte appends c to the buffer. The error is always nil; it exists to satisfy io.ByteWriter.

func (*Builder) WriteRune

func (b *Builder) WriteRune(r rune) (int, error)

WriteRune appends the UTF-8 encoding of r to the buffer and returns the number of bytes written. The error is always nil.

func (*Builder) WriteString

func (b *Builder) WriteString(s string) (int, error)

WriteString appends s to the buffer. The error is always nil.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL