Skip to content

Commit

Permalink
Merge pull request #306 from projectdiscovery/add_conversion_pkg
Browse files Browse the repository at this point in the history
add conversion pkg
  • Loading branch information
Mzack9999 authored Jan 10, 2024
2 parents c0f279c + 577730d commit 3d0e493
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
14 changes: 14 additions & 0 deletions conversion/conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package conversion

import "unsafe"

func Bytes(s string) []byte {
return unsafe.Slice(unsafe.StringData(s), len(s))
}

func String(b []byte) string {
if len(b) == 0 {
return ""
}
return unsafe.String(unsafe.SliceData(b), len(b))
}
40 changes: 40 additions & 0 deletions conversion/conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package conversion

import (
"bytes"
"testing"
)

func TestBytes(t *testing.T) {
testCases := []struct {
input string
expected []byte
}{
{"test", []byte("test")},
{"", []byte("")},
}

for _, tc := range testCases {
result := Bytes(tc.input)
if !bytes.Equal(result, tc.expected) {
t.Errorf("Expected %v, but got %v", tc.expected, result)
}
}
}

func TestString(t *testing.T) {
testCases := []struct {
input []byte
expected string
}{
{[]byte("test"), "test"},
{[]byte(""), ""},
}

for _, tc := range testCases {
result := String(tc.input)
if result != tc.expected {
t.Errorf("Expected %s, but got %s", tc.expected, result)
}
}
}

0 comments on commit 3d0e493

Please sign in to comment.