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

escape_test.gno

package varintdemo

import (
	"strings"
	"testing"

	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/md/v0"
	"gno.land/p/nt/uassert/v0"
)

// The path is the visitor's text and it is rendered inside a code span, so a
// backtick in it must not close that span and let a link through. A
// backslash does not escape a backtick inside a span: only a wider fence
// does, which is what md.InlineCode picks.
func TestRenderArgCannotCloseItsCodeSpan(t *testing.T) {
	arg := "x` [claim](https://evil.example) `"
	out := Render("/" + arg)
	uassert.True(t, strings.Contains(out, md.InlineCode(arg)), out)
	uassert.False(t, strings.Contains(out, "`x\\` [claim](https://evil.example) \\``"), out)
}

// A value above MaxInt64 is one this codec carries: the unsigned line is
// rendered, so the page must not then say the codec cannot carry it.
func TestRenderMaxUint64DoesNotContradictItself(t *testing.T) {
	out := Render("/18446744073709551615")
	uassert.True(t, strings.Contains(out, "ff ff ff ff ff ff ff ff ff 01"), out)
	uassert.False(t, strings.Contains(out, "Not an integer"), out)
}

// Neither parse succeeding is the only case that message belongs to.
func TestRenderRejectsNonInteger(t *testing.T) {
	out := Render("/notanumber")
	uassert.True(t, strings.Contains(out, "Not an integer"), out)
}

// Whitespace is not trimmed: trimming would launder an input the parsers
// refuse into a clean encoding of a value nobody submitted.
func TestRenderDoesNotTrim(t *testing.T) {
	out := Render("/ 300 ")
	uassert.True(t, strings.Contains(out, "Not an integer"), out)
}

gnomod.toml

module = "gno.land/r/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/daily/varintdemo/v0"
gno = "0.9"
private = true

[addpkg]
  creator = "g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv"
  height = 914308
  max_deposit = "6000000ugnot"

render_example_test.gno

package varintdemo

// ExampleRender pins the realm's Render output as a testable example. The
// unsigned column is the protobuf base-128 vector set, so this doubles as a
// conformance test against the library.
func ExampleRender() {
	print(Render(""))
	// Output:
	// # Varint
	//
	// LEB128 base-128 integers, demoing the [`p/moul/x/daily/varint`](/p/moul/x/daily/varint/v0) library.
	//
	// ## Unsigned
	//
	// Seven payload bits per byte, so the length steps at every power of 128.
	//
	// | value | encoding | bytes |
	// | --- | --- | --- |
	// | `0` | `00` | 1 |
	// | `1` | `01` | 1 |
	// | `127` | `7f` | 1 |
	// | `128` | `80 01` | 2 |
	// | `300` | `ac 02` | 2 |
	// | `16383` | `ff 7f` | 2 |
	// | `16384` | `80 80 01` | 3 |
	// | `4294967296` | `80 80 80 80 10` | 5 |
	// | `18446744073709551615` | `ff ff ff ff ff ff ff ff ff 01` | 10 |
	//
	// ## Signed, through zigzag
	//
	// Encoding a negative directly would sign-extend it to ten bytes. Zigzag interleaves the signs first, so `-1` costs one byte and not ten.
	//
	// | value | zigzag | encoding | bytes |
	// | --- | --- | --- | --- |
	// | `0` | `0` | `00` | 1 |
	// | `-1` | `1` | `01` | 1 |
	// | `1` | `2` | `02` | 1 |
	// | `-64` | `127` | `7f` | 1 |
	// | `64` | `128` | `80 01` | 2 |
	// | `-9223372036854775808` | `18446744073709551615` | `ff ff ff ff ff ff ff ff ff 01` | 10 |
	// | `9223372036854775807` | `18446744073709551614` | `fe ff ff ff ff ff ff ff ff 01` | 10 |
	//
	// ## Only the canonical encoding decodes
	//
	// A padded varint means the same number has two spellings, which is a different hash for the same state. The decoder refuses them.
	//
	// | input | result |
	// | --- | --- |
	// | `80 00`, zero with one padding group | `varint: non-canonical encoding` |
	// | `81 00`, one with one padding group | `varint: non-canonical encoding` |
	// | `80`, continuation bit then nothing | `varint: truncated` |
	// | `ff ... ff 02`, ten groups, eleven bits too many | `varint: value overflows uint64` |
	//
	// > Try another value: append it to the path, for instance [`/-300`](/r/moul/x/daily/varintdemo/v0:/-300).
}

// ExampleRender_one pins the single-value path, and with it the escaping the
// realm owes a caller-supplied argument.
func ExampleRender_one() {
	print(Render("/-300"))
	// Output:
	// # Varint
	//
	// LEB128 base-128 integers, demoing the [`p/moul/x/daily/varint`](/p/moul/x/daily/varint/v0) library.
	//
	// ## `-300`
	//
	// - zigzag: `599`
	// - signed: `d7 04` (2 bytes)
}

varintdemo.gno

// Package varintdemo is a small gnoweb demo of the
// [p/moul/x/daily/varint](/p/moul/x/daily/varint/v0) library: it shows what the
// group boundaries cost, what zigzag buys on negative numbers, and which
// encodings the decoder refuses.
//
// It contains no codec logic of its own. Stateless, so Render is deterministic.
package varintdemo

import (
	"strconv"
	"strings"

	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/kit/ui/v0"
	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/md/v0"
	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/daily/varint/v0"
)

