Skip to content

Commit

Permalink
Merge pull request #1 from bobg/v2
Browse files Browse the repository at this point in the history
New API
  • Loading branch information
bobg authored Jul 9, 2023
2 parents d623d46 + f86ca3c commit 1417d90
Show file tree
Hide file tree
Showing 17 changed files with 287 additions and 454 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.18

- name: Unit tests
run: go test -v -coverprofile=cover.out ./...
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/cover.out
*~
19 changes: 7 additions & 12 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,21 @@ and vice versa.
To get the Base30 encoding of the number 412:

```go
result, err := basexx.Digits(412, basexx.Base30)
var sb strings.Builder
if err := basexx.EncodeInt64(&sb, 412, basexx.Base30); err != nil { ... }
result := sb.String()
```

To decode the Base30 digit string `"fr"`:

```go
result, err := basexx.Value("fr", basexx.Base30)
result, err := basexx.DecodeString("fr", basexx.Base30)
```

To convert a digit string `x` in base `from` to a new digit string in base `to`:

```go
var (
src = basexx.NewBuffer(x, from)
destbuf = make([]byte, basexx.Length(from, to, len(x)))
dest = basexx.NewBuffer(destbuf, to)
)
_, err := Convert(dest, src)
if err != nil { ... }
result := dest.Written()
result, err := basexx.Convert(x, from, to)
```

To define your own new number base:
Expand All @@ -44,14 +39,14 @@ type ReverseBase10 struct{}

func (ReverseBase10) N() int64 { return 10 }

func (ReverseBase10) Encode(val int64) (byte, error) {
func (ReverseBase10) Digit(val int64) (byte, error) {
if val < 0 || val > 9 {
return 0, errors.New("digit value out of range")
}
return byte('9' - val), nil
}

func (ReverseBase10) Decode(digit byte) (int64, error) {
func (ReverseBase10) Val(digit byte) (int64, error) {
if digit < '0’ || digit > '9' {
return 0, errors.New("invalid encoded digit")
}
Expand Down
10 changes: 5 additions & 5 deletions alnum.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ type Alnum int
// N implements Base.N.
func (a Alnum) N() int64 { return int64(a) }

// Encode implements Base.Encode.
func (a Alnum) Encode(val int64) (byte, error) {
// Digit implements Base.Digit.
func (a Alnum) Digit(val int64) (byte, error) {
if val < 0 || val >= int64(a) {
return 0, ErrInvalid
}
if val < 10 {
return byte(val) + '0', nil
}
return byte(val) + 'a' - 10, nil
return byte(val) - 10 + 'a', nil
}

// Decode implements Base.Decode.
func (a Alnum) Decode(digit byte) (int64, error) {
// Val implements Base.Val.
func (a Alnum) Val(digit byte) (int64, error) {
switch {
case '0' <= digit && digit <= '9':
return int64(digit - '0'), nil
Expand Down
39 changes: 0 additions & 39 deletions base30.go

This file was deleted.

37 changes: 0 additions & 37 deletions base50.go

This file was deleted.

4 changes: 2 additions & 2 deletions base62.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type base62 struct{}

func (b base62) N() int64 { return 62 }

func (b base62) Encode(val int64) (byte, error) {
func (b base62) Digit(val int64) (byte, error) {
if val < 0 || val > 61 {
return 0, ErrInvalid
}
Expand All @@ -17,7 +17,7 @@ func (b base62) Encode(val int64) (byte, error) {
return byte(val) - 36 + 'A', nil
}

func (b base62) Decode(digit byte) (int64, error) {
func (b base62) Val(digit byte) (int64, error) {
switch {
case '0' <= digit && digit <= '9':
return int64(digit - '0'), nil
Expand Down
5 changes: 2 additions & 3 deletions base94.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ type base94 struct{}

func (b base94) N() int64 { return 94 }

func (b base94) Encode(val int64) (byte, error) {
func (b base94) Digit(val int64) (byte, error) {
if val < 0 || val > 93 {
return 0, ErrInvalid
}
return byte(val + 33), nil
}

func (b base94) Decode(inp byte) (int64, error) {
digit := inp
func (b base94) Val(digit byte) (int64, error) {
if digit < 33 || digit > 126 {
return 0, ErrInvalid
}
Expand Down
Loading

0 comments on commit 1417d90

Please sign in to comment.