Skip to content

Commit

Permalink
Merge pull request #1493 from ClickHouse/safer-unsafe
Browse files Browse the repository at this point in the history
use unsafe.Slice/unsafe.StringData
  • Loading branch information
SpencerTorres authored Feb 7, 2025
2 parents 8d87c23 + bc002d9 commit 36e3cc1
Showing 1 changed file with 1 addition and 14 deletions.
15 changes: 1 addition & 14 deletions lib/binary/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,9 @@
package binary

import (
"reflect"
"unsafe"
)

// Copied from https://github.com/m3db/m3/blob/master/src/x/unsafe/string.go#L62

func unsafeStr2Bytes(str string) []byte {
if len(str) == 0 {
return nil
}
var scratch []byte
{
slice := (*reflect.SliceHeader)(unsafe.Pointer(&scratch))
slice.Len = len(str)
slice.Cap = len(str)
slice.Data = (*reflect.StringHeader)(unsafe.Pointer(&str)).Data
}
return scratch
return unsafe.Slice(unsafe.StringData(str), len(str))
}

0 comments on commit 36e3cc1

Please sign in to comment.