-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathformatter.go
411 lines (382 loc) · 11 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package stringFormatter
import (
"fmt"
"strconv"
"strings"
)
const argumentFormatSeparator = ":"
const bytesPerArgDefault = 20
// Format
/* Func that makes string formatting from template
* It differs from above function only by generic interface that allow to use only primitive data types:
* - integers (int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uin64)
* - floats (float32, float64)
* - boolean
* - string
* - complex
* - objects
* This function defines format automatically
* Parameters
* - template - string that contains template
* - args - values that are using for formatting with template
* Returns formatted string
*/
func Format(template string, args ...any) string {
if args == nil {
return template
}
start := strings.Index(template, "{")
if start < 0 {
return template
}
templateLen := len(template)
formattedStr := &strings.Builder{}
argsLen := bytesPerArgDefault * len(args)
formattedStr.Grow(templateLen + argsLen + 1)
j := -1 //nolint:ineffassign
nestedBrackets := false
formattedStr.WriteString(template[:start])
for i := start; i < templateLen; i++ {
if template[i] == '{' {
// possibly it is a template placeholder
if i == templateLen-1 {
// if we gave { at the end of line i.e. -> type serviceHealth struct {,
// without this write we got type serviceHealth struct
formattedStr.WriteByte('{')
break
}
// considering in 2 phases - {{ }}
if template[i+1] == '{' {
formattedStr.WriteByte('{')
continue
}
// find end of placeholder
// process empty pair - {}
if template[i+1] == '}' {
i++
formattedStr.WriteString("{}")
continue
}
// process non-empty placeholder
j = i + 2
for {
if j >= templateLen {
break
}
if template[j] == '{' {
// multiple nested curly brackets ...
nestedBrackets = true
formattedStr.WriteString(template[i:j])
i = j
}
if template[j] == '}' {
break
}
j++
}
// double curly brackets processed here, convert {{N}} -> {N}
// so we catch here {{N}
if j+1 < templateLen && template[j+1] == '}' && template[i-1] == '{' {
formattedStr.WriteString(template[i+1 : j+1])
i = j + 1
} else {
argNumberStr := template[i+1 : j]
// is here we should support formatting ?
var argNumber int
var err error
var argFormatOptions string
if len(argNumberStr) == 1 {
// this calculation makes work a little faster than AtoI
argNumber = int(argNumberStr[0] - '0')
} else {
argNumber = -1
// Here we are going to process argument either with additional formatting or not
// i.e. 0 for arg without formatting && 0:format for an argument wit formatting
// todo(UMV): we could format json or yaml here ...
formatOptionIndex := strings.Index(argNumberStr, argumentFormatSeparator)
// formatOptionIndex can't be == 0, because 0 is a position of arg number
if formatOptionIndex > 0 {
// trim formatting string to remove spaces
argFormatOptions = strings.Trim(argNumberStr[formatOptionIndex+1:], " ")
argNumberStrPart := argNumberStr[:formatOptionIndex]
argNumber, err = strconv.Atoi(strings.Trim(argNumberStrPart, " "))
if err == nil {
argNumberStr = argNumberStrPart
}
// make formatting option str for further pass to an argument
}
//
if argNumber < 0 {
argNumber, err = strconv.Atoi(argNumberStr)
}
}
if (err == nil || (argFormatOptions != "" && !nestedBrackets)) &&
len(args) > argNumber {
// get number from placeholder
strVal := getItemAsStr(&args[argNumber], &argFormatOptions)
formattedStr.WriteString(strVal)
} else {
formattedStr.WriteString(template[i:j])
if j < templateLen-1 {
formattedStr.WriteByte(template[j])
}
}
i = j
}
} else {
j = i //nolint:ineffassign
formattedStr.WriteByte(template[i])
}
}
return formattedStr.String()
}
// FormatComplex
/* Function that format text using more complex templates contains string literals i.e "Hello {username} here is our application {appname}
* Parameters
* - template - string that contains template
* - args - values (dictionary: string key - any value) that are using for formatting with template
* Returns formatted string
*/
func FormatComplex(template string, args map[string]any) string {
if args == nil {
return template
}
start := strings.Index(template, "{")
if start < 0 {
return template
}
templateLen := len(template)
formattedStr := &strings.Builder{}
argsLen := bytesPerArgDefault * len(args)
formattedStr.Grow(templateLen + argsLen + 1)
j := -1 //nolint:ineffassign
nestedBrackets := false
formattedStr.WriteString(template[:start])
for i := start; i < templateLen; i++ {
if template[i] == '{' {
// possibly it is a template placeholder
if i == templateLen-1 {
// if we gave { at the end of line i.e. -> type serviceHealth struct {,
// without this write we got type serviceHealth struct
formattedStr.WriteByte('{')
break
}
if template[i+1] == '{' {
formattedStr.WriteByte('{')
continue
}
// find end of placeholder
// process empty pair - {}
if template[i+1] == '}' {
i++
formattedStr.WriteString("{}")
continue
}
// process non-empty placeholder
// find end of placeholder
j = i + 2
for {
if j >= templateLen {
break
}
if template[j] == '{' {
// multiple nested curly brackets ...
nestedBrackets = true
formattedStr.WriteString(template[i:j])
i = j
}
if template[j] == '}' {
break
}
j++
}
// double curly brackets processed here, convert {{N}} -> {N}
// so we catch here {{N}
if j+1 < templateLen && template[j+1] == '}' {
formattedStr.WriteString(template[i+1 : j+1])
i = j + 1
} else {
var argFormatOptions string
argNumberStr := template[i+1 : j]
arg, ok := args[argNumberStr]
if !ok {
formatOptionIndex := strings.Index(argNumberStr, argumentFormatSeparator)
if formatOptionIndex >= 0 {
argFormatOptions = strings.Trim(argNumberStr[formatOptionIndex+1:], " ")
argNumberStr = strings.Trim(argNumberStr[:formatOptionIndex], " ")
}
arg, ok = args[argNumberStr]
}
if ok || (argFormatOptions != "" && !nestedBrackets) {
// get number from placeholder
strVal := ""
if arg != nil {
strVal = getItemAsStr(&arg, &argFormatOptions)
} else {
formattedStr.WriteString(template[i:j])
if j < templateLen-1 {
formattedStr.WriteByte(template[j])
}
}
formattedStr.WriteString(strVal)
} else {
formattedStr.WriteString(template[i:j])
if j < templateLen-1 {
formattedStr.WriteByte(template[j])
}
}
i = j
}
} else {
j = i //nolint:ineffassign
formattedStr.WriteByte(template[i])
}
}
return formattedStr.String()
}
func getItemAsStr(item *any, itemFormat *string) string {
base := 10
var floatFormat byte = 'f'
precision := -1
var preparedArgFormat string
var argStr string
postProcessingRequired := false
intNumberFormat := false
floatNumberFormat := false
if itemFormat != nil && len(*itemFormat) > 0 {
/* for numbers there are following formats:
* d(D) - decimal
* b(B) - binary
* f(F) - fixed point i.e {0:F}, 10.5467890 -> 10.546789 ; {0:F4}, 10.5467890 -> 10.5468
* e(E) - exponential - float point with scientific format {0:E2}, 191.0784 -> 1.91e+02
* x(X) - hexadecimal i.e. {0:X}, 250 -> fa ; {0:X4}, 250 -> 00fa
* p(P) - percent i.e. {0:P100}, 12 -> 12%
* Following formats are not supported yet:
* 1. c(C) currency it requires also country code
* 2. g(G),and others with locales
* 3. f(F) - fixed point, {0,F4}, 123.15 -> 123.1500
* OUR own addition:
* 1. O(o) - octahedral number format
*/
preparedArgFormat = *itemFormat
postProcessingRequired = len(preparedArgFormat) > 1
switch rune((*itemFormat)[0]) {
case 'd', 'D':
base = 10
intNumberFormat = true
case 'x', 'X':
base = 16
intNumberFormat = true
case 'o', 'O':
base = 8
intNumberFormat = true
case 'b', 'B':
base = 2
intNumberFormat = true
case 'e', 'E', 'f', 'F':
if rune(preparedArgFormat[0]) == 'e' || rune(preparedArgFormat[0]) == 'E' {
floatFormat = 'e'
}
// precision was passed, take [1:end], extract precision
if postProcessingRequired {
precisionStr := preparedArgFormat[1:]
precisionVal, err := strconv.Atoi(precisionStr)
if err == nil {
precision = precisionVal
}
}
postProcessingRequired = false
floatNumberFormat = floatFormat == 'f'
case 'p', 'P':
// percentage processes here ...
if postProcessingRequired {
dividerStr := preparedArgFormat[1:]
dividerVal, err := strconv.ParseFloat(dividerStr, 32)
if err == nil {
// 1. Convert arg to float
val := (*item).(interface{})
var floatVal float64
switch val.(type) {
case float64:
floatVal = val.(float64)
case int:
floatVal = float64(val.(int))
default:
floatVal = 0
}
// 2. Divide arg / divider and multiply by 100
percentage := (floatVal / dividerVal) * 100
return strconv.FormatFloat(percentage, floatFormat, 2, 64)
}
}
default:
base = 10
}
}
switch v := (*item).(type) {
case string:
argStr = v
case int8:
argStr = strconv.FormatInt(int64(v), base)
case int16:
argStr = strconv.FormatInt(int64(v), base)
case int32:
argStr = strconv.FormatInt(int64(v), base)
case int64:
argStr = strconv.FormatInt(v, base)
case int:
argStr = strconv.FormatInt(int64(v), base)
case uint8:
argStr = strconv.FormatUint(uint64(v), base)
case uint16:
argStr = strconv.FormatUint(uint64(v), base)
case uint32:
argStr = strconv.FormatUint(uint64(v), base)
case uint64:
argStr = strconv.FormatUint(v, base)
case uint:
argStr = strconv.FormatUint(uint64(v), base)
case bool:
argStr = strconv.FormatBool(v)
case float32:
argStr = strconv.FormatFloat(float64(v), floatFormat, precision, 32)
case float64:
argStr = strconv.FormatFloat(v, floatFormat, precision, 64)
default:
argStr = fmt.Sprintf("%v", v)
}
if !postProcessingRequired {
return argStr
}
// 1. If integer numbers add filling
if intNumberFormat {
symbolsStr := preparedArgFormat[1:]
symbolsStrVal, err := strconv.Atoi(symbolsStr)
if err == nil {
symbolsToAdd := symbolsStrVal - len(argStr)
if symbolsToAdd > 0 {
advArgStr := strings.Builder{}
advArgStr.Grow(len(argStr) + symbolsToAdd + 1)
for i := 0; i < symbolsToAdd; i++ {
advArgStr.WriteByte('0')
}
advArgStr.WriteString(argStr)
return advArgStr.String()
}
}
}
if floatNumberFormat && precision > 0 {
pointIndex := strings.Index(argStr, ".")
if pointIndex > 0 {
advArgStr := strings.Builder{}
advArgStr.Grow(len(argStr) + precision + 1)
advArgStr.WriteString(argStr)
numberOfSymbolsAfterPoint := len(argStr) - (pointIndex + 1)
for i := numberOfSymbolsAfterPoint; i < precision; i++ {
advArgStr.WriteByte(0)
}
return advArgStr.String()
}
}
return argStr
}