-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.go
201 lines (184 loc) · 4.29 KB
/
functions.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright © 2019 Praveen Tirumandyam [email protected]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package efp
import (
"strings"
"text/scanner"
"unicode"
)
// Concat implement Excel's CONCAT function
func concat(args ...string) string {
var b strings.Builder
for _, v := range args {
b.WriteString(v)
}
return b.String()
}
// Exact implments Excel's EXACT function
func Exact(a string, b string) bool {
if strings.Compare(a, b) != 0 {
return false
}
return true
}
// Find implements Excel's FIND function
// Deviation: pos is mandatory input
func Find(f, w string, pos int) int {
if pos > len(w) {
return -1
}
r := strings.Index(w[pos-1:], f)
if r == -1 {
return r
}
return pos + r
}
// Left implements Excel's LEFT function
// Deviation: No default value for l unlike Excel where it is 1
func Left(s string, l int) string {
if l > len(s) {
l = len(s)
}
return s[:l]
}
// Len implements Excel's LEN function
func Len(s string) int {
return len(s)
}
// Lower implements Excel's LOWER function
func Lower(s string) string {
return strings.ToLower(s)
}
// Mid implements Excel's MID function
func Mid(s string, strt, num int) string {
l := len(s)
if strt > l || strt < 1 || num < 0 {
return ""
}
if strt+num > l {
return s[strt-1:]
}
return s[strt-1 : strt-1+num]
}
// Proper implements Excel's PROPER function
func Proper(s string) string {
type scanState int
const (
inString scanState = iota
outString
)
var sb strings.Builder
var sc scanner.Scanner
currState := outString
sc.Init(strings.NewReader(s))
for rn := sc.Next(); rn != scanner.EOF; rn = sc.Next() {
if unicode.IsLetter(rn) {
switch currState {
case inString:
sb.WriteRune(unicode.ToLower(rn))
case outString:
currState = inString
sb.WriteRune(unicode.ToUpper(rn))
}
} else {
if currState == inString {
currState = outString
}
sb.WriteRune(rn)
}
}
return sb.String()
}
// Replace implements Excel's REPLACE function
func Replace(old string, strt, num int, newStr string) string {
strt = strt - 1
l := len(old)
if strt+num > l {
num = l - strt
if strt > l {
strt = l
num = 0
}
}
left := old[:strt]
right := old[strt+num:]
var sb strings.Builder
sb.WriteString(left)
sb.WriteString(newStr)
sb.WriteString(right)
return sb.String()
}
// Rept implements Excel's REPT function
func Rept(s string, r int) string {
var sb strings.Builder
for i := 0; i < r; i++ {
sb.WriteString(s)
}
return sb.String()
}
// Right implements Excel's RIGHT function
func Right(s string, num int) string {
if num <= 0 {
return ""
}
l := len(s)
if num > l {
return s
}
return s[l-num:]
}
// Search implements Excel's SEARCH function
// Deviation: pos parameter is mandatory in GO
func Search(fnd, in string, pos int) int {
fnd = strings.ToLower(fnd)
in = strings.ToLower(in)
return Find(fnd, in, pos)
}
// Substitute implements Excel's SUBSTITUTE function
// Deviation: Instance number is mandatory and if all instances need to be substituted provide 0
func Substitute(src, old, new string, inst int) string {
if inst > 0 {
substrA := strings.Split(src, old)
var sb strings.Builder
for i, val := range substrA {
if i > 0 {
switch i {
case inst:
sb.WriteString(new)
default:
sb.WriteString(old)
}
}
sb.WriteString(val)
}
return sb.String()
}
return strings.ReplaceAll(src, old, new)
}
// Trim implements Excel's TRIM function
func Trim(s string) string {
s = strings.TrimSpace(s)
var sc scanner.Scanner
var sb strings.Builder
sc.Init(strings.NewReader(s))
for tok := sc.Scan(); tok != scanner.EOF; tok = sc.Scan() {
sb.WriteString(sc.TokenText())
sb.WriteString(" ")
}
return strings.TrimSpace(sb.String())
}
// Upper implements Excel's UPPER function
func Upper(s string) string {
return strings.ToUpper(s)
}