UnsafeConvertStringToBytes converts a string to a byte array to be used with string encoding functions. Note that the output byte array should not be modified if the input string is expected to be used again - doing so could violate Go semantics.
(s string)
| 675 | // modified if the input string is expected to be used again - doing so could |
| 676 | // violate Go semantics. |
| 677 | func UnsafeConvertStringToBytes(s string) []byte { |
| 678 | if len(s) == 0 { |
| 679 | return nil |
| 680 | } |
| 681 | // We unsafely convert the string to a []byte to avoid the |
| 682 | // usual allocation when converting to a []byte. This is |
| 683 | // kosher because we know that EncodeBytes{,Descending} does |
| 684 | // not keep a reference to the value it encodes. The first |
| 685 | // step is getting access to the string internals. |
| 686 | hdr := (*reflect.StringHeader)(unsafe.Pointer(&s)) |
| 687 | // Next we treat the string data as a maximally sized array which we |
| 688 | // slice. This usage is safe because the pointer value remains in the string. |
| 689 | return (*[0x7fffffff]byte)(unsafe.Pointer(hdr.Data))[:len(s):len(s)] |
| 690 | } |
| 691 | |
| 692 | // EncodeStringAscending encodes the string value using an escape-based encoding. See |
| 693 | // EncodeBytes for details. The encoded bytes are append to the supplied buffer |
no outgoing calls
no test coverage detected
searching dependent graphs…