Playground
gnomod.toml
module = "gno.land/p/nym-moultest123/x/daily/rle/v0" gno = "0.9" [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186405 max_deposit = "5000000ugnot"
rle.gno
// Package rle implements run-length encoding as a pure, reusable package: runs
// of a repeated byte collapse to a count and the byte.
//
// The encoding is `<count><char>` with counts in decimal, e.g. "aaabbc" →
// "3a2b1c". Every run is emitted with its count, including runs of one — a
// uniform grammar is cheaper to decode and impossible to get subtly wrong,
// at the cost of expanding data that has no runs at all. RLE is a *win only on
// runny data*; Encode can legitimately produce output longer than its input,
// and the demo shows exactly that case rather than hiding it.
//
// Digits cannot appear in the input, since they would be indistinguishable from
// a count on the way back; Encode rejects them rather than round-tripping wrong.
//
// A live demo of this package is at
// [r/moul/x/daily/rledemo](/r/nym-moultest123/x/daily/rledemo/v0).
package rle
import (
"errors"
"strconv"
"strings"
)
// MaxLen bounds input so encode/decode gas stays predictable.
const MaxLen = 4096
var (
// ErrTooLong is returned when input exceeds MaxLen.
ErrTooLong = errors.New("rle: input too long")
// ErrDigit is returned when input contains a digit, which would be
// ambiguous with a run count.
ErrDigit = errors.New("rle: input must not contain digits")
// ErrMalformed is returned when decoding input that is not <count><char>.
ErrMalformed = errors.New("rle: malformed input")
)
// Encode collapses runs of repeated bytes into <count><char> pairs.
func Encode(s string) (string, error) {
if len(s) > MaxLen {
return "", ErrTooLong
}
for i := 0; i < len(s); i++ {
if s[i] >= '0' && s[i] <= '9' {
return "", ErrDigit
}
}
if s == "" {
return "", nil
}
var b strings.Builder
run := 1
for i := 1; i <= len(s); i++ {
if i < len(s) && s[i] == s[i-1] {
run++
continue
}
b.WriteString(strconv.Itoa(run))
b.WriteByte(s[i-1])
run = 1
}
return b.String(), nil
}
// Decode expands <count><char> pairs back into the original string.
func Decode(s string) (string, error) {
if len(s) > MaxLen {
return "", ErrTooLong
}
var b strings.Builder
i := 0
for i < len(s) {
j := i
for j < len(s) && s[j] >= '0' && s[j] <= '9' {
j++
}
if j == i || j == len(s) { // no count, or a count with no character
return "", ErrMalformed
}
n, err := strconv.Atoi(s[i:j])
if err != nil || n <= 0 {
return "", ErrMalformed
}
if b.Len()+n > MaxLen {
return "", ErrTooLong // a short input can decode to an enormous one
}
b.WriteString(strings.Repeat(string(s[j]), n))
i = j + 1
}
return b.String(), nil
}
// Ratio returns len(encoded)/len(original) as a percentage, rounded down.
// Over 100 means the encoding made the data BIGGER, which is the honest
// outcome for input without runs.
func Ratio(original, encoded string) int {
if len(original) == 0 {
return 0
}
return len(encoded) * 100 / len(original)
}
rle_test.gno
package rle
import (
"strings"
"testing"
"gno.land/p/nt/uassert/v0"
)
func TestEncode(t *testing.T) {
cases := []struct{ in, want string }{
{"", ""},
{"a", "1a"},
{"aaa", "3a"},
{"aaabbc", "3a2b1c"},
{"abc", "1a1b1c"},
{"aabbaa", "2a2b2a"}, // runs, not totals
}
for _, c := range cases {
got, err := Encode(c.in)
uassert.NoError(t, err)
uassert.Equal(t, c.want, got)
}
}
func TestDecode(t *testing.T) {
cases := []struct{ in, want string }{
{"", ""},
{"3a", "aaa"},
{"3a2b1c", "aaabbc"},
{"12x", strings.Repeat("x", 12)}, // multi-digit counts
}
for _, c := range cases {
got, err := Decode(c.in)
uassert.NoError(t, err)
uassert.Equal(t, c.want, got)
}
}
// The property that matters: decode(encode(s)) == s.
func TestRoundTrip(t *testing.T) {
for _, s := range []string{"a", "aaa", "aaabbc", "abcdef", "zzzzzzzzzzzz", "ab"} {
enc, err := Encode(s)
uassert.NoError(t, err)
dec, err2 := Decode(enc)
uassert.NoError(t, err2)
uassert.Equal(t, s, dec)
}
}
// Digits would be ambiguous with a run count, so they are refused up front.
func TestEncodeRejectsDigits(t *testing.T) {
_, err := Encode("ab3c")
uassert.ErrorIs(t, err, ErrDigit)
}
func TestDecodeMalformed(t *testing.T) {
for _, s := range []string{"a", "abc", "3", "3a2", "0a"} {
_, err := Decode(s)
uassert.Error(t, err)
}
}
// A short encoded input must not be able to blow up memory on decode.
func TestDecodeExpansionIsBounded(t *testing.T) {
_, err := Decode("999999x")
uassert.ErrorIs(t, err, ErrTooLong)
}
func TestEncodeTooLong(t *testing.T) {
_, err := Encode(strings.Repeat("a", MaxLen+1))
uassert.ErrorIs(t, err, ErrTooLong)
}
// RLE is a win only on runny data — say so rather than hide it.
func TestRatioCanExceed100(t *testing.T) {
enc, _ := Encode("abcdef")
uassert.Equal(t, "1a1b1c1d1e1f", enc)
uassert.Equal(t, 200, Ratio("abcdef", enc)) // twice the size!
enc2, _ := Encode(strings.Repeat("a", 100))
uassert.Equal(t, 4, Ratio(strings.Repeat("a", 100), enc2))
uassert.Equal(t, 0, Ratio("", ""))
}