You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

format.go 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. import (
  5. "bytes"
  6. "unicode/utf8"
  7. )
  8. // ExpandTabs can expand tabs ('\t') rune in str to one or more spaces dpending on
  9. // current column and tabSize.
  10. // The column number is reset to zero after each newline ('\n') occurring in the str.
  11. //
  12. // ExpandTabs uses RuneWidth to decide rune's width.
  13. // For example, CJK characters will be treated as two characters.
  14. //
  15. // If tabSize <= 0, ExpandTabs panics with error.
  16. //
  17. // Samples:
  18. // ExpandTabs("a\tbc\tdef\tghij\tk", 4) => "a bc def ghij k"
  19. // ExpandTabs("abcdefg\thij\nk\tl", 4) => "abcdefg hij\nk l"
  20. // ExpandTabs("z中\t文\tw", 4) => "z中 文 w"
  21. func ExpandTabs(str string, tabSize int) string {
  22. if tabSize <= 0 {
  23. panic("tab size must be positive")
  24. }
  25. var r rune
  26. var i, size, column, expand int
  27. var output *bytes.Buffer
  28. orig := str
  29. for len(str) > 0 {
  30. r, size = utf8.DecodeRuneInString(str)
  31. if r == '\t' {
  32. expand = tabSize - column%tabSize
  33. if output == nil {
  34. output = allocBuffer(orig, str)
  35. }
  36. for i = 0; i < expand; i++ {
  37. output.WriteByte(byte(' '))
  38. }
  39. column += expand
  40. } else {
  41. if r == '\n' {
  42. column = 0
  43. } else {
  44. column += RuneWidth(r)
  45. }
  46. if output != nil {
  47. output.WriteRune(r)
  48. }
  49. }
  50. str = str[size:]
  51. }
  52. if output == nil {
  53. return orig
  54. }
  55. return output.String()
  56. }
  57. // LeftJustify returns a string with pad string at right side if str's rune length is smaller than length.
  58. // If str's rune length is larger than length, str itself will be returned.
  59. //
  60. // If pad is an empty string, str will be returned.
  61. //
  62. // Samples:
  63. // LeftJustify("hello", 4, " ") => "hello"
  64. // LeftJustify("hello", 10, " ") => "hello "
  65. // LeftJustify("hello", 10, "123") => "hello12312"
  66. func LeftJustify(str string, length int, pad string) string {
  67. l := Len(str)
  68. if l >= length || pad == "" {
  69. return str
  70. }
  71. remains := length - l
  72. padLen := Len(pad)
  73. output := &bytes.Buffer{}
  74. output.Grow(len(str) + (remains/padLen+1)*len(pad))
  75. output.WriteString(str)
  76. writePadString(output, pad, padLen, remains)
  77. return output.String()
  78. }
  79. // RightJustify returns a string with pad string at left side if str's rune length is smaller than length.
  80. // If str's rune length is larger than length, str itself will be returned.
  81. //
  82. // If pad is an empty string, str will be returned.
  83. //
  84. // Samples:
  85. // RightJustify("hello", 4, " ") => "hello"
  86. // RightJustify("hello", 10, " ") => " hello"
  87. // RightJustify("hello", 10, "123") => "12312hello"
  88. func RightJustify(str string, length int, pad string) string {
  89. l := Len(str)
  90. if l >= length || pad == "" {
  91. return str
  92. }
  93. remains := length - l
  94. padLen := Len(pad)
  95. output := &bytes.Buffer{}
  96. output.Grow(len(str) + (remains/padLen+1)*len(pad))
  97. writePadString(output, pad, padLen, remains)
  98. output.WriteString(str)
  99. return output.String()
  100. }
  101. // Center returns a string with pad string at both side if str's rune length is smaller than length.
  102. // If str's rune length is larger than length, str itself will be returned.
  103. //
  104. // If pad is an empty string, str will be returned.
  105. //
  106. // Samples:
  107. // Center("hello", 4, " ") => "hello"
  108. // Center("hello", 10, " ") => " hello "
  109. // Center("hello", 10, "123") => "12hello123"
  110. func Center(str string, length int, pad string) string {
  111. l := Len(str)
  112. if l >= length || pad == "" {
  113. return str
  114. }
  115. remains := length - l
  116. padLen := Len(pad)
  117. output := &bytes.Buffer{}
  118. output.Grow(len(str) + (remains/padLen+1)*len(pad))
  119. writePadString(output, pad, padLen, remains/2)
  120. output.WriteString(str)
  121. writePadString(output, pad, padLen, (remains+1)/2)
  122. return output.String()
  123. }
  124. func writePadString(output *bytes.Buffer, pad string, padLen, remains int) {
  125. var r rune
  126. var size int
  127. repeats := remains / padLen
  128. for i := 0; i < repeats; i++ {
  129. output.WriteString(pad)
  130. }
  131. remains = remains % padLen
  132. if remains != 0 {
  133. for i := 0; i < remains; i++ {
  134. r, size = utf8.DecodeRuneInString(pad)
  135. output.WriteRune(r)
  136. pad = pad[size:]
  137. }
  138. }
  139. }