Skip to content

Commit

Permalink
add conversion pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
dogancanbakir committed Jan 9, 2024
1 parent c0f279c commit 577730d
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 577730d

Please sign in to comment.