我可以使用 make 或 new 在 golang 中制作预填充字符串吗?
Can I make a prefilled string in golang with make or new?
我正在尝试用 Go 优化我的 stringpad 库。到目前为止,我发现用已知字符值(例如 0 或“”)填充字符串(实际上 bytes.Buffer)的唯一方法是使用 for 循环。
代码片段是:
// PadLeft pads string on left side with p, c times
func PadLeft(s string, p string, c int) string {
var t bytes.Buffer
if c <= 0 {
return s
}
if len(p) < 1 {
return s
}
for i := 0; i < c; i++ {
t.WriteString(p)
}
t.WriteString(s)
return t.String()
}
string pad越大我相信t缓冲区的内存副本越多。有没有更优雅的方法来制作一个已知大小的缓冲区,并在初始化时使用已知值?
您只能使用make()
and new()
to allocate buffers (byte slices or arrays) that are zeroed. You may use composite literals获取初始包含非零值的切片或数组,但不能动态描述初始值(索引必须是常量)。
从相似但非常高效的 strings.Repeat()
函数中汲取灵感。它以给定的计数重复给定的字符串:
func Repeat(s string, count int) string {
// Since we cannot return an error on overflow,
// we should panic if the repeat will generate
// an overflow.
// See Issue golang.org/issue/16237
if count < 0 {
panic("strings: negative Repeat count")
} else if count > 0 && len(s)*count/count != len(s) {
panic("strings: Repeat count causes overflow")
}
b := make([]byte, len(s)*count)
bp := copy(b, s)
for bp < len(b) {
copy(b[bp:], b[:bp])
bp *= 2
}
return string(b)
}
strings.Repeat()
进行一次分配以获得工作缓冲区(这将是一个字节切片 []byte
),并使用内置的 copy()
函数复制可重复的字符串。值得注意的一件事是它使用工作副本并尝试逐步复制整个副本,这意味着例如如果字符串已被复制 4 次,则复制此缓冲区将使其复制 8 次,依此类推。这将最大限度地减少对 copy()
的调用。该解决方案还利用 copy()
可以从 string
复制字节而无需将其转换为字节片。
我们想要的是类似的东西,但我们希望将结果添加到字符串之前。
我们可以解决这个问题,只需分配一个在内部使用的缓冲区 Repeat()
加上我们左填充的字符串的长度。
结果(不检查 count
参数):
func PadLeft(s, p string, count int) string {
ret := make([]byte, len(p)*count+len(s))
b := ret[:len(p)*count]
bp := copy(b, p)
for bp < len(b) {
copy(b[bp:], b[:bp])
bp *= 2
}
copy(ret[len(b):], s)
return string(ret)
}
正在测试:
fmt.Println(PadLeft("aa", "x", 1))
fmt.Println(PadLeft("aa", "x", 2))
fmt.Println(PadLeft("abc", "xy", 3))
输出(在 Go Playground 上尝试):
xaa
xxaa
xyxyxyabc
查看类似/相关问题:
我正在尝试用 Go 优化我的 stringpad 库。到目前为止,我发现用已知字符值(例如 0 或“”)填充字符串(实际上 bytes.Buffer)的唯一方法是使用 for 循环。
代码片段是:
// PadLeft pads string on left side with p, c times
func PadLeft(s string, p string, c int) string {
var t bytes.Buffer
if c <= 0 {
return s
}
if len(p) < 1 {
return s
}
for i := 0; i < c; i++ {
t.WriteString(p)
}
t.WriteString(s)
return t.String()
}
string pad越大我相信t缓冲区的内存副本越多。有没有更优雅的方法来制作一个已知大小的缓冲区,并在初始化时使用已知值?
您只能使用make()
and new()
to allocate buffers (byte slices or arrays) that are zeroed. You may use composite literals获取初始包含非零值的切片或数组,但不能动态描述初始值(索引必须是常量)。
从相似但非常高效的 strings.Repeat()
函数中汲取灵感。它以给定的计数重复给定的字符串:
func Repeat(s string, count int) string {
// Since we cannot return an error on overflow,
// we should panic if the repeat will generate
// an overflow.
// See Issue golang.org/issue/16237
if count < 0 {
panic("strings: negative Repeat count")
} else if count > 0 && len(s)*count/count != len(s) {
panic("strings: Repeat count causes overflow")
}
b := make([]byte, len(s)*count)
bp := copy(b, s)
for bp < len(b) {
copy(b[bp:], b[:bp])
bp *= 2
}
return string(b)
}
strings.Repeat()
进行一次分配以获得工作缓冲区(这将是一个字节切片 []byte
),并使用内置的 copy()
函数复制可重复的字符串。值得注意的一件事是它使用工作副本并尝试逐步复制整个副本,这意味着例如如果字符串已被复制 4 次,则复制此缓冲区将使其复制 8 次,依此类推。这将最大限度地减少对 copy()
的调用。该解决方案还利用 copy()
可以从 string
复制字节而无需将其转换为字节片。
我们想要的是类似的东西,但我们希望将结果添加到字符串之前。
我们可以解决这个问题,只需分配一个在内部使用的缓冲区 Repeat()
加上我们左填充的字符串的长度。
结果(不检查 count
参数):
func PadLeft(s, p string, count int) string {
ret := make([]byte, len(p)*count+len(s))
b := ret[:len(p)*count]
bp := copy(b, p)
for bp < len(b) {
copy(b[bp:], b[:bp])
bp *= 2
}
copy(ret[len(b):], s)
return string(ret)
}
正在测试:
fmt.Println(PadLeft("aa", "x", 1))
fmt.Println(PadLeft("aa", "x", 2))
fmt.Println(PadLeft("abc", "xy", 3))
输出(在 Go Playground 上尝试):
xaa
xxaa
xyxyxyabc
查看类似/相关问题: