Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

gnomod.toml

module = "gno.land/r/nym-moultest123/x/daily/rledemo/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186411
  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/nym-moultest123/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.
func ExampleRender_rejectsDigits() {
	print(Render("/ab3c"))
	// Output:
	// # Run-Length Encoding
	//
	// `<count><char>` pairs, demoing the [`p/moul/x/daily/rle`](/p/nym-moultest123/x/daily/rle/v0) library.
	//
	// ## `ab3c`
	//
	// _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/nym-moultest123/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/nym-moultest123/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/nym-moultest123/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()
}

func one(in string) string {
	var b strings.Builder
	b.WriteString("## `")
	b.WriteString(in)
	b.WriteString("`\n\n")
	enc, err := rle.Encode(in)
	if err != nil {
		b.WriteString("_")
		b.WriteString(err.Error())
		b.WriteString("_\n")
		return b.String()
	}
	dec, _ := rle.Decode(enc)
	b.WriteString("- encoded: `")
	b.WriteString(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
}