Skip to content

Commit 77f3ebc

Browse files
committed
Support of short number check in 'is_valid_number' function #4
1 parent 1e4e3dc commit 77f3ebc

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

src/numinfo.cpp

+19-3
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,37 @@
77

88
#include "phonenumbers/phonenumber.pb.h"
99
#include "phonenumbers/phonenumberutil.h"
10+
#include "phonenumbers/shortnumberinfo.h"
1011

1112
using i18n::phonenumbers::PhoneNumber;
1213
using i18n::phonenumbers::PhoneNumberUtil;
14+
using i18n::phonenumbers::ShortNumberInfo;
1315

14-
int is_valid_number(char* number, char* country, int local) {
16+
int is_valid_number(char* number, char* country, int local, int short_code) {
1517

1618
PhoneNumber num;
1719
const PhoneNumberUtil& phone_util = *PhoneNumberUtil::GetInstance();
20+
const ShortNumberInfo& short_info = ShortNumberInfo();
21+
bool is_valid;
22+
bool is_valid_short_number;
1823
phone_util.Parse(number, country, &num);
1924

2025
if (local) {
21-
return (int) phone_util.IsValidNumberForRegion(num, country);
26+
is_valid = phone_util.IsValidNumberForRegion(num, country);
2227
} else {
23-
return (int) phone_util.IsValidNumber(num);
28+
is_valid = phone_util.IsValidNumber(num) || short_info.IsValidShortNumber(num);
2429
}
30+
31+
if (short_code > 0) {
32+
if (local) {
33+
is_valid_short_number = short_info.IsValidShortNumberForRegion(num, country);
34+
} else {
35+
is_valid_short_number = short_info.IsValidShortNumber(num);
36+
}
37+
is_valid = (short_code == 2) ? (is_valid || is_valid_short_number) : is_valid_short_number;
38+
}
39+
40+
return (int) is_valid;
2541
}
2642

2743
int get_region(char* number, char* country, char *region) {

src/numinfo.h

+9-6
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,16 @@ static const char *phone_type_str[] = {
3838
/**
3939
* Check the validity of a number.
4040
*
41-
* @param number Number to check
42-
* @param country Two-letter country (ISO 3166-1) where the national number will be taken into account.
43-
* @param local 1 if the number is valid in the country specified otherwise the check is done in national and
44-
* international.
45-
* @return 1 if the number is valid and 0 if it is invalid.
41+
* @param number Number to check
42+
* @param country Two-letter country (ISO 3166-1) where the national number will be taken into account.
43+
* @param local 1 if the number is valid in the country specified otherwise the check is done in national and
44+
* international.
45+
* @param short_code 0 Don't check if the number is a short number
46+
* 1 Check only if it's a short number
47+
* 2 Check if it's a short number or a classic number
48+
* @return 1 if the number is valid and 0 if it is invalid.
4649
*/
47-
int is_valid_number(char* number, char* country, int local);
50+
int is_valid_number(char* number, char* country, int local, int short_code);
4851

4952
/**
5053
* Get the country based on the number's information.

test/display.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@ void print_region_info(char* country) {
1010
void print_num_info(char* num, char* country) {
1111
char buf[100];
1212

13-
printf("=== NUMBER INFO ===\n");
13+
printf("=== NUMBER VALIDITY ===\n");
1414

15-
printf(" - Is valid number: %s\n", BOOL2STR(is_valid_number(num, country, 0)));
16-
printf(" - Is valid number for %s: %s\n", country, BOOL2STR(is_valid_number(num, country, 1)));
15+
printf(" - Is valid number: %s\n", BOOL2STR(is_valid_number(num, country, 0, 0)));
16+
printf(" - Is valid short number: %s\n", BOOL2STR(is_valid_number(num, country, 0, 1)));
17+
printf(" - Is valid number or short number: %s\n", BOOL2STR(is_valid_number(num, country, 0, 2)));
18+
printf(" - Is valid number for %s: %s\n", country, BOOL2STR(is_valid_number(num, country, 1, 0)));
19+
printf(" - Is valid short number for %s: %s\n", country, BOOL2STR(is_valid_number(num, country, 1, 1)));
20+
printf(" - Is valid number or short number for %s: %s\n", country, BOOL2STR(is_valid_number(num, country, 1, 2)));
21+
22+
printf("=== NUMBER INFO ===\n");
1723

1824
printf(" - Country calling code: %d\n", get_country_code(num, country));
1925
get_region(num, country, buf);

0 commit comments

Comments
 (0)