| 1 | package tools |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "regexp" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | var ( |
| 10 | // quoteFieldRe greedily matches between matching pairs of '', "", or |
| 11 | // non-word characters. |
| 12 | quoteFieldRe = regexp.MustCompile("'(.*)'|\"(.*)\"|(\\S*)") |
| 13 | ) |
| 14 | |
| 15 | // QuotedFields is an alternative to strings.Fields (see: |
| 16 | // https://golang.org/pkg/strings#Fields) that respects spaces between matching |
| 17 | // pairs of quotation delimiters. |
| 18 | // |
| 19 | // For instance, the quoted fields of the string "foo bar 'baz etc'" would be: |
| 20 | // |
| 21 | // []string{"foo", "bar", "baz etc"} |
| 22 | // |
| 23 | // Whereas the same argument given to strings.Fields, would return: |
| 24 | // |
| 25 | // []string{"foo", "bar", "'baz", "etc'"} |
| 26 | func QuotedFields(s string) []string { |
| 27 | submatches := quoteFieldRe.FindAllStringSubmatch(s, -1) |
| 28 | out := make([]string, 0, len(submatches)) |
| 29 | |
| 30 | for _, matches := range submatches { |
| 31 | // if a leading or trailing space is found, ignore that |
| 32 | if matches[0] == "" { |
| 33 | continue |
| 34 | } |
| 35 | |
| 36 | // otherwise, find the first non-empty match (inside balanced |
| 37 | // quotes, or a space-delimited string) |
| 38 | var str string |
| 39 | for _, m := range matches[1:] { |
| 40 | if len(m) > 0 { |
| 41 | str = m |
| 42 | break |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | out = append(out, str) |
| 47 | } |
| 48 | |
| 49 | return out |
| 50 | } |
| 51 | |
| 52 | // Ljust returns a copied string slice where each element is left justified to |
| 53 | // match the width of the longest element in the set. |
| 54 | func Ljust(strs []string) []string { |
| 55 | llen := len(Longest(strs)) |
| 56 | |
| 57 | dup := make([]string, len(strs), cap(strs)) |
| 58 | copy(dup, strs) |
| 59 | |
| 60 | for i, str := range strs { |
| 61 | width := max(0, llen-len(str)) |
| 62 | padding := strings.Repeat(" ", width) |
| 63 | |
| 64 | dup[i] = str + padding |
| 65 | } |
| 66 | |
| 67 | return dup |
| 68 | } |
| 69 | |
| 70 | // Rjust returns a copied string slice where each element is right justified to |
| 71 | // match the width of the longest element in the set. |
| 72 | func Rjust(strs []string) []string { |
| 73 | llen := len(Longest(strs)) |
| 74 | |
| 75 | dup := make([]string, len(strs), cap(strs)) |
| 76 | copy(dup, strs) |
| 77 | |
| 78 | for i, str := range strs { |
| 79 | width := max(0, llen-len(str)) |
| 80 | padding := strings.Repeat(" ", width) |
| 81 | |
| 82 | dup[i] = padding + str |
| 83 | } |
| 84 | |
| 85 | return dup |
| 86 | } |
| 87 | |
| 88 | // Longest returns the longest element in the string slice in O(n) time and O(1) |
| 89 | // space. If strs is empty or nil, an empty string will be returned. |
| 90 | func Longest(strs []string) string { |
| 91 | if len(strs) == 0 { |
| 92 | return "" |
| 93 | } |
| 94 | |
| 95 | var longest string |
| 96 | var llen int |
| 97 | for _, str := range strs { |
| 98 | if len(str) >= llen { |
| 99 | longest = str |
| 100 | llen = len(longest) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return longest |
| 105 | } |
| 106 | |
| 107 | // Indent returns a string which prepends "\t" TAB characters to the beginning |
| 108 | // of each line in the given string "str". |
| 109 | func Indent(str string) string { |
| 110 | indented := strings.Replace(str, "\n", "\n\t", -1) |
| 111 | if len(indented) > 0 { |
| 112 | indented = "\t" + indented |
| 113 | } |
| 114 | |
| 115 | return indented |
| 116 | } |
| 117 | |
| 118 | var ( |
| 119 | tabRe = regexp.MustCompile(`(?m)^[ \t]+`) |
| 120 | ) |
| 121 | |
| 122 | // Undent removes all leading tabs in the given string "str", line-wise. |
| 123 | func Undent(str string) string { |
| 124 | return tabRe.ReplaceAllString(str, "") |
| 125 | } |
| 126 | |
| 127 | // FormatFraction returns a string in the format "x/y" if y >= 0. |
| 128 | // If y < 0, a string in the format "x/?" is returned, with the |
| 129 | // number of "?" characters equal to the length of x when formatted |
| 130 | // as a decimal integer. |
| 131 | func FormatFraction(x, y int64) string { |
| 132 | s := fmt.Sprintf("%d/", x) |
| 133 | if y >= 0 { |
| 134 | s += fmt.Sprintf("%d", y) |
| 135 | } else { |
| 136 | s += strings.Repeat("?", len(s)-1) |
| 137 | } |
| 138 | return s |
| 139 | } |