2020-01-28 08:57:15 -05:00
|
|
|
// Copyright 2015 Huan Du. All rights reserved.
|
|
|
|
// Licensed under the MIT license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package xstrings
|
|
|
|
|
|
|
|
const bufferMaxInitGrowSize = 2048
|
|
|
|
|
|
|
|
// Lazy initialize a buffer.
|
2020-10-16 01:06:27 -04:00
|
|
|
func allocBuffer(orig, cur string) *stringBuilder {
|
|
|
|
output := &stringBuilder{}
|
2020-01-28 08:57:15 -05:00
|
|
|
maxSize := len(orig) * 4
|
|
|
|
|
|
|
|
// Avoid to reserve too much memory at once.
|
|
|
|
if maxSize > bufferMaxInitGrowSize {
|
|
|
|
maxSize = bufferMaxInitGrowSize
|
|
|
|
}
|
|
|
|
|
|
|
|
output.Grow(maxSize)
|
|
|
|
output.WriteString(orig[:len(orig)-len(cur)])
|
|
|
|
return output
|
|
|
|
}
|