-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Numbers starting with the same sequence as country prefix are parsed incorrectly #68
Comments
How are you calling both libraries? Because if you just |
In both cases I'm setting the country to IT. I'm using this as libphonenumber reference: http://libphonenumber.appspot.com/phonenumberparser?number=3912312312&country=IT |
Can reproduce with this test case: diff --git a/src/phone_number.rs b/src/phone_number.rs
index 560bbbd..53ed717 100644
--- a/src/phone_number.rs
+++ b/src/phone_number.rs
@@ -265,6 +265,12 @@ mod test {
.unwrap()
}
+ fn parsed_local(country: country::Id, number: &str) -> PhoneNumber {
+ parser::parse(Some(country), number)
+ .with_context(|| format!("parsing {number}"))
+ .unwrap()
+ }
+
#[template]
#[rstest]
#[case(parsed("+80012340000"), None, Type::TollFree)]
@@ -282,6 +288,7 @@ mod test {
// https://github.com/whisperfish/rust-phonenumber/issues/46 and
// https://github.com/whisperfish/rust-phonenumber/issues/47
// #[case(parsed("+1 520-878-2491"), US)]
+ #[case(parsed_local(IT, "3912312312"), Some(IT), Type::Mobile)]
fn phone_numbers(
#[case] number: PhoneNumber,
#[case] country: Option<country::Id>,
@@ -295,7 +302,7 @@ mod test {
#[case] country: Option<country::Id>,
#[case] _type: Type,
) -> anyhow::Result<()> {
- assert_eq!(country, number.country().id());
+ assert_eq!(country, number.country().id(), "parsed as {number:?}");
Ok(())
} It's funny indeed, if you change 39 to 38, it just works fine. |
Weird, can't repro with the Belgian number |
As far as I can tell, the decision is made to get into this branch: if number.national.starts_with(&code)
&& (!meta.descriptors().general().is_match(&number.national)
|| !validator::length(meta, &number, Type::Unknown).is_possible())
{
number.country = country::Source::Number;
number.national = trim(number.national, code.len());
} in We'd need to find the equivalent logic in the Google library to see what's happening exactly, and what should be happening instead. This also explains why my Belgian example doesn't work as expected. |
Still the same case with the database present in #67, so that does not resolve this. |
How does the original library handle that case? |
libphonenumber handles just the 39... number without country context as invalid: https://libphonenumber.appspot.com/phonenumberparser?number=3912312312
With the country given, or with the plus sign, it's parsed correctly: https://libphonenumber.appspot.com/phonenumberparser?number=3912312312&country=IT
Without country but just a plus added results in an valid-looking-but-still-invalid number: https://libphonenumber.appspot.com/phonenumberparser?number=%2B3912312312
So I believe this is the behavior we should replicate. |
Test case:
3912312312
is parsed with the lastestmain
as12312312
libphone parses this number correctly:
The text was updated successfully, but these errors were encountered: