<commit_before># Hasher
Hasher is a tool to automate the creation of methods and tables for a string &#8594; uint32 mapper. It uses the fact that all keys are known apriori, allowing it to generate a very efficient hashtable.

It is really just a copy-paste from https://github.com/golang/tools/blob/master/cmd/stringer/stringer.go and https://github.com/golang/net/tree/master/html/atom/gen.go with some tweaks. So nothing is really my work!

For example, given this snippet,
``` go
	package painkiller

	type Pill uint32

	const (
		Placebo Pill = iota
		Aspirin
		Ibuprofen
		Paracetamol
		Acetaminophen = Paracetamol
	)
```

running this command

	hasher -type=Pill -file=pill.go

in the same directory will __OVERWRITE__ the same file with the constant list itself (except for different values than `iota`), tables, hashes and conversion functions between `string` and `uint32`. The tool is thus one that reiterates over its own output! To add more keys after it ran, just add rows like:

``` go
	// ...
	Aspirin     Pill = 0x7
	Ibuprofen   Pill = 0x709
	Paracetamol Pill = 0x100b
	Placebo     Pill = 0x1b07
	NewPillA Pill = 0
	NewPillB Pill = 0
	// ...
```

and rerun `hasher`!

## Usage
Typically this process would be run using go generate, like this:
``` go
	//go:generate hasher -type=Pill -file=pill.go
```
but `hasher` adds that row itself too, so you can use the command-line just as well. Just make sure the file consists solely of the new type and the constants!

It must have the -type and -file flag, that accepts a single type and filename respectively. The given file will be __OVERWRITTEN__.

## Lower and upper case and dashes
The first upper case of the constants is lowered and any upper case after an underscore. Any underscore is replace by a dash. So that:
``` go
	fmt.Print(painkiller.Aspirin) // aspirin
	fmt.Print(painkiller.StrongMorphine) // strongMorphine
	fmt.Print(painkiller.Amitriptyline_Gabapentin) // amitriptyline-gabapentin
```

## Hash to string
Translate the value of a Pill constant to the string representation of the respective constant name, so that the call `fmt.Print(painkiller.Aspirin)` will print the string `"aspirin"`.
``` go
	func (Pill) String() string
```

## String to hash
Translate a string to a value of a Pill constant with the same name. So that `ToPill("aspirin") == painkiller.Aspirin`. This function is called `"To" + typeName` so it changes name when the type name changes.
``` go
	func ToPill(s []byte) Pill
```<commit_msg>Readme update<commit_after># Hasher
Hasher is a tool to automate the creation of methods and tables for a string &#8594; uint32 mapper. It uses the fact that all keys are known apriori, allowing it to generate a very efficient hashtable.

It is really just a copy-paste from https://github.com/golang/tools/blob/master/cmd/stringer/stringer.go and https://github.com/golang/net/tree/master/html/atom/gen.go with some tweaks. So nothing is really my work!

For example, given this snippet,
``` go
package painkiller

type Pill uint32

const (
	Placebo Pill = iota
	Aspirin
	Ibuprofen
	Paracetamol
	Acetaminophen = Paracetamol
)
```

running this command

	hasher -type=Pill -file=pill.go

in the same directory will __OVERWRITE__ the same file with the constant list itself (except for different values than `iota`), tables, hashes and conversion functions between `string` and `uint32`. The tool is thus one that reiterates over its own output! To add more keys after it ran, just add rows like:

``` go
// ...
Aspirin     Pill = 0x7
Ibuprofen   Pill = 0x709
Paracetamol Pill = 0x100b
Placebo     Pill = 0x1b07
NewPillA Pill = 0
NewPillB Pill = 0
// ...
```

and rerun `hasher`!

## Installation
Run the following command

	go get github.com/tdewolff/hasher

## Usage
Typically this process would be run using go generate, like this:
``` go
//go:generate hasher -type=Pill -file=pill.go
```
but `hasher` adds that row itself too, so you can use the command-line just as well. Just make sure the file consists solely of the new type and the constants!

It must have the -type and -file flag, that accepts a single type and filename respectively. The given file will be __OVERWRITTEN__.

## Lower- and uppercase and dashes
The first uppercase of the constants is lowered and any uppercase after an underscore. Any underscore is replace by a dash. So that:
``` go
fmt.Print(painkiller.Aspirin) // aspirin
fmt.Print(painkiller.StrongMorphine) // strongMorphine
fmt.Print(painkiller.Amitriptyline_Gabapentin) // amitriptyline-gabapentin
```

## Hash to string
Translate the value of a Pill constant to the string representation of the respective constant name, so that the call `fmt.Print(painkiller.Aspirin)` will print the string `"aspirin"`.
``` go
func (Pill) String() string
```

## String to hash
Translate a string to a value of a Pill constant with the same name. So that `ToPill("aspirin") == painkiller.Aspirin`. This function is called `"To" + typeName` so it changes name when the type name changes.
``` go
func ToPill(s []byte) Pill
```
{'fraction_non_alphanumeric': 0.08263806118394915, 'fraction_numerical': 0.010329757647993643, 'mean_word_length': 4.096153846153846, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7670649', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1537, 'n_words': 682}