2015-07-17 20:22:30 +00:00
|
|
|
#include <exception>
|
2015-07-28 21:11:44 +00:00
|
|
|
#include <string>
|
2015-07-17 20:22:30 +00:00
|
|
|
|
|
|
|
#include "phonenumbers/phonenumberutil.h"
|
|
|
|
|
2015-07-22 19:23:40 +00:00
|
|
|
#include "mask.h"
|
|
|
|
|
2018-02-10 03:21:50 +00:00
|
|
|
/**
|
|
|
|
* Raised when a phone number is too long to fit in a ShortPhoneNumber
|
|
|
|
*
|
|
|
|
* This shouldn't happen for any valid phone numbers.
|
|
|
|
*/
|
2015-07-21 21:41:44 +00:00
|
|
|
class PhoneNumberTooLongException : public std::runtime_error {
|
2017-02-18 17:11:41 +00:00
|
|
|
public:
|
|
|
|
PhoneNumberTooLongException(const i18n::phonenumbers::PhoneNumber& number, const char* msg);
|
2015-07-17 20:22:30 +00:00
|
|
|
|
2018-02-10 03:21:50 +00:00
|
|
|
/// Returns the original phone number object
|
2017-02-18 17:11:41 +00:00
|
|
|
i18n::phonenumbers::PhoneNumber number() const {
|
|
|
|
return _number;
|
|
|
|
}
|
2015-07-28 21:11:44 +00:00
|
|
|
|
2017-02-18 17:11:41 +00:00
|
|
|
//TODO: just get the number string from which the PhoneNumber was parsed? (if it exists...)
|
|
|
|
std::string number_string() const;
|
|
|
|
private:
|
|
|
|
i18n::phonenumbers::PhoneNumber _number;
|
2015-07-17 20:22:30 +00:00
|
|
|
|
2017-02-18 17:11:41 +00:00
|
|
|
static const i18n::phonenumbers::PhoneNumberUtil* const phoneUtil;
|
2015-07-17 20:22:30 +00:00
|
|
|
};
|
|
|
|
|
2017-02-18 17:11:41 +00:00
|
|
|
/**
|
|
|
|
* Stores a phone number (packed into a 64-bit integer)
|
|
|
|
*/
|
2015-07-21 17:32:30 +00:00
|
|
|
class ShortPhoneNumber {
|
2017-02-18 17:11:41 +00:00
|
|
|
public:
|
|
|
|
enum : size_t {
|
2018-02-10 03:21:50 +00:00
|
|
|
/// The largest possible country code
|
2017-02-18 17:11:41 +00:00
|
|
|
MAX_COUNTRY_CODE = 999,
|
2018-02-10 03:21:50 +00:00
|
|
|
/// The maximum number of leading zeroes in a national phone number
|
2017-02-18 17:11:41 +00:00
|
|
|
MAX_LEADING_ZEROS = 15,
|
2018-02-10 03:21:50 +00:00
|
|
|
/// The largest possible national number
|
2017-02-18 17:11:41 +00:00
|
|
|
MAX_NATIONAL_NUMBER = 999999999999999,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum : size_t {
|
2018-02-10 03:21:50 +00:00
|
|
|
/// The number of bits reserved for a country code
|
2017-02-18 17:11:41 +00:00
|
|
|
COUNTRY_CODE_BITS = 10,
|
2018-02-10 03:21:50 +00:00
|
|
|
/// The number of bits reserved for the leading zero count
|
2017-02-18 17:11:41 +00:00
|
|
|
LEADING_ZEROS_BITS = 4,
|
2018-02-10 03:21:50 +00:00
|
|
|
/// The number of bits reserved for the national number
|
2017-02-18 17:11:41 +00:00
|
|
|
NATIONAL_NUMBER_BITS = 50,
|
|
|
|
};
|
|
|
|
|
2018-02-10 03:21:50 +00:00
|
|
|
// Bit offsets of the number components
|
2017-02-18 17:11:41 +00:00
|
|
|
enum : size_t {
|
|
|
|
COUNTRY_CODE_OFFSET = 0,
|
|
|
|
LEADING_ZEROS_OFFSET = COUNTRY_CODE_OFFSET + COUNTRY_CODE_BITS,
|
|
|
|
NATIONAL_NUMBER_OFFSET = LEADING_ZEROS_OFFSET + LEADING_ZEROS_BITS,
|
|
|
|
};
|
|
|
|
|
|
|
|
ShortPhoneNumber(i18n::phonenumbers::PhoneNumber number);
|
|
|
|
|
|
|
|
bool operator == (const ShortPhoneNumber other) const {
|
|
|
|
return this->_data == other._data;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator != (const ShortPhoneNumber other) const {
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
2018-02-10 03:21:50 +00:00
|
|
|
/// Casts this object to a libphonenumber PhoneNumber object
|
2017-02-18 17:11:41 +00:00
|
|
|
operator i18n::phonenumbers::PhoneNumber() const;
|
|
|
|
|
|
|
|
/*
|
2018-02-10 03:21:50 +00:00
|
|
|
* Compares to another ShortPhoneNumber using a fast collation heuristic
|
2017-02-18 17:11:41 +00:00
|
|
|
*
|
|
|
|
* May not produce intuitive results for numbers with the same
|
|
|
|
* country code but different lengths
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* - <0 (if a < b)
|
|
|
|
* - 0 (if a == b)
|
|
|
|
* - >0 (if a > b)
|
|
|
|
*/
|
|
|
|
google::protobuf::int64 compare_fast(ShortPhoneNumber other) const {
|
|
|
|
return other._data - this->_data;
|
|
|
|
}
|
|
|
|
|
2018-02-10 03:21:50 +00:00
|
|
|
/// Returns the number's country code
|
2017-02-18 17:11:41 +00:00
|
|
|
google::protobuf::uint32 country_code() const {
|
2017-03-13 19:04:26 +00:00
|
|
|
return get_masked(_data, COUNTRY_CODE_BITS, COUNTRY_CODE_OFFSET);
|
2017-02-18 17:11:41 +00:00
|
|
|
}
|
|
|
|
|
2018-02-10 03:21:50 +00:00
|
|
|
/// Sets the number's country code
|
2017-02-18 17:11:41 +00:00
|
|
|
void country_code(google::protobuf::uint32 value) {
|
2017-03-13 19:04:26 +00:00
|
|
|
_data = set_masked(_data, (google::protobuf::uint64)value, COUNTRY_CODE_BITS, COUNTRY_CODE_OFFSET);
|
2017-02-18 17:11:41 +00:00
|
|
|
}
|
|
|
|
|
2018-02-10 03:21:50 +00:00
|
|
|
/// Returns the national number
|
2017-02-18 17:11:41 +00:00
|
|
|
google::protobuf::uint64 national_number() const {
|
2017-03-13 19:04:26 +00:00
|
|
|
return get_masked(_data, NATIONAL_NUMBER_BITS, NATIONAL_NUMBER_OFFSET);
|
2017-02-18 17:11:41 +00:00
|
|
|
}
|
|
|
|
|
2018-02-10 03:21:50 +00:00
|
|
|
/// Sets the national number
|
2017-02-18 17:11:41 +00:00
|
|
|
void national_number(google::protobuf::uint64 value) {
|
2017-03-13 19:04:26 +00:00
|
|
|
_data = set_masked(_data, value, NATIONAL_NUMBER_BITS, NATIONAL_NUMBER_OFFSET);
|
2017-02-18 17:11:41 +00:00
|
|
|
}
|
|
|
|
|
2018-02-10 03:21:50 +00:00
|
|
|
/// Returns the number of leading zeros
|
2017-02-18 17:11:41 +00:00
|
|
|
google::protobuf::uint64 leading_zeros() const {
|
2017-03-13 19:04:26 +00:00
|
|
|
return get_masked(_data, LEADING_ZEROS_BITS, LEADING_ZEROS_OFFSET);
|
2017-02-18 17:11:41 +00:00
|
|
|
}
|
|
|
|
|
2018-02-10 03:21:50 +00:00
|
|
|
/// Sets the number of leading zeros
|
2017-02-18 17:11:41 +00:00
|
|
|
void leading_zeros(google::protobuf::uint64 value) {
|
2017-03-13 19:04:26 +00:00
|
|
|
_data = set_masked(_data, value, LEADING_ZEROS_BITS, LEADING_ZEROS_OFFSET);
|
2017-02-18 17:11:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
google::protobuf::uint64 _data;
|
2015-07-17 20:22:30 +00:00
|
|
|
};
|
2017-03-13 19:04:26 +00:00
|
|
|
|
2018-02-10 03:21:50 +00:00
|
|
|
/*
|
|
|
|
* If the size of the ShortPhoneNumber class changes for any reason, it will trip this assertion and remind us to
|
|
|
|
* update the SQL definition of the short_phone_number type.
|
|
|
|
*/
|
2017-03-13 19:04:26 +00:00
|
|
|
static_assert(sizeof(ShortPhoneNumber) == 8);
|