Compare commits
11 commits
Author | SHA1 | Date | |
---|---|---|---|
|
c3854fac43 | ||
|
9fa0ef78da | ||
|
1fd8a22846 | ||
|
e480a37c9e | ||
|
afa0e30776 | ||
|
eaec5aff93 | ||
|
7823443488 | ||
90bf8beb0c | |||
|
c11c23dd98 | ||
7bd5c7ec48 | |||
f5636aea58 |
6 changed files with 180 additions and 41 deletions
10
.drone.yml
Normal file
10
.drone.yml
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
---
|
||||||
|
kind: pipeline
|
||||||
|
type: docker
|
||||||
|
name: default
|
||||||
|
steps:
|
||||||
|
- name: backend
|
||||||
|
image: golang
|
||||||
|
commands:
|
||||||
|
- go build
|
||||||
|
- go test
|
12
CHANGELOG
Normal file
12
CHANGELOG
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
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
|
||||||
|
* support negative values
|
||||||
|
|
||||||
|
v1.0.1, released 2024-08-08
|
||||||
|
* fix encoding to zero
|
22
README.md
22
README.md
|
@ -1,3 +1,23 @@
|
||||||
# abcex
|
# abcex
|
||||||
|
|
||||||
Computing system based on the number 36
|
[](https://drone.arns.lt/zordsdavini/abcex)
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
**Version 1** - based on 32: number and case insensitive 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
|
||||||
|
|
122
abcex.go
122
abcex.go
|
@ -1,53 +1,109 @@
|
||||||
package abcex
|
package abcex
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
import "math"
|
"fmt"
|
||||||
|
"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",
|
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",
|
||||||
22: "m", 23: "n", 24: "o", 25: "p", 26: "q", 27: "r", 28: "s",
|
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" }
|
29: "t", 30: "u", 31: "v", 32: "w", 33: "x", 34: "y", 35: "z",
|
||||||
|
|
||||||
var abcexToDec = flipMap()
|
|
||||||
|
|
||||||
func reverse(s string) string {
|
|
||||||
runes := []rune(s)
|
|
||||||
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
|
|
||||||
runes[i], runes[j] = runes[j], runes[i]
|
|
||||||
}
|
|
||||||
return string(runes)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func flipMap() map[string]int {
|
var decToAbcex62 = map[int]string{
|
||||||
flipped := make(map[string]int)
|
0: "0", 1: "1", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7",
|
||||||
for i, val := range decToAbcex {
|
8: "8", 9: "9", 10: "A", 11: "B", 12: "C", 13: "D", 14: "E",
|
||||||
flipped[val] = i
|
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",
|
||||||
return flipped
|
36: "a", 37: "b", 38: "c", 39: "d", 40: "e", 41: "f", 42: "g",
|
||||||
|
43: "h", 44: "i", 45: "j", 46: "k", 47: "l", 48: "m", 49: "n",
|
||||||
|
50: "o", 51: "p", 52: "q", 53: "r", 54: "s", 55: "t", 56: "u",
|
||||||
|
57: "v", 58: "w", 59: "x", 60: "y", 61: "z",
|
||||||
}
|
}
|
||||||
|
|
||||||
func Encode(number int64) string{
|
var (
|
||||||
result := ""
|
BASE36 int = 36
|
||||||
|
BASE62 int = 62
|
||||||
|
)
|
||||||
|
|
||||||
for number > 0 {
|
func getDict(base int) map[int]string {
|
||||||
result = fmt.Sprintf("%s%s", decToAbcex[int(number % 36)], result)
|
if base == 36 {
|
||||||
number /= 36
|
return decToAbcex36
|
||||||
|
} else if base == 62 {
|
||||||
|
return decToAbcex62
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
panic("Invalid base")
|
||||||
}
|
}
|
||||||
|
|
||||||
func Decode(str string) int64 {
|
func reverse(s string) string {
|
||||||
var result int64
|
runes := []rune(s)
|
||||||
|
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
|
||||||
|
runes[i], runes[j] = runes[j], runes[i]
|
||||||
|
}
|
||||||
|
return string(runes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func flipMap(dict map[int]string) map[string]int {
|
||||||
|
flipped := make(map[string]int)
|
||||||
|
for i, val := range dict {
|
||||||
|
flipped[val] = i
|
||||||
|
}
|
||||||
|
|
||||||
|
return flipped
|
||||||
|
}
|
||||||
|
|
||||||
|
func Encode(number int64, base int) string {
|
||||||
|
if number == 0 {
|
||||||
|
return "0"
|
||||||
|
}
|
||||||
|
|
||||||
|
result := ""
|
||||||
|
negative := false
|
||||||
|
if number < 0 {
|
||||||
|
number = number * (-1)
|
||||||
|
negative = true
|
||||||
|
}
|
||||||
|
|
||||||
|
for number > 0 {
|
||||||
|
result = fmt.Sprintf("%s%s", getDict(base)[int(number%int64(base))], result)
|
||||||
|
number /= int64(base)
|
||||||
|
}
|
||||||
|
|
||||||
|
if negative {
|
||||||
|
return "-" + result
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func Decode(str string, base int) int64 {
|
||||||
|
var result int64
|
||||||
result = 0
|
result = 0
|
||||||
str = reverse(str)
|
abcexToDec := flipMap(getDict(base))
|
||||||
|
negative := false
|
||||||
|
if string(str[0]) == "-" {
|
||||||
|
negative = true
|
||||||
|
str = str[1:]
|
||||||
|
}
|
||||||
|
|
||||||
for i, c := range str {
|
str = reverse(str)
|
||||||
result = int64(math.Pow(36, float64(i))) * int64(abcexToDec[string(c)]) + result
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
for i, c := range str {
|
||||||
|
result = int64(math.Pow(float64(base), float64(i)))*int64(abcexToDec[string(c)]) + result
|
||||||
|
}
|
||||||
|
|
||||||
|
if negative {
|
||||||
|
return result * (-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func Convert(str string, fromBase int, toBase int) string {
|
||||||
|
return Encode(Decode(str, fromBase), toBase)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,55 @@
|
||||||
package abcex
|
package abcex
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
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) {
|
||||||
fmt.Println(Encode(12345))
|
if Encode(12345, BASE62) != "3D7" {
|
||||||
// Output: 9ix
|
t.Fatal("Failed encoding")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDecode(t *testing.T) {
|
func TestDecode(t *testing.T) {
|
||||||
fmt.Println(Decode("9ix"))
|
if Decode("3D7", BASE62) != 12345 {
|
||||||
// Output: 12345
|
t.Fatal("Failed decoding")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestZero(t *testing.T) {
|
||||||
|
if Encode(0, BASE62) != "0" {
|
||||||
|
t.Fatal("Failed zero encoding")
|
||||||
|
}
|
||||||
|
|
||||||
|
if Decode("0", BASE62) != 0 {
|
||||||
|
t.Fatal("Failed zero decoding")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNegative(t *testing.T) {
|
||||||
|
if Encode(-20, BASE62) != "-K" {
|
||||||
|
t.Fatal("Failed negative encoding:", Encode(-20, BASE62))
|
||||||
|
}
|
||||||
|
|
||||||
|
if Decode("-ab", BASE62) != -2269 {
|
||||||
|
t.Fatal("Failed negative decoding", Decode("-ab", BASE62))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -1,3 +1,3 @@
|
||||||
module g.arns.lt/zordsdavini/abcex
|
module g.arns.lt/zordsdavini/abcex/v4
|
||||||
|
|
||||||
go 1.16
|
go 1.16
|
||||||
|
|
Loading…
Add table
Reference in a new issue