Skip to content

Instantly share code, notes, and snippets.

@ice1000
Created January 1, 2017 16:46
Show Gist options
  • Save ice1000/781c58a4a81058481a1c5d790d3c55be to your computer and use it in GitHub Desktop.
Save ice1000/781c58a4a81058481a1c5d790d3c55be to your computer and use it in GitHub Desktop.
Java-like StringBuffer for Golang
package sb
import (
"fmt"
"strings"
)
const maxLength = 150
/// a java style string buffer
type stringBuffer struct {
data []string
index int
indent int
}
func NewStringBuffer() *stringBuffer {
ret := new(stringBuffer)
ret.index = 0
ret.indent = 0
ret.data = make([]string, maxLength)
return ret
}
/// append a string to the tail of this buffer
func (sb *stringBuffer) Append(str string) *stringBuffer {
sb.data[sb.index] = strings.Repeat("\t", sb.indent) + str
sb.index++
if sb.index >= maxLength {
sb.slice()
}
return sb
}
func (sb *stringBuffer) AppendFormat(str string, args ...interface{}) *stringBuffer {
return sb.Append(fmt.Sprintf(str, args...))
}
func (sb *stringBuffer) AppendLine(str string) *stringBuffer {
return sb.Append(str + "\n")
}
func (sb *stringBuffer) AppendLineIndent(str string) *stringBuffer {
sb.AppendLine(str)
sb.indent++
return sb
}
func (sb *stringBuffer) AppendLineClose(str string) *stringBuffer {
sb.indent--
return sb.AppendLine(str)
}
func (sb *stringBuffer) AppendClose(str string) *stringBuffer {
sb.indent--
return sb.Append(str)
}
func (sb *stringBuffer) slice() *stringBuffer {
sb.data[0] = sb.ToString()
sb.index = 1
return sb
}
/// append a line as comment.
func (sb *stringBuffer) AppendComment(str string) *stringBuffer {
return sb.AppendLine("/// " + str)
}
func (sb *stringBuffer) ToString() string {
return strings.Join(sb.data, "")
}
/// clear elements.
func (sb *stringBuffer) Clear() {
sb.indent = 0
}
@gonejack
Copy link

gonejack commented Feb 5, 2018

Missing the setlength method for easy truncating, isn't it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment