parser_sync.go 528 B

1234567891011121314151617181920212223242526272829
  1. package ansi
  2. import (
  3. "sync"
  4. "github.com/charmbracelet/x/ansi/parser"
  5. )
  6. var parserPool = sync.Pool{
  7. New: func() any {
  8. p := NewParser()
  9. p.SetParamsSize(parser.MaxParamsSize)
  10. p.SetDataSize(1024 * 1024 * 4) // 4MB of data buffer
  11. return p
  12. },
  13. }
  14. // GetParser returns a parser from a sync pool.
  15. func GetParser() *Parser {
  16. return parserPool.Get().(*Parser)
  17. }
  18. // PutParser returns a parser to a sync pool. The parser is reset
  19. // automatically.
  20. func PutParser(p *Parser) {
  21. p.Reset()
  22. p.dataLen = 0
  23. parserPool.Put(p)
  24. }