1
// Copyright 2015 Huan Du. All rights reserved.
2
// Licensed under the MIT license that can be found in the LICENSE file.
10
// ExpandTabs can expand tabs ('\t') rune in str to one or more spaces dpending on
11
// current column and tabSize.
12
// The column number is reset to zero after each newline ('\n') occurring in the str.
14
// ExpandTabs uses RuneWidth to decide rune's width.
15
// For example, CJK characters will be treated as two characters.
17
// If tabSize <= 0, ExpandTabs panics with error.
20
// ExpandTabs("a\tbc\tdef\tghij\tk", 4) => "a bc def ghij k"
21
// ExpandTabs("abcdefg\thij\nk\tl", 4) => "abcdefg hij\nk l"
22
// ExpandTabs("z中\t文\tw", 4) => "z中 文 w"
23
func ExpandTabs(str string, tabSize int) string {
25
panic("tab size must be positive")
29
var i, size, column, expand int
30
var output *stringBuilder
35
r, size = utf8.DecodeRuneInString(str)
38
expand = tabSize - column%tabSize
41
output = allocBuffer(orig, str)
44
for i = 0; i < expand; i++ {
53
column += RuneWidth(r)
68
return output.String()
71
// LeftJustify returns a string with pad string at right side if str's rune length is smaller than length.
72
// If str's rune length is larger than length, str itself will be returned.
74
// If pad is an empty string, str will be returned.
77
// LeftJustify("hello", 4, " ") => "hello"
78
// LeftJustify("hello", 10, " ") => "hello "
79
// LeftJustify("hello", 10, "123") => "hello12312"
80
func LeftJustify(str string, length int, pad string) string {
83
if l >= length || pad == "" {
90
output := &stringBuilder{}
91
output.Grow(len(str) + (remains/padLen+1)*len(pad))
92
output.WriteString(str)
93
writePadString(output, pad, padLen, remains)
94
return output.String()
97
// RightJustify returns a string with pad string at left side if str's rune length is smaller than length.
98
// If str's rune length is larger than length, str itself will be returned.
100
// If pad is an empty string, str will be returned.
103
// RightJustify("hello", 4, " ") => "hello"
104
// RightJustify("hello", 10, " ") => " hello"
105
// RightJustify("hello", 10, "123") => "12312hello"
106
func RightJustify(str string, length int, pad string) string {
109
if l >= length || pad == "" {
113
remains := length - l
116
output := &stringBuilder{}
117
output.Grow(len(str) + (remains/padLen+1)*len(pad))
118
writePadString(output, pad, padLen, remains)
119
output.WriteString(str)
120
return output.String()
123
// Center returns a string with pad string at both side if str's rune length is smaller than length.
124
// If str's rune length is larger than length, str itself will be returned.
126
// If pad is an empty string, str will be returned.
129
// Center("hello", 4, " ") => "hello"
130
// Center("hello", 10, " ") => " hello "
131
// Center("hello", 10, "123") => "12hello123"
132
func Center(str string, length int, pad string) string {
135
if l >= length || pad == "" {
139
remains := length - l
142
output := &stringBuilder{}
143
output.Grow(len(str) + (remains/padLen+1)*len(pad))
144
writePadString(output, pad, padLen, remains/2)
145
output.WriteString(str)
146
writePadString(output, pad, padLen, (remains+1)/2)
147
return output.String()
150
func writePadString(output *stringBuilder, pad string, padLen, remains int) {
154
repeats := remains / padLen
156
for i := 0; i < repeats; i++ {
157
output.WriteString(pad)
160
remains = remains % padLen
163
for i := 0; i < remains; i++ {
164
r, size = utf8.DecodeRuneInString(pad)