1. new package nazastring 2. badge line size

pull/2/head
q191201771 5 years ago
parent a457b59d5c
commit 5d8805c735

@ -6,7 +6,7 @@ Go语言基础库
<a title="codecov" target="_blank" href="https://codecov.io/gh/q191201771/naza"><img src="https://codecov.io/gh/q191201771/naza/branch/master/graph/badge.svg?style=flat-square"></a>
<a title="goreportcard" target="_blank" href="https://goreportcard.com/report/github.com/q191201771/naza"><img src="https://goreportcard.com/badge/github.com/q191201771/naza?style=flat-square"></a>
<br>
<a title="codesize" target="_blank" href="https://github.com/q191201771/naza"><img src="https://img.shields.io/github/languages/code-size/q191201771/naza.svg?style=flat-square?style=flat-square"></a>
<a title="codeline" target="_blank" href="https://github.com/q191201771/naza"><img src="https://sloc.xyz/github/q191201771/naza/?category=code"></a>
<a title="license" target="_blank" href="https://github.com/q191201771/naza/blob/master/LICENSE"><img src="https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square"></a>
<a title="lastcommit" target="_blank" href="https://github.com/q191201771/naza/commits/master"><img src="https://img.shields.io/github/commit-activity/m/q191201771/naza.svg?style=flat-square"></a>
<a title="commitactivity" target="_blank" href="https://github.com/q191201771/naza/graphs/commit-activity"><img src="https://img.shields.io/github/last-commit/q191201771/naza.svg?style=flat-square"></a>

@ -0,0 +1,24 @@
package nazastring
import "unsafe"
type sliceT struct {
array unsafe.Pointer
len int
cap int
}
type stringStruct struct {
str unsafe.Pointer
len int
}
func SliceByteToStringTmp(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
func StringToSliceByteTmp(s string) []byte {
str := (*stringStruct)(unsafe.Pointer(&s))
ret := sliceT{array: unsafe.Pointer(str.str), len: str.len, cap: str.len}
return *(*[]byte)(unsafe.Pointer(&ret))
}

@ -0,0 +1,56 @@
package nazastring
import (
"bytes"
"github.com/q191201771/naza/pkg/assert"
"testing"
)
var inbuf = bytes.Repeat([]byte{'1', '2', '3', '4'}, 5678)
var instr = string(inbuf)
func TestSliceByteToStringTmp(t *testing.T) {
str := SliceByteToStringTmp(inbuf)
assert.Equal(t, instr, str)
}
func TestStringToSliceByteTmp(t *testing.T) {
buf := StringToSliceByteTmp(instr)
assert.Equal(t, inbuf, buf)
}
func BenchmarkSliceByteToStringOrigin(b *testing.B) {
var str string
for i := 0; i < b.N; i++ {
str = string(inbuf)
}
assert.Equal(b, instr, str)
}
func BenchmarkSliceByteToStringTmp(b *testing.B) {
var str string
for i := 0; i < b.N; i++ {
str = SliceByteToStringTmp(inbuf)
}
assert.Equal(b, instr, str)
}
func BenchmarkStringToSliceByteOrigin(b *testing.B) {
var buf []byte
for i := 0; i < b.N; i++ {
buf = []byte(instr)
}
assert.Equal(b, buf, inbuf)
}
func BenchmarkStringToSliceByteTmp(b *testing.B) {
var buf []byte
for i := 0; i < b.N; i++ {
buf = StringToSliceByteTmp(instr)
}
assert.Equal(b, buf, inbuf)
}
Loading…
Cancel
Save