Compare commits

...

5 commits

Author SHA1 Message Date
Arnas Udovic
c3854fac43 v4
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2025-01-06 21:37:40 +02:00
Arnas Udovic
9fa0ef78da update README and CHANGELOG
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2025-01-06 21:06:37 +02:00
Arnas Udovic
1fd8a22846 support 36 and 64 base
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2025-01-06 20:57:22 +02:00
Arnas Udovic
e480a37c9e v3
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2024-09-20 11:02:50 +03:00
Arnas Udovic
afa0e30776 fix mod v2
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2024-09-20 10:43:36 +03:00
5 changed files with 83 additions and 20 deletions

View file

@ -1,3 +1,10 @@
v4.0.0, released 2025-01-06
* support both 32 and 64 bases
* added Convert command between bases
v2.0.0, released 2024-09-20
* move to base 62
v1.0.2, released 2024-08-08 v1.0.2, released 2024-08-08
* support negative values * support negative values

View file

@ -2,10 +2,22 @@
[![Build Status](https://drone.arns.lt/api/badges/zordsdavini/abcex/status.svg)](https://drone.arns.lt/zordsdavini/abcex) [![Build Status](https://drone.arns.lt/api/badges/zordsdavini/abcex/status.svg)](https://drone.arns.lt/zordsdavini/abcex)
Computing system based on the number 62 Computing system based on the number 62 or 36
## Usage
`Encode(int, int) -> string` - convert number into string based on given base.
`Decode(string, int) -> int` - convert from encoded string into number based on given base.
`Convert(string, int, int) -> string` - convert encoded string from one base to another.
## History ## History
**Version 1** - based on 32: number and case insensitive letters **Version 1** - based on 32: number and case insensitive letters
**Version 2** - based on 62: number and case sensitive letters **Version 2** - failed release
**Version 3** - based on 62: number and case sensitive letters
**Version 4** - support both versions, based on 62 and 32

View file

@ -5,7 +5,15 @@ import (
"math" "math"
) )
var decToAbcex = map[int]string{ var decToAbcex36 = map[int]string{
0: "0", 1: "1", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7",
8: "8", 9: "9", 10: "a", 11: "b", 12: "c", 13: "d", 14: "e",
15: "f", 16: "g", 17: "h", 18: "i", 19: "j", 20: "k", 21: "l",
22: "m", 23: "n", 24: "o", 25: "p", 26: "q", 27: "r", 28: "s",
29: "t", 30: "u", 31: "v", 32: "w", 33: "x", 34: "y", 35: "z",
}
var decToAbcex62 = map[int]string{
0: "0", 1: "1", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7", 0: "0", 1: "1", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7",
8: "8", 9: "9", 10: "A", 11: "B", 12: "C", 13: "D", 14: "E", 8: "8", 9: "9", 10: "A", 11: "B", 12: "C", 13: "D", 14: "E",
15: "F", 16: "G", 17: "H", 18: "I", 19: "J", 20: "K", 21: "L", 15: "F", 16: "G", 17: "H", 18: "I", 19: "J", 20: "K", 21: "L",
@ -17,7 +25,20 @@ var decToAbcex = map[int]string{
57: "v", 58: "w", 59: "x", 60: "y", 61: "z", 57: "v", 58: "w", 59: "x", 60: "y", 61: "z",
} }
var abcexToDec = flipMap() var (
BASE36 int = 36
BASE62 int = 62
)
func getDict(base int) map[int]string {
if base == 36 {
return decToAbcex36
} else if base == 62 {
return decToAbcex62
}
panic("Invalid base")
}
func reverse(s string) string { func reverse(s string) string {
runes := []rune(s) runes := []rune(s)
@ -27,16 +48,16 @@ func reverse(s string) string {
return string(runes) return string(runes)
} }
func flipMap() map[string]int { func flipMap(dict map[int]string) map[string]int {
flipped := make(map[string]int) flipped := make(map[string]int)
for i, val := range decToAbcex { for i, val := range dict {
flipped[val] = i flipped[val] = i
} }
return flipped return flipped
} }
func Encode(number int64) string { func Encode(number int64, base int) string {
if number == 0 { if number == 0 {
return "0" return "0"
} }
@ -49,8 +70,8 @@ func Encode(number int64) string {
} }
for number > 0 { for number > 0 {
result = fmt.Sprintf("%s%s", decToAbcex[int(number%62)], result) result = fmt.Sprintf("%s%s", getDict(base)[int(number%int64(base))], result)
number /= 62 number /= int64(base)
} }
if negative { if negative {
@ -60,9 +81,10 @@ func Encode(number int64) string {
return result return result
} }
func Decode(str string) int64 { func Decode(str string, base int) int64 {
var result int64 var result int64
result = 0 result = 0
abcexToDec := flipMap(getDict(base))
negative := false negative := false
if string(str[0]) == "-" { if string(str[0]) == "-" {
negative = true negative = true
@ -72,7 +94,7 @@ func Decode(str string) int64 {
str = reverse(str) str = reverse(str)
for i, c := range str { for i, c := range str {
result = int64(math.Pow(62, float64(i)))*int64(abcexToDec[string(c)]) + result result = int64(math.Pow(float64(base), float64(i)))*int64(abcexToDec[string(c)]) + result
} }
if negative { if negative {
@ -81,3 +103,7 @@ func Decode(str string) int64 {
return result return result
} }
func Convert(str string, fromBase int, toBase int) string {
return Encode(Decode(str, fromBase), toBase)
}

View file

@ -4,34 +4,52 @@ import (
"testing" "testing"
) )
func TestConvert(t *testing.T) {
if Convert("9ix", BASE36, BASE62) != "3D7" {
t.Fatal("Failed conversion")
}
}
func TestEncode36(t *testing.T) {
if Encode(12345, BASE36) != "9ix" {
t.Fatal("Failed encoding")
}
}
func TestDecode36(t *testing.T) {
if Decode("9ix", BASE36) != 12345 {
t.Fatal("Failed decoding")
}
}
func TestEncode(t *testing.T) { func TestEncode(t *testing.T) {
if Encode(12345) != "3D7" { if Encode(12345, BASE62) != "3D7" {
t.Fatal("Failed encoding") t.Fatal("Failed encoding")
} }
} }
func TestDecode(t *testing.T) { func TestDecode(t *testing.T) {
if Decode("3D7") != 12345 { if Decode("3D7", BASE62) != 12345 {
t.Fatal("Failed decoding") t.Fatal("Failed decoding")
} }
} }
func TestZero(t *testing.T) { func TestZero(t *testing.T) {
if Encode(0) != "0" { if Encode(0, BASE62) != "0" {
t.Fatal("Failed zero encoding") t.Fatal("Failed zero encoding")
} }
if Decode("0") != 0 { if Decode("0", BASE62) != 0 {
t.Fatal("Failed zero decoding") t.Fatal("Failed zero decoding")
} }
} }
func TestNegative(t *testing.T) { func TestNegative(t *testing.T) {
if Encode(-20) != "-K" { if Encode(-20, BASE62) != "-K" {
t.Fatal("Failed negative encoding:", Encode(-20)) t.Fatal("Failed negative encoding:", Encode(-20, BASE62))
} }
if Decode("-ab") != -2269 { if Decode("-ab", BASE62) != -2269 {
t.Fatal("Failed negative decoding", Decode("-ab")) t.Fatal("Failed negative decoding", Decode("-ab", BASE62))
} }
} }

2
go.mod
View file

@ -1,3 +1,3 @@
module g.arns.lt/zordsdavini/abcex module g.arns.lt/zordsdavini/abcex/v4
go 1.16 go 1.16