Coder Social home page Coder Social logo

Comments (9)

patzearfoss avatar patzearfoss commented on July 24, 2024

After doing some more exploration, theres some connection to the number of digits and parsing of the extension value:

(lldb) po PhoneNumber(rawNumber: "7035205555 x555")
▿ PhoneNumber
  - countryCode : 1
  - leadingZero : false
  - nationalNumber : 7035205555
  ▿ numberExtension : Optional("555")
    - Some : "555"
  - rawNumber : "7035205555 x555"

(lldb) po PhoneNumber(rawNumber: "7035205555 x5555")
▿ PhoneNumber
  - countryCode : 1
  - leadingZero : false
  - nationalNumber : 7035205555
  ▿ numberExtension : Optional("5555")
    - Some : "5555"
  - rawNumber : "7035205555 x5555"

(lldb) po PhoneNumber(rawNumber: "7035205555 x55")
▿ PhoneNumber
  - countryCode : 1
  - leadingZero : false
  - nationalNumber : 7035205555
  - numberExtension : nil
  - rawNumber : "7035205555 x55"

(lldb) po PhoneNumber(rawNumber: "7035205555 x5")
▿ PhoneNumber
  - countryCode : 1
  - leadingZero : false
  - nationalNumber : 7035205555
  - numberExtension : nil
  - rawNumber : "7035205555 x5"

from phonenumberkit.

patzearfoss avatar patzearfoss commented on July 24, 2024

I've forked the library and dug in a bit, and this problem is actually caused by NSDataDetector in RegularExpressions.swift in the phoneDataDetectorMatches method.

This code snippet highlights the issue. Drop into a play ground to see it in action.

import Foundation

let dataDetector = try! NSDataDetector(types: NSTextCheckingType.PhoneNumber.rawValue)

var nsString = "7035551234 x42" as NSString
var stringRange = NSMakeRange(0, nsString.length)
var matches = dataDetector.matchesInString("7035551234 x42", options: [], range: stringRange)

matches.forEach { (result) -> () in
    print(result.phoneNumber)   // prints "Optional("7035551234")"
}

nsString = "7035551234 x422" as NSString
stringRange = NSMakeRange(0, nsString.length)
matches = dataDetector.matchesInString("7035551234 x422", options: [], range: stringRange)

matches.forEach { (result) -> () in
    print(result.phoneNumber)   // prints "Optional("7035551234;422")"
}

I guess the question is, long term, whether PhoneNumberKit should rely on the iOS data detectors.

from phonenumberkit.

marmelroy avatar marmelroy commented on July 24, 2024

Thanks a lot for looking into this @pzearfoss, very interesting bug...

The NSDataDetector was added for efficiency as part of the effort to boost parsing speeds. If it fails there is a fallback to libPhoneNumber's original Regular Expression. The problem here is that it doesn't technically fail but doesn't produce the expected result either.

I will have a solution for this soon...

from phonenumberkit.

patzearfoss avatar patzearfoss commented on July 24, 2024

Thanks @marmelroy, this is great news.

We can work around it in the meantime. Do you think this represents a bug with NSDataDetector? It seems like it to me, but it's so obvious that I can't believe it would be missed. I think I might file a radar for it.

from phonenumberkit.

mitchellporter avatar mitchellporter commented on July 24, 2024

Any updates on this?

from phonenumberkit.

marmelroy avatar marmelroy commented on July 24, 2024

Not yet. It definitely seems like a bug in NSDataDetector but I need to properly benchmark the benefit of using it vs the custom regular expression before deciding on the next move.

from phonenumberkit.

mitchellporter avatar mitchellporter commented on July 24, 2024

@marmelroy Thanks for the update!

from phonenumberkit.

marmelroy avatar marmelroy commented on July 24, 2024

Quick update to say that this is still an issue...

from phonenumberkit.

marmelroy avatar marmelroy commented on July 24, 2024

Fixed (finally) by b71cbad

from phonenumberkit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.