Clean up code somewhat

This commit is contained in:
Ben Merritt 2019-07-18 23:03:42 -07:00
parent 76d8bdeca3
commit f35b9e30b5
No known key found for this signature in database
GPG Key ID: F8AD20ED4E6239B7
3 changed files with 27 additions and 33 deletions

View File

@ -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)

View File

@ -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);

View File

@ -33,30 +33,24 @@ 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);
@ -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");