summaryrefslogtreecommitdiff
path: root/vendor/github.com/buckket/go-blurhash/base83
diff options
context:
space:
mode:
authorLibravatar Tobi Smethurst <31960611+tsmethurst@users.noreply.github.com>2021-08-12 21:03:24 +0200
committerLibravatar GitHub <noreply@github.com>2021-08-12 21:03:24 +0200
commit98263a7de64269898a2f81207e38943b5c8e8653 (patch)
tree743c90f109a6c5d27832d1dcef2388d939f0f77a /vendor/github.com/buckket/go-blurhash/base83
parentText duplication fix (#137) (diff)
downloadgotosocial-98263a7de64269898a2f81207e38943b5c8e8653.tar.xz
Grand test fixup (#138)
* start fixing up tests * fix up tests + automate with drone * fiddle with linting * messing about with drone.yml * some more fiddling * hmmm * add cache * add vendor directory * verbose * ci updates * update some little things * update sig
Diffstat (limited to 'vendor/github.com/buckket/go-blurhash/base83')
-rw-r--r--vendor/github.com/buckket/go-blurhash/base83/base83.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/vendor/github.com/buckket/go-blurhash/base83/base83.go b/vendor/github.com/buckket/go-blurhash/base83/base83.go
new file mode 100644
index 000000000..6d1882811
--- /dev/null
+++ b/vendor/github.com/buckket/go-blurhash/base83/base83.go
@@ -0,0 +1,58 @@
+package base83
+
+import (
+ "fmt"
+ "math"
+ "strings"
+)
+
+const characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~"
+
+// An InvalidCharacterError occurs when a characters is found which is not part of the Base83 character set.
+type InvalidCharacterError rune
+
+func (e InvalidCharacterError) Error() string {
+ return fmt.Sprintf("base83: invalid string (character %q out of range)", rune(e))
+}
+
+// An InvalidLengthError occurs when a given value cannot be encoded to a string of given length.
+type InvalidLengthError int
+
+func (e InvalidLengthError) Error() string {
+ return fmt.Sprintf("base83: invalid length (%d)", int(e))
+}
+
+// Encode will encode the given integer value to a Base83 string with given length.
+// If length is too short to encode the given value InvalidLengthError will be returned.
+func Encode(value, length int) (string, error) {
+ divisor := int(math.Pow(83, float64(length)))
+ if value/divisor != 0 {
+ return "", InvalidLengthError(length)
+ }
+ divisor /= 83
+
+ var str strings.Builder
+ str.Grow(length)
+ for i := 0; i < length; i++ {
+ if divisor <= 0 {
+ return "", InvalidLengthError(length)
+ }
+ digit := (value / divisor) % 83
+ divisor /= 83
+ str.WriteRune(rune(characters[digit]))
+ }
+
+ return str.String(), nil
+}
+
+// Decode will decode the given Base83 string to an integer.
+func Decode(str string) (value int, err error) {
+ for _, r := range str {
+ idx := strings.IndexRune(characters, r)
+ if idx == -1 {
+ return 0, InvalidCharacterError(r)
+ }
+ value = value*83 + idx
+ }
+ return value, nil
+}