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)
| 450 | // modified if the input string is expected to be used again - doing so could |
| 451 | // violate Go semantics. |
| 452 | func UnsafeConvertStringToBytes(s string) []byte { |
| 453 | if len(s) == 0 { |
| 454 | return nil |
| 455 | } |
| 456 | // We unsafely convert the string to a []byte to avoid the |
| 457 | // usual allocation when converting to a []byte. This is |
| 458 | // kosher because we know that EncodeBytes{,Descending} does |
| 459 | // not keep a reference to the value it encodes. The first |
| 460 | // step is getting access to the string internals. |
| 461 | data := unsafe.StringData(s) |
| 462 | // Next we treat the string data as a maximally sized array which we |
| 463 | // slice. This usage is safe because the pointer value remains in the string. |
| 464 | return (*[0x7fffffff]byte)(unsafe.Pointer(data))[:len(s):len(s)] |
| 465 | } |
| 466 | |
| 467 | // EncodeStringAscending encodes the string value using an escape-based encoding. See |
| 468 | // EncodeBytes for details. The encoded bytes are append to the supplied buffer |
no outgoing calls
no test coverage detected