-
Notifications
You must be signed in to change notification settings - Fork 7
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
Dean Karn
committed
Jun 29, 2020
1 parent
4569437
commit eecc242
Showing
4 changed files
with
49 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
module github.com/go-playground/pkg/v4 | ||
|
||
require ( | ||
github.com/go-playground/assert v1.2.1 // indirect | ||
github.com/go-playground/assert/v2 v2.0.1 | ||
github.com/go-playground/form/v4 v4.0.0 | ||
github.com/go-playground/form/v4 v4.1.1 | ||
) | ||
|
||
go 1.13 | ||
go 1.14 |
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,23 @@ | ||
package unsafeext | ||
|
||
import ( | ||
"reflect" | ||
"unsafe" | ||
) | ||
|
||
// BytesToString converts an array of bytes into a string without allocating. | ||
// The byte slice passed to this function is not to be used after this call as it's unsafe; you have been warned. | ||
func BytesToString(b []byte) string { | ||
return *(*string)(unsafe.Pointer(&b)) | ||
} | ||
|
||
// StringToBytes converts an existing string into an []byte without allocating. | ||
// The string passed to this functions is not to be used again after this call as it's unsafe; you have been warned. | ||
func StringToBytes(s string) (b []byte) { | ||
strHdr := (*reflect.StringHeader)(unsafe.Pointer(&s)) | ||
sliceHdr := (*reflect.SliceHeader)(unsafe.Pointer(&b)) | ||
sliceHdr.Data = strHdr.Data | ||
sliceHdr.Cap = strHdr.Len | ||
sliceHdr.Len = strHdr.Len | ||
return | ||
} |
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,21 @@ | ||
package unsafeext | ||
|
||
import "testing" | ||
|
||
func TestBytesToString(t *testing.T) { | ||
b := []byte{'g', 'o', '-', 'p', 'l', 'a', 'y', 'g', 'r', 'o', 'u', 'n', 'd'} | ||
s := BytesToString(b) | ||
expected := string(b) | ||
if s != expected { | ||
t.Fatalf("expected '%s' got '%s'", expected, s) | ||
} | ||
} | ||
|
||
func TestStringToBytes(t *testing.T) { | ||
s := "go-playground" | ||
b := StringToBytes(s) | ||
|
||
if string(b) != s { | ||
t.Fatalf("expected '%s' got '%s'", s, string(b)) | ||
} | ||
} |