-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b2e66a5
commit 9d5a7bf
Showing
10 changed files
with
75 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |