-
-
Save squeek502/bf453a6ebbbd9eef8ad16ca43df78f1a to your computer and use it in GitHub Desktop.
iso3166.zig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes | |
//! https://www.iso.org/standard/72482.html | |
const std = @import("std"); | |
pub const Country = enum { | |
afghanistan, | |
aland_islands, | |
albania, | |
algeria, | |
american_samoa, | |
andorra, | |
angola, | |
anguilla, | |
antarctica, | |
antigua_and_barbuda, | |
argentina, | |
armenia, | |
aruba, | |
australia, | |
austria, | |
azerbaijan, | |
bahamas_the, | |
bahrain, | |
bangladesh, | |
barbados, | |
belarus, | |
belgium, | |
belize, | |
benin, | |
bermuda, | |
bhutan, | |
bolivia_plurinational_state_of, | |
bonaire_sint_eustatius_and_saba, | |
bosnia_and_herzegovina, | |
botswana, | |
bouvet_island, | |
brazil, | |
british_indian_ocean_territory_the, | |
brunei_darussalam, | |
bulgaria, | |
burkina_faso, | |
burundi, | |
cabo_verde, | |
cambodia, | |
cameroon, | |
canada, | |
cayman_islands_the, | |
central_african_republic_the, | |
chad, | |
chile, | |
china, | |
christmas_island, | |
cocos_keeling_islands_the, | |
colombia, | |
comoros_the, | |
congo_the_democratic_republic_of_the, | |
congo_the, | |
cook_islands_the, | |
costa_rica, | |
cote_divoire, | |
croatia, | |
cuba, | |
curacao, | |
cyprus, | |
czechia, | |
denmark, | |
djibouti, | |
dominica, | |
dominican_republic_the, | |
ecuador, | |
egypt, | |
el_salvador, | |
equatorial_guinea, | |
eritrea, | |
estonia, | |
eswatini, | |
ethiopia, | |
falkland_islands_the_malvinas, | |
faroe_islands_the, | |
fiji, | |
finland, | |
france, | |
french_guiana, | |
french_polynesia, | |
french_southern_territories_the, | |
gabon, | |
gambia_the, | |
georgia, | |
germany, | |
ghana, | |
gibraltar, | |
greece, | |
greenland, | |
grenada, | |
guadeloupe, | |
guam, | |
guatemala, | |
guernsey, | |
guinea, | |
guinea_bissau, | |
guyana, | |
haiti, | |
heard_island_and_mcdonald_islands, | |
holy_see_the, | |
honduras, | |
hong_kong, | |
hungary, | |
iceland, | |
india, | |
indonesia, | |
iran_islamic_republic_of, | |
iraq, | |
ireland, | |
isle_of_man, | |
israel, | |
italy, | |
jamaica, | |
japan, | |
jersey, | |
jordan, | |
kazakhstan, | |
kenya, | |
kiribati, | |
korea_the_democratic_peoples_republic_of, | |
korea_the_republic_of, | |
kuwait, | |
kyrgyzstan, | |
lao_peoples_democratic_republic_the, | |
latvia, | |
lebanon, | |
lesotho, | |
liberia, | |
libya, | |
liechtenstein, | |
lithuania, | |
luxembourg, | |
macao, | |
north_macedonia, | |
madagascar, | |
malawi, | |
malaysia, | |
maldives, | |
mali, | |
malta, | |
marshall_islands_the, | |
martinique, | |
mauritania, | |
mauritius, | |
mayotte, | |
mexico, | |
micronesia_federated_states_of, | |
moldova_the_republic_of, | |
monaco, | |
mongolia, | |
montenegro, | |
montserrat, | |
morocco, | |
mozambique, | |
myanmar, | |
namibia, | |
nauru, | |
nepal, | |
netherlands_the, | |
new_caledonia, | |
new_zealand, | |
nicaragua, | |
niger_the, | |
nigeria, | |
niue, | |
norfolk_island, | |
northern_mariana_islands_the, | |
norway, | |
oman, | |
pakistan, | |
palau, | |
palestine_state_of, | |
panama, | |
papua_new_guinea, | |
paraguay, | |
peru, | |
philippines_the, | |
pitcairn, | |
poland, | |
portugal, | |
puerto_rico, | |
qatar, | |
reunion, | |
romania, | |
russian_federation_the, | |
rwanda, | |
saint_barthelemy, | |
saint_helena_ascension_island_and_tristan_da_cunha, | |
saint_kitts_and_nevis, | |
saint_lucia, | |
saint_martin_french_part, | |
saint_pierre_and_miquelon, | |
saint_vincent_and_the_grenadines, | |
samoa, | |
san_marino, | |
sao_tome_and_principe, | |
saudi_arabia, | |
senegal, | |
serbia, | |
seychelles, | |
sierra_leone, | |
singapore, | |
sint_maarten_dutch_part, | |
slovakia, | |
slovenia, | |
solomon_islands, | |
somalia, | |
south_africa, | |
south_georgia_and_the_south_sandwich_islands, | |
south_sudan, | |
spain, | |
sri_lanka, | |
sudan_the, | |
suriname, | |
svalbard_and_jan_mayen, | |
sweden, | |
switzerland, | |
syrian_arab_republic_the, | |
taiwan_province_of_china, | |
tajikistan, | |
tanzania_the_united_republic_of, | |
thailand, | |
timor_leste, | |
togo, | |
tokelau, | |
tonga, | |
trinidad_and_tobago, | |
tunisia, | |
turkiye, | |
turkmenistan, | |
turks_and_caicos_islands_the, | |
tuvalu, | |
uganda, | |
ukraine, | |
united_arab_emirates_the, | |
united_kingdom_of_great_britain_and_northern_ireland_the, | |
united_states_minor_outlying_islands_the, | |
united_states_of_america_the, | |
uruguay, | |
uzbekistan, | |
vanuatu, | |
venezuela_bolivarian_republic_of, | |
viet_nam, | |
virgin_islands_british, | |
virgin_islands_us, | |
wallis_and_futuna, | |
western_sahara, | |
yemen, | |
zambia, | |
zimbabwe, | |
/// Only intended for internal use for the alpha2 lookup | |
invalid, | |
/// Case-insensitive alpha2 code -> country lookup | |
pub fn fromAlpha2(alpha2_code: []const u8) error{InvalidAlpha2Code}!Country { | |
const lookup_id = try alpha2_to_lookup_id(alpha2_code); | |
const country = alpha2_lookup[lookup_id]; | |
if (country == .invalid) return error.InvalidAlpha2Code; | |
return country; | |
} | |
}; | |
test "alpha2 lookup" { | |
try std.testing.expectEqual(Country.united_states_of_america_the, try Country.fromAlpha2("us")); | |
try std.testing.expectEqual(Country.united_states_of_america_the, try Country.fromAlpha2("US")); | |
try std.testing.expectError(error.InvalidAlpha2Code, Country.fromAlpha2("usa")); | |
try std.testing.expectError(error.InvalidAlpha2Code, Country.fromAlpha2("u")); | |
try std.testing.expectError(error.InvalidAlpha2Code, Country.fromAlpha2("12")); | |
} | |
pub fn alpha2_to_lookup_id(alpha2_code: []const u8) error{InvalidAlpha2Code}!usize { | |
if (alpha2_code.len != 2) return error.InvalidAlpha2Code; | |
const a = try alpha2_digit(alpha2_code[0]); | |
const b = try alpha2_digit(alpha2_code[1]); | |
return @as(u16, a) * 26 + b; | |
} | |
fn alpha2_digit(c: u8) error{InvalidAlpha2Code}!u8 { | |
return switch (c) { | |
'a'...'z' => c - 'a', | |
'A'...'Z' => c - 'A', | |
else => return error.InvalidAlpha2Code, | |
}; | |
} | |
const alpha2_lookup = lookup: { | |
// The maximum possible 2-length alpha codes is small enough | |
// that we can just create an array element for each possibility, | |
// and set all the unspecified indexes to `.invalid` | |
const max_2_digit_alpha_codes = 26 * 26; | |
var lookup = [_]Country{.invalid} ** max_2_digit_alpha_codes; | |
for (alpha2_codes) |alpha_code_mapping| { | |
const alpha_code = alpha_code_mapping.@"0"; | |
const country = alpha_code_mapping.@"1"; | |
const lookup_id = alpha2_to_lookup_id(alpha_code) catch unreachable; | |
lookup[lookup_id] = country; | |
} | |
break :lookup lookup; | |
}; | |
const alpha2_codes = .{ | |
.{ "af", Country.afghanistan }, | |
.{ "ax", Country.aland_islands }, | |
.{ "al", Country.albania }, | |
.{ "dz", Country.algeria }, | |
.{ "as", Country.american_samoa }, | |
.{ "ad", Country.andorra }, | |
.{ "ao", Country.angola }, | |
.{ "ai", Country.anguilla }, | |
.{ "aq", Country.antarctica }, | |
.{ "ag", Country.antigua_and_barbuda }, | |
.{ "ar", Country.argentina }, | |
.{ "am", Country.armenia }, | |
.{ "aw", Country.aruba }, | |
.{ "au", Country.australia }, | |
.{ "at", Country.austria }, | |
.{ "az", Country.azerbaijan }, | |
.{ "bs", Country.bahamas_the }, | |
.{ "bh", Country.bahrain }, | |
.{ "bd", Country.bangladesh }, | |
.{ "bb", Country.barbados }, | |
.{ "by", Country.belarus }, | |
.{ "be", Country.belgium }, | |
.{ "bz", Country.belize }, | |
.{ "bj", Country.benin }, | |
.{ "bm", Country.bermuda }, | |
.{ "bt", Country.bhutan }, | |
.{ "bo", Country.bolivia_plurinational_state_of }, | |
.{ "bq", Country.bonaire_sint_eustatius_and_saba }, | |
.{ "ba", Country.bosnia_and_herzegovina }, | |
.{ "bw", Country.botswana }, | |
.{ "bv", Country.bouvet_island }, | |
.{ "br", Country.brazil }, | |
.{ "io", Country.british_indian_ocean_territory_the }, | |
.{ "bn", Country.brunei_darussalam }, | |
.{ "bg", Country.bulgaria }, | |
.{ "bf", Country.burkina_faso }, | |
.{ "bi", Country.burundi }, | |
.{ "cv", Country.cabo_verde }, | |
.{ "kh", Country.cambodia }, | |
.{ "cm", Country.cameroon }, | |
.{ "ca", Country.canada }, | |
.{ "ky", Country.cayman_islands_the }, | |
.{ "cf", Country.central_african_republic_the }, | |
.{ "td", Country.chad }, | |
.{ "cl", Country.chile }, | |
.{ "cn", Country.china }, | |
.{ "cx", Country.christmas_island }, | |
.{ "cc", Country.cocos_keeling_islands_the }, | |
.{ "co", Country.colombia }, | |
.{ "km", Country.comoros_the }, | |
.{ "cd", Country.congo_the_democratic_republic_of_the }, | |
.{ "cg", Country.congo_the }, | |
.{ "ck", Country.cook_islands_the }, | |
.{ "cr", Country.costa_rica }, | |
.{ "ci", Country.cote_divoire }, | |
.{ "hr", Country.croatia }, | |
.{ "cu", Country.cuba }, | |
.{ "cw", Country.curacao }, | |
.{ "cy", Country.cyprus }, | |
.{ "cz", Country.czechia }, | |
.{ "dk", Country.denmark }, | |
.{ "dj", Country.djibouti }, | |
.{ "dm", Country.dominica }, | |
.{ "do", Country.dominican_republic_the }, | |
.{ "ec", Country.ecuador }, | |
.{ "eg", Country.egypt }, | |
.{ "sv", Country.el_salvador }, | |
.{ "gq", Country.equatorial_guinea }, | |
.{ "er", Country.eritrea }, | |
.{ "ee", Country.estonia }, | |
.{ "sz", Country.eswatini }, | |
.{ "et", Country.ethiopia }, | |
.{ "fk", Country.falkland_islands_the_malvinas }, | |
.{ "fo", Country.faroe_islands_the }, | |
.{ "fj", Country.fiji }, | |
.{ "fi", Country.finland }, | |
.{ "fr", Country.france }, | |
.{ "gf", Country.french_guiana }, | |
.{ "pf", Country.french_polynesia }, | |
.{ "tf", Country.french_southern_territories_the }, | |
.{ "ga", Country.gabon }, | |
.{ "gm", Country.gambia_the }, | |
.{ "ge", Country.georgia }, | |
.{ "de", Country.germany }, | |
.{ "gh", Country.ghana }, | |
.{ "gi", Country.gibraltar }, | |
.{ "gr", Country.greece }, | |
.{ "gl", Country.greenland }, | |
.{ "gd", Country.grenada }, | |
.{ "gp", Country.guadeloupe }, | |
.{ "gu", Country.guam }, | |
.{ "gt", Country.guatemala }, | |
.{ "gg", Country.guernsey }, | |
.{ "gn", Country.guinea }, | |
.{ "gw", Country.guinea_bissau }, | |
.{ "gy", Country.guyana }, | |
.{ "ht", Country.haiti }, | |
.{ "hm", Country.heard_island_and_mcdonald_islands }, | |
.{ "va", Country.holy_see_the }, | |
.{ "hn", Country.honduras }, | |
.{ "hk", Country.hong_kong }, | |
.{ "hu", Country.hungary }, | |
.{ "is", Country.iceland }, | |
.{ "in", Country.india }, | |
.{ "id", Country.indonesia }, | |
.{ "ir", Country.iran_islamic_republic_of }, | |
.{ "iq", Country.iraq }, | |
.{ "ie", Country.ireland }, | |
.{ "im", Country.isle_of_man }, | |
.{ "il", Country.israel }, | |
.{ "it", Country.italy }, | |
.{ "jm", Country.jamaica }, | |
.{ "jp", Country.japan }, | |
.{ "je", Country.jersey }, | |
.{ "jo", Country.jordan }, | |
.{ "kz", Country.kazakhstan }, | |
.{ "ke", Country.kenya }, | |
.{ "ki", Country.kiribati }, | |
.{ "kp", Country.korea_the_democratic_peoples_republic_of }, | |
.{ "kr", Country.korea_the_republic_of }, | |
.{ "kw", Country.kuwait }, | |
.{ "kg", Country.kyrgyzstan }, | |
.{ "la", Country.lao_peoples_democratic_republic_the }, | |
.{ "lv", Country.latvia }, | |
.{ "lb", Country.lebanon }, | |
.{ "ls", Country.lesotho }, | |
.{ "lr", Country.liberia }, | |
.{ "ly", Country.libya }, | |
.{ "li", Country.liechtenstein }, | |
.{ "lt", Country.lithuania }, | |
.{ "lu", Country.luxembourg }, | |
.{ "mo", Country.macao }, | |
.{ "mk", Country.north_macedonia }, | |
.{ "mg", Country.madagascar }, | |
.{ "mw", Country.malawi }, | |
.{ "my", Country.malaysia }, | |
.{ "mv", Country.maldives }, | |
.{ "ml", Country.mali }, | |
.{ "mt", Country.malta }, | |
.{ "mh", Country.marshall_islands_the }, | |
.{ "mq", Country.martinique }, | |
.{ "mr", Country.mauritania }, | |
.{ "mu", Country.mauritius }, | |
.{ "yt", Country.mayotte }, | |
.{ "mx", Country.mexico }, | |
.{ "fm", Country.micronesia_federated_states_of }, | |
.{ "md", Country.moldova_the_republic_of }, | |
.{ "mc", Country.monaco }, | |
.{ "mn", Country.mongolia }, | |
.{ "me", Country.montenegro }, | |
.{ "ms", Country.montserrat }, | |
.{ "ma", Country.morocco }, | |
.{ "mz", Country.mozambique }, | |
.{ "mm", Country.myanmar }, | |
.{ "na", Country.namibia }, | |
.{ "nr", Country.nauru }, | |
.{ "np", Country.nepal }, | |
.{ "nl", Country.netherlands_the }, | |
.{ "nc", Country.new_caledonia }, | |
.{ "nz", Country.new_zealand }, | |
.{ "ni", Country.nicaragua }, | |
.{ "ne", Country.niger_the }, | |
.{ "ng", Country.nigeria }, | |
.{ "nu", Country.niue }, | |
.{ "nf", Country.norfolk_island }, | |
.{ "mp", Country.northern_mariana_islands_the }, | |
.{ "no", Country.norway }, | |
.{ "om", Country.oman }, | |
.{ "pk", Country.pakistan }, | |
.{ "pw", Country.palau }, | |
.{ "ps", Country.palestine_state_of }, | |
.{ "pa", Country.panama }, | |
.{ "pg", Country.papua_new_guinea }, | |
.{ "py", Country.paraguay }, | |
.{ "pe", Country.peru }, | |
.{ "ph", Country.philippines_the }, | |
.{ "pn", Country.pitcairn }, | |
.{ "pl", Country.poland }, | |
.{ "pt", Country.portugal }, | |
.{ "pr", Country.puerto_rico }, | |
.{ "qa", Country.qatar }, | |
.{ "re", Country.reunion }, | |
.{ "ro", Country.romania }, | |
.{ "ru", Country.russian_federation_the }, | |
.{ "rw", Country.rwanda }, | |
.{ "bl", Country.saint_barthelemy }, | |
.{ "sh", Country.saint_helena_ascension_island_and_tristan_da_cunha }, | |
.{ "kn", Country.saint_kitts_and_nevis }, | |
.{ "lc", Country.saint_lucia }, | |
.{ "mf", Country.saint_martin_french_part }, | |
.{ "pm", Country.saint_pierre_and_miquelon }, | |
.{ "vc", Country.saint_vincent_and_the_grenadines }, | |
.{ "ws", Country.samoa }, | |
.{ "sm", Country.san_marino }, | |
.{ "st", Country.sao_tome_and_principe }, | |
.{ "sa", Country.saudi_arabia }, | |
.{ "sn", Country.senegal }, | |
.{ "rs", Country.serbia }, | |
.{ "sc", Country.seychelles }, | |
.{ "sl", Country.sierra_leone }, | |
.{ "sg", Country.singapore }, | |
.{ "sx", Country.sint_maarten_dutch_part }, | |
.{ "sk", Country.slovakia }, | |
.{ "si", Country.slovenia }, | |
.{ "sb", Country.solomon_islands }, | |
.{ "so", Country.somalia }, | |
.{ "za", Country.south_africa }, | |
.{ "gs", Country.south_georgia_and_the_south_sandwich_islands }, | |
.{ "ss", Country.south_sudan }, | |
.{ "es", Country.spain }, | |
.{ "lk", Country.sri_lanka }, | |
.{ "sd", Country.sudan_the }, | |
.{ "sr", Country.suriname }, | |
.{ "sj", Country.svalbard_and_jan_mayen }, | |
.{ "se", Country.sweden }, | |
.{ "ch", Country.switzerland }, | |
.{ "sy", Country.syrian_arab_republic_the }, | |
.{ "tw", Country.taiwan_province_of_china }, | |
.{ "tj", Country.tajikistan }, | |
.{ "tz", Country.tanzania_the_united_republic_of }, | |
.{ "th", Country.thailand }, | |
.{ "tl", Country.timor_leste }, | |
.{ "tg", Country.togo }, | |
.{ "tk", Country.tokelau }, | |
.{ "to", Country.tonga }, | |
.{ "tt", Country.trinidad_and_tobago }, | |
.{ "tn", Country.tunisia }, | |
.{ "tr", Country.turkiye }, | |
.{ "tm", Country.turkmenistan }, | |
.{ "tc", Country.turks_and_caicos_islands_the }, | |
.{ "tv", Country.tuvalu }, | |
.{ "ug", Country.uganda }, | |
.{ "ua", Country.ukraine }, | |
.{ "ae", Country.united_arab_emirates_the }, | |
.{ "gb", Country.united_kingdom_of_great_britain_and_northern_ireland_the }, | |
.{ "um", Country.united_states_minor_outlying_islands_the }, | |
.{ "us", Country.united_states_of_america_the }, | |
.{ "uy", Country.uruguay }, | |
.{ "uz", Country.uzbekistan }, | |
.{ "vu", Country.vanuatu }, | |
.{ "ve", Country.venezuela_bolivarian_republic_of }, | |
.{ "vn", Country.viet_nam }, | |
.{ "vg", Country.virgin_islands_british }, | |
.{ "vi", Country.virgin_islands_us }, | |
.{ "wf", Country.wallis_and_futuna }, | |
.{ "eh", Country.western_sahara }, | |
.{ "ye", Country.yemen }, | |
.{ "zm", Country.zambia }, | |
.{ "zw", Country.zimbabwe }, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment