Skip to content

Commit

Permalink
✨ feat: add buffer
Browse files Browse the repository at this point in the history
✅ test: update datetime testing case
  • Loading branch information
jingyuexing committed Jun 10, 2024
1 parent e7d0900 commit c7de414
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
39 changes: 39 additions & 0 deletions buffer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package utils

import "strings"


const (
hextable = "0123456789abcdef"
)

type Buffer []byte

func NewBuffer() Buffer {
return make([]byte,0)
}

func (b Buffer) Length() int{
return len(b) // the unint is byte
}

func (b Buffer) String() string {
str := make([]byte,b.Length() << 1)
j := 0
for _,item := range b {
str[j] = hextable[item>>4]
str[j+1] = hextable[item&0x0f]
j += 2
}
return string(str)
}

func (b Buffer) Load(data string) Buffer{
for i:= 0;i < len(data);i+=2 {
cache := 0x00
cache = strings.Index(hextable,string(data[i])) << 4
cache = cache | strings.Index(hextable,string(data[i+1]))
b = append(b, byte(cache))
}
return b
}
10 changes: 8 additions & 2 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,14 @@ func TestDateTime(t *testing.T) {
t.Error("caculate has wrong")
}

datetime2 := utils.NewDateTime()

datetime2 = *datetime2.Parse("2024/06/06/23:10:40","YYYY/MM/DD/HH:mm:ss")

if datetime2.Year != 2024 {
t.Error("paser has wrong")
}
fmt.Printf("解析后时间 %s",datetime2.String())
}

func TestNumberConver(t *testing.T) {
Expand Down Expand Up @@ -441,8 +449,6 @@ func TestReferenceString(t *testing.T){
func TestBuffer(t *testing.T){
bf := utils.NewBuffer()
bf = bf.Load("320c3a83c202880e83fa0814320c3a83c202880e83fa10")
fmt.Printf("%#v\n",bf)

if bf[0] != 0x32 {
t.Error(fmt.Sprintf("TestBuffer expect: %d,but got %d",0x32,bf[0]))
}
Expand Down

0 comments on commit c7de414

Please sign in to comment.