From 9d5a7bf7e3086d12e71b078da56128e1e324fe4a Mon Sep 17 00:00:00 2001 From: Erik Dubbelboer Date: Fri, 3 Feb 2023 15:41:39 +0800 Subject: [PATCH] Add support for Go 1.20 (#1481) --- .github/workflows/lint.yml | 4 ++-- .github/workflows/security.yml | 4 ++-- .github/workflows/test.yml | 2 +- b2s_new.go | 16 ++++++++++++++++ b2s_old.go | 16 ++++++++++++++++ bytesconv.go | 27 --------------------------- go.mod | 4 +++- go.sum | 8 -------- s2b_new.go | 11 +++++++++++ s2b_old.go | 24 ++++++++++++++++++++++++ 10 files changed, 75 insertions(+), 41 deletions(-) create mode 100644 b2s_new.go create mode 100644 b2s_old.go create mode 100644 s2b_new.go create mode 100644 s2b_old.go diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 5a0541d..fc1fe71 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -11,7 +11,7 @@ jobs: - uses: actions/checkout@v3 - uses: actions/setup-go@v3 with: - go-version: 1.19.x + go-version: 1.20.x - name: Get Go cache paths id: go-env @@ -28,7 +28,7 @@ jobs: ${{ steps.go-env.outputs.modcache }} - name: Install golangci-lint - run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.48.0 + run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.51.0 - name: Get golangci-lint cache path id: golangci-lint-cache-status diff --git a/.github/workflows/security.yml b/.github/workflows/security.yml index 1b4d2b2..bf0b4b3 100644 --- a/.github/workflows/security.yml +++ b/.github/workflows/security.yml @@ -8,7 +8,7 @@ jobs: test: strategy: matrix: - go-version: [1.19.x] + go-version: [1.20.x] platform: [ubuntu-latest] runs-on: ${{ matrix.platform }} env: @@ -16,6 +16,6 @@ jobs: steps: - uses: actions/checkout@v3 - name: Run Gosec Security Scanner - uses: securego/gosec@v2.12.0 + uses: securego/gosec@v2.14.0 with: args: '-exclude=G104,G304,G402 ./...' diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8fc0843..a731c51 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -8,7 +8,7 @@ jobs: test: strategy: matrix: - go-version: [1.16.x, 1.17.x, 1.18.x, 1.19.x] + go-version: [1.17.x, 1.18.x, 1.19.x, 1.20.x] os: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.os }} steps: diff --git a/b2s_new.go b/b2s_new.go new file mode 100644 index 0000000..2cbf5e3 --- /dev/null +++ b/b2s_new.go @@ -0,0 +1,16 @@ +//go:build go1.20 +// +build go1.20 + +package fasthttp + +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 { + if len(b) == 0 { + return "" + } + + return unsafe.String(&b[0], len(b)) +} diff --git a/b2s_old.go b/b2s_old.go new file mode 100644 index 0000000..f1d3228 --- /dev/null +++ b/b2s_old.go @@ -0,0 +1,16 @@ +//go:build !go1.20 +// +build !go1.20 + +package fasthttp + +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 { + /* #nosec G103 */ + return *(*string)(unsafe.Pointer(&b)) +} diff --git a/bytesconv.go b/bytesconv.go index 274082f..9b2ffeb 100644 --- a/bytesconv.go +++ b/bytesconv.go @@ -10,10 +10,8 @@ import ( "io" "math" "net" - "reflect" "sync" "time" - "unsafe" ) // AppendHTMLEscape appends html-escaped s to dst and returns the extended dst. @@ -317,31 +315,6 @@ func lowercaseBytes(b []byte) { } } -// 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 { - /* #nosec G103 */ - return *(*string)(unsafe.Pointer(&b)) -} - -// s2b converts string to a byte slice without memory allocation. -// -// Note it may break if string and/or slice header will change -// in the future go versions. -func s2b(s string) (b []byte) { - /* #nosec G103 */ - bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) - /* #nosec G103 */ - sh := (*reflect.StringHeader)(unsafe.Pointer(&s)) - bh.Data = sh.Data - bh.Cap = sh.Len - bh.Len = sh.Len - return b -} - // AppendUnquotedArg appends url-decoded src to dst and returns appended dst. // // dst may point to src. In this case src will be overwritten. diff --git a/go.mod b/go.mod index 0f02f59..baf709e 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/valyala/fasthttp -go 1.16 +go 1.20 require ( github.com/andybalholm/brotli v1.0.4 @@ -11,3 +11,5 @@ require ( golang.org/x/net v0.0.0-20220906165146-f3363e06e74c golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 ) + +require golang.org/x/text v0.3.7 // indirect diff --git a/go.sum b/go.sum index 1f0f0cd..7f98283 100644 --- a/go.sum +++ b/go.sum @@ -8,17 +8,9 @@ github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVS github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= golang.org/x/crypto v0.0.0-20220214200702-86341886e292 h1:f+lwQ+GtmgoY+A2YaQxlSOnDjXcQ7ZRLWOHbC6HtRqE= golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220906165146-f3363e06e74c h1:yKufUcDwucU5urd+50/Opbt4AYpqthk7wHpHok8f1lo= golang.org/x/net v0.0.0-20220906165146-f3363e06e74c/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 h1:WIoqL4EROvwiPdUtaip4VcDdpZ4kha7wBWZrbVKCIZg= golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/s2b_new.go b/s2b_new.go new file mode 100644 index 0000000..45ec2db --- /dev/null +++ b/s2b_new.go @@ -0,0 +1,11 @@ +//go:build go1.20 +// +build go1.20 + +package fasthttp + +import "unsafe" + +// s2b converts string to a byte slice without memory allocation. +func s2b(s string) []byte { + return unsafe.Slice(unsafe.StringData(s), len(s)) +} diff --git a/s2b_old.go b/s2b_old.go new file mode 100644 index 0000000..4cc141c --- /dev/null +++ b/s2b_old.go @@ -0,0 +1,24 @@ +//go:build !go1.20 +// +build !go1.20 + +package fasthttp + +import ( + "reflect" + "unsafe" +) + +// s2b converts string to a byte slice without memory allocation. +// +// Note it may break if string and/or slice header will change +// in the future go versions. +func s2b(s string) (b []byte) { + /* #nosec G103 */ + bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) + /* #nosec G103 */ + sh := (*reflect.StringHeader)(unsafe.Pointer(&s)) + bh.Data = sh.Data + bh.Cap = sh.Len + bh.Len = sh.Len + return b +}