Compare commits

...

11 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
Arnas Udovic
eaec5aff93 move to case sensitive letters
All checks were successful
continuous-integration/drone/tag Build is passing
continuous-integration/drone/push Build is passing
2024-09-20 10:13:21 +03:00
Arnas Udovic
7823443488 added negative support
All checks were successful
continuous-integration/drone/push Build is passing
2024-08-08 18:25:21 +03:00
90bf8beb0c Update CHANGELOG
All checks were successful
continuous-integration/drone/push Build is passing
2024-08-08 15:08:44 +00:00
Arnas Udovic
c11c23dd98 fix encoding to zero
All checks were successful
continuous-integration/drone/push Build is passing
2024-08-08 14:53:11 +03:00
7bd5c7ec48 Update 'README.md'
All checks were successful
continuous-integration/drone/push Build is passing
2022-08-08 05:26:40 +00:00
f5636aea58 Add '.drone.yml' 2022-08-08 05:25:48 +00:00
6 changed files with 180 additions and 41 deletions

10
.drone.yml Normal file
View 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
View 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

View file

@ -1,3 +1,23 @@
# abcex
Computing system based on the number 36
[![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 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
View file

@ -1,53 +1,109 @@
package abcex
import "fmt"
import "math"
import (
"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",
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 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)
29: "t", 30: "u", 31: "v", 32: "w", 33: "x", 34: "y", 35: "z",
}
func flipMap() map[string]int {
flipped := make(map[string]int)
for i, val := range decToAbcex {
flipped[val] = i
}
return flipped
var decToAbcex62 = 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",
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{
result := ""
var (
BASE36 int = 36
BASE62 int = 62
)
for number > 0 {
result = fmt.Sprintf("%s%s", decToAbcex[int(number % 36)], result)
number /= 36
func getDict(base int) map[int]string {
if base == 36 {
return decToAbcex36
} else if base == 62 {
return decToAbcex62
}
return result
panic("Invalid base")
}
func Decode(str string) int64 {
var result int64
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(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
str = reverse(str)
abcexToDec := flipMap(getDict(base))
negative := false
if string(str[0]) == "-" {
negative = true
str = str[1:]
}
for i, c := range str {
result = int64(math.Pow(36, float64(i))) * int64(abcexToDec[string(c)]) + result
}
str = reverse(str)
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)
}

View file

@ -1,14 +1,55 @@
package abcex
import "fmt"
import "testing"
import (
"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) {
fmt.Println(Encode(12345))
// Output: 9ix
if Encode(12345, BASE62) != "3D7" {
t.Fatal("Failed encoding")
}
}
func TestDecode(t *testing.T) {
fmt.Println(Decode("9ix"))
// Output: 12345
if Decode("3D7", BASE62) != 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
View file

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