-
Notifications
You must be signed in to change notification settings - Fork 6
/
ns.go
64 lines (54 loc) · 1012 Bytes
/
ns.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
/*
Copyright © 2022 KAI CHU CHUNG <cage.chung@gmail.com>
*/
package cmd
import (
"github.com/spf13/cobra"
)
func format(in string, l int, sep rune) string {
numOfDigits := len(in)
numOfCommas := (numOfDigits - 1) / l
out := make([]byte, len(in)+numOfCommas)
for i, j, k := len(in)-1, len(out)-1, 0; ; i, j = i-1, j-1 {
out[j] = in[i]
if i == 0 {
return string(out)
}
if k++; k == l {
j, k = j-1, 0
out[j] = byte(sep)
}
}
}
func DecimalFormat(in string) string {
if len(in) > 3 {
return format(in, 3, ',')
}
return in
}
func OctalFormat(in string) string {
if len(in) > 3 {
return format(in, 3, ' ')
}
return in
}
func BinaryFormat(in string) string {
if len(in) > 4 {
return format(in, 4, ' ')
}
return in
}
func HexFormat(in string) string {
if len(in) > 4 {
return format(in, 4, ' ')
}
return in
}
// nsCmd represents the nbc command
var nsCmd = &cobra.Command{
Use: "ns",
Short: "Number system convert",
}
func init() {
rootCmd.AddCommand(nsCmd)
}