Skip to content

Commit 74b9f35

Browse files
add function 'ToBase'
1 parent 3f2b73e commit 74b9f35

File tree

3 files changed

+281
-70
lines changed

3 files changed

+281
-70
lines changed

README.md

Lines changed: 104 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
> SliceSort - Wrapper for sorting slice
88
> ToNum - Wrapper for convert into numerable types
99
> ToStr - Wrapper for convert to string by 'fmt.Sprintf'
10+
> ToBase - Wrapper for convert to other base (2, 8, 10, 16 ...)
1011
> ```
1112
1213
### Examples:
@@ -16,50 +17,122 @@ package main
1617
1718
import (
1819
"fmt"
20+
1921
helper "github.com/KusoKaihatsuSha/tinyHelper"
2022
)
2123
2224
func main() {
25+
i := 0
26+
var inc = func() int {
27+
i++
28+
return i
29+
}
30+
mask := "%d) %[2]v - TYPE[%[2]T]\n"
2331
test001 := []byte("0123456789")
24-
fmt.Println(string(helper.SliceRotate(test001, true)))
25-
fmt.Println(string(helper.SliceRotate(test001)))
32+
fmt.Printf(mask, inc(), string(helper.SliceRotate(test001, true))) // 1
33+
fmt.Printf(mask, inc(), string(helper.SliceRotate(test001))) // 2
2634
test002 := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
2735
test002s := helper.SliceRotate(test002)
2836
helper.SliceSort(test002s)
29-
fmt.Println(test002s)
37+
fmt.Printf(mask, inc(), test002s) // 3
3038
test003 := []float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
3139
test003s := helper.SliceRotate(test003, true)
3240
helper.SliceSort(test003s)
33-
fmt.Println(test003s)
41+
fmt.Printf(mask, inc(), test003s) // 4
3442
test004 := []rune("👺👾👺👾👾👾")
35-
fmt.Println(string(helper.SliceRotate(test004)))
36-
fmt.Println(string(helper.SliceRotate(test004, true)))
37-
fmt.Println(helper.ToStr("123") + ".")
38-
fmt.Println(helper.ToStr(123) + ".")
39-
fmt.Println(helper.ToStr(byte(123)) + ".")
40-
fmt.Println(helper.ToStr(float64(123)) + ".")
41-
fmt.Println(helper.ToInt("122") + 1)
42-
fmt.Println(helper.ToNum[uint]("18446744073709551615"))
43-
fmt.Println(helper.ToNum[int](byte(122)) + 1)
44-
fmt.Println(helper.ToNum[float64](float64(122.1)) + 1)
45-
fmt.Println(helper.ToNum[int](true) + 1)
46-
43+
fmt.Printf(mask, inc(), string(helper.SliceRotate(test004))) // 5
44+
fmt.Printf(mask, inc(), string(helper.SliceRotate(test004, true))) // 6
45+
fmt.Printf(mask, inc(), helper.ToStr("123")+".") // 7
46+
fmt.Printf(mask, inc(), helper.ToStr(123)+".") // 8
47+
fmt.Printf(mask, inc(), helper.ToStr(byte(123))+".") // 9
48+
fmt.Printf(mask, inc(), helper.ToStr(float64(123))+".") // 10
49+
fmt.Printf(mask, inc(), helper.ToInt("122")+1) // 11
50+
fmt.Printf(mask, inc(), helper.ToInt("122")+1) // 12
51+
fmt.Printf(mask, inc(), helper.ToNum[uint]("18446744073709551615")) // 13
52+
fmt.Printf(mask, inc(), helper.ToNum[int](byte(122))+1) // 14
53+
fmt.Printf(mask, inc(), helper.ToNum[byte](byte(122))+1) // 15
54+
fmt.Printf(mask, inc(), helper.ToNum[int](-123+1)) // 16
55+
fmt.Printf(mask, inc(), helper.ToNum[float64](float64(122.1))+1) // 17
56+
fmt.Printf(mask, inc(), helper.ToNum[int](true)+1) // 18
57+
fmt.Printf(mask, inc(), helper.ToNum[int](10001, 2)) // 19
58+
fmt.Printf(mask, inc(), helper.ToNum[int]("00010001", 2)) // 20
59+
fmt.Printf(mask, inc(), helper.ToNum[int]("0b010001", 2)) // 21
60+
fmt.Printf(mask, inc(), helper.ToNum[int]("0b10001")) // 22
61+
fmt.Printf(mask, inc(), helper.ToNum[float64]("10001", 2)) // 23
62+
fmt.Printf(mask, inc(), helper.ToNum[int]("0o21", 8)) // 24
63+
fmt.Printf(mask, inc(), helper.ToNum[int]("0o21")) // 25
64+
fmt.Printf(mask, inc(), helper.ToNum[float64]("21", 8)) // 26
65+
fmt.Printf(mask, inc(), helper.ToNum[int]("0x11", 16)) // 27
66+
fmt.Printf(mask, inc(), helper.ToNum[int]("0x11")) // 28
67+
fmt.Printf(mask, inc(), helper.ToNum[float64]("11", 16)) // 29
68+
fmt.Printf(mask, inc(), helper.ToBase(17, 10, 2, 8)) // 30
69+
fmt.Printf(mask, inc(), helper.ToBase(17, 10, 8)) // 31
70+
fmt.Printf(mask, inc(), helper.ToBase(17, 10, 16)) // 32
71+
fmt.Printf(mask, inc(), helper.ToInt(17, 2)) // 33 // error base, return 17
72+
fmt.Printf(mask, inc(), helper.ToBase(10001, 2, 16)) // 34
73+
fmt.Printf(mask, inc(), helper.ToBase("1a", 11, 10)) // 35
74+
fmt.Printf(mask, inc(), helper.ToInt("111", 36)) // 36
75+
fmt.Printf(mask, inc(), helper.ToBase(30, 7, 16)) // 37
76+
fmt.Printf(mask, inc(), helper.ToInt("11a", 16)) // 38
77+
fmt.Printf(mask, inc(), helper.ToBase(21, 10, 11)) // 39
78+
fmt.Printf(mask, inc(), helper.ToBase(21, 10, 16)) // 40
79+
fmt.Printf(mask, inc(), helper.ToBase(10, 11, 16, 2)) // 41
80+
fmt.Printf(mask, inc(), helper.ToBase("00010001", 2, 16)) // 42
81+
fmt.Printf(mask, inc(), helper.ToBase("00001111", 2, 16, 2)) // 43
82+
fmt.Printf(mask, inc(), helper.ToBase("0o00021", 8, 16)) // 44
83+
fmt.Printf(mask, inc(), helper.ToNum[byte](1222)) // 45 // error bitSize, return quotient = 1222%256
84+
fmt.Printf(mask, inc(), helper.ToBase("0b1", 16, 10, 2)) // 46
85+
fmt.Printf(mask, inc(), helper.ToBase("0b1", 2, 10, 2)) // 47
86+
fmt.Printf(mask, inc(), helper.ToBase(3, 10, 10, 2)) // 48
4787
// Output:
48-
// 1032547698
49-
// 9876543210
50-
// [0 1 2 3 4 5 6 7 8 9]
51-
// [0 1 2 3 4 5 6 7 8 9]
52-
// 👾👾👾👺👾👺
53-
// 👾👺👾👺👾👾
54-
// 123.
55-
// 123.
56-
// 123.
57-
// 123.
58-
// 123
59-
// 18446744073709551615
60-
// 123
61-
// 123.1
62-
// 2
88+
// 1) 1032547698 - TYPE[string]
89+
// 2) 9876543210 - TYPE[string]
90+
// 3) [0 1 2 3 4 5 6 7 8 9] - TYPE[[]string]
91+
// 4) [0 1 2 3 4 5 6 7 8 9] - TYPE[[]float64]
92+
// 5) 👾👾👾👺👾👺 - TYPE[string]
93+
// 6) 👾👺👾👺👾👾 - TYPE[string]
94+
// 7) 123. - TYPE[string]
95+
// 8) 123. - TYPE[string]
96+
// 9) 123. - TYPE[string]
97+
// 10) 123. - TYPE[string]
98+
// 11) 123 - TYPE[int]
99+
// 12) 123 - TYPE[int]
100+
// 13) 18446744073709551615 - TYPE[uint]
101+
// 14) 123 - TYPE[int]
102+
// 15) 123 - TYPE[uint8]
103+
// 16) -122 - TYPE[int]
104+
// 17) 123.1 - TYPE[float64]
105+
// 18) 2 - TYPE[int]
106+
// 19) 17 - TYPE[int]
107+
// 20) 17 - TYPE[int]
108+
// 21) 17 - TYPE[int]
109+
// 22) 17 - TYPE[int]
110+
// 23) 17 - TYPE[float64]
111+
// 24) 17 - TYPE[int]
112+
// 25) 17 - TYPE[int]
113+
// 26) 17 - TYPE[float64]
114+
// 27) 17 - TYPE[int]
115+
// 28) 17 - TYPE[int]
116+
// 29) 17 - TYPE[float64]
117+
// 30) 00010001 - TYPE[string]
118+
// 31) 21 - TYPE[string]
119+
// 32) 11 - TYPE[string]
120+
// 33) 17 - TYPE[int]
121+
// 34) 11 - TYPE[string]
122+
// 35) 21 - TYPE[string]
123+
// 36) 1333 - TYPE[int]
124+
// 37) 15 - TYPE[string]
125+
// 38) 282 - TYPE[int]
126+
// 39) 1a - TYPE[string]
127+
// 40) 15 - TYPE[string]
128+
// 41) 0b - TYPE[string]
129+
// 42) 11 - TYPE[string]
130+
// 43) 0f - TYPE[string]
131+
// 44) 11 - TYPE[string]
132+
// 45) 198 - TYPE[uint8]
133+
// 46) 177 - TYPE[string]
134+
// 47) 01 - TYPE[string]
135+
// 48) 03 - TYPE[string]
63136
}
64137
```
65138

