// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package array

import (
	
	
	
	
	

	
	
	
)

type StringLike interface {
	arrow.Array
	Value(int) string
	ValueLen(int) int
}

// String represents an immutable sequence of variable-length UTF-8 strings.
type String struct {
	array
	offsets []int32
	values  string
}

// NewStringData constructs a new String array from data.
func ( arrow.ArrayData) *String {
	 := &String{}
	.refCount.Add(1)
	.setData(.(*Data))
	return 
}

// Reset resets the String with a different set of Data.
func ( *String) ( arrow.ArrayData) {
	.setData(.(*Data))
}

// Value returns the slice at index i. This value should not be mutated.
func ( *String) ( int) string {
	 =  + .data.offset
	return .values[.offsets[]:.offsets[+1]]
}

func ( *String) ( int) string {
	if .IsNull() {
		return NullValueStr
	}
	return .Value()
}

// ValueOffset returns the offset of the value at index i.
func ( *String) ( int) int {
	if  < 0 ||  >= .data.length {
		panic("arrow/array: index out of range")
	}
	return int(.offsets[+.data.offset])
}

func ( *String) ( int) int64 {
	return int64(.ValueOffset())
}

func ( *String) ( int) int {
	if  < 0 ||  >= .data.length {
		panic("arrow/array: index out of range")
	}
	 := .data.offset + 
	return int(.offsets[+1] - .offsets[])
}

func ( *String) () []int32 {
	 := .data.offset
	 :=  + .data.length + 1
	return .offsets[:]
}

func ( *String) () []byte {
	 := .data.offset
	 :=  + .data.length
	if .data.buffers[2] != nil {
		return .array.data.buffers[2].Bytes()[.offsets[]:.offsets[]]
	}
	return nil
}

func ( *String) () string {
	 := new(strings.Builder)
	.WriteString("[")
	for  := 0;  < .Len(); ++ {
		if  > 0 {
			.WriteString(" ")
		}
		switch {
		case .IsNull():
			.WriteString(NullValueStr)
		default:
			fmt.Fprintf(, "%q", .Value())
		}
	}
	.WriteString("]")
	return .String()
}

func ( *String) ( *Data) {
	if len(.buffers) != 3 {
		panic("arrow/array: len(data.buffers) != 3")
	}

	.array.setData()

	if  := .buffers[2];  != nil {
		 := .Bytes()
		.values = *(*string)(unsafe.Pointer(&))
	}

	if  := .buffers[1];  != nil {
		.offsets = arrow.Int32Traits.CastFromBytes(.Bytes())
	}

	if .data.length < 1 {
		return
	}

	 := .data.offset + .data.length + 1
	if len(.offsets) <  {
		panic(fmt.Errorf("arrow/array: string offset buffer must have at least %d values", ))
	}

	if int(.offsets[-1]) > len(.values) {
		panic("arrow/array: string offsets out of bounds of data buffer")
	}
}

func ( *String) ( int) interface{} {
	if .IsValid() {
		return .Value()
	}
	return nil
}

func ( *String) () ([]byte, error) {
	 := make([]interface{}, .Len())
	for  := 0;  < .Len(); ++ {
		if .IsValid() {
			[] = .Value()
		} else {
			[] = nil
		}
	}
	return json.Marshal()
}

func arrayEqualString(,  *String) bool {
	for  := 0;  < .Len(); ++ {
		if .IsNull() {
			continue
		}
		if .Value() != .Value() {
			return false
		}
	}
	return true
}

// String represents an immutable sequence of variable-length UTF-8 strings.
type LargeString struct {
	array
	offsets []int64
	values  string
}

// NewStringData constructs a new String array from data.
func ( arrow.ArrayData) *LargeString {
	 := &LargeString{}
	.refCount.Add(1)
	.setData(.(*Data))
	return 
}

// Reset resets the String with a different set of Data.
func ( *LargeString) ( arrow.ArrayData) {
	.setData(.(*Data))
}

// Value returns the slice at index i. This value should not be mutated.
func ( *LargeString) ( int) string {
	 =  + .data.offset
	return .values[.offsets[]:.offsets[+1]]
}

func ( *LargeString) ( int) string {
	if .IsNull() {
		return NullValueStr
	}
	return .Value()
}

