common.go 520 B

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