diff options
author | Erik Dubbelboer <erik@dubbelboer.com> | 2023-08-26 12:49:17 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-26 12:49:17 +0200 |
commit | 0e99e64ee836adc5a3c4b35a0d8148900c2e7898 (patch) | |
tree | 74ed52ccb6d24a75f6e25d0f52b62e3050bef387 /fasthttpadaptor | |
parent | fix round2_32, split round2 tests because they depend on sizeof int at compil... (diff) | |
download | fasthttp-0e99e64ee836adc5a3c4b35a0d8148900c2e7898.tar.gz fasthttp-0e99e64ee836adc5a3c4b35a0d8148900c2e7898.tar.bz2 fasthttp-0e99e64ee836adc5a3c4b35a0d8148900c2e7898.zip |
Diffstat (limited to 'fasthttpadaptor')
-rw-r--r-- | fasthttpadaptor/b2s_new.go | 12 | ||||
-rw-r--r-- | fasthttpadaptor/b2s_old.go | 15 | ||||
-rw-r--r-- | fasthttpadaptor/request.go | 6 |
3 files changed, 27 insertions, 6 deletions
diff --git a/fasthttpadaptor/b2s_new.go b/fasthttpadaptor/b2s_new.go new file mode 100644 index 0000000..09ef72a --- /dev/null +++ b/fasthttpadaptor/b2s_new.go @@ -0,0 +1,12 @@ +//go:build go1.20 +// +build go1.20 + +package fasthttpadaptor + +import "unsafe" + +// b2s converts byte slice to a string without memory allocation. +// See https://groups.google.com/forum/#!msg/Golang-Nuts/ENgbUzYvCuU/90yGx7GUAgAJ . +func b2s(b []byte) string { + return unsafe.String(unsafe.SliceData(b), len(b)) +} diff --git a/fasthttpadaptor/b2s_old.go b/fasthttpadaptor/b2s_old.go new file mode 100644 index 0000000..08e2ac6 --- /dev/null +++ b/fasthttpadaptor/b2s_old.go @@ -0,0 +1,15 @@ +//go:build !go1.20 +// +build !go1.20 + +package fasthttpadaptor + +import "unsafe" + +// b2s converts byte slice to a string without memory allocation. +// See https://groups.google.com/forum/#!msg/Golang-Nuts/ENgbUzYvCuU/90yGx7GUAgAJ . +// +// Note it may break if string and/or slice header will change +// in the future go versions. +func b2s(b []byte) string { + return *(*string)(unsafe.Pointer(&b)) +} diff --git a/fasthttpadaptor/request.go b/fasthttpadaptor/request.go index 827ab92..62a8523 100644 --- a/fasthttpadaptor/request.go +++ b/fasthttpadaptor/request.go @@ -5,7 +5,6 @@ import ( "io" "net/http" "net/url" - "unsafe" "github.com/valyala/fasthttp" ) @@ -65,8 +64,3 @@ func ConvertRequest(ctx *fasthttp.RequestCtx, r *http.Request, forServer bool) e return nil } - -func b2s(b []byte) string { - /* #nosec G103 */ - return *(*string)(unsafe.Pointer(&b)) -} |