// ValueOffset returns the offset of the value at index i.
func ( *LargeString) ( int) int64 {
	if  < 0 ||  > .data.length {
		panic("arrow/array: index out of range")
	}
	return .offsets[+.data.offset]
}

func ( *LargeString) ( int) int64 {
	return .ValueOffset()
}

func ( *LargeString) ( int) int {
	if  < 0 ||  >= .data.length {
		panic("arrow/array: index out of range")
	}
	 := .data.offset + 
	return int(.offsets[+1] - .offsets[])
}

func ( *LargeString) () []int64 {
	 := .data.offset
	 :=  + .data.length + 1
	return .offsets[:]
}

func ( *LargeString) () []byte {
	 := .data.offset
	 :=  + .data.length
	if .data.buffers[2] != nil {
		return .array.data.buffers[2].Bytes()[.offsets[]:.offsets[]]
	}
	return nil
}

func ( *LargeString) () string {
	 := new(strings.Builder)
	.WriteString("[")
	for  := 0;  < .Len(); ++ {
		if  > 0 {
			.WriteString(" ")
		}
		switch {
		case .IsNull():
			.WriteString(NullValueStr)
		default:
			fmt.Fprintf(, "%q", .Value())
		}
	}
	.WriteString("]")
	return .String()
}

func ( *LargeString) ( *Data) {
	if len(.buffers) != 3 {
		panic("arrow/array: len(data.buffers) != 3")
	}

	.array.setData()

	if  := .buffers[2];  != nil {
		 := .Bytes()
		.values = *(*string)(unsafe.Pointer(&))
	}

	if  := .buffers[1];  != nil {
		.offsets = arrow.Int64Traits.CastFromBytes(.Bytes())
	}

	if .data.length < 1 {
		return
	}

	 := .data.offset + .data.length + 1
	if len(.offsets) <  {
		panic(fmt.Errorf("arrow/array: string offset buffer must have at least %d values", ))
	}

	if int(.offsets[-1]) > len(.values) {
		panic("arrow/array: string offsets out of bounds of data buffer")
	}
}

func ( *LargeString) ( int) interface{} {
	if .IsValid() {
		return .Value()
	}
	return nil
}

func ( *LargeString) () ([]byte, error) {
	 := make([]interface{}, .Len())
	for  := 0;  < .Len(); ++ {
		[] = .GetOneForMarshal()
	}
	return json.Marshal()
}

func arrayEqualLargeString(,  *LargeString) bool {
	for  := 0;  < .Len(); ++ {
		if .IsNull() {
			continue
		}
		if .Value() != .Value() {
			return false
		}
	}
	return true
}

type StringView struct {
	array
	values      []arrow.ViewHeader
	dataBuffers []*memory.Buffer
}

func ( arrow.ArrayData) *StringView {
	 := &StringView{}
	.refCount.Add(1)
	.setData(.(*Data))
	return 
}

// Reset resets the String with a different set of Data.
func ( *StringView) ( arrow.ArrayData) {
	.setData(.(*Data))
}

func ( *StringView) ( *Data) {
	if len(.buffers) < 2 {
		panic("len(data.buffers) < 2")
	}
	.array.setData()

	if  := .buffers[1];  != nil {
		.values = arrow.ViewHeaderTraits.CastFromBytes(.Bytes())
	}

	.dataBuffers = .buffers[2:]
}

func ( *StringView) ( int) *arrow.ViewHeader {
	if  < 0 ||  >= .data.length {
		panic("arrow/array: index out of range")
	}
	return &.values[.data.offset+]
}

func ( *StringView) ( int) string {
	 := .ValueHeader()
	if .IsInline() {
		return .InlineString()
	}
	 := .BufferOffset()
	 := .dataBuffers[.BufferIndex()]
	 := .Bytes()[ : +int32(.Len())]
	return *(*string)(unsafe.Pointer(&))
}

func ( *StringView) ( int) int {
	 := .ValueHeader()
	return .Len()
}

func ( *StringView) () string {
	var  strings.Builder
	.WriteString("[")
	for  := 0;  < .Len(); ++ {
		if  > 0 {
			.WriteString(" ")
		}
		switch {
		case .IsNull():
			.WriteString(NullValueStr)
		default:
			fmt.Fprintf(&, "%q", .Value())
		}
	}
	.WriteString("]")
	return .String()
}

func ( *StringView) ( int) string {
	if .IsNull() {
		return NullValueStr
	}
	return .Value()
}

func ( *StringView) ( int) interface{} {
	if .IsNull() {
		return nil
	}
	return .Value()
}

func ( *StringView) () ([]byte, error) {
	 := make([]interface{}, .Len())
	for  := 0;  < .Len(); ++ {
		[] = .GetOneForMarshal()
	}
	return json.Marshal()
}

func arrayEqualStringView(,  *StringView) bool {
	,  := .dataBuffers, .dataBuffers
	for  := 0;  < .Len(); ++ {
		if .IsNull() {
			continue
		}
		if !.ValueHeader().Equals(, .ValueHeader(), ) {
			return false
		}
	}
	return true
}

// A StringBuilder is used to build a String array using the Append methods.
type StringBuilder struct {
	*BinaryBuilder
}

// NewStringBuilder creates a new StringBuilder.
func ( memory.Allocator) *StringBuilder {
	 := &StringBuilder{
		BinaryBuilder: NewBinaryBuilder(, arrow.BinaryTypes.String),
	}
	return 
}

func ( *StringBuilder) () arrow.DataType {
	return arrow.BinaryTypes.String
}

// Append appends a string to the builder.
func ( *StringBuilder) ( string) {
	.BinaryBuilder.Append([]byte())
}

// AppendValues will append the values in the v slice. The valid slice determines which values
// in v are valid (not null). The valid slice must either be empty or be equal in length to v. If empty,
// all values in v are appended and considered valid.
func ( *StringBuilder) ( []string,  []bool) {
	.AppendStringValues(, )
}

// Value returns the string at index i.
func ( *StringBuilder) ( int) string {
	return string(.BinaryBuilder.Value())
}

// NewArray creates a String array from the memory buffers used by the builder and resets the StringBuilder
// so it can be used to build a new array.
func ( *StringBuilder) () arrow.Array {
	return .NewStringArray()
}

// NewStringArray creates a String array from the memory buffers used by the builder and resets the StringBuilder
// so it can be used to build a new array.
func ( *StringBuilder) () ( *String) {
	 := .newData()
	 = NewStringData()
	.Release()
	return
}

func ( *StringBuilder) ( *json.Decoder) error {
	,  := .Token()
	if  != nil {
		return 
	}

	switch v := .(type) {
	case nil:
		.AppendNull()
	case string:
		.Append()
	default:
		return &json.UnmarshalTypeError{
			Value:  fmt.Sprint(),
			Type:   reflect.TypeOf(string("")),
			Offset: .InputOffset(),
		}
	}
	return nil
}

func ( *StringBuilder) ( *json.Decoder) error {
	for .More() {
		if  := .UnmarshalOne();  != nil {
			return 
		}
	}
	return nil
}

func ( *StringBuilder) ( []byte) error {
	 := json.NewDecoder(bytes.NewReader())
	,  := .Token()
	if  != nil {
		return 
	}

	if ,  := .(json.Delim); ! ||  != '[' {
		return fmt.Errorf("string builder must unpack from json array, found %s", )
	}

	return .Unmarshal()
}

// A LargeStringBuilder is used to build a LargeString array using the Append methods.
// LargeString is for when you need the offset buffer to be 64-bit integers
// instead of 32-bit integers.
type LargeStringBuilder struct {
	*BinaryBuilder
}

// NewStringBuilder creates a new StringBuilder.
func ( memory.Allocator) *LargeStringBuilder {
	 := &LargeStringBuilder{
		BinaryBuilder: NewBinaryBuilder(, arrow.BinaryTypes.LargeString),
	}
	return 
}

func ( *LargeStringBuilder) () arrow.DataType { return arrow.BinaryTypes.LargeString }

// Append appends a string to the builder.
func ( *LargeStringBuilder) ( string) {
	.BinaryBuilder.Append([]byte())
}

// AppendValues will append the values in the v slice. The valid slice determines which values
// in v are valid (not null). The valid slice must either be empty or be equal in length to v. If empty,
// all values in v are appended and considered valid.
func ( *LargeStringBuilder) ( []string,  []bool) {
	.AppendStringValues(, )
}

// Value returns the string at index i.
func ( *LargeStringBuilder) ( int) string {
	return string(.BinaryBuilder.Value())
}

// NewArray creates a String array from the memory buffers used by the builder and resets the StringBuilder
// so it can be used to build a new array.
func ( *LargeStringBuilder) () arrow.Array {
	return .NewLargeStringArray()
}

// NewStringArray creates a String array from the memory buffers used by the builder and resets the StringBuilder
// so it can be used to build a new array.
func ( *LargeStringBuilder) () ( *LargeString) {
	 := .newData()
	 = NewLargeStringData()
	.Release()
	return
}

func ( *LargeStringBuilder) ( *json.Decoder) error {
	,  := .Token()
	if  != nil {
		return 
	}

	switch v := .(type) {
	case nil:
		.AppendNull()
	case string:
		.Append()
	default:
		return &json.UnmarshalTypeError{
			Value:  fmt.Sprint(),
			Type:   reflect.TypeOf(string("")),
			Offset: .InputOffset(),
		}
	}
	return nil
}

func ( *LargeStringBuilder) ( *json.Decoder) error {
	for .More() {
		if  := .UnmarshalOne();  != nil {
			return 
		}
	}
	return nil
}

func ( *LargeStringBuilder) ( []byte) error {
	 := json.NewDecoder(bytes.NewReader())
	,  := .Token()
	if  != nil {
		return 
	}

	if ,  := .(json.Delim); ! ||  != '[' {
		return fmt.Errorf("string builder must unpack from json array, found %s", )
	}

	return .Unmarshal()
}

type StringViewBuilder struct {
	*BinaryViewBuilder
}

func ( memory.Allocator) *StringViewBuilder {
	 := &StringViewBuilder{
		BinaryViewBuilder: NewBinaryViewBuilder(),
	}
	.dtype = arrow.BinaryTypes.StringView
	return 
}

func ( *StringViewBuilder) ( string) {
	.AppendString()
}

func ( *StringViewBuilder) ( []string,  []bool) {
	.AppendStringValues(, )
}

func ( *StringViewBuilder) ( *json.Decoder) error {
	,  := .Token()
	if  != nil {
		return 
	}

	switch v := .(type) {
	case string:
		.Append()
	case []byte:
		.BinaryViewBuilder.Append()
	case nil:
		.AppendNull()
	default:
		return &json.UnmarshalTypeError{
			Value:  fmt.Sprint(),
			Type:   reflect.TypeOf([]byte{}),
			Offset: .InputOffset(),
		}
	}
	return nil
}

func ( *StringViewBuilder) ( *json.Decoder) error {
	for .More() {
		if  := .UnmarshalOne();  != nil {
			return 
		}
	}
	return nil
}

func ( *StringViewBuilder) ( []byte) error {
	 := json.NewDecoder(bytes.NewReader())
	,  := .Token()
	if  != nil {
		return 
	}

	if ,  := .(json.Delim); ! ||  != '[' {
		return fmt.Errorf("binary view builder must unpack from json array, found %s", )
	}

	return .Unmarshal()
}

func ( *StringViewBuilder) () arrow.Array {
	return .NewStringViewArray()
}

func ( *StringViewBuilder) () ( *StringView) {
	 := .newData()
	 = NewStringViewData()
	.Release()
	return
}

type StringLikeBuilder interface {
	Builder
	Append(string)
	AppendValues([]string, []bool)
	UnsafeAppend([]byte)
	ReserveData(int)
}

var (
	_ arrow.Array       = (*String)(nil)
	_ arrow.Array       = (*LargeString)(nil)
	_ arrow.Array       = (*StringView)(nil)
	_ Builder           = (*StringBuilder)(nil)
	_ Builder           = (*LargeStringBuilder)(nil)
	_ Builder           = (*StringViewBuilder)(nil)
	_ StringLikeBuilder = (*StringBuilder)(nil)
	_ StringLikeBuilder = (*LargeStringBuilder)(nil)
	_ StringLikeBuilder = (*StringViewBuilder)(nil)
	_ StringLike        = (*String)(nil)
	_ StringLike        = (*LargeString)(nil)
	_ StringLike        = (*StringView)(nil)

	_ arrow.TypedArray[string] = (*String)(nil)
	_ arrow.TypedArray[string] = (*LargeString)(nil)
	_ arrow.TypedArray[string] = (*StringView)(nil)
)