//go:build ignore

package main

import (
	"fmt"
	"log"
	"os"
)

const ToLower = 'a' - 'A'

const template = `// Code generated by go run tables_gen.go; DO NOT EDIT.

package percent

const (
	ToLowerTable      = %q
	ToUpperTable      = %q
	ShouldEscapeTable = %q
)
`

func main() {
	var toLowerTable [256]byte

	for i := 0; i < 256; i++ {
		c := byte(i)
		if c >= 'A' && c <= 'Z' {
			c += ToLower
		}
		toLowerTable[i] = c
	}

	var toUpperTable [256]byte

	for i := 0; i < 256; i++ {
		c := byte(i)
		if c >= 'a' && c <= 'z' {
			c -= ToLower
		}
		toUpperTable[i] = c
	}

	var shouldEscapeTable [256]byte

	for i := 0; i < 256; i++ {
		shouldEscapeTable[i] = 1
	}

	// ALPHA
	for i := int('a'); i <= int('z'); i++ {
		shouldEscapeTable[i] = 0
	}
	for i := int('A'); i <= int('Z'); i++ {
		shouldEscapeTable[i] = 0
	}

	// DIGIT
	for i := int('0'); i <= int('9'); i++ {
		shouldEscapeTable[i] = 0
	}

	// Unreserved characters
	for _, v := range `-._~` {
		shouldEscapeTable[v] = 0
	}

	data := fmt.Sprintf(template, toLowerTable, toUpperTable, shouldEscapeTable)

	err := os.WriteFile("tables.go", []byte(data), 0o660)
	if err != nil {
		log.Fatal(err)
	}
}
