MCPcopy Index your code
hub / github.com/wavetermdev/waveterm / NullEncodeStr

Function NullEncodeStr

pkg/util/utilfn/utilfn.go:485–508  ·  view source on GitHub ↗

encodes a string, removing null/zero bytes (and separators '|') a zero byte is encoded as "\0", a '\' is encoded as "\\", sep is encoded as "\s" allows for easy double splitting (first on \x00, and next on "|")

(s string)

Source from the content-addressed store, hash-verified

483// a zero byte is encoded as "\0", a '\' is encoded as "\\", sep is encoded as "\s"
484// allows for easy double splitting (first on \x00, and next on "|")
485func NullEncodeStr(s string) []byte {
486 strBytes := []byte(s)
487 if bytes.IndexByte(strBytes, 0) == -1 &&
488 bytes.IndexByte(strBytes, nullEncodeEscByte) == -1 &&
489 bytes.IndexByte(strBytes, nullEncodeSepByte) == -1 &&
490 bytes.IndexByte(strBytes, nullEncodeEqByte) == -1 {
491 return strBytes
492 }
493 var rtn []byte
494 for _, b := range strBytes {
495 if b == 0 {
496 rtn = append(rtn, nullEncodeEscByte, nullEncodeZeroByteEsc)
497 } else if b == nullEncodeEscByte {
498 rtn = append(rtn, nullEncodeEscByte, nullEncodeEscByteEsc)
499 } else if b == nullEncodeSepByte {
500 rtn = append(rtn, nullEncodeEscByte, nullEncodeSepByteEsc)
501 } else if b == nullEncodeEqByte {
502 rtn = append(rtn, nullEncodeEscByte, nullEncodeEqByteEsc)
503 } else {
504 rtn = append(rtn, b)
505 }
506 }
507 return rtn
508}
509
510func NullDecodeStr(barr []byte) (string, error) {
511 if bytes.IndexByte(barr, nullEncodeEscByte) == -1 {

Callers 3

EncodeStringMapFunction · 0.85
EncodeStringArrayFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected