...

Source file src/go.formulabun.club/functional/strings/strings.go

Documentation: go.formulabun.club/functional/strings

     1  package strings
     2  
     3  // NullTerminatedString returns a string from a null-terminated byte slice,
     4  // removing any bytes over 127.
     5  func SafeNullTerminated(data []byte) string {
     6  	newBytes := make([]byte, 0)
     7  
     8  	// Filter out all bytes above 127 and cut at a null terminator
     9  	for i := 0; i < len(data); i++ {
    10  		if data[i] >= 128 {
    11  			continue
    12  		} else if data[i] == 0 {
    13  			break
    14  		}
    15  		newBytes = append(newBytes, data[i])
    16  	}
    17  	return string(newBytes)
    18  }
    19  
    20  // NullTerminatedString returns a slice of bytes from a null-terminated byte slice.
    21  func NullTerminated(data []byte) []byte {
    22  	newBytes := make([]byte, 0)
    23  
    24  	// Cut at a null terminator
    25  	for i := 0; i < len(data); i++ {
    26  		if data[i] == 0 {
    27  			break
    28  		}
    29  		newBytes = append(newBytes, data[i])
    30  	}
    31  	return newBytes
    32  }
    33  

View as plain text