mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-25 16:55:11 +01:00
Add a lang/base38 package.
This commit is contained in:
1 parent
198dd1cab0
commit
6d5e2f0193
2 files changed
+174
No files matched your search
@@ -0,0 +1,79 @@
|
|||||||
|
// Use of this source code is governed by a BSD-style license that can be found
|
||||||
|
// in the LICENSE file.
|
||||||
|
|
||||||
|
// Package base38 converts a 4-byte string, each byte in [ 0-9?a-z], to a base
|
||||||
|
// 38 number.
|
||||||
|
package base38
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Max is the inclusive upper bound for the value returned by Encode. Its
|
||||||
|
// value equals (38*38*38*38 - 1).
|
||||||
|
Max = 2085135
|
||||||
|
// MaxBits is the number of bits required to represent Max. It satisfies
|
||||||
|
// (1<<(MaxBits-1) <= Max) && (Max < 1<<MaxBits).
|
||||||
|
MaxBits = 21
|
||||||
|
)
|
||||||
|
|
||||||
|
// Encode encodes a 4-byte string as a uint32 in the range [0, Max].
|
||||||
|
//
|
||||||
|
// Each byte must be ' ', be in the range '0'-'9', be '?' or be in the range
|
||||||
|
// 'a'-'z'.
|
||||||
|
//
|
||||||
|
// The string " " is mapped to zero.
|
||||||
|
func Encode(s string) (u uint32, ok bool) {
|
||||||
|
if len(s) == 4 {
|
||||||
|
for i := 0; i < 4; i++ {
|
||||||
|
x := uint32(table[s[i]])
|
||||||
|
if x == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
u += x - 1
|
||||||
|
if i == 3 {
|
||||||
|
return u, true
|
||||||
|
}
|
||||||
|
u *= 38
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
var table = [256]uint8{
|
||||||
|
' ': 1,
|
||||||
|
'0': 2,
|
||||||
|
'1': 3,
|
||||||
|
'2': 4,
|
||||||
|
'3': 5,
|
||||||
|
'4': 6,
|
||||||
|
'5': 7,
|
||||||
|
'6': 8,
|
||||||
|
'7': 9,
|
||||||
|
'8': 10,
|
||||||
|
'9': 11,
|
||||||
|
'?': 12,
|
||||||
|
'a': 13,
|
||||||
|
'b': 14,
|
||||||
|
'c': 15,
|
||||||
|
'd': 16,
|
||||||
|
'e': 17,
|
||||||
|
'f': 18,
|
||||||
|
'g': 19,
|
||||||
|
'h': 20,
|
||||||
|
'i': 21,
|
||||||
|
'j': 22,
|
||||||
|
'k': 23,
|
||||||
|
'l': 24,
|
||||||
|
'm': 25,
|
||||||
|
'n': 26,
|
||||||
|
'o': 27,
|
||||||
|
'p': 28,
|
||||||
|
'q': 29,
|
||||||
|
'r': 30,
|
||||||
|
's': 31,
|
||||||
|
't': 32,
|
||||||
|
'u': 33,
|
||||||
|
'v': 34,
|
||||||
|
'w': 35,
|
||||||
|
'x': 36,
|
||||||
|
'y': 37,
|
||||||
|
'z': 38,
|
||||||
|
}
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
// Use of this source code is governed by a BSD-style license that can be found
|
||||||
|
// in the LICENSE file.
|
||||||
|
|
||||||
|
package base38
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMax(t *testing.T) {
|
||||||
|
if Max == 38*38*38*38-1 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t.Fatal("Max does not satisfy its definition")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMaxBits(t *testing.T) {
|
||||||
|
if (1<<(MaxBits-1) <= Max) && (Max < 1<<MaxBits) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t.Fatal("MaxBits does not satisfy its definition")
|
||||||
|
}
|
||||||
|
|
||||||
|
func mk(a, b, c, d uint32) uint32 {
|
||||||
|
return a*38*38*38 + b*38*38 + c*38 + d
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValid(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
s string
|
||||||
|
want uint32
|
||||||
|
}{
|
||||||
|
{" ", mk(0, 0, 0, 0)},
|
||||||
|
{"0 ", mk(1, 0, 0, 0)},
|
||||||
|
{" 0 ", mk(0, 1, 0, 0)},
|
||||||
|
{" 0 ", mk(0, 0, 1, 0)},
|
||||||
|
{" 0", mk(0, 0, 0, 1)},
|
||||||
|
{"??12", mk(11, 11, 2, 3)},
|
||||||
|
{"6543", mk(7, 6, 5, 4)},
|
||||||
|
{"789a", mk(8, 9, 10, 12)},
|
||||||
|
{"bcde", mk(13, 14, 15, 16)},
|
||||||
|
{"fghi", mk(17, 18, 19, 20)},
|
||||||
|
{"jklm", mk(21, 22, 23, 24)},
|
||||||
|
{" m0m", mk(0, 24, 1, 24)},
|
||||||
|
{"nopq", mk(25, 26, 27, 28)},
|
||||||
|
{"rstu", mk(29, 30, 31, 32)},
|
||||||
|
{"vwxy", mk(33, 34, 35, 36)},
|
||||||
|
{"z?z9", mk(37, 11, 37, 10)},
|
||||||
|
{"zzzz", mk(37, 37, 37, 37)},
|
||||||
|
}
|
||||||
|
|
||||||
|
maxSeen := false
|
||||||
|
for _, tc := range testCases {
|
||||||
|
got, gotOK := Encode(tc.s)
|
||||||
|
if !gotOK {
|
||||||
|
t.Errorf("%q: ok: got %t, want %t", tc.s, gotOK, true)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if got != tc.want {
|
||||||
|
t.Errorf("%q: got %d, want %d", tc.s, got, tc.want)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if got > Max || got > 1<<MaxBits-1 {
|
||||||
|
t.Errorf("%q: got %d, want <= %d and <= %d", tc.s, got, Max, 1<<MaxBits-1)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
maxSeen = maxSeen || (got == Max)
|
||||||
|
}
|
||||||
|
if !maxSeen {
|
||||||
|
t.Error("Max was not seen")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInvalid(t *testing.T) {
|
||||||
|
testCases := []string{
|
||||||
|
"",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
"....",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
"Abcd",
|
||||||
|
"a\x00cd",
|
||||||
|
"ab+d",
|
||||||
|
"abc\x80",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
if _, gotOK := Encode(tc); gotOK {
|
||||||
|
t.Errorf("%q: ok: got %t, want %t", tc, gotOK, false)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user