tbuf

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2023 License: MIT Imports: 2 Imported by: 1

README

tbuf

Simple text buffer with fixed max size optimized for deletion from begin/end and random elements access

Sumary

  • Go version >= 1.20
  • Require github.com/stretchr/testify

Install

go get github.com/wmentor/tbuf

Usage

package main

import (
  "fmt"
  "strconv"

  "github.com/wmentor/tbuf"
)

func main() {
  buf, e := tbuf.New(-1)
  fmt.Println(buf ,e) // nil and error

  fmt.Println("create buffer:")

  buf, e = tbuf.New(5) // *tbuf.Buffer and nil
  for i := 0 ; i < 5 ; i++ {
    buf.Push(strconv.Itoa(i))
  }

  fmt.Println(buf.IsFull()) // true

  for i := 0 ; i < buf.Len() ; i++ {
    val, err := buf.Get(i)
    fmt.Println(val, err)
  }

  buf.Push("11")
  buf.Push("12")

  fmt.Println("buffer after adding two items:")

  for i := 0 ; i < buf.Len() ; i++ {
    val, err := buf.Get(i)
    fmt.Println(val, err)
  }

  fmt.Println("join examples:")

  fmt.Printf("%s\n", buf.Join("+"))
  fmt.Printf("%s\n", buf.JoinFirst(3, "//"))

  buf.ShiftN(1)
  buf.PopN(2)

  fmt.Println("remove 1 from start and 2 from end")

  for i := 0 ; i < buf.Len() ; i++ {
    val, err := buf.Get(i)
    fmt.Println(val, err)
  }

  fmt.Println(buf.String())

  buf.Reset() // remove all items
  fmt.Println(buf.IsEmpty()) // true
}

Output:

<nil> invalid buffer size
create buffer:
true
0 <nil>
1 <nil>
2 <nil>
3 <nil>
4 <nil>
buffer after adding two items:
2 <nil>
3 <nil>
4 <nil>
11 <nil>
12 <nil>
join examples:
2+3+4+11+12
2//3//4
remove 1 from start and 2 from end
3 <nil>
4 <nil>
3 4
true

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidBufferSize error = errors.New("invalid buffer size")
	ErrInvalidIndexRange error = errors.New("invalid index")
)

Functions

This section is empty.

Types

type Buffer

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

func New

func New(maxSize int) (*Buffer, error)

func (*Buffer) Get

func (b *Buffer) Get(idx int) (string, error)

func (*Buffer) IsEmpty

func (b *Buffer) IsEmpty() bool

func (*Buffer) IsFull

func (b *Buffer) IsFull() bool

func (*Buffer) Join added in v1.0.2

func (b *Buffer) Join(sep string) string

func (*Buffer) JoinFirst added in v1.0.2

func (b *Buffer) JoinFirst(limit int, sep string) string

func (*Buffer) Len

func (b *Buffer) Len() int

func (*Buffer) Pop

func (b *Buffer) Pop()

func (*Buffer) PopN

func (b *Buffer) PopN(num int)

func (*Buffer) Push

func (b *Buffer) Push(val string)

func (*Buffer) Reset

func (b *Buffer) Reset()

func (*Buffer) Shift

func (b *Buffer) Shift()

func (*Buffer) ShiftN

func (b *Buffer) ShiftN(num int)

func (*Buffer) String added in v1.0.1

func (b *Buffer) String() string

Jump to

Keyboard shortcuts

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