// Fixed inputs, never chain state: a demo whose output moves with the block
// height cannot be pinned by an example test.
var (
	unsigned = []uint64{0, 1, 127, 128, 300, 16383, 16384, 1 << 32, 1<<64 - 1}
	signed   = []int64{0, -1, 1, -64, 64, -1 << 63, 1<<63 - 1}
	rejected = []struct{ label, in string }{
		{"`80 00`, zero with one padding group", "\x80\x00"},
		{"`81 00`, one with one padding group", "\x81\x00"},
		{"`80`, continuation bit then nothing", "\x80"},
		{"`ff ... ff 02`, ten groups, eleven bits too many", "\xff\xff\xff\xff\xff\xff\xff\xff\xff\x02"},
	}
)

// Render renders the demo for gnoweb.
//
//	Render("") / Render("/")  -> the vector tables and the refusals
//	Render("/<integer>")      -> encode that integer both ways
func Render(path string) string {
	var b strings.Builder
	b.WriteString("# Varint\n\n")
	b.WriteString("LEB128 base-128 integers, demoing the ")
	b.WriteString("[`p/moul/x/daily/varint`](/p/moul/x/daily/varint/v0) library.\n\n")

	if arg := parseArg(path); arg != "" {
		b.WriteString(renderOne(arg))
		return b.String()
	}

	b.WriteString("## Unsigned\n\n")
	b.WriteString("Seven payload bits per byte, so the length steps at every power of 128.\n\n")
	t := ui.NewTable("value", "encoding", "bytes")
	for _, x := range unsigned {
		t.Row("`"+strconv.FormatUint(x, 10)+"`", "`"+hex(varint.PutUvarint(x))+"`", strconv.Itoa(varint.Len(x)))
	}
	b.WriteString(t.String())

	b.WriteString("\n## Signed, through zigzag\n\n")
	b.WriteString("Encoding a negative directly would sign-extend it to ten bytes. ")
	b.WriteString("Zigzag interleaves the signs first, so `-1` costs one byte and not ten.\n\n")
	t = ui.NewTable("value", "zigzag", "encoding", "bytes")
	for _, x := range signed {
		t.Row(
			"`"+strconv.FormatInt(x, 10)+"`",
			"`"+strconv.FormatUint(varint.ZigZag(x), 10)+"`",
			"`"+hex(varint.PutVarint(x))+"`",
			strconv.Itoa(varint.LenVarint(x)),
		)
	}
	b.WriteString(t.String())

	b.WriteString("\n## Only the canonical encoding decodes\n\n")
	b.WriteString("A padded varint means the same number has two spellings, which is a ")
	b.WriteString("different hash for the same state. The decoder refuses them.\n\n")
	t = ui.NewTable("input", "result")
	for _, c := range rejected {
		_, _, err := varint.Uvarint(c.in)
		t.Row(c.label, "`"+err.Error()+"`")
	}
	b.WriteString(t.String())

	b.WriteString("\n> Try another value: append it to the path, for instance ")
	b.WriteString("[`/-300`](/r/moul/x/daily/varintdemo/v0:/-300).\n")
	return b.String()
}

// renderOne encodes a single integer supplied in the path.
//
// The argument is the visitor's text and it is shown inside a code span, so
// it goes through md.InlineCode and not ui.Cell: a cell escaper backslashes
// a backtick, and a backslash does not close a backtick inside a span. Only
// a fence wider than anything in the argument does, which is what
// md.InlineCode picks.
func renderOne(arg string) string {
	var b strings.Builder
	b.WriteString("## ")
	b.WriteString(md.InlineCode(arg))
	b.WriteString("\n\n")

	unsigned := false
	if u, err := strconv.ParseUint(arg, 10, 64); err == nil {
		unsigned = true
		b.WriteString("- unsigned: `" + hex(varint.PutUvarint(u)) + "` (" + strconv.Itoa(varint.Len(u)) + " bytes)\n")
	}
	i, err := strconv.ParseInt(arg, 10, 64)
	if err != nil {
		// Above MaxInt64 the unsigned line above is the whole answer: the
		// codec carries the value, only the signed form cannot hold it.
		if !unsigned {
			b.WriteString("Not an integer this codec can carry: 64 bits, signed or unsigned.\n")
		}
		return b.String()
	}
	b.WriteString("- zigzag: `" + strconv.FormatUint(varint.ZigZag(i), 10) + "`\n")
	b.WriteString("- signed: `" + hex(varint.PutVarint(i)) + "` (" + strconv.Itoa(varint.LenVarint(i)) + " bytes)\n")
	return b.String()
}

// hex renders a byte string as lowercase hex pairs. Presentation only: the
// library deals in bytes and says nothing about how they are shown.
func hex(s string) string {
	const digits = "0123456789abcdef"
	var b strings.Builder
	for i := 0; i < len(s); i++ {
		if i > 0 {
			b.WriteByte(' ')
		}
		b.WriteByte(digits[s[i]>>4])
		b.WriteByte(digits[s[i]&0x0f])
	}
	return b.String()
}

// parseArg takes the one routing slash off and nothing else. It does not cut
// at a later slash and it does not trim: a page that offers to encode what
// the caller typed either encodes that string or says it refused it, and
// never quietly substitutes a shorter or tidier one.
func parseArg(path string) string {
	return strings.TrimPrefix(path, "/")
}