Skip to content

Commit bb32d1f

Browse files
committed
Refactor regexes and mappings class and constants.
1 parent 0bd1332 commit bb32d1f

9 files changed

Lines changed: 662 additions & 556 deletions

File tree

cpp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ set (
272272
"src/phonenumbers/phonenumber.pb.cc" # Generated by Protocol Buffers.
273273
"src/phonenumbers/phonenumberutil.cc"
274274
"src/phonenumbers/regex_based_matcher.cc"
275+
"src/phonenumbers/regexpsandmappings.cc"
275276
"src/phonenumbers/regexp_cache.cc"
276277
"src/phonenumbers/shortnumberinfo.cc"
277278
"src/phonenumbers/string_byte_sink.cc"

cpp/src/phonenumbers/constants.h

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (C) 2025 The Libphonenumber Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <stddef.h>
16+
17+
#ifndef I18N_PHONENUMBERS_CONSTANTS_H_
18+
#define I18N_PHONENUMBERS_CONSTANTS_H_
19+
20+
namespace i18n {
21+
namespace phonenumbers {
22+
23+
class Constants {
24+
friend class PhoneNumberMatcherRegExps;
25+
friend class PhoneNumberRegExpsAndMappings;
26+
friend class PhoneNumberUtil;
27+
28+
private:
29+
// The kPlusSign signifies the international prefix.
30+
static constexpr char kPlusSign[] = "+";
31+
32+
static constexpr char kStarSign[] = "*";
33+
34+
static constexpr char kRfc3966ExtnPrefix[] = ";ext=";
35+
static constexpr char kRfc3966VisualSeparator[] = "[\\-\\.\\(\\)]?";
36+
37+
static constexpr char kDigits[] = "\\p{Nd}";
38+
39+
// We accept alpha characters in phone numbers, ASCII only. We store
40+
// lower-case here only since our regular expressions are case-insensitive.
41+
static constexpr char kValidAlpha[] = "a-z";
42+
static constexpr char kValidAlphaInclUppercase[] = "A-Za-z";
43+
44+
static constexpr char kPossibleSeparatorsBetweenNumberAndExtLabel[] =
45+
"[ \xC2\xA0\\t,]*";
46+
47+
// Optional full stop (.) or colon, followed by zero or more
48+
// spaces/tabs/commas.
49+
static constexpr char kPossibleCharsAfterExtLabel[] =
50+
"[:\\.\xEF\xBC\x8E]?[ \xC2\xA0\\t,-]*";
51+
52+
static constexpr char kOptionalExtSuffix[] = "#?";
53+
54+
// The minimum and maximum length of the national significant number.
55+
static constexpr size_t kMinLengthForNsn = 2;
56+
57+
static constexpr char kPlusChars[] = "+\xEF\xBC\x8B"; /* "++" */
58+
59+
// Regular expression of acceptable punctuation found in phone numbers, used
60+
// to find numbers in text and to decide what is a viable phone number. This
61+
// excludes diallable characters.
62+
// This consists of dash characters, white space characters, full stops,
63+
// slashes, square brackets, parentheses and tildes. It also includes the
64+
// letter 'x' as that is found as a placeholder for carrier information in
65+
// some phone numbers. Full-width variants are also present. To find out the
66+
// unicode code-point of the characters below in vim, highlight the character
67+
// and type 'ga'. Note that the - is used to express ranges of full-width
68+
// punctuation below, as well as being present in the expression itself. In
69+
// emacs, you can use M-x unicode-what to query information about the unicode
70+
// character.
71+
static constexpr char kValidPunctuation[] =
72+
/* "-x‐-―−ー--/ ­<U+200B><U+2060> ()()[].\\[\\]/~⁓∼" */
73+
"-x\xE2\x80\x90-\xE2\x80\x95\xE2\x88\x92\xE3\x83\xBC\xEF\xBC\x8D-\xEF\xBC"
74+
"\x8F \xC2\xA0\xC2\xAD\xE2\x80\x8B\xE2\x81\xA0\xE3\x80\x80()\xEF\xBC\x88"
75+
"\xEF\xBC\x89\xEF\xBC\xBB\xEF\xBC\xBD.\\[\\]/~\xE2\x81\x93\xE2\x88\xBC";
76+
77+
static constexpr char kCaptureUpToSecondNumberStart[] = "(.*)[\\\\/] *x";
78+
};
79+
80+
} // namespace phonenumbers
81+
} // namespace i18n
82+
83+
#endif // I18N_PHONENUMBERS_CONSTANTS_H_

cpp/src/phonenumbers/phonenumbermatcher.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,21 @@
2727

2828
#include <ctype.h>
2929
#include <stddef.h>
30+
#include <unicode/uchar.h>
31+
3032
#include <limits>
3133
#include <map>
3234
#include <memory>
3335
#include <string>
3436
#include <utility>
3537
#include <vector>
36-
#include <unicode/uchar.h>
3738

3839
#include "phonenumbers/alternate_format.h"
3940
#include "phonenumbers/base/logging.h"
4041
#include "phonenumbers/base/memory/scoped_ptr.h"
4142
#include "phonenumbers/base/memory/singleton.h"
4243
#include "phonenumbers/callback.h"
44+
#include "phonenumbers/constants.h"
4345
#include "phonenumbers/default_logger.h"
4446
#include "phonenumbers/encoding_utils.h"
4547
#include "phonenumbers/normalize_utf8.h"
@@ -283,10 +285,10 @@ class PhoneNumberMatcherRegExps : public Singleton<PhoneNumberMatcherRegExps> {
283285
digit_block_limit_(PhoneNumberUtil::kMaxLengthForNsn +
284286
PhoneNumberUtil::kMaxLengthCountryCode),
285287
block_limit_(Limit(0, digit_block_limit_)),
286-
punctuation_(StrCat("[", PhoneNumberUtil::kValidPunctuation, "]",
288+
punctuation_(StrCat("[", Constants::kValidPunctuation, "]",
287289
punctuation_limit_)),
288290
digit_sequence_(StrCat("\\p{Nd}", Limit(1, digit_block_limit_))),
289-
lead_class_chars_(StrCat(opening_parens_, PhoneNumberUtil::kPlusChars)),
291+
lead_class_chars_(StrCat(opening_parens_, Constants::kPlusChars)),
290292
lead_class_(StrCat("[", lead_class_chars_, "]")),
291293
regexp_factory_for_pattern_(new ICURegExpFactory()),
292294
#ifdef I18N_PHONENUMBERS_USE_RE2
@@ -314,7 +316,7 @@ class PhoneNumberMatcherRegExps : public Singleton<PhoneNumberMatcherRegExps> {
314316
inner_matches_(new std::vector<const RegExp*>()),
315317
capture_up_to_second_number_start_pattern_(
316318
regexp_factory_->CreateRegExp(
317-
PhoneNumberUtil::kCaptureUpToSecondNumberStart)),
319+
Constants::kCaptureUpToSecondNumberStart)),
318320
capturing_ascii_digits_pattern_(
319321
regexp_factory_->CreateRegExp("(\\d+)")),
320322
lead_class_pattern_(regexp_factory_->CreateRegExp(lead_class_)),

0 commit comments

Comments
 (0)