Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bufPoolItem to support more methods #15

Closed
gonyyi opened this issue Jan 5, 2022 · 2 comments
Closed

bufPoolItem to support more methods #15

gonyyi opened this issue Jan 5, 2022 · 2 comments
Labels
Accepted This will be fixed or enhanced for upcoming release Coded Feature/fix is ready to be added to the code Feature - New New feature or request

Comments

@gonyyi
Copy link
Owner

gonyyi commented Jan 5, 2022

Now all the buf pool item from GetBuffer() has to saved to buf.Buf again as below.
And this became common bug when writing a code with buf pool item.

buf := gosl.GetBuffer()
buf.Buf = buf.Buf.WriteString(s.time.Format("2006/01/02 15:04:05"))

Add methods to simplify as below.

buf := gosl.GetBuffer()
buf.WriteString(s.time.Format("2006/01/02 15:04:05"))
@gonyyi gonyyi added Feature - New New feature or request Accepted This will be fixed or enhanced for upcoming release labels Jan 5, 2022
@gonyyi
Copy link
Owner Author

gonyyi commented Jan 10, 2022

// Bytes returns []byte of current buffer
func (b *bufPoolItem) Bytes() []byte {
	return b.Buf.Bytes()
}

// Cap returns current capacity
func (b *bufPoolItem) Cap() int {
	return b.Buf.Cap()
}

// Len returns current length
func (b *bufPoolItem) Len() int {
	return b.Buf.Len()
}

// Println prints current buffer
func (b *bufPoolItem) Println() {
	b.Buf.Println()
}

// Reset resets current buffer 
func (b *bufPoolItem) Reset() {
	b.Buf = b.Buf.Reset()
}

// Set resets and set current buffer with a given string
func (b *bufPoolItem) Set(s string) {
	b.Buf = b.Buf.Set(s)
}

// String returns current buffer in string format 
func (b *bufPoolItem) String() string {
	return b.Buf.String()
}

// Write writes bytes into current buffer 
// This meets Writer interface.
func (b *bufPoolItem) Write(p []byte) (n int, err error) {
	return b.Buf.Write(p)
}

// WriteBytes will write byte or bytes to current buffer 
func (b *bufPoolItem) WriteBytes(bytes ...byte) {
	b.Buf = b.Buf.WriteBytes(bytes...)
}

// WriteBool will write boolean t to current buffer in string
// true => "true", false => "false"
func (b *bufPoolItem) WriteBool(t bool) {
	b.Buf = b.Buf.WriteBool(t)
}

// WriteFloat will write float f with decimal point dec to current buffer
func (b *bufPoolItem) WriteFloat(f float64, dec uint8) {
	b.Buf = b.Buf.WriteFloat(f, dec)
}

// WriteInt will write integer i to buffer as string format
func (b *bufPoolItem) WriteInt(i int) {
	b.Buf = b.Buf.WriteInt(i)
}

// WriteString will write a string s to current buffer
func (b *bufPoolItem) WriteString(s string) {
	b.Buf = b.Buf.WriteString(s)
}

// WriteStrings will write string slice to current buffer with a delimiter
func (b *bufPoolItem) WriteStrings(s []string, delim ...byte) {
	b.Buf = b.Buf.WriteStrings(s, delim...)
}

// WriteTo will write current buffer to writer w. 
func (b *bufPoolItem) WriteTo(w Writer) (n int, err error) {
	return b.Buf.WriteTo(w)
}

For test cases,

func TestBuffer(t *testing.T) {
	b1 := gosl.GetBuffer()
	t.Run("Bytes()", func(t *testing.T) {
		b1.Set("test1")
		tmp := b1.Bytes()
		gosl.Test(t, true,  gosl.BytesEqual(b1.Buf, tmp))
	})
	t.Run("Cap()", func(t *testing.T) {
		b1.Set("test2")
		gosl.Test(t, true, b1.Buf.Cap() == b1.Cap())
	})
	t.Run("Len()", func(t *testing.T) {
		b1.Set("test3")
		gosl.Test(t, 5, b1.Len())
	})
	t.Run("Println()", func(t *testing.T) {
		b1.Set("test4")
		// b1.Println()
	})
	t.Run("Reset()", func(t *testing.T) {
		b1.Set("test5")
		b1.Reset()
		gosl.Test(t, 0, b1.Len())
	})
	t.Run("Set()", func(t *testing.T) {
		b1.Reset()
		b1.Set("test5")
		b1.Set("test6")
		gosl.Test(t, "test6", b1.String())
	})
	t.Run("String()", func(t *testing.T) {
		b1.Reset()
		b1.Set("test7")
		gosl.Test(t, "test7", b1.String())
	})
	t.Run("Write()", func(t *testing.T) {
		b1.Set("te")
		n, err := b1.Write([]byte("st8"))
		gosl.Test(t, nil, err)
		gosl.Test(t, 3, n)
		gosl.Test(t, "test8", b1.String())
	})
	t.Run("WriteBytes()", func(t *testing.T) {
		b1.Set("test9")
		b1.WriteBytes('-')
		b1.WriteBytes('1','2','3')
		gosl.Test(t, "test9-123", b1.String())
	})
	t.Run("WriteBool()", func(t *testing.T) {
		b1.Set("test10")
		b1.WriteBool(true)
		b1.WriteBool(false)
		gosl.Test(t, "test10truefalse", b1.String())
	})
	t.Run("WriteFloat()", func(t *testing.T) {
		b1.Set("test11-")
		b1.WriteFloat(3.141592, 2)
		gosl.Test(t, "test11-3.14", b1.String())
	})
	t.Run("WriteInt()", func(t *testing.T) {
		b1.Set("test12-")
		b1.WriteInt(1212)
		gosl.Test(t, "test12-1212", b1.String())
	})
	t.Run("WriteString()", func(t *testing.T) {
		b1.Set("test13-")
		b1.WriteString("done")
		gosl.Test(t, "test13-done", b1.String())
	})
	t.Run("WriteStrings()", func(t *testing.T) {
		b1.Set("test14-")
		b1.WriteStrings([]string{"a", "b", "c"}, ',', ' ')
		gosl.Test(t, "test14-a, b, c", b1.String())
	})
	t.Run("WriteTo()", func(t *testing.T) {
		tmpBuf := make(gosl.Buf, 0, 1024)
		b1.Set("test15")
		b1.WriteTo(&tmpBuf)
		gosl.Test(t, "test15", string(tmpBuf))
	})
}

@gonyyi gonyyi added the Coded Feature/fix is ready to be added to the code label Jan 10, 2022
@gonyyi
Copy link
Owner Author

gonyyi commented Jan 11, 2022

ef002fc

Done

@gonyyi gonyyi closed this as completed Jan 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Accepted This will be fixed or enhanced for upcoming release Coded Feature/fix is ready to be added to the code Feature - New New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant