github.com/dylandreimerink/gobpfld@v0.6.1-0.20220205171531-e79c330ad608/internal/cstr/cstring.go (about)

     1  package cstr
     2  
     3  import "strings"
     4  
     5  // ToString trims the string at the first null byte which is used in C to indicate the end of the string
     6  func ToString(cstr string) string {
     7  	nbi := strings.IndexByte(cstr, 0x00)
     8  	if nbi != -1 {
     9  		return cstr[:nbi]
    10  	}
    11  	return cstr
    12  }
    13  
    14  // BytesToString converts bytes to string assuming it is a C string
    15  func BytesToString(b []byte) string {
    16  	return ToString(string(b))
    17  }
    18  
    19  // StringToCStrBytes turns the string into a null terminated byte slice
    20  func StringToCStrBytes(str string) []byte {
    21  	return []byte(str + "\x00")
    22  }