-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstring_funcs.go
61 lines (55 loc) · 1.88 KB
/
string_funcs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// -----------------------------------------------------------------------------
// github.com/balacode/udpt /[string_funcs.go]
// (c) [email protected] License: MIT
// -----------------------------------------------------------------------------
package udpt
import (
"fmt"
"strings"
)
// getPart returns the substring between 'prefix' and 'suffix'.
//
// When the prefix is blank, returns the part from the beginning of 's'.
//
// When the suffix is blank, returns the part up to the end of 's'.
// I.e. if prefix and suffix are both blank, returns 's'.
//
// When either prefix or suffix is not found, returns a zero-length string.
//
func getPart(s, prefix, suffix string) string {
at := strings.Index(s, prefix)
if at == -1 {
return ""
}
s = s[at+len(prefix):]
if suffix == "" {
return s
}
at = strings.Index(s, suffix)
if at == -1 {
return ""
}
return s[:at]
} // getPart
// joinArgs joins 'a' into a single string, with a space between arguments.
func joinArgs(tag string, a ...interface{}) string {
ar := make([]string, len(a))
for i, arg := range a {
ar[i] = fmt.Sprint(arg)
}
ret := strings.TrimSpace(tag + " " + strings.Join(ar, " "))
return ret
} // joinArgs
// padf suffixes a string with spaces to make sure it is at least
// 'minLength' characters wide. I.e. the string is left-aligned.
//
// If the string is wider than 'minLength', returns the string as it is.
//
func padf(minLength int, format string, a ...interface{}) string {
format = fmt.Sprintf(format, a...)
if len(format) < minLength {
return format + strings.Repeat(" ", minLength-len(format))
}
return format
} // padf
// end