Transaction
6A3B94D127B9F4…B229AC79C96E
Block 25,949 · index 1 · indexed
Summary
- Hash
- 6A3B94D127B9F422350997EFFF006D828DF033AA91957B4A338DB229AC79C96E
- Block
- 25,949
- Size
- 5171 bytes
- Gas used
- 9,492,816 / 11,391,439
- Fee
- 1000000ugnot
- Memo
- gnopublish
- Status
- success
Messages
Arguments · 9
- #1rle
- #2README.md
- #3# `gno.land/p/moul/x/daily/rle/v0` **Run-length encoding** — `Encode`, `Decode`, `Ratio`, `MaxLen`. Runs of a repeated byte collapse to `<count><char>`, counts in decimal: `"aaabbc"` → `"3a2b1c"`. ```go import "gno.land/p/moul/x/daily/rle/v0" rle.Encode("aaabbc") // "3a2b1c", nil rle.Decode("3a2b1c") // "aaabbc", nil rle.Ratio("abcdef", "1a1b1c1d1e1f") // 200 — bigger than the input! ``` **Every run carries a 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. That trade is deliberate and visible: `Ratio` returns over 100 when the "compression" grew the data, because **RLE only wins on runny input** and pretending otherwise would be dishonest. **Digits are rejected** by `Encode` — in the output they would be indistinguishable from a run count, so a round-trip would silently return the wrong string. Better to refuse. `Decode` bounds the *expanded* size too, not just its input: `"999999x"` is seven bytes that would otherwise become a megabyte. **Live demo:** [`r/moul/x/daily/rledemo`](https://github.com/moul/gno-contracts/tree/main/r/moul/x/daily/rledemo/v0) · render it at [`/r/moul/x/daily/rledemo/v0`](https://gno.land/r/moul/x/daily/rledemo/v0). <!-- BEGIN GNOCONTRACTS FOOTER (generated by `make readmes`; do not edit below) --> --- Part of **[moul/gno-contracts](https://github.com/moul/gno-contracts)** — moul's versioned gno.land contracts. See the repository for the full catalog, build/test tooling, and usage. > 🧪 **Highly experimental — potentially vibe-coded.** Not audited; may break, change, or be removed at any time. Do not use with anything of value. Full disclaimer: [DISCLAIMER](https://github.com/moul/gno-contracts/blob/main/DISCLAIMER.md). <!-- END GNOCONTRACTS FOOTER -->
- #4gnomod.toml
- #5module = "gno.land/p/moul/x/daily/rle/v0" gno = "0.9"
- #6rle.gno
- #7// 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/moul/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) }
- #8/gno.MemPackageType
- #9 MPUserProd
Result log
msg:0,success:true,log:,events:[]