-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatter.go
118 lines (106 loc) · 3.54 KB
/
formatter.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"regexp"
"strings"
)
// Formatter defines the format method that receives the value it should operate on and the current
// renamer state. All formatter must adhere to this interface to be part of a FormattingPipeline
type Formatter interface {
// Format receives a property value and the state of the renamer, applies the formatting
// implementation and returns the formatted string. The value is passed to the next formatter
// in the pipeline or inserted in the output string
Format(string, renamerState) (string, error)
}
// PaddingFormatter can pad a value with another character. For example, it can be used to zero-pad
// the counter: $cnt[%03]
type PaddingFormatter struct {
PadCharacter rune
PadLength int
}
// NewPaddingFormatter creates a new padding formatter that pads a property with the given char up
// to the maximum length
func NewPaddingFormatter(char rune, length int) PaddingFormatter {
return PaddingFormatter{
PadCharacter: char,
PadLength: length,
}
}
// Format applies padding to the given value with the character configured
func (f *PaddingFormatter) Format(value string, rstate renamerState) (string, error) {
if len(value) >= f.PadLength {
return value, nil
}
newValue := ""
idx := 0
for idx < f.PadLength-len(value) {
newValue += string(f.PadCharacter)
idx++
}
newValue += value
return newValue, nil
}
// SliceFormatter can cut a substring out of a value. The slice formatter is called with the >
// character and receives two parameters separated by ":": The beginning index at which to cut the value
// and the end index. If either value is left blank the system assumes 0 for the beginning and max
// for the end. For example, >:10 declares a formatter that cuts values to a maximum length of 10
// characters. If the starting index is higher than the length of the value the formatter will return
// an empty string.
type SliceFormatter struct {
Start int
End int
}
// NewSliceFormatter initializes a new SliceFormatter with the given start and end indexes
func NewSliceFormatter(start, end int) SliceFormatter {
return SliceFormatter{
Start: start,
End: end,
}
}
// Format trims the string to the start and end points specified by the SliceFormatter
func (f *SliceFormatter) Format(value string, rstate renamerState) (string, error) {
strlen := len(value)
strstart := 0
if f.Start > 0 {
strstart = f.Start
}
if strstart > strlen {
return "", nil
}
if f.End > 0 && f.End < strlen {
strlen = f.End
}
return value[strstart:strlen], nil
}
// ReplacingFormatter portions of a property's value that match the Pattern property with the
// string Value.
type ReplacingFormatter struct {
Pattern *regexp.Regexp
PatternString string
Value string
}
// NewReplacingFormatter creates a new replacing formatter and attempts to compile the given from
// string into a regular expression
func NewReplacingFormatter(from, to string) (ReplacingFormatter, error) {
regex, err := regexp.Compile(from)
if err != nil {
return ReplacingFormatter{}, err
}
return ReplacingFormatter{
Pattern: regex,
PatternString: from,
Value: to,
}, nil
}
// Format performs the replacement of the matched values in the given value string
func (f *ReplacingFormatter) Format(value string, rstate renamerState) (string, error) {
matches := f.Pattern.FindAllStringSubmatch(value, -1)
if matches == nil {
return value, nil
}
outValue := value
for _, m := range matches {
match := m[len(m)-1]
outValue = strings.ReplaceAll(outValue, match, f.Value)
}
return outValue, nil
}