countries

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package countries holds the phone-number metadata generated from Google's libphonenumber PhoneNumberMetadata.xml.

Metadata values are always used through a *Metadata pointer: they embed sync.Once guards for lazily compiled regular expressions and must not be copied.

Index

Constants

View Source
const MaxCallingCodeLen = 3

MaxCallingCodeLen is the length of the longest calling code.

View Source
const SourceHash = "20d5582c8eb8e3a291a1499359c6b8e4c5f46610cc5fe04ec5f2c989f812f014"

SourceHash is the SHA-256 of the PhoneNumberMetadata.xml this file was generated from. It identifies exactly which upstream revision of the metadata is baked in, and lets a test confirm the vendored XML and the generated table have not drifted apart.

Variables

View Source
var Data = map[string]*Metadata{}/* 245 elements not displayed */

Data maps ISO-3166 alpha-2 region codes to their metadata.

View Source
var NonGeo = map[string]*Metadata{
	"800": md001_800,
	"808": md001_808,
	"870": md001_870,
	"878": md001_878,
	"881": md001_881,
	"882": md001_882,
	"883": md001_883,
	"888": md001_888,
	"979": md001_979,
}

NonGeo maps calling codes of non-geographical entities (libphonenumber region "001") to their metadata.

Functions

This section is empty.

Types

type Desc

type Desc struct {
	Pattern lazyRe
	// Lengths holds the sorted possible national number lengths.
	Lengths []int32
	// LocalOnly holds lengths that are only valid when dialled locally.
	LocalOnly []int32
	Example   string
}

Desc describes a single number range (fixed line, mobile, toll free, ...).

func (*Desc) Exists

func (d *Desc) Exists() bool

Exists reports whether the country defines this range at all.

func (*Desc) HasLength

func (d *Desc) HasLength(n int) bool

HasLength reports whether n is a possible national number length.

func (*Desc) HasLocalOnlyLength

func (d *Desc) HasLocalOnlyLength(n int) bool

HasLocalOnlyLength reports whether n is valid only for local dialling.

func (*Desc) Match

func (d *Desc) Match(nsn string) bool

Match reports whether the national significant number matches this range.

type Metadata

type Metadata struct {
	ISO2 string
	ISO3 string
	Name string

	// DialCode is the country calling code without a leading '+'.
	DialCode string
	// IsMainCountry reports whether this region owns DialCode. Several
	// regions may share one code (e.g. +1); exactly one is the main region.
	IsMainCountry bool
	// LeadingDigits disambiguates regions sharing a DialCode.
	LeadingDigits lazyRe

	NationalPrefix string
	// SimpleNationalPrefix is set when the trunk prefix is a plain literal
	// with no carrier-code group and no transform rule, which lets the
	// parser strip it without the regexp engine.
	SimpleNationalPrefix string
	// NationalPrefixForParsing matches the trunk prefix (and any carrier
	// selection code) to strip before validation.
	NationalPrefixForParsing lazyRe
	// NationalPrefixTransformRule rewrites the number after stripping.
	NationalPrefixTransformRule string

	InternationalPrefix          lazyRe
	PreferredInternationalPrefix string
	PreferredExtnPrefix          string
	MobileNumberPortable         bool

	// General matches any valid number in the region.
	General Desc
	// Descs holds the per-type ranges, indexed by PhoneType.
	Descs [NumDescs]Desc
	// NoIntlDialling matches numbers that cannot be dialled from abroad.
	NoIntlDialling Desc

	Formats []NumberFormat

	// MinLength and MaxLength are the shortest and longest possible national
	// number lengths, derived from General.Lengths.
	MinLength int
	MaxLength int

	// Timezones holds the region's IANA time zones. It comes from
	// internal/metadata/metadata.json, not from libphonenumber, and is
	// empty in the bundled data.
	Timezones []string
}

Metadata holds everything known about one calling region.

func RegionsForCode

func RegionsForCode(digits string) []*Metadata

RegionsForCode returns the metadata of every region sharing the calling code made of the first n digits of digits, main region first. It returns nil when no region uses that code. The lookup performs no allocation.

func (*Metadata) Desc

func (m *Metadata) Desc(t PhoneType) *Desc

Desc returns the descriptor for t, or nil if t has no descriptor.

type NumberFormat

type NumberFormat struct {
	// Pattern is anchored and captures the groups referenced by Format.
	Pattern lazyRe
	// Format is the replacement template using $1..$9.
	Format string
	// LeadingDigits, when set, must match the start of the national number
	// for this rule to apply.
	LeadingDigits lazyRe
	// NationalPrefixFormattingRule wraps the formatted groups, e.g. "$NP$FG".
	NationalPrefixFormattingRule string
	// NationalPrefixOptional reports whether the national prefix may be
	// omitted when formatting nationally.
	NationalPrefixOptional bool
	// CarrierCodeFormattingRule wraps the groups when a carrier code is used.
	CarrierCodeFormattingRule string
	// IntlFormat overrides Format for international formatting. The value
	// "NA" means the number must not be formatted internationally.
	IntlFormat string
}

NumberFormat is one national formatting rule.

type PhoneType

type PhoneType uint8

PhoneType identifies a number range within a country's metadata.

const (
	FixedLine PhoneType = iota
	Mobile
	TollFree
	PremiumRate
	SharedCost
	VoIP
	PersonalNumber
	Pager
	UAN
	Voicemail

	// NumDescs is the number of per-type descriptors stored in Metadata.Descs.
	NumDescs

	// FixedLineOrMobile is reported when a number matches both ranges.
	FixedLineOrMobile
	// Unknown is reported when no range matches.
	Unknown
)

func (PhoneType) String

func (t PhoneType) String() string

Jump to

Keyboard shortcuts

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