Skip to content
Myr

Convert strings and slices using the officially recommended way (#3344)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Feat: Convert strings and slices using the officially recommended way. Go official is expected to provide unsafe.{SliceData, Slice, StringData, String} series methods in version 1.20 for conversion of strings and slices. * chore: add reference documentation link to comment of code * chore: update Copyright * chore: remove build tag "+build !go1.20"

authored by hopehook · committed by GitHub · parent de1c4ec · Browse files · 4 files+50 added−25 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+50 added−25 removed

  • other:+50 added−25 removed

4 files+50 added−25 removed

added:internal/bytesconv/bytesconv_1.19.go+26 added
1// Copyright 2020 Gin Core Team. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45//go:build !go1.2067package bytesconv89import (10 "unsafe"11)1213// StringToBytes converts string to byte slice without a memory allocation.14func StringToBytes(s string) []byte {15 return *(*[]byte)(unsafe.Pointer(16 &struct {17 string18 Cap int19 }{s, len(s)},20 ))21}2223// BytesToString converts byte slice to string without a memory allocation.24func BytesToString(b []byte) string {25 return *(*string)(unsafe.Pointer(&b))26}
added:internal/bytesconv/bytesconv_1.20.go+23 added
1// Copyright 2023 Gin Core Team. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45//go:build go1.2067package bytesconv89import (10 "unsafe"11)1213// StringToBytes converts string to byte slice without a memory allocation.14// For more details, see https://github.com/golang/go/issues/53003#issuecomment-1140276077.15func StringToBytes(s string) []byte {16 return unsafe.Slice(unsafe.StringData(s), len(s))17}1819// BytesToString converts byte slice to string without a memory allocation.20// For more details, see https://github.com/golang/go/issues/53003#issuecomment-1140276077.21func BytesToString(b []byte) string {22 return unsafe.String(unsafe.SliceData(b), len(b))23}
deleted:internal/bytesconv/bytesconv.go-24 removed
1// Copyright 2020 Gin Core Team. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package bytesconv67import (8 "unsafe"9)1011// StringToBytes converts string to byte slice without a memory allocation.12func StringToBytes(s string) []byte {13 return *(*[]byte)(unsafe.Pointer(14 &struct {15 string16 Cap int17 }{s, len(s)},18 ))19}2021// BytesToString converts byte slice to string without a memory allocation.22func BytesToString(b []byte) string {23 return *(*string)(unsafe.Pointer(&b))24}
modified:go.mod+1 added-1 removed
1module github.com/gin-gonic/gin23go 1.183go 1.2045require (6 github.com/bytedance/sonic v1.8.27 github.com/gin-contrib/sse v0.1.0
29 unmodified lines