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

     1  package stringslice
     2  
     3  import "gitlab.com/evatix-go/core/constants"
     4  
     5  func InPlaceReverse(list *[]string) *[]string {
     6  	if list == nil {
     7  		return &[]string{}
     8  	}
     9  
    10  	nonPtrList := *list
    11  	length := len(nonPtrList)
    12  
    13  	if length <= 1 {
    14  		return list
    15  	}
    16  
    17  	if length == constants.Capacity2 {
    18  		nonPtrList[0], nonPtrList[1] =
    19  			nonPtrList[1], nonPtrList[0]
    20  
    21  		return &nonPtrList
    22  	}
    23  
    24  	mid := length / 2
    25  	lastIndex := length - 1
    26  
    27  	for i := 0; i < mid; i++ {
    28  		nonPtrList[i], nonPtrList[lastIndex-i] =
    29  			nonPtrList[lastIndex-i], nonPtrList[i]
    30  	}
    31  
    32  	return &nonPtrList
    33  }