-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
string.go
83 lines (73 loc) · 2.07 KB
/
string.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package utils
import (
"strconv"
"strings"
)
func StringToInt(input string) (int, error) {
i, err := strconv.Atoi(input)
if err != nil {
return 0, err
}
return i, nil
}
func SnakeCaseToCamelCase(inputUnderScoreStr string) (camelCase string) {
//snake_case to camelCase
isToUpper := false
for k, v := range inputUnderScoreStr {
if k == 0 {
camelCase = strings.ToUpper(string(inputUnderScoreStr[0]))
} else {
if isToUpper {
camelCase += strings.ToUpper(string(v))
isToUpper = false
} else {
if v == '_' {
isToUpper = true
} else {
camelCase += string(v)
}
}
}
}
return
}
// https://github.com/DaddyOh/golang-samples/blob/master/pad.go
/*
* leftPad and rightPad just repoeat the padStr the indicated
* number of times
*
*/
func LeftPad(s string, padStr string, pLen int) string {
return strings.Repeat(padStr, pLen) + s
}
func RightPad(s string, padStr string, pLen int) string {
return s + strings.Repeat(padStr, pLen)
}
/* the Pad2Len functions are generally assumed to be padded with short sequences of strings
* in many cases with a single character sequence
*
* so we assume we can build the string out as if the char seq is 1 char and then
* just substr the string if it is longer than needed
*
* this means we are wasting some cpu and memory work
* but this always get us to want we want it to be
*
* in short not optimized to for massive string work
*
* If the overallLen is shorter than the original string length
* the string will be shortened to this length (substr)
*
*/
func RightPad2Len(s string, padStr string, overallLen int) string {
padCountInt := 1 + ((overallLen - len(padStr)) / len(padStr))
var retStr = s + strings.Repeat(padStr, padCountInt)
return retStr[:overallLen]
}
func LeftPad2Len(s string, padStr string, overallLen int) string {
padCountInt := 1 + ((overallLen - len(padStr)) / len(padStr))
var retStr = strings.Repeat(padStr, padCountInt) + s
return retStr[(len(retStr) - overallLen):]
}
func StripIndent(multilineStr string) string {
return strings.Replace(multilineStr, "\t", "", -1)
}