| 1 | package toolsscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools`/ |
| 2 | |
| 3 | import ( |
| 4 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 5 | "regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/" |
| 6 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 7 | ) |
| 8 | |
| 9 | var ( |
| 10 | // quoteFieldRe greedily matches between matching pairs of '', "", or |
| 11 | // non-word characters. |
| 12 | quoteFieldRescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools`/quoteFieldRe. = regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.25.0 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 QuotedFieldsscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools`/QuotedFields().(s string) []string { |
| 27 | submatches := quoteFieldRescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools`/quoteFieldRe..FindAllStringSubmatchscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#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 Ljustscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools`/Ljust().(strs []string) []string { |
| 55 | llen := len(Longestscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools`/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 := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Repeatscip-go gomod github.com/golang/go/src go1.25.0 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 Rjustscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools`/Rjust().(strs []string) []string { |
| 73 | llen := len(Longestscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools`/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 := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Repeatscip-go gomod github.com/golang/go/src go1.25.0 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 Longestscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools`/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 Indentscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools`/Indent().(str string) string { |
| 110 | indented := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Replacescip-go gomod github.com/golang/go/src go1.25.0 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 | tabRescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools`/tabRe. = regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.25.0 regexp/MustCompile().(`(?m)^[ \t]+`) |
| 120 | ) |
| 121 | |
| 122 | // Undent removes all leading tabs in the given string "str", line-wise. |
| 123 | func Undentscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools`/Undent().(str string) string { |
| 124 | return tabRescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools`/tabRe..ReplaceAllStringscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#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 FormatFractionscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools`/FormatFraction().(x, y int64) string { |
| 132 | s := fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%d/", x) |
| 133 | if y >= 0 { |
| 134 | s += fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%d", y) |
| 135 | } else { |
| 136 | s += stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Repeatscip-go gomod github.com/golang/go/src go1.25.0 strings/Repeat().("?", len(s)-1) |
| 137 | } |
| 138 | return s |
| 139 | } |