gitlab.com/evatix-go/core@v1.3.55/coredata/stringslice/SafeRangeItems.go (about)

     1  package stringslice
     2  
     3  import "gitlab.com/evatix-go/core/constants"
     4  
     5  func SafeRangeItems(
     6  	slice []string,
     7  	start, end int,
     8  ) []string {
     9  	if slice == nil {
    10  		return []string{}
    11  	}
    12  
    13  	length := len(slice)
    14  	if length == 0 {
    15  		return []string{}
    16  	}
    17  
    18  	lastIndex := length - 1
    19  	if start > lastIndex {
    20  		return []string{}
    21  	}
    22  
    23  	if end > lastIndex || end == constants.InvalidValue {
    24  		end = lastIndex
    25  	}
    26  
    27  	if start <= lastIndex && start != constants.InvalidValue {
    28  		return slice[start:end]
    29  	}
    30  
    31  	return slice[:end]
    32  }