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)
| 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 "|") |
| 485 | func 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 | |
| 510 | func NullDecodeStr(barr []byte) (string, error) { |
| 511 | if bytes.IndexByte(barr, nullEncodeEscByte) == -1 { |
no outgoing calls
no test coverage detected