github.com/cs3org/reva/v2@v2.27.7/pkg/mentix/utils/countries/countries.go (about) 1 // Copyright 2018-2020 CERN 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package countries 20 21 import ( 22 "strconv" 23 "strings" 24 ) 25 26 const countriesData = ` 27 Afghanistan AF AFG 004 28 Albania AL ALB 008 29 Algeria DZ DZA 012 30 American Samoa AS ASM 016 31 Andorra AD AND 020 32 Angola AO AGO 024 33 Anguilla AI AIA 660 34 Antarctica AQ ATA 010 35 Antigua and Barbuda AG ATG 028 36 Argentina AR ARG 032 37 Armenia AM ARM 051 38 Aruba AW ABW 533 39 Australia AU AUS 036 40 Austria AT AUT 040 41 Azerbaijan AZ AZE 031 42 Bahamas (the) BS BHS 044 43 Bahrain BH BHR 048 44 Bangladesh BD BGD 050 45 Barbados BB BRB 052 46 Belarus BY BLR 112 47 Belgium BE BEL 056 48 Belize BZ BLZ 084 49 Benin BJ BEN 204 50 Bermuda BM BMU 060 51 Bhutan BT BTN 064 52 Bolivia (Plurinational State of) BO BOL 068 53 Bonaire, Sint Eustatius and Saba BQ BES 535 54 Bosnia and Herzegovina BA BIH 070 55 Botswana BW BWA 072 56 Bouvet Island BV BVT 074 57 Brazil BR BRA 076 58 British Indian Ocean Territory (the) IO IOT 086 59 Brunei Darussalam BN BRN 096 60 Bulgaria BG BGR 100 61 Burkina Faso BF BFA 854 62 Burundi BI BDI 108 63 Cabo Verde CV CPV 132 64 Cambodia KH KHM 116 65 Cameroon CM CMR 120 66 Canada CA CAN 124 67 Cayman Islands (the) KY CYM 136 68 Central African Republic (the) CF CAF 140 69 Chad TD TCD 148 70 Chile CL CHL 152 71 China CN CHN 156 72 Christmas Island CX CXR 162 73 Cocos (Keeling) Islands (the) CC CCK 166 74 Colombia CO COL 170 75 Comoros (the) KM COM 174 76 Congo (the Democratic Republic of the) CD COD 180 77 Congo (the) CG COG 178 78 Cook Islands (the) CK COK 184 79 Costa Rica CR CRI 188 80 Croatia HR HRV 191 81 Cuba CU CUB 192 82 Curaçao CW CUW 531 83 Cyprus CY CYP 196 84 Czechia CZ CZE 203 85 Côte d'Ivoire CI CIV 384 86 Denmark DK DNK 208 87 Djibouti DJ DJI 262 88 Dominica DM DMA 212 89 Dominican Republic (the) DO DOM 214 90 Ecuador EC ECU 218 91 Egypt EG EGY 818 92 El Salvador SV SLV 222 93 Equatorial Guinea GQ GNQ 226 94 Eritrea ER ERI 232 95 Estonia EE EST 233 96 Eswatini SZ SWZ 748 97 Ethiopia ET ETH 231 98 Falkland Islands (the) [Malvinas] FK FLK 238 99 Faroe Islands (the) FO FRO 234 100 Fiji FJ FJI 242 101 Finland FI FIN 246 102 France FR FRA 250 103 French Guiana GF GUF 254 104 French Polynesia PF PYF 258 105 French Southern Territories (the) TF ATF 260 106 Gabon GA GAB 266 107 Gambia (the) GM GMB 270 108 Georgia GE GEO 268 109 Germany DE DEU 276 110 Ghana GH GHA 288 111 Gibraltar GI GIB 292 112 Greece GR GRC 300 113 Greenland GL GRL 304 114 Grenada GD GRD 308 115 Guadeloupe GP GLP 312 116 Guam GU GUM 316 117 Guatemala GT GTM 320 118 Guernsey GG GGY 831 119 Guinea GN GIN 324 120 Guinea-Bissau GW GNB 624 121 Guyana GY GUY 328 122 Haiti HT HTI 332 123 Heard Island and McDonald Islands HM HMD 334 124 Holy See (the) VA VAT 336 125 Honduras HN HND 340 126 Hong Kong HK HKG 344 127 Hungary HU HUN 348 128 Iceland IS ISL 352 129 India IN IND 356 130 Indonesia ID IDN 360 131 Iran (Islamic Republic of) IR IRN 364 132 Iraq IQ IRQ 368 133 Ireland IE IRL 372 134 Isle of Man IM IMN 833 135 Israel IL ISR 376 136 Italy IT ITA 380 137 Jamaica JM JAM 388 138 Japan JP JPN 392 139 Jersey JE JEY 832 140 Jordan JO JOR 400 141 Kazakhstan KZ KAZ 398 142 Kenya KE KEN 404 143 Kiribati KI KIR 296 144 Korea (the Democratic People's Republic of) KP PRK 408 145 Korea (the Republic of) KR KOR 410 146 Kuwait KW KWT 414 147 Kyrgyzstan KG KGZ 417 148 Lao People's Democratic Republic (the) LA LAO 418 149 Latvia LV LVA 428 150 Lebanon LB LBN 422 151 Lesotho LS LSO 426 152 Liberia LR LBR 430 153 Libya LY LBY 434 154 Liechtenstein LI LIE 438 155 Lithuania LT LTU 440 156 Luxembourg LU LUX 442 157 Macao MO MAC 446 158 Madagascar MG MDG 450 159 Malawi MW MWI 454 160 Malaysia MY MYS 458 161 Maldives MV MDV 462 162 Mali ML MLI 466 163 Malta MT MLT 470 164 Marshall Islands (the) MH MHL 584 165 Martinique MQ MTQ 474 166 Mauritania MR MRT 478 167 Mauritius MU MUS 480 168 Mayotte YT MYT 175 169 Mexico MX MEX 484 170 Micronesia (Federated States of) FM FSM 583 171 Moldova (the Republic of) MD MDA 498 172 Monaco MC MCO 492 173 Mongolia MN MNG 496 174 Montenegro ME MNE 499 175 Montserrat MS MSR 500 176 Morocco MA MAR 504 177 Mozambique MZ MOZ 508 178 Myanmar MM MMR 104 179 Namibia NA NAM 516 180 Nauru NR NRU 520 181 Nepal NP NPL 524 182 Netherlands (the) NL NLD 528 183 New Caledonia NC NCL 540 184 New Zealand NZ NZL 554 185 Nicaragua NI NIC 558 186 Niger (the) NE NER 562 187 Nigeria NG NGA 566 188 Niue NU NIU 570 189 Norfolk Island NF NFK 574 190 Northern Mariana Islands (the) MP MNP 580 191 Norway NO NOR 578 192 Oman OM OMN 512 193 Pakistan PK PAK 586 194 Palau PW PLW 585 195 Palestine, State of PS PSE 275 196 Panama PA PAN 591 197 Papua New Guinea PG PNG 598 198 Paraguay PY PRY 600 199 Peru PE PER 604 200 Philippines (the) PH PHL 608 201 Pitcairn PN PCN 612 202 Poland PL POL 616 203 Portugal PT PRT 620 204 Puerto Rico PR PRI 630 205 Qatar QA QAT 634 206 Republic of North Macedonia MK MKD 807 207 Romania RO ROU 642 208 Russian Federation (the) RU RUS 643 209 Rwanda RW RWA 646 210 Réunion RE REU 638 211 Saint Barthélemy BL BLM 652 212 Saint Helena, Ascension and Tristan da Cunha SH SHN 654 213 Saint Kitts and Nevis KN KNA 659 214 Saint Lucia LC LCA 662 215 Saint Martin (French part) MF MAF 663 216 Saint Pierre and Miquelon PM SPM 666 217 Saint Vincent and the Grenadines VC VCT 670 218 Samoa WS WSM 882 219 San Marino SM SMR 674 220 Sao Tome and Principe ST STP 678 221 Saudi Arabia SA SAU 682 222 Senegal SN SEN 686 223 Serbia RS SRB 688 224 Seychelles SC SYC 690 225 Sierra Leone SL SLE 694 226 Singapore SG SGP 702 227 Sint Maarten (Dutch part) SX SXM 534 228 Slovakia SK SVK 703 229 Slovenia SI SVN 705 230 Solomon Islands SB SLB 090 231 Somalia SO SOM 706 232 South Africa ZA ZAF 710 233 South Georgia and the South Sandwich Islands GS SGS 239 234 South Sudan SS SSD 728 235 Spain ES ESP 724 236 Sri Lanka LK LKA 144 237 Sudan (the) SD SDN 729 238 Suriname SR SUR 740 239 Svalbard and Jan Mayen SJ SJM 744 240 Sweden SE SWE 752 241 Switzerland CH CHE 756 242 Syrian Arab Republic SY SYR 760 243 Taiwan (Province of China) TW TWN 158 244 Tajikistan TJ TJK 762 245 Tanzania, United Republic of TZ TZA 834 246 Thailand TH THA 764 247 Timor-Leste TL TLS 626 248 Togo TG TGO 768 249 Tokelau TK TKL 772 250 Tonga TO TON 776 251 Trinidad and Tobago TT TTO 780 252 Tunisia TN TUN 788 253 Turkey TR TUR 792 254 Turkmenistan TM TKM 795 255 Turks and Caicos Islands (the) TC TCA 796 256 Tuvalu TV TUV 798 257 Uganda UG UGA 800 258 Ukraine UA UKR 804 259 United Arab Emirates (the) AE ARE 784 260 United Kingdom of Great Britain and Northern Ireland (the) GB GBR 826 261 United States Minor Outlying Islands (the) UM UMI 581 262 United States of America (the) US USA 840 263 Uruguay UY URY 858 264 Uzbekistan UZ UZB 860 265 Vanuatu VU VUT 548 266 Venezuela (Bolivarian Republic of) VE VEN 862 267 Viet Nam VN VNM 704 268 Virgin Islands (British) VG VGB 092 269 Virgin Islands (U.S.) VI VIR 850 270 Wallis and Futuna WF WLF 876 271 Western Sahara EH ESH 732 272 Yemen YE YEM 887 273 Zambia ZM ZMB 894 274 Zimbabwe ZW ZWE 716 275 Åland Islands AX ALA 248 276 ` 277 278 type countryCode struct { 279 Alpha2 string 280 Alpha3 string 281 Numerical int 282 } 283 284 var countryCodeTable map[string]countryCode 285 286 // LookupCountry searches for the country specified by the given code (Alpha2/3 or numerical). 287 // If no country with the code exists, an empty string is returned. 288 func LookupCountry(code string) string { 289 numerical, err := strconv.Atoi(code) 290 if err != nil { 291 numerical = -1 292 } 293 294 for name, cc := range countryCodeTable { 295 if strings.EqualFold(code, cc.Alpha2) || strings.EqualFold(code, cc.Alpha3) || cc.Numerical == numerical { 296 return name 297 } 298 } 299 return "" 300 } 301 302 func init() { 303 countryCodeTable = make(map[string]countryCode, 250) 304 for _, countryData := range strings.Split(countriesData, "\n") { 305 tokens := strings.Split(countryData, "\t") 306 if len(tokens) == 4 { 307 numerical, _ := strconv.Atoi(tokens[3]) 308 309 countryCodeTable[tokens[0]] = countryCode{ 310 Alpha2: tokens[1], 311 Alpha3: tokens[2], 312 Numerical: numerical, 313 } 314 } 315 } 316 }