fastpostcode

package module
v0.0.0-...-fb0bd38 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2020 License: MIT Imports: 1 Imported by: 0

README

fastpostcode

A fast postcode validator written in Go. Capable of validating every postcode in the United Kingdom in milliseconds.

The regular expressions for the matcher are from this stackoverflow answer. These were rewritten as ragel expressions and compiled into a Finite State Machine.

The trade-off for this performance is binary size, with the matcher being ~1600 lines of code. If you want an idiomatic Go implementation, the regex version is here.

Validating a postcode by its pattern catches invalid inputs, but there will be false positives. Validator is tested against all ~1.8 million postcodes.

See

Benchmarks

Benchmark_IsValid/fastpostcode_Valid           111773841	      10.7 ns/op
Benchmark_IsValid/fastpostcode_Invalid         150052951	      7.82 ns/op

Benchmark_IsValid/regex_Valid                	 4851324	       222 ns/op
Benchmark_IsValid/regex_Invalid               	  714759	      1516 ns/op

Example

package main

import (
	"fmt"
	"github.com/T-J-L/fastpostcode"
)

func main() {
	if fastpostcode.IsValid("SW1A 1AA") {
		fmt.Println("This looks like a valid postcode")
	}
}

License

fastpostcode is released under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsValid

func IsValid(postcode string) bool

IsValid validates the input matches the format of a UK Postcode.

This is a complete implementation that accounts for all formats, overseas territories and special cases.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL