Playground
escape_test.gno
package rledemo
import (
"testing"
"gno.land/p/nt/uassert/v0"
)
// payload closes a one-backtick code span and opens an image. Written raw, as
// v0 does, it renders as markup the realm appears to have authored.
const payload = "x`  `y"
// TestRenderEscapesInput pins that an accepted input is echoed as a code span
// it cannot close, and so is its encoding, which carries the backtick through.
func TestRenderEscapesInput(t *testing.T) {
uassert.Equal(t, "# Run-Length Encoding\n\n`<count><char>` pairs, demoing the [`p/moul/x/daily/rle`](/p/moul/x/daily/rle/v0) library.\n\n## ``x`  `y``\n\n- encoded: ``1x1`1 1!1[1p1]1(1h2t1p1s1:1e1.1i1o1)1 1`1y``\n- size: **190%**\n- round-trips: ✅\n", Render("/"+payload))
}
// TestRenderRejectsBeforeEcho pins the order: rle refuses digits, and the
// refused input is never written back. v0 wrote the heading first.
func TestRenderRejectsBeforeEcho(t *testing.T) {
uassert.Equal(t, "# Run-Length Encoding\n\n`<count><char>` pairs, demoing the [`p/moul/x/daily/rle`](/p/moul/x/daily/rle/v0) library.\n\n_rle: input must not contain digits_\n", Render("/1"+payload))
}
gnomod.toml
module = "gno.land/r/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/daily/rledemo/v1" gno = "0.9" private = true [addpkg] creator = "g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv" height = 755201 max_deposit = "5000000ugnot"
render_example_test.gno
package rledemo
// ExampleRender pins the realm's root Render output as a testable example.
func ExampleRender() {
print(Render(""))
// Output:
// # Run-Length Encoding
//
// `<count><char>` pairs, demoing the [`p/moul/x/daily/rle`](/p/moul/x/daily/rle/v0) library.
//
// | input | encoded | size | round-trips |
// |---|---|---|---|
// | `aaaaaaaaaabbbbbbbbbb` | `10a10b` | 30% | ✅ |
// | `aaabbc` | `3a2b1c` | 100% | ✅ |
// | `abcdef` | `1a1b1c1d1e1f` | 200% | ✅ |
//
// > Over 100% means the encoding made the data **bigger**. RLE only wins on runny input, and `abcdef` is the honest counter-example.
//
// > Append text to the path to encode it — digits are rejected, since they would be ambiguous with a run count.
}
// ExampleRender_rejectsDigits pins the refusal path: digits cannot be encoded
// because they are indistinguishable from a run count on the way back, and a
// refused input is not echoed.
func ExampleRender_rejectsDigits() {
print(Render("/ab3c"))
// Output:
// # Run-Length Encoding
//
// `<count><char>` pairs, demoing the [`p/moul/x/daily/rle`](/p/moul/x/daily/rle/v0) library.
//
// _rle: input must not contain digits_
}
rledemo.gno
// Package rledemo is a small gnoweb demo of the run-length codec provided by
// the [p/moul/x/daily/rle](/p/moul/x/daily/rle/v0) library: it encodes a few
// sample strings, round-trips them, and reports the size ratio — including the
// case where the "compression" makes things bigger.
//
// It contains no codec logic of its own. Stateless, so Render is deterministic.
package rledemo
import (
"strconv"
"strings"
"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/md/v0"
"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/daily/rle/v0"
)
// samples are picked to show both outcomes: runny data compresses, data
// without runs expands.
var samples = []string{
"aaaaaaaaaabbbbbbbbbb",
"aaabbc",
"abcdef",
}
// Render renders the demo for gnoweb.
//
// Render("") / Render("/") -> the samples table
// Render("/<text>") -> encode that text
func Render(path string) string {
var b strings.Builder
b.WriteString("# Run-Length Encoding\n\n")
b.WriteString("`<count><char>` pairs, demoing the ")
b.WriteString("[`p/moul/x/daily/rle`](/p/moul/x/daily/rle/v0) library.\n\n")
if in := parseArg(path); in != "" {
return b.String() + one(in)
}
b.WriteString("| input | encoded | size | round-trips |\n|---|---|---|---|\n")
for _, s := range samples {
enc, err := rle.Encode(s)
if err != nil {
continue
}
dec, _ := rle.Decode(enc)
b.WriteString("| `")
b.WriteString(s)
b.WriteString("` | `")
b.WriteString(enc)
b.WriteString("` | ")
b.WriteString(strconv.Itoa(rle.Ratio(s, enc)))
b.WriteString("% | ")
if dec == s {
b.WriteString("✅")
} else {
b.WriteString("❌")
}
b.WriteString(" |\n")
}
b.WriteString("\n> Over 100% means the encoding made the data **bigger**. ")
b.WriteString("RLE only wins on runny input, and `abcdef` is the honest counter-example.\n\n")
b.WriteString("> Append text to the path to encode it — digits are rejected, ")
b.WriteString("since they would be ambiguous with a run count.\n")
return b.String()
}
// one encodes a caller's text. The encoder runs first, so a rejected input is
// never echoed, and an accepted one still goes through md.InlineCode: rle
// accepts a backtick, and its encoding carries it through.
func one(in string) string {
var b strings.Builder
enc, err := rle.Encode(in)
if err != nil {
b.WriteString("_")
b.WriteString(err.Error())
b.WriteString("_\n")
return b.String()
}
b.WriteString("## " + md.InlineCode(in) + "\n\n")
dec, _ := rle.Decode(enc)
b.WriteString("- encoded: " + md.InlineCode(enc))
b.WriteString("\n- size: **")
b.WriteString(strconv.Itoa(rle.Ratio(in, enc)))
b.WriteString("%**\n- round-trips: ")
if dec == in {
b.WriteString("✅\n")
} else {
b.WriteString("❌\n")
}
return b.String()
}
func parseArg(path string) string {
s := strings.TrimSpace(path)
s = strings.TrimPrefix(s, "/")
if i := strings.IndexByte(s, '/'); i >= 0 {
s = s[:i]
}
return s
}