// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

// DO NOT MODIFY. Generated by gotmpl.
// source: internal/shared/attrnorm/truncate.go.tmpl

package attrnorm

import (
	
	
	

	
)

// Truncate returns a truncated version of attr. Only string, string slice,
// byte slice, slice, and map attribute values are truncated. String values are
// truncated according to limit. Each string slice value is truncated in this
// fashion (the slice length itself is unaffected), and byte slice values are
// truncated to at most limit bytes. For slice and map attribute values, the
// limit is applied recursively to contained values.
//
// No truncation is performed for a negative limit.
func ( int,  attribute.KeyValue) attribute.KeyValue {
	if  < 0 {
		return 
	}

	// Keep scalar cases in this wrapper. Routing them through the recursive,
	// multi-result transform adds measurable overhead to the dominant simple
	// attribute path.
	switch .Value.Type() {
	case attribute.STRING:
		.Value = attribute.StringValue(truncate(, .Value.AsString()))
	case attribute.BYTESLICE:
		 := .Value.AsString()
		if len() >  {
			.Value = attribute.ByteSliceValue([]byte([:]))
		}
	case attribute.STRINGSLICE:
		.Value, _ = truncateStringSliceValue(, .Value)
	case attribute.SLICE:
		.Value, _ = truncateSliceValue(, .Value)
	case attribute.MAP:
		.Value, _ = truncateMapValue(, .Value)
	}
	return 
}

// TruncateValue returns a truncated version of v. Only string, string
// slice, byte slice, and (recursively) slice and map values are modified.
//
// No truncation is performed for a negative limit.
func ( int,  attribute.Value) attribute.Value {
	if  < 0 {
		return 
	}

	// Match Truncate's direct scalar path to avoid paying for the recursive,
	// multi-result transform when no composite traversal is needed.
	switch .Type() {
	case attribute.STRING:
		return attribute.StringValue(truncate(, .AsString()))
	case attribute.BYTESLICE:
		 := .AsString()
		if len() >  {
			return attribute.ByteSliceValue([]byte([:]))
		}
	case attribute.STRINGSLICE:
		, _ = truncateStringSliceValue(, )
	case attribute.SLICE:
		, _ = truncateSliceValue(, )
	case attribute.MAP:
		, _ = truncateMapValue(, )
	}
	return 
}

// truncateValue traverses composite values once and rebuilds them lazily on
// the first changed child. Recursion provides O(depth) temporary stack space
// without forcing a heap allocation on the common no-op path.
func truncateValue( int,  attribute.Value) (attribute.Value, bool) {
	switch .Type() {
	case attribute.STRING:
		 := .AsString()
		 := truncate(, )
		if  !=  {
			return attribute.StringValue(), true
		}
	case attribute.STRINGSLICE:
		return truncateStringSliceValue(, )
	case attribute.BYTESLICE:
		// len(v.AsString()) is identical to len(v.AsByteSlice()) but
		// avoids allocating the full slice before truncation.
		 := .AsString()
		if len() >  {
			return attribute.ByteSliceValue([]byte([:])), true
		}
	case attribute.SLICE:
		return truncateSliceValue(, )
	case attribute.MAP:
		return truncateMapValue(, )
	}
	return , false
}

func truncateStringSliceValue( int,  attribute.Value) (attribute.Value, bool) {
	 := valueStorage()
	switch values := .(type) {
	case [0]string:
		return , false
	case [1]string:
		return truncateStrings(, , [:])
	case [2]string:
		return truncateStrings(, , [:])
	case [3]string:
		return truncateStrings(, , [:])
	default:
		 := reflect.ValueOf()
		if .Kind() != reflect.Array || .Type().Elem() != stringType {
			return , false
		}
		return truncateStringArray(, , )
	}
}

func truncateStrings( int,  attribute.Value,  []string) (attribute.Value, bool) {
	var  []string
	for ,  := range  {
		 := truncate(, )
		if  != nil {
			[] = 
			continue
		}
		if  ==  {
			continue
		}

		 = make([]string, len())
		copy(, [:])
		[] = 
	}
	if  == nil {
		return , false
	}
	return attribute.StringSliceValue(), true
}

func truncateStringArray( int,  attribute.Value,  reflect.Value) (attribute.Value, bool) {
	var  []string
	for  := range .Len() {
		 := .Index().String()
		 := truncate(, )
		if  != nil {
			[] = 
			continue
		}
		if  ==  {
			continue
		}

		 = make([]string, .Len())
		for  := range  {
			[] = .Index().String()
		}
		[] = 
	}
	if  == nil {
		return , false
	}
	return attribute.StringSliceValue(), true
}

func truncateSliceValue( int,  attribute.Value) (attribute.Value, bool) {
	 := valueStorage()
	switch values := .(type) {
	case [0]attribute.Value:
		return , false
	case [1]attribute.Value:
		return truncateValueSlice(, , [:])
	case [2]attribute.Value:
		return truncateValueSlice(, , [:])
	case [3]attribute.Value:
		return truncateValueSlice(, , [:])
	case [4]attribute.Value:
		return truncateValueSlice(, , [:])
	case [5]attribute.Value:
		return truncateValueSlice(, , [:])
	default:
		 := reflect.ValueOf()
		if .Kind() != reflect.Array || .Type().Elem() != valueType {
			return , false
		}
		return truncateValueArray(, , )
	}
}

func truncateValueSlice( int,  attribute.Value,  []attribute.Value) (attribute.Value, bool) {
	var  []attribute.Value
	for ,  := range  {
		,  := truncateValue(, )
		if  != nil {
			[] = 
			continue
		}
		if ! {
			continue
		}

		 = make([]attribute.Value, len())
		copy(, [:])
		[] = 
	}
	if  == nil {
		return , false
	}
	return attribute.SliceValue(...), true
}

func truncateValueArray( int,  attribute.Value,  reflect.Value) (attribute.Value, bool) {
	var  []attribute.Value
	for  := range .Len() {
		 := .Index().Interface().(attribute.Value)
		,  := truncateValue(, )
		if  != nil {
			[] = 
			continue
		}
		if ! {
			continue
		}

		 = make([]attribute.Value, .Len())
		for  := range  {
			[] = .Index().Interface().(attribute.Value)
		}
		[] = 
	}
	if  == nil {
		return , false
	}
	return attribute.SliceValue(...), true
}

func truncateMapValue( int,  attribute.Value) (attribute.Value, bool) {
	 := valueStorage()
	switch keyValues := .(type) {
	case [0]attribute.KeyValue:
		return , false
	case [1]attribute.KeyValue:
		return truncateKeyValueSlice(, , [:])
	case [2]attribute.KeyValue:
		return truncateKeyValueSlice(, , [:])
	case [3]attribute.KeyValue:
		return truncateKeyValueSlice(, , [:])
	case [4]attribute.KeyValue:
		return truncateKeyValueSlice(, , [:])
	case [5]attribute.KeyValue:
		return truncateKeyValueSlice(, , [:])
	default:
		 := reflect.ValueOf()
		if .Kind() != reflect.Array || .Type().Elem() != keyValueType {
			return , false
		}
		return truncateKeyValueArray(, , )
	}
}

func truncateKeyValueSlice(
	 int,
	 attribute.Value,
	 []attribute.KeyValue,
) (attribute.Value, bool) {
	var  []attribute.KeyValue
	for ,  := range  {
		,  := truncateValue(, .Value)
		if  {
			.Value = 
		}
		if  != nil {
			[] = 
			continue
		}
		if ! {
			continue
		}

		 = make([]attribute.KeyValue, len())
		copy(, [:])
		[] = 
	}
	if  == nil {
		return , false
	}
	return attribute.MapValue(...), true
}

func truncateKeyValueArray( int,  attribute.Value,  reflect.Value) (attribute.Value, bool) {
	var  []attribute.KeyValue
	for  := range .Len() {
		 := .Index().Interface().(attribute.KeyValue)
		,  := truncateValue(, .Value)
		if  {
			.Value = 
		}
		if  != nil {
			[] = 
			continue
		}
		if ! {
			continue
		}

		 = make([]attribute.KeyValue, .Len())
		for  := range  {
			[] = .Index().Interface().(attribute.KeyValue)
		}
		[] = 
	}
	if  == nil {
		return , false
	}
	return attribute.MapValue(...), true
}

var stringType = reflect.TypeFor[string]()

// truncate returns a version of s truncated according to limit.
//
// If limit is negative, it returns the original string.
//
// UTF-8 is supported. When truncating, all invalid UTF-8 bytes are dropped
// before applying truncation.
//
// If s already contains at most limit bytes, it is returned unchanged. No
// invalid characters are removed.
func truncate( int,  string) string {
	// This prioritizes performance in the following order based on the most
	// common expected use cases.
	//
	//  - Values whose byte length does not exceed the limit.
	//  - Strings with valid encodings that exceed the limit.
	//  - No limit.
	//  - Strings with invalid encodings that exceed the limit.
	if  < 0 || len() <=  {
		return 
	}

	// Optimistically, assume all valid UTF-8.
	var  strings.Builder
	 := 0
	for ,  := range  {
		if  != utf8.RuneError {
			++
			if  >  {
				return [:]
			}
			continue
		}

		// A zero limit also truncates a leading invalid byte or U+FFFD.
		if  == 0 {
			return ""
		}
		// Range reports RuneError for both an invalid byte and a valid U+FFFD.
		// Only a size-1 decoding indicates invalid UTF-8.
		,  := utf8.DecodeRuneInString([:])
		if  == 1 {
			// Removing this byte makes len(s)-1 an upper bound for a rebuilt
			// result. Keep the valid prefix before rescanning s[i:].
			.Grow(len() - 1)
			_, _ = .WriteString([:])
			 = [:]
			break
		}

		// A valid U+FFFD still consumes one character of the limit.
		++
		if  >  {
			return [:]
		}
	}

	// Fast-path, no invalid input.
	if .Cap() == 0 {
		return 
	}

	// The builder holds the valid prefix before the first invalid byte, and
	// count includes its runes. Skip malformed bytes without counting them
	// while copying the remaining valid runes up to the limit.
	for  := 0;  < len() &&  < ; {
		 := []
		if  < utf8.RuneSelf {
			// Optimization for single byte runes (common case).
			_ = .WriteByte()
			++
			++
			continue
		}

		,  := utf8.DecodeRuneInString([:])
		if  == 1 {
			// We checked for all 1-byte runes above, this is a RuneError.
			++
			continue
		}

		_, _ = .WriteString([ : +])
		 += 
		++
	}

	return .String()
}