mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 14:19:53 +01:00
Accept phone numbers starting with +.
This commit is contained in:
@@ -19,7 +19,7 @@ class CountryCodes {
|
|||||||
new Country("AR", "Argentina", "54", "00", "0"),
|
new Country("AR", "Argentina", "54", "00", "0"),
|
||||||
new Country("AS", "American Samoa", "1", "011", "1"),
|
new Country("AS", "American Samoa", "1", "011", "1"),
|
||||||
new Country("AT", "Austria", "43", "00", "0"),
|
new Country("AT", "Austria", "43", "00", "0"),
|
||||||
new Country("AU", "Australia", "61", "00", ""),
|
new Country("AU", "Australia", "61", "0011", "0"),
|
||||||
new Country("AW", "Aruba", "297", "00", ""),
|
new Country("AW", "Aruba", "297", "00", ""),
|
||||||
new Country("AZ", "Azerbaijan", "994", "00", "8"),
|
new Country("AZ", "Azerbaijan", "994", "00", "8"),
|
||||||
new Country("BA", "Bosnia and Herzegovina", "387", "00", "0"),
|
new Country("BA", "Bosnia and Herzegovina", "387", "00", "0"),
|
||||||
@@ -43,9 +43,9 @@ class CountryCodes {
|
|||||||
new Country("BZ", "Belize", "501", "00", "0"),
|
new Country("BZ", "Belize", "501", "00", "0"),
|
||||||
new Country("CA", "Canada", "1", "011", "1"),
|
new Country("CA", "Canada", "1", "011", "1"),
|
||||||
new Country("CC", "Cocos (Keeling) Islands", "61", "0011", "0"),
|
new Country("CC", "Cocos (Keeling) Islands", "61", "0011", "0"),
|
||||||
new Country("CD", "Congo", "243", "00", ""),
|
new Country("CD", "Congo (Republic)", "243", "00", ""),
|
||||||
new Country("CF", "Central African Republic", "236", "00", ""),
|
new Country("CF", "Central African Republic", "236", "00", ""),
|
||||||
new Country("CG", "Congo", "242", "00", ""),
|
new Country("CG", "Congo (Democratic Republic)", "242", "00", "0"),
|
||||||
new Country("CH", "Switzerland", "41", "00", "0"),
|
new Country("CH", "Switzerland", "41", "00", "0"),
|
||||||
new Country("CI", "Cote D'Ivoire", "225", "00", "0"),
|
new Country("CI", "Cote D'Ivoire", "225", "00", "0"),
|
||||||
new Country("CK", "Cook Islands", "682", "00", "00"),
|
new Country("CK", "Cook Islands", "682", "00", "00"),
|
||||||
@@ -257,12 +257,14 @@ class CountryCodes {
|
|||||||
static String translate(String number, String fromIso, String toIso) {
|
static String translate(String number, String fromIso, String toIso) {
|
||||||
Country from = COUNTRY_MAP.get(fromIso), to = COUNTRY_MAP.get(toIso);
|
Country from = COUNTRY_MAP.get(fromIso), to = COUNTRY_MAP.get(toIso);
|
||||||
if(from == null || to == null) return null;
|
if(from == null || to == null) return null;
|
||||||
// Strip the IDD prefix and country code from the number if present
|
// Strip any prefixes and country codes from the number
|
||||||
|
String plusCountryCode = "+" + to.countryCode;
|
||||||
String iddCountryCode = to.idd + to.countryCode;
|
String iddCountryCode = to.idd + to.countryCode;
|
||||||
if(number.startsWith(iddCountryCode))
|
if(number.startsWith(plusCountryCode))
|
||||||
|
number = number.substring(plusCountryCode.length());
|
||||||
|
else if(number.startsWith(iddCountryCode))
|
||||||
number = number.substring(iddCountryCode.length());
|
number = number.substring(iddCountryCode.length());
|
||||||
// Strip the NDD prefix from the number if present
|
else if(number.startsWith(to.ndd))
|
||||||
if(number.startsWith(to.ndd))
|
|
||||||
number = number.substring(to.ndd.length());
|
number = number.substring(to.ndd.length());
|
||||||
if(from == to) return from.ndd + number; // National
|
if(from == to) return from.ndd + number; // National
|
||||||
return from.idd + to.countryCode + number; // International
|
return from.idd + to.countryCode + number; // International
|
||||||
|
|||||||
@@ -8,27 +8,54 @@ public class CountryCodesTest extends BriarTestCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTranslation() {
|
public void testTranslation() {
|
||||||
// Unknown country for caller
|
// Unrecognised country for caller
|
||||||
assertNull(CountryCodes.translate("02012345678", "ZZ", "GB"));
|
assertNull(CountryCodes.translate("02012345678", "ZZ", "GB"));
|
||||||
// Unknown country for callee
|
// Unrecognised country for callee
|
||||||
assertNull(CountryCodes.translate("02012345678", "GB", "ZZ"));
|
assertNull(CountryCodes.translate("02012345678", "GB", "ZZ"));
|
||||||
// GB to GB, callee has included NDD prefix
|
|
||||||
assertEquals("02012345678",
|
|
||||||
CountryCodes.translate("02012345678", "GB", "GB"));
|
|
||||||
// GB to GB, callee has included IDD prefix and country code
|
|
||||||
assertEquals("02012345678",
|
|
||||||
CountryCodes.translate("00442012345678", "GB", "GB"));
|
|
||||||
// GB to GB, callee has not included a prefix
|
// GB to GB, callee has not included a prefix
|
||||||
assertEquals("02012345678",
|
assertEquals("02012345678",
|
||||||
CountryCodes.translate("2012345678", "GB", "GB"));
|
CountryCodes.translate("2012345678", "GB", "GB"));
|
||||||
// Russia to GB, callee has included NDD prefix
|
// GB to GB, callee has included NDD prefix
|
||||||
assertEquals("8**10442012345678",
|
assertEquals("02012345678",
|
||||||
CountryCodes.translate("02012345678", "RU", "GB"));
|
CountryCodes.translate("02012345678", "GB", "GB"));
|
||||||
// Russia to GB, callee has included IDD prefix and country code
|
// GB to GB, callee has included plus sign and country code
|
||||||
assertEquals("8**10442012345678",
|
assertEquals("02012345678",
|
||||||
CountryCodes.translate("00442012345678", "RU", "GB"));
|
CountryCodes.translate("+442012345678", "GB", "GB"));
|
||||||
|
// GB to GB, callee has included IDD prefix and country code
|
||||||
|
assertEquals("02012345678",
|
||||||
|
CountryCodes.translate("00442012345678", "GB", "GB"));
|
||||||
|
|
||||||
// Russia to GB, callee has not included a prefix
|
// Russia to GB, callee has not included a prefix
|
||||||
assertEquals("8**10442012345678",
|
assertEquals("8**10442012345678",
|
||||||
CountryCodes.translate("2012345678", "RU", "GB"));
|
CountryCodes.translate("2012345678", "RU", "GB"));
|
||||||
|
// Russia to GB, callee has included NDD prefix
|
||||||
|
assertEquals("8**10442012345678",
|
||||||
|
CountryCodes.translate("02012345678", "RU", "GB"));
|
||||||
|
// Russia to GB, callee has included plus sign and country code
|
||||||
|
assertEquals("8**10442012345678",
|
||||||
|
CountryCodes.translate("+442012345678", "RU", "GB"));
|
||||||
|
// Russia to GB, callee has included IDD prefix and country code
|
||||||
|
assertEquals("8**10442012345678",
|
||||||
|
CountryCodes.translate("00442012345678", "RU", "GB"));
|
||||||
|
|
||||||
|
// Andorra to Andorra (no NDD), callee has not included a prefix
|
||||||
|
assertEquals("765432", CountryCodes.translate("765432", "AD", "AD"));
|
||||||
|
// Andorra to Andorra, callee has included plus sign and country code
|
||||||
|
assertEquals("765432",
|
||||||
|
CountryCodes.translate("+376765432", "AD", "AD"));
|
||||||
|
// Andorra to Andorra, callee has included IDD and country code
|
||||||
|
assertEquals("765432",
|
||||||
|
CountryCodes.translate("00376765432", "AD", "AD"));
|
||||||
|
|
||||||
|
// GB to Andorra (no NDD), callee has not included a prefix
|
||||||
|
assertEquals("00376765432",
|
||||||
|
CountryCodes.translate("765432", "GB", "AD"));
|
||||||
|
// GB to Andorra, callee has included plus sign and country code
|
||||||
|
assertEquals("00376765432",
|
||||||
|
CountryCodes.translate("+376765432", "GB", "AD"));
|
||||||
|
// GB to Andorra, callee has included IDD and country code
|
||||||
|
assertEquals("00376765432",
|
||||||
|
CountryCodes.translate("00376765432", "GB", "AD"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user