1 // Copyright 2015 Huan Du. All rights reserved. 2 // Licensed under the MIT license that can be found in the LICENSE file. 3 4 package xstrings 5 6 const bufferMaxInitGrowSize = 2048 7 8 // Lazy initialize a buffer. 9 func allocBuffer(orig, cur string) *stringBuilder { 10 output := &stringBuilder{} 11 maxSize := len(orig) * 4 12 13 // Avoid to reserve too much memory at once. 14 if maxSize > bufferMaxInitGrowSize { 15 maxSize = bufferMaxInitGrowSize 16 } 17 18 output.Grow(maxSize) 19 output.WriteString(orig[:len(orig)-len(cur)]) 20 return output 21 } 22