Clean up code somewhat
This commit is contained in:
parent
76d8bdeca3
commit
f35b9e30b5
2
Makefile
2
Makefile
|
@ -15,7 +15,7 @@ MODULE_big := pg_libphonenumber
|
||||||
OBJS := $(patsubst %.cpp,%.o,$(cpp_files))
|
OBJS := $(patsubst %.cpp,%.o,$(cpp_files))
|
||||||
|
|
||||||
# C flags
|
# C flags
|
||||||
PG_CPPFLAGS := -fPIC -std=gnu++14
|
PG_CPPFLAGS := -fPIC -std=c++14
|
||||||
PG_CPPFLAGS += -Isrc/ -I/usr/include
|
PG_CPPFLAGS += -Isrc/ -I/usr/include
|
||||||
PG_CPPFLAGS += -Wall -Wextra
|
PG_CPPFLAGS += -Wall -Wextra
|
||||||
ifeq ($(CONFIG),debug)
|
ifeq ($(CONFIG),debug)
|
||||||
|
|
|
@ -16,20 +16,20 @@ std::string PhoneNumberTooLongException::number_string() const {
|
||||||
|
|
||||||
ShortPhoneNumber::ShortPhoneNumber(i18n::phonenumbers::PhoneNumber number) {
|
ShortPhoneNumber::ShortPhoneNumber(i18n::phonenumbers::PhoneNumber number) {
|
||||||
uint32 country_code = number.country_code();
|
uint32 country_code = number.country_code();
|
||||||
if(country_code > MAX_COUNTRY_CODE) {
|
if(country_code > max_country_code) {
|
||||||
throw PhoneNumberTooLongException(number, "Country code is too long");
|
throw PhoneNumberTooLongException(number, "Country code is too long");
|
||||||
}
|
}
|
||||||
this->country_code(country_code);
|
this->country_code(country_code);
|
||||||
|
|
||||||
uint64_t national_number = number.national_number();
|
uint64_t national_number = number.national_number();
|
||||||
if(national_number > MAX_NATIONAL_NUMBER) {
|
if(national_number > max_national_number) {
|
||||||
throw PhoneNumberTooLongException(number, "National number is too long");
|
throw PhoneNumberTooLongException(number, "National number is too long");
|
||||||
}
|
}
|
||||||
this->national_number(national_number);
|
this->national_number(national_number);
|
||||||
|
|
||||||
if(number.has_number_of_leading_zeros()) {
|
if(number.has_number_of_leading_zeros()) {
|
||||||
uint32 leading_zeros = number.number_of_leading_zeros();
|
uint32 leading_zeros = number.number_of_leading_zeros();
|
||||||
if(leading_zeros > MAX_LEADING_ZEROS) {
|
if(leading_zeros > max_leading_zeros) {
|
||||||
throw PhoneNumberTooLongException(number, "Too many leading zeros");
|
throw PhoneNumberTooLongException(number, "Too many leading zeros");
|
||||||
}
|
}
|
||||||
this->leading_zeros(leading_zeros);
|
this->leading_zeros(leading_zeros);
|
||||||
|
|
|
@ -33,34 +33,28 @@ class PhoneNumberTooLongException : public std::runtime_error {
|
||||||
*/
|
*/
|
||||||
class ShortPhoneNumber {
|
class ShortPhoneNumber {
|
||||||
public:
|
public:
|
||||||
enum : size_t {
|
|
||||||
/// The largest possible country code
|
/// The largest possible country code
|
||||||
MAX_COUNTRY_CODE = 999,
|
static constexpr size_t max_country_code = 999;
|
||||||
/// The maximum number of leading zeroes in a national phone number
|
/// The maximum number of leading zeroes in a national phone number
|
||||||
MAX_LEADING_ZEROS = 15,
|
static constexpr size_t max_leading_zeros = 15;
|
||||||
/// The largest possible national number
|
/// The largest possible national number
|
||||||
MAX_NATIONAL_NUMBER = 999999999999999,
|
static constexpr size_t max_national_number = 999999999999999;
|
||||||
};
|
|
||||||
|
|
||||||
enum : size_t {
|
|
||||||
/// The number of bits reserved for a country code
|
/// The number of bits reserved for a country code
|
||||||
COUNTRY_CODE_BITS = 10,
|
static constexpr size_t country_code_bits = 10;
|
||||||
/// The number of bits reserved for the leading zero count
|
/// The number of bits reserved for the leading zero count
|
||||||
LEADING_ZEROS_BITS = 4,
|
static constexpr size_t leading_zeros_bits = 4;
|
||||||
/// The number of bits reserved for the national number
|
/// The number of bits reserved for the national number
|
||||||
NATIONAL_NUMBER_BITS = 50,
|
static constexpr size_t national_number_bits = 50;
|
||||||
};
|
|
||||||
|
|
||||||
// Bit offsets of the number components
|
// Bit offsets of the number components
|
||||||
enum : size_t {
|
static constexpr size_t country_code_offset = 0;
|
||||||
COUNTRY_CODE_OFFSET = 0,
|
static constexpr size_t leading_zeros_offset = country_code_offset + country_code_bits;
|
||||||
LEADING_ZEROS_OFFSET = COUNTRY_CODE_OFFSET + COUNTRY_CODE_BITS,
|
static constexpr size_t national_number_offset = leading_zeros_offset + leading_zeros_bits;
|
||||||
NATIONAL_NUMBER_OFFSET = LEADING_ZEROS_OFFSET + LEADING_ZEROS_BITS,
|
|
||||||
};
|
|
||||||
|
|
||||||
ShortPhoneNumber(i18n::phonenumbers::PhoneNumber number);
|
ShortPhoneNumber(i18n::phonenumbers::PhoneNumber number);
|
||||||
|
|
||||||
bool operator == (const ShortPhoneNumber other) const {
|
bool operator==(const ShortPhoneNumber other) const {
|
||||||
return this->_data == other._data;
|
return this->_data == other._data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,32 +82,32 @@ class ShortPhoneNumber {
|
||||||
|
|
||||||
/// Returns the number's country code
|
/// Returns the number's country code
|
||||||
google::protobuf::uint32 country_code() const {
|
google::protobuf::uint32 country_code() const {
|
||||||
return get_masked(_data, COUNTRY_CODE_BITS, COUNTRY_CODE_OFFSET);
|
return get_masked(_data, country_code_bits, country_code_offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the number's country code
|
/// Sets the number's country code
|
||||||
void country_code(uint32_t value) {
|
void country_code(uint32_t value) {
|
||||||
_data = set_masked(_data, static_cast<uint64_t>(value), COUNTRY_CODE_BITS, COUNTRY_CODE_OFFSET);
|
_data = set_masked(_data, static_cast<uint64_t>(value), country_code_bits, country_code_offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the national number
|
/// Returns the national number
|
||||||
uint64_t national_number() const {
|
uint64_t national_number() const {
|
||||||
return get_masked(_data, NATIONAL_NUMBER_BITS, NATIONAL_NUMBER_OFFSET);
|
return get_masked(_data, national_number_bits, national_number_offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the national number
|
/// Sets the national number
|
||||||
void national_number(uint64_t value) {
|
void national_number(uint64_t value) {
|
||||||
_data = set_masked(_data, value, NATIONAL_NUMBER_BITS, NATIONAL_NUMBER_OFFSET);
|
_data = set_masked(_data, value, national_number_bits, national_number_offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the number of leading zeros
|
/// Returns the number of leading zeros
|
||||||
uint64_t leading_zeros() const {
|
uint64_t leading_zeros() const {
|
||||||
return get_masked(_data, LEADING_ZEROS_BITS, LEADING_ZEROS_OFFSET);
|
return get_masked(_data, leading_zeros_bits, leading_zeros_offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the number of leading zeros
|
/// Sets the number of leading zeros
|
||||||
void leading_zeros(uint64_t value) {
|
void leading_zeros(uint64_t value) {
|
||||||
_data = set_masked(_data, value, LEADING_ZEROS_BITS, LEADING_ZEROS_OFFSET);
|
_data = set_masked(_data, value, leading_zeros_bits, leading_zeros_offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -124,4 +118,4 @@ class ShortPhoneNumber {
|
||||||
* If the size of the ShortPhoneNumber class changes for any reason, it will trip this assertion and remind us to
|
* 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.
|
* update the SQL definition of the short_phone_number type.
|
||||||
*/
|
*/
|
||||||
static_assert(sizeof(ShortPhoneNumber) == 8);
|
static_assert(sizeof(ShortPhoneNumber) == 8, "unexpected size for ShortPhoneNumber");
|
||||||
|
|
Loading…
Reference in New Issue