tinyHelper.go

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ import (
66
"strconv"
77
)
88

9+
const (
10+
prfBase = "0"
11+
Base02 = "b"
12+
Base08 = "o"
13+
Base16 = "x"
14+
digits = "0123456789abcdefghijklmnopqrstuvwxyz"
15+
)
16+
17+
type IntTypes interface {
18+
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
19+
}
20+
921
type ComparableTypes interface {
1022
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64 | ~string
1123
}
@@ -18,25 +30,78 @@ type GeneralTypes interface {
1830
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64 | ~complex64 | ~complex128 | ~string | ~bool
1931
}
2032

21-
// ToStr[GeneralTypes](GeneralTypes) (string)
33+
// ToStr[GeneralTypes](GeneralTypes) string
2234
// Wrapper for convert to string by 'fmt.Sprintf'
2335
func ToStr[T GeneralTypes](val T) (nv string) {
2436
return fmt.Sprintf("%v", val)
2537
}
2638

39+
// toBaseOther(int, int) string
40+
// Wrapper for convert to another base
41+
func toBaseOther(v, base int) string {
42+
if v/base > 0 {
43+
return toBaseOther(v/base, base) + string(digits[v%base])
44+
} else {
45+
return string(digits[v%base])
46+
}
47+
}
48+
49+
// ToBase[GeneralTypes](T, int, int, ...int) string
50+
// Wrapper for convert from one base to another base
51+
func ToBase[T GeneralTypes](val T, from, to int, prefixNulls ...int) (nv string) {
52+
to10 := ToInt(val, from)
53+
nv = ToStr(to10)
54+
prx := ""
55+
if prefixNulls != nil || len(prefixNulls) > 0 {
56+
prx = ToStr(prefixNulls[0])
57+
}
58+
switch to {
59+
case 2:
60+
nv = fmt.Sprintf("%"+prfBase+prx+Base02, to10)
61+
case 8:
62+
nv = fmt.Sprintf("%"+prfBase+prx+Base08, to10)
63+
case 16:
64+
nv = fmt.Sprintf("%"+prfBase+prx+Base16, to10)
65+
default:
66+
nv = fmt.Sprintf("%"+prfBase+prx+"v", toBaseOther(to10, to))
67+
}
68+
return
69+
}
70+
2771
// ToNum[NumsTypes,GeneralTypes](GeneralTypes) NumsTypes {
28-
// Wrapper for convert into numerable types
72+
// Wrapper for convert into numerable types.
73+
// !!! Caution, if val more than T1 bitSize - may return quotient of division by dimension
2974
// Using example: 'ToNum[int](true)'
3075
// 'ToNum[float64](float64(122.1)) + 1)'
31-
func ToNum[T1 NumsTypes, T GeneralTypes](val T) T1 {
76+
func ToNum[T1 NumsTypes, T GeneralTypes](val T, base ...int) T1 {
3277
f := ToStr(val)
33-
if nv, err := strconv.ParseInt(f, 10, strconv.IntSize); err == nil {
78+
parBase := 10 // decimal as default
79+
if base != nil || len(base) > 0 {
80+
parBase = base[0] // tests result max == 36 (26 alphabet + 10 digits), more == 10 base
81+
}
82+
// Del prefix. Parse values with prefix 0b, 0o, 0x works if base '0' value in parameter.
83+
if len(f) >= 2 {
84+
switch {
85+
case f[:2] == (prfBase + Base02) && parBase == 16:
86+
case f[:2] == (prfBase + Base02):
87+
parBase = 2
88+
f = f[2:]
89+
case f[:2] == (prfBase + Base08):
90+
parBase = 8
91+
f = f[2:]
92+
case f[:2] == (prfBase + Base16):
93+
parBase = 16
94+
f = f[2:]
95+
}
96+
}
97+
98+
if nv, err := strconv.ParseInt(f, parBase, strconv.IntSize); err == nil {
3499
return T1(nv)
35100
}
36-
if nv, err := strconv.ParseUint(f, 10, strconv.IntSize); err == nil {
101+
if nv, err := strconv.ParseUint(f, parBase, strconv.IntSize); err == nil {
37102
return T1(nv)
38103
}
39-
if nv, err := strconv.ParseFloat(f, 10); err == nil {
104+
if nv, err := strconv.ParseFloat(f, strconv.IntSize); err == nil {
40105
return T1(nv)
41106
}
42107
if nv, err := strconv.ParseBool(f); err == nil {
@@ -48,8 +113,10 @@ func ToNum[T1 NumsTypes, T GeneralTypes](val T) T1 {
48113
return T1(0)
49114
}
50115

51-
func ToInt[T GeneralTypes](val T) int {
52-
return ToNum[int](val)
116+
// ToInt[GeneralTypes](GeneralTypes, ...int) int {
117+
// Wrapper for convert into 'int'.
118+
func ToInt[T GeneralTypes](val T, base ...int) int {
119+
return ToNum[int](val, base...)
53120
}
54121

55122
// SliceRotate[any]([]T, ...bool) []any

0 commit comments

Comments
 (0)