github.com/Mericusta/go-stp@v0.6.8/string.go (about)

     1  package stp
     2  
     3  import (
     4  	"strings"
     5  	"unsafe"
     6  )
     7  
     8  // ConvertStringToStringStruct
     9  // @template1                  string struct
    10  // @param1                     to handle string
    11  // @param2                     string splitter
    12  // @return                     string struct pointer
    13  func ConvertStringToStringStruct[T any](str, splitter string) *T {
    14  	sSlice := strings.Split(str, splitter)
    15  	offset := unsafe.Sizeof(str) // must be 16
    16  	len := len(sSlice)
    17  
    18  	sStruct := new(T)
    19  	ptr := uintptr(unsafe.Pointer(sStruct))
    20  	for index := uintptr(0); uintptr(len) > index; index++ {
    21  		*(*string)(unsafe.Pointer(ptr + offset*index)) = sSlice[index]
    22  	}
    23  
    24  	return sStruct
    25  }