private void doTestNumberMatchesForLeniency(List <NumberTest> TestCases,
                                                    PhoneNumberUtil.Leniency leniency)
        {
            int noMatchFoundCount    = 0;
            int wrongMatchFoundCount = 0;

            foreach (NumberTest Test in TestCases)
            {
                var iterator =
                    findNumbersForLeniency(Test.rawString, Test.region, leniency);
                PhoneNumberMatch match = iterator.FirstOrDefault();
                if (match == null)
                {
                    noMatchFoundCount++;
                    Console.WriteLine("No match found in " + Test.ToString() + " for leniency: " + leniency);
                }
                else
                {
                    if (!Test.rawString.Equals(match.RawString))
                    {
                        wrongMatchFoundCount++;
                        Console.WriteLine("Found wrong match in Test " + Test.ToString() +
                                          ". Found " + match.RawString);
                    }
                }
            }
            Assert.Equal(0, noMatchFoundCount);
            Assert.Equal(0, wrongMatchFoundCount);
        }
示例#2
0
        private void DoTestNumberMatchesForLeniency(List <NumberTest> testCases,
                                                    PhoneNumberUtil.Leniency leniency)
        {
            var noMatchFoundCount    = 0;
            var wrongMatchFoundCount = 0;

            foreach (var test in testCases)
            {
                var iterator =
                    FindNumbersForLeniency(test.RawString, test.Region, leniency);
                var match = iterator.FirstOrDefault();
                if (match == null)
                {
                    noMatchFoundCount++;
                    Console.WriteLine("No match found in " + test + " for leniency: " + leniency);
                }
                else if (!test.RawString.Equals(match.RawString))
                {
                    wrongMatchFoundCount++;
                    Console.WriteLine("Found wrong match in test " + test +
                                      ". Found " + match.RawString);
                }
            }
            Assert.Equal(0, noMatchFoundCount);
            Assert.Equal(0, wrongMatchFoundCount);
        }
示例#3
0
 /**
  * Exhaustively searches for phone numbers from each index within {@code text} to test that
  * finding matches always terminates.
  */
 private void EnsureTermination(string text, string defaultCountry, PhoneNumberUtil.Leniency leniency)
 {
     for (var index = 0; index <= text.Length; index++)
     {
         var sub     = text.Substring(index);
         var matches = new StringBuilder();
         // Iterates over all matches.
         foreach (var match in phoneUtil.FindNumbers(sub, defaultCountry, leniency, long.MaxValue))
         {
             matches.Append(", ").Append(match);
         }
     }
 }
示例#4
0
        /// <summary>
        /// Creates a new instance. See the factory methods in {@link PhoneNumberUtil} on how to obtain a
        /// new instance.
        /// </summary>
        ///
        /// <param name="util">      the phone number util to use</param>
        /// <param name="text">      the character sequence that we will search, null for no text</param>
        /// <param name="country">   the country to assume for phone numbers not written in international format
        ///                          (with a leading plus, or with the international dialing prefix of the
        ///                          specified region). May be null or "ZZ" if only numbers with a
        ///                          leading plus should be considered.</param>
        /// <param name="leniency">  the leniency to use when evaluating candidate phone numbers</param>
        /// <param name="maxTries">  the maximum number of invalid numbers to try before giving up on the text.
        ///                          This is to cover degenerate cases where the text has a lot of false positives
        ///                          in it. Must be <c> >= 0</c>.</param>
        public PhoneNumberMatcher(PhoneNumberUtil util, string text, string country, PhoneNumberUtil.Leniency leniency,
                                  long maxTries)
        {
            if (maxTries < 0)
            {
                throw new ArgumentOutOfRangeException();
            }

            phoneUtil       = util ?? throw new ArgumentNullException();
            this.text       = text ?? "";
            preferredRegion = country;
            this.leniency   = leniency;
            this.maxTries   = maxTries;
        }
示例#5
0
 /**
  * Creates a new instance. See the factory methods in {@link PhoneNumberUtil} on how to obtain a
  * new instance.
  *
  * @param util      the phone number util to use
  * @param text      the character sequence that we will search, null for no text
  * @param country   the country to assume for phone numbers not written in international format
  *                  (with a leading plus, or with the international dialing prefix of the
  *                  specified region). May be null or "ZZ" if only numbers with a
  *                  leading plus should be considered.
  * @param leniency  the leniency to use when evaluating candidate phone numbers
  * @param maxTries  the maximum number of invalid numbers to try before giving up on the text.
  *                  This is to cover degenerate cases where the text has a lot of false positives
  *                  in it. Must be {@code >= 0}.
  */
 public PhoneNumberMatcher(PhoneNumberUtil util, string text, String country, PhoneNumberUtil.Leniency?leniency,
                           long maxTries)
 {
     if ((util == null) || (leniency == null))
     {
         throw new NullReferenceException();
     }
     if (maxTries < 0)
     {
         throw new ArgumentException();
     }
     this.phoneUtil       = util;
     this.text            = (text != null) ? text : "";
     this.preferredRegion = country;
     this.leniency        = leniency.Value;
     this.maxTries        = maxTries;
 }
示例#6
0
        private void DoTestNumberNonMatchesForLeniency(List <NumberTest> testCases,
                                                       PhoneNumberUtil.Leniency leniency)
        {
            var matchFoundCount = 0;

            foreach (var test in testCases)
            {
                var iterator =
                    FindNumbersForLeniency(test.RawString, test.Region, leniency);
                var match = iterator.FirstOrDefault();
                if (match != null)
                {
                    matchFoundCount++;
                    Console.WriteLine("Match found in " + test + " for leniency: " + leniency);
                }
            }
            Assert.Equal(0, matchFoundCount);
        }
        private void doTestNumberNonMatchesForLeniency(List <NumberTest> TestCases,
                                                       PhoneNumberUtil.Leniency leniency)
        {
            int matchFoundCount = 0;

            foreach (NumberTest Test in TestCases)
            {
                var iterator =
                    findNumbersForLeniency(Test.rawString, Test.region, leniency);
                PhoneNumberMatch match = iterator.FirstOrDefault();
                if (match != null)
                {
                    matchFoundCount++;
                    Console.WriteLine("Match found in " + Test.ToString() + " for leniency: " + leniency);
                }
            }
            Assert.Equal(0, matchFoundCount);
        }
        /**
         * Creates a new instance. See the factory methods in {@link PhoneNumberUtil} on how to obtain a
         * new instance.
         *
         * @param util      the phone number util to use
         * @param text      the character sequence that we will search, null for no text
         * @param country   the country to assume for phone numbers not written in international format
         *                  (with a leading plus, or with the international dialing prefix of the
         *                  specified region). May be null or "ZZ" if only numbers with a
         *                  leading plus should be considered.
         * @param leniency  the leniency to use when evaluating candidate phone numbers
         * @param maxTries  the maximum number of invalid numbers to try before giving up on the text.
         *                  This is to cover degenerate cases where the text has a lot of false positives
         *                  in it. Must be {@code >= 0}.
         */
        public PhoneNumberMatcher(PhoneNumberUtil util, String text, String country, PhoneNumberUtil.Leniency leniency,
                                  long maxTries)
        {
            if (util == null)
            {
                throw new ArgumentNullException();
            }

            if (maxTries < 0)
            {
                throw new ArgumentOutOfRangeException();
            }

            this.phoneUtil       = util;
            this.text            = (text != null) ? text : "";
            this.preferredRegion = country;
            this.leniency        = leniency;
            this.maxTries        = maxTries;
        }
示例#9
0
        public static bool verify(this PhoneNumberUtil.Leniency leniency, PhoneNumber number, String candidate, PhoneNumberUtil util)
        {
            switch (leniency)
            {
            case PhoneNumberUtil.Leniency.POSSIBLE:
                return(util.isPossibleNumber(number));

            case PhoneNumberUtil.Leniency.VALID:
            {
                if (!util.isValidNumber(number) ||
                    !PhoneNumberMatcher.containsOnlyValidXChars(number, candidate, util))
                {
                    return(false);
                }
                return(PhoneNumberMatcher.isNationalPrefixPresentIfRequired(number, util));
            }

            case PhoneNumberUtil.Leniency.STRICT_GROUPING:
            {
                if (!util.isValidNumber(number) ||
                    !PhoneNumberMatcher.containsOnlyValidXChars(number, candidate, util) ||
                    PhoneNumberMatcher.containsMoreThanOneSlashInNationalNumber(number, candidate) ||
                    !PhoneNumberMatcher.isNationalPrefixPresentIfRequired(number, util))
                {
                    return(false);
                }
                return(PhoneNumberMatcher.checkNumberGroupingIsValid(number, candidate, util, new StrictGroupingChecker()));
            }

            default:     //case PhoneNumberUtil.Leniency.EXACT_GROUPING:
            {
                if (!util.isValidNumber(number) ||
                    !PhoneNumberMatcher.containsOnlyValidXChars(number, candidate, util) ||
                    PhoneNumberMatcher.containsMoreThanOneSlashInNationalNumber(number, candidate) ||
                    !PhoneNumberMatcher.isNationalPrefixPresentIfRequired(number, util))
                {
                    return(false);
                }
                return(PhoneNumberMatcher.checkNumberGroupingIsValid(number, candidate, util, new ExactGroupingChecker()));
            }
            }
        }
示例#10
0
        private void DoTestInContext(string number, string defaultCountry,
                                     List <NumberContext> contextPairs, PhoneNumberUtil.Leniency leniency)
        {
            foreach (var context in contextPairs)
            {
                var prefix = context.LeadingText;
                var text   = prefix + number + context.TrailingText;

                var start    = prefix.Length;
                var length   = number.Length;
                var iterator = phoneUtil.FindNumbers(text, defaultCountry, leniency, long.MaxValue);

                var match = iterator.First();
                Assert.NotNull(match);

                var extracted = text.Substring(match.Start, match.Length);
                Assert.True(start == match.Start && length == match.Length,
                            "Unexpected phone region in '" + text + "'; extracted '" + extracted + "'");
                Assert.Equal(number, extracted);
                Assert.Equal(match.RawString, extracted);

                EnsureTermination(text, defaultCountry, leniency);
            }
        }
        private void doTestInContext(String number, String defaultCountry,
                                     List <NumberContext> contextPairs, PhoneNumberUtil.Leniency leniency)
        {
            foreach (var context in contextPairs)
            {
                String prefix = context.leadingText;
                String text   = prefix + number + context.trailingText;

                int start    = prefix.Length;
                int length   = number.Length;
                var iterator = phoneUtil.FindNumbers(text, defaultCountry, leniency, long.MaxValue);

                PhoneNumberMatch match = iterator.First();
                Assert.IsNotNull(match, "Did not find a number in '" + text + "'; expected '" + number + "'");

                String extracted = text.Substring(match.Start, match.Length);
                Assert.IsTrue(start == match.Start && length == match.Length,
                              "Unexpected phone region in '" + text + "'; extracted '" + extracted + "'");
                Assert.AreEqual(number, extracted);
                Assert.AreEqual(match.RawString, extracted);

                EnsureTermination(text, defaultCountry, leniency);
            }
        }
示例#12
0
 private IEnumerable <PhoneNumberMatch> FindNumbersForLeniency(
     string text, string defaultCountry, PhoneNumberUtil.Leniency leniency)
 {
     return(phoneUtil.FindNumbers(text, defaultCountry, leniency, long.MaxValue));
 }