-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
75 lines (63 loc) · 1.37 KB
/
format.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
package zc
import (
"fmt"
"strings"
"github.com/blackchip-org/scan"
"github.com/blackchip-org/zc/v6/msg"
)
func FormatItems(items []Item) []string {
strs := make([]string, len(items))
for i, item := range items {
strs[i] = item.String()
}
return strs
}
func FormatList(vals []string) string {
var s scan.Scanner
var strs []string
for _, val := range vals {
s.InitFromString("", val)
for s.HasMore() {
switch {
case s.This == '|':
s.Val.WriteString(`\|`)
s.Skip()
default:
s.Val.WriteString(msg.Escape(s.This))
s.Skip()
}
}
strs = append(strs, s.Emit().Val)
}
return strings.Join(strs, " | ")
}
func Strings(xs ...any) []string {
strs := make([]string, len(xs))
for i, x := range xs {
strs[i] = fmt.Sprint(x)
}
return strs
}
func FormatExponent(str string) string {
s := scan.NewScannerFromString("", str)
// Keep everything before the exponent
scan.Until(s, scan.Rune('E', 'e'), s.Keep)
// If no more, we didn't see the exponent
if !s.HasMore() {
return str
}
// We did see the exponent. Always write this out in lower case.
s.Val.WriteRune('e')
s.Skip()
// Omit positive signs but keep the negative ones
if s.This == '+' {
s.Skip()
}
// Remove all leading zeros
for s.This == '0' && s.Next != scan.EndOfText {
s.Skip()
}
// Actual digits of the exponent
scan.While(s, scan.IsAny, s.Keep)
return s.Emit().Val
}