package pgdialect

import (
	
	
	
)

type arrayParser struct {
	b []byte
	i int

	buf []byte
	err error
}

func ( []byte) *arrayParser {
	 := &arrayParser{
		b: ,
		i: 1,
	}
	if len() < 2 || [0] != '{' || [len()-1] != '}' {
		.err = fmt.Errorf("bun: can't parse array: %q", )
	}
	return 
}

func ( *arrayParser) () ([]byte, error) {
	if .err != nil {
		return nil, .err
	}

	,  := .readByte()
	if  != nil {
		return nil, 
	}

	switch  {
	case '}':
		return nil, io.EOF
	case '"':
		,  := .readSubstring()
		if  != nil {
			return nil, 
		}

		if .peek() == ',' {
			.skipNext()
		}

		return , nil
	default:
		 := .readSimple()
		if bytes.Equal(, []byte("NULL")) {
			 = nil
		}

		if .peek() == ',' {
			.skipNext()
		}

		return , nil
	}
}

func ( *arrayParser) () []byte {
	.unreadByte()

	if  := bytes.IndexByte(.b[.i:], ',');  >= 0 {
		 := .b[.i : .i+]
		.i += 
		return 
	}

	 := .b[.i : len(.b)-1]
	.i = len(.b) - 1
	return 
}

func ( *arrayParser) () ([]byte, error) {
	,  := .readByte()
	if  != nil {
		return nil, 
	}

	.buf = .buf[:0]
	for {
		if  == '"' {
			break
		}

		,  := .readByte()
		if  != nil {
			return nil, 
		}

		if  == '\\' {
			switch  {
			case '\\', '"':
				.buf = append(.buf, )

				,  = .readByte()
				if  != nil {
					return nil, 
				}
			default:
				.buf = append(.buf, '\\')
				 = 
			}
			continue
		}

		.buf = append(.buf, )
		 = 
	}

	return .buf, nil
}

func ( *arrayParser) () bool {
	return .i < len(.b)
}

func ( *arrayParser) () (byte, error) {
	if .valid() {
		 := .b[.i]
		.i++
		return , nil
	}
	return 0, io.EOF
}

func ( *arrayParser) () {
	.i--
}

func ( *arrayParser) () byte {
	if .valid() {
		return .b[.i]
	}
	return 0
}

func ( *arrayParser) () {
	.i++
}