Skip to content

Commit

Permalink
feat: add internal bytesconv
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed Jun 4, 2024
1 parent ede210b commit f05e418
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 6 deletions.
5 changes: 3 additions & 2 deletions confuse/alphabet.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (
cryptoRand "crypto/rand"
"math/bits"
"math/rand/v2"
"unsafe"

"github.com/things-go/proc/internal/bytesconv"
)

// Previous defined bytes, do not change this.
Expand Down Expand Up @@ -71,7 +72,7 @@ func Bytes(length int, alphabets ...byte) []byte {

func randString(length int, alphabets []byte) string {
b := randBytes(length, alphabets)
return *(*string)(unsafe.Pointer(&b))
return bytesconv.BytesToString(b)
}

func randBytes(length int, alphabets []byte) []byte {
Expand Down
5 changes: 3 additions & 2 deletions confuse/complexity.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import (
"math/big"
"math/rand/v2"
"strings"
"unsafe"

"github.com/things-go/proc/internal/bytesconv"
)

const (
Expand Down Expand Up @@ -174,7 +175,7 @@ func (sf *Complexity) Generate(n int) string {
}
buffer[j] = sf.chars[idx]
}
v := *(*string)(unsafe.Pointer(&buffer))
v := bytesconv.BytesToString(buffer)
if sf.IsComplexEnough(v) && v[:1] != " " && v[n-1:] != " " {
return v
}
Expand Down
51 changes: 51 additions & 0 deletions infra/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,54 @@ func Test_PascalCase(t *testing.T) {
})
}
}

func Test_LowTitle(t *testing.T) {
tests := []struct {
name string
args string
want string
}{
{
name: "empty",
args: "",
want: "",
},
{
name: "start with capital letter",
args: "PabBBicNotify",
want: "pabBBicNotify",
},
{
name: "start with lower letter",
args: "pabBBicNotify",
want: "pabBBicNotify",
},
{
name: "start with number",
args: "1PabBBicNotify",
want: "1PabBBicNotify",
},
{
name: "start with _",
args: "_abc",
want: "_abc",
},
{
name: "start with space",
args: "\u85FFabc",
want: "\u85FFabc",
},
{
name: "start with 中文",
args: "中abc",
want: "中abc",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := LowTitle(tt.args); got != tt.want {
t.Errorf("LowTitle() = %v, want %v", got, tt.want)
}
})
}
}
17 changes: 17 additions & 0 deletions internal/bytesconv/bytesconv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package bytesconv

import (
"unsafe"
)

// StringToBytes converts string to byte slice without a memory allocation.
// For more details, see https://github.com/golang/go/issues/53003#issuecomment-1140276077.
func StringToBytes(s string) []byte {
return unsafe.Slice(unsafe.StringData(s), len(s))
}

// BytesToString converts byte slice to string without a memory allocation.
// For more details, see https://github.com/golang/go/issues/53003#issuecomment-1140276077.
func BytesToString(b []byte) string {
return unsafe.String(unsafe.SliceData(b), len(b))
}
5 changes: 3 additions & 2 deletions kata/signature/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"crypto/rsa"
"encoding/base64"
"errors"
"unsafe"

"github.com/things-go/proc/internal/bytesconv"
)

// error defined
Expand Down Expand Up @@ -39,7 +40,7 @@ func RsaDecrypt(pri *rsa.PrivateKey, ciphertext string) (string, error) {
if err != nil {
return "", err
}
return *(*string)(unsafe.Pointer(&bb)), nil
return bytesconv.BytesToString(bb), nil
}

// AesCbcEncrypt aes cbc, iv + ciphertext with base64 encoded.
Expand Down

0 comments on commit f05e418

Please sign in to comment.