Bug fixes and upgrades
This commit is contained in:
@@ -15,7 +15,7 @@ pub const PLUS_CHARS: &'static str = "+\u{FF0B}";
|
|||||||
pub const VALID_PUNCTUATION: &'static str = "-x\
|
pub const VALID_PUNCTUATION: &'static str = "-x\
|
||||||
\u{2010}-\u{2015}\u{2212}\u{30FC}\u{FF0D}-\u{FF0F} \u{00A0}\
|
\u{2010}-\u{2015}\u{2212}\u{30FC}\u{FF0D}-\u{FF0F} \u{00A0}\
|
||||||
\u{00AD}\u{200B}\u{2060}\u{3000}()\u{FF08}\u{FF09}\u{FF3B}\
|
\u{00AD}\u{200B}\u{2060}\u{3000}()\u{FF08}\u{FF09}\u{FF3B}\
|
||||||
\u{FF3D}.[]/~\u{2053}\u{223C}";
|
\u{FF3D}.\\[\\]/~\u{2053}\u{223C}";
|
||||||
|
|
||||||
// Regular expression of characters typically used to start a second phone
|
// Regular expression of characters typically used to start a second phone
|
||||||
// number for the purposes of parsing. This allows us to strip off parts of
|
// number for the purposes of parsing. This allows us to strip off parts of
|
||||||
|
|||||||
@@ -20,14 +20,14 @@ use super::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
/// Loads metadata from helper constants METADATA array
|
/// Loads metadata from helper constants METADATA array
|
||||||
pub(super) fn load_compiled_metadata() -> Result<PhoneMetadataCollection, protobuf::Error> {
|
pub fn load_compiled_metadata() -> Result<PhoneMetadataCollection, protobuf::Error> {
|
||||||
let result = PhoneMetadataCollection::parse_from_bytes(&METADATA)?;
|
let result = PhoneMetadataCollection::parse_from_bytes(&METADATA)?;
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a pointer to the description inside the metadata of the appropriate
|
/// Returns a pointer to the description inside the metadata of the appropriate
|
||||||
/// type.
|
/// type.
|
||||||
pub(super) fn get_number_desc_by_type(
|
pub fn get_number_desc_by_type(
|
||||||
metadata: &PhoneMetadata,
|
metadata: &PhoneMetadata,
|
||||||
phone_number_type: PhoneNumberType,
|
phone_number_type: PhoneNumberType,
|
||||||
) -> &PhoneNumberDesc {
|
) -> &PhoneNumberDesc {
|
||||||
@@ -48,7 +48,7 @@ pub(super) fn get_number_desc_by_type(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A helper function that is used by Format and FormatByPattern.
|
/// A helper function that is used by Format and FormatByPattern.
|
||||||
pub(super) fn prefix_number_with_country_calling_code(
|
pub fn prefix_number_with_country_calling_code(
|
||||||
country_calling_code: i32,
|
country_calling_code: i32,
|
||||||
number_format: PhoneNumberFormat,
|
number_format: PhoneNumberFormat,
|
||||||
formatted_number: &mut String,
|
formatted_number: &mut String,
|
||||||
@@ -91,7 +91,7 @@ pub(super) fn prefix_number_with_country_calling_code(
|
|||||||
|
|
||||||
// Returns true when one national number is the suffix of the other or both are
|
// Returns true when one national number is the suffix of the other or both are
|
||||||
// the same.
|
// the same.
|
||||||
pub(super) fn is_national_number_suffix_of_the_other(
|
pub fn is_national_number_suffix_of_the_other(
|
||||||
first_number: &PhoneNumber,
|
first_number: &PhoneNumber,
|
||||||
second_number: &PhoneNumber,
|
second_number: &PhoneNumber,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
@@ -106,7 +106,7 @@ pub(super) fn is_national_number_suffix_of_the_other(
|
|||||||
|
|
||||||
/// Helper method for constructing regular expressions for parsing. Creates an
|
/// Helper method for constructing regular expressions for parsing. Creates an
|
||||||
/// expression that captures up to max_length digits.
|
/// expression that captures up to max_length digits.
|
||||||
pub(super) fn extn_digits(max_length: u32) -> String {
|
pub fn extn_digits(max_length: u32) -> String {
|
||||||
let mut buf = itoa::Buffer::new();
|
let mut buf = itoa::Buffer::new();
|
||||||
let max_length_str = buf.format(max_length);
|
let max_length_str = buf.format(max_length);
|
||||||
const HELPER_STR_LEN: usize = 2 + 4 + 2;
|
const HELPER_STR_LEN: usize = 2 + 4 + 2;
|
||||||
@@ -131,7 +131,7 @@ pub(super) fn extn_digits(max_length: u32) -> String {
|
|||||||
// number is changed, MaybeStripExtension needs to be updated.
|
// number is changed, MaybeStripExtension needs to be updated.
|
||||||
// - The only capturing groups should be around the digits that you want to
|
// - The only capturing groups should be around the digits that you want to
|
||||||
// capture as part of the extension, or else parsing will fail!
|
// capture as part of the extension, or else parsing will fail!
|
||||||
pub(super) fn create_extn_pattern(for_parsing: bool) -> String {
|
pub fn create_extn_pattern(for_parsing: bool) -> String {
|
||||||
// We cap the maximum length of an extension based on the ambiguity of the
|
// We cap the maximum length of an extension based on the ambiguity of the
|
||||||
// way the extension is prefixed. As per ITU, the officially allowed
|
// way the extension is prefixed. As per ITU, the officially allowed
|
||||||
// length for extensions is actually 40, but we don't support this since we
|
// length for extensions is actually 40, but we don't support this since we
|
||||||
@@ -255,7 +255,7 @@ pub(super) fn create_extn_pattern(for_parsing: bool) -> String {
|
|||||||
/// left unchanged in the number.
|
/// left unchanged in the number.
|
||||||
///
|
///
|
||||||
/// Returns: normalized_string
|
/// Returns: normalized_string
|
||||||
pub(super) fn normalize_helper(
|
pub fn normalize_helper(
|
||||||
normalization_replacements: &HashMap<char, char>,
|
normalization_replacements: &HashMap<char, char>,
|
||||||
remove_non_matches: bool,
|
remove_non_matches: bool,
|
||||||
phone_number: &str
|
phone_number: &str
|
||||||
@@ -276,7 +276,7 @@ pub(super) fn normalize_helper(
|
|||||||
|
|
||||||
/// Returns `true` if there is any possible number data set for a particular
|
/// Returns `true` if there is any possible number data set for a particular
|
||||||
/// PhoneNumberDesc.
|
/// PhoneNumberDesc.
|
||||||
pub(super) fn desc_has_possible_number_data(desc: &PhoneNumberDesc) -> bool {
|
pub fn desc_has_possible_number_data(desc: &PhoneNumberDesc) -> bool {
|
||||||
// If this is empty, it means numbers of this type inherit from the "general
|
// If this is empty, it means numbers of this type inherit from the "general
|
||||||
// desc" -> the value "-1" means that no numbers exist for this type.
|
// desc" -> the value "-1" means that no numbers exist for this type.
|
||||||
return desc.possible_length.len() != 1
|
return desc.possible_length.len() != 1
|
||||||
@@ -296,7 +296,7 @@ pub(super) fn desc_has_possible_number_data(desc: &PhoneNumberDesc) -> bool {
|
|||||||
/// mention why during a review without needing to change MetadataFilter.
|
/// mention why during a review without needing to change MetadataFilter.
|
||||||
///
|
///
|
||||||
/// Returns `true` if there is any data set for a particular PhoneNumberDesc.
|
/// Returns `true` if there is any data set for a particular PhoneNumberDesc.
|
||||||
pub(super) fn desc_has_data(desc: &PhoneNumberDesc) -> bool {
|
pub fn desc_has_data(desc: &PhoneNumberDesc) -> bool {
|
||||||
// Checking most properties since we don't know what's present, since a custom
|
// Checking most properties since we don't know what's present, since a custom
|
||||||
// build may have stripped just one of them (e.g. USE_METADATA_LITE strips
|
// build may have stripped just one of them (e.g. USE_METADATA_LITE strips
|
||||||
// exampleNumber). We don't bother checking the PossibleLengthsLocalOnly,
|
// exampleNumber). We don't bother checking the PossibleLengthsLocalOnly,
|
||||||
@@ -309,7 +309,7 @@ pub(super) fn desc_has_data(desc: &PhoneNumberDesc) -> bool {
|
|||||||
|
|
||||||
/// Returns the types we have metadata for based on the PhoneMetadata object
|
/// Returns the types we have metadata for based on the PhoneMetadata object
|
||||||
/// passed in.
|
/// passed in.
|
||||||
pub(super) fn populate_supported_types_for_metadata(
|
pub fn populate_supported_types_for_metadata(
|
||||||
metadata: &PhoneMetadata,
|
metadata: &PhoneMetadata,
|
||||||
types: &mut HashSet<PhoneNumberType>,
|
types: &mut HashSet<PhoneNumberType>,
|
||||||
) {
|
) {
|
||||||
@@ -329,7 +329,7 @@ pub(super) fn populate_supported_types_for_metadata(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn get_supported_types_for_metadata(metadata: &PhoneMetadata) -> HashSet<PhoneNumberType> {
|
pub fn get_supported_types_for_metadata(metadata: &PhoneMetadata) -> HashSet<PhoneNumberType> {
|
||||||
const EFFECTIVE_NUMBER_TYPES: usize = 11 /* count */ - 2 /* filter type or unknown */;
|
const EFFECTIVE_NUMBER_TYPES: usize = 11 /* count */ - 2 /* filter type or unknown */;
|
||||||
let mut types = HashSet::with_capacity(EFFECTIVE_NUMBER_TYPES);
|
let mut types = HashSet::with_capacity(EFFECTIVE_NUMBER_TYPES);
|
||||||
populate_supported_types_for_metadata(metadata, &mut types);
|
populate_supported_types_for_metadata(metadata, &mut types);
|
||||||
@@ -338,7 +338,7 @@ pub(super) fn get_supported_types_for_metadata(metadata: &PhoneMetadata) -> Hash
|
|||||||
|
|
||||||
/// Helper method to check a number against possible lengths for this number
|
/// Helper method to check a number against possible lengths for this number
|
||||||
/// type, and determine whether it matches, or is too short or too long.
|
/// type, and determine whether it matches, or is too short or too long.
|
||||||
pub(super) fn test_number_length(
|
pub fn test_number_length(
|
||||||
phone_number: &str,
|
phone_number: &str,
|
||||||
phone_metadata: &PhoneMetadata,
|
phone_metadata: &PhoneMetadata,
|
||||||
phone_number_type: PhoneNumberType,
|
phone_number_type: PhoneNumberType,
|
||||||
@@ -425,7 +425,7 @@ pub(super) fn test_number_length(
|
|||||||
/// Helper method to check a number against possible lengths for this region,
|
/// Helper method to check a number against possible lengths for this region,
|
||||||
/// based on the metadata being passed in, and determine whether it matches, or
|
/// based on the metadata being passed in, and determine whether it matches, or
|
||||||
/// is too short or too long.
|
/// is too short or too long.
|
||||||
pub(super) fn test_number_length_with_unknown_type(
|
pub fn test_number_length_with_unknown_type(
|
||||||
phone_number: &str,
|
phone_number: &str,
|
||||||
phone_metadata: &PhoneMetadata,
|
phone_metadata: &PhoneMetadata,
|
||||||
) -> Result<ValidNumberLenType, ValidationResultErr> {
|
) -> Result<ValidNumberLenType, ValidationResultErr> {
|
||||||
@@ -454,7 +454,7 @@ pub(crate) fn copy_core_fields_only(from_number: &PhoneNumber) -> PhoneNumber {
|
|||||||
|
|
||||||
/// Determines whether the given number is a national number match for the given
|
/// Determines whether the given number is a national number match for the given
|
||||||
/// PhoneNumberDesc. Does not check against possible lengths!
|
/// PhoneNumberDesc. Does not check against possible lengths!
|
||||||
pub(super) fn is_match(
|
pub fn is_match(
|
||||||
matcher_api: &Box<dyn MatcherApi>,
|
matcher_api: &Box<dyn MatcherApi>,
|
||||||
number: &str,
|
number: &str,
|
||||||
number_desc: &PhoneNumberDesc,
|
number_desc: &PhoneNumberDesc,
|
||||||
|
|||||||
@@ -164,7 +164,10 @@ pub(super) struct PhoneNumberRegExpsAndMappings {
|
|||||||
/// followed by a single digit, separated by valid phone number punctuation.
|
/// followed by a single digit, separated by valid phone number punctuation.
|
||||||
/// This prevents invalid punctuation (such as the star sign in Israeli star
|
/// This prevents invalid punctuation (such as the star sign in Israeli star
|
||||||
/// numbers) getting into the output of the AYTF.
|
/// numbers) getting into the output of the AYTF.
|
||||||
pub is_format_eligible_as_you_type_formatting_regex: Regex
|
pub is_format_eligible_as_you_type_formatting_regex: Regex,
|
||||||
|
|
||||||
|
/// Added for function `formatting_rule_has_first_group_only`
|
||||||
|
pub formatting_rule_has_first_group_only_regex: Regex
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PhoneNumberRegExpsAndMappings {
|
impl PhoneNumberRegExpsAndMappings {
|
||||||
@@ -323,8 +326,17 @@ impl PhoneNumberRegExpsAndMappings {
|
|||||||
is_format_eligible_as_you_type_formatting_regex: Regex::new(
|
is_format_eligible_as_you_type_formatting_regex: Regex::new(
|
||||||
&format!("[{}]*\\$1[{}]*(\\$\\d[{}]*)*",VALID_PUNCTUATION, VALID_PUNCTUATION, VALID_PUNCTUATION)
|
&format!("[{}]*\\$1[{}]*(\\$\\d[{}]*)*",VALID_PUNCTUATION, VALID_PUNCTUATION, VALID_PUNCTUATION)
|
||||||
).unwrap(),
|
).unwrap(),
|
||||||
|
formatting_rule_has_first_group_only_regex: Regex::new("\\(?\\$1\\)?").unwrap()
|
||||||
};
|
};
|
||||||
instance.initialize_regexp_mappings();
|
instance.initialize_regexp_mappings();
|
||||||
instance
|
instance
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
#[test]
|
||||||
|
fn check_regexps_are_compiling() {
|
||||||
|
super::PhoneNumberRegExpsAndMappings::new();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
use std::{
|
use std::{
|
||||||
borrow::Cow, cmp::max, collections::{HashMap, HashSet, VecDeque}, sync::Arc
|
borrow::Cow, cmp::max, collections::{hash_map, HashMap, HashSet, VecDeque}, sync::Arc
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::phone_number_regexps_and_mappings::PhoneNumberRegExpsAndMappings;
|
use super::phone_number_regexps_and_mappings::PhoneNumberRegExpsAndMappings;
|
||||||
@@ -126,29 +126,18 @@ impl PhoneNumberUtil {
|
|||||||
instance
|
instance
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_supported_regions(&self) -> Vec<&str> {
|
pub fn get_supported_regions(&self) -> impl Iterator<Item=&str> {
|
||||||
let mut regions = Vec::new();
|
self.region_to_metadata_map.keys().map(| k | k.as_str())
|
||||||
for (k, _) in self.region_to_metadata_map.iter() {
|
|
||||||
regions.push(k.as_str());
|
|
||||||
}
|
|
||||||
regions
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_supported_global_network_calling_codes(&self) -> HashSet<i32> {
|
pub fn get_supported_global_network_calling_codes(&self) -> impl Iterator<Item=i32> {
|
||||||
let mut codes = HashSet::new();
|
self.country_code_to_non_geographical_metadata_map.keys().map(| k | *k)
|
||||||
for (k, _) in self.country_code_to_non_geographical_metadata_map.iter() {
|
|
||||||
codes.insert(*k);
|
|
||||||
}
|
|
||||||
codes
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_supported_calling_codes(&self) -> HashSet<i32> {
|
pub fn get_supported_calling_codes(&self) -> impl Iterator<Item=i32> {
|
||||||
let mut codes = HashSet::new();
|
self.country_calling_code_to_region_code_map
|
||||||
|
.iter()
|
||||||
for (k, _) in self.country_calling_code_to_region_code_map.iter() {
|
.map(| (k, _) | *k)
|
||||||
codes.insert(*k);
|
|
||||||
}
|
|
||||||
codes
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_supported_types_for_region(
|
pub fn get_supported_types_for_region(
|
||||||
@@ -1991,7 +1980,7 @@ impl PhoneNumberUtil {
|
|||||||
&self,
|
&self,
|
||||||
phone_number_type: PhoneNumberType,
|
phone_number_type: PhoneNumberType,
|
||||||
) -> ExampleNumberResult {
|
) -> ExampleNumberResult {
|
||||||
if let Some(number) = self.get_supported_regions().iter()
|
if let Some(number) = self.get_supported_regions()
|
||||||
.find_map(| region_code |
|
.find_map(| region_code |
|
||||||
self.get_example_number_for_type_and_region_code(region_code, phone_number_type).ok()
|
self.get_example_number_for_type_and_region_code(region_code, phone_number_type).ok()
|
||||||
) {
|
) {
|
||||||
|
|||||||
Reference in New Issue
Block a user