From 481e2d9058312113846025c77d2d7a2160a52f96 Mon Sep 17 00:00:00 2001 From: BLM Date: Tue, 21 Jul 2015 12:32:30 -0500 Subject: [PATCH] Created ShortPhoneNumber type --- Makefile | 2 +- get_sizeof_phone_number.cpp | 6 ++---- pg_libphonenumber.cpp | 32 ++++++++++++++++---------------- short_phone_number.cpp | 9 +++++++++ short_phone_number.h | 4 +++- 5 files changed, 31 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index 062d74f..d07aca2 100644 --- a/Makefile +++ b/Makefile @@ -18,4 +18,4 @@ $(extension_script): $(EXTENSION).sql.template get_sizeof_phone_number sed "s/SIZEOF_PHONE_NUMBER/$(shell ./get_sizeof_phone_number)/" $< > $@ get_sizeof_phone_number: get_sizeof_phone_number.cpp - g++ $< -o $@ + $(CXX) -std=c++11 $< -o $@ diff --git a/get_sizeof_phone_number.cpp b/get_sizeof_phone_number.cpp index 45307ff..874d197 100644 --- a/get_sizeof_phone_number.cpp +++ b/get_sizeof_phone_number.cpp @@ -1,10 +1,8 @@ #include -#include "phonenumbers/phonenumber.pb.h" - -using namespace i18n::phonenumbers; +#include "short_phone_number.h" int main(int argc, const char** argv) { - std::cout << sizeof(PhoneNumber) << std::endl; + std::cout << sizeof(ShortPhoneNumber) << std::endl; return 0; } diff --git a/pg_libphonenumber.cpp b/pg_libphonenumber.cpp index 4d810eb..e648fdc 100755 --- a/pg_libphonenumber.cpp +++ b/pg_libphonenumber.cpp @@ -65,21 +65,22 @@ static void logInfo(const char* msg) { */ //Internal function used by phone_number_in and parse_phone_number -PhoneNumber* parsePhoneNumber(const char* number_str, const char* country) throw() { +ShortPhoneNumber* parsePhoneNumber(const char* number_str, const char* country) throw() { try { - PhoneNumber *number; + PhoneNumber number; + ShortPhoneNumber* short_number; - number = (PhoneNumber*)palloc0(sizeof(PhoneNumber)); + short_number = (ShortPhoneNumber*)palloc0(sizeof(ShortPhoneNumber)); //TODO: can this be removed? (palloc normally handles this, right?) - if(number == nullptr) { + if(short_number == nullptr) { throw std::bad_alloc(); } - new(number) PhoneNumber(); PhoneNumberUtil::ErrorType error; - error = phoneUtil->Parse(number_str, country, number); + error = phoneUtil->Parse(number_str, country, &number); if(error == PhoneNumberUtil::NO_PARSING_ERROR) { - return number; + new(short_number) ShortPhoneNumber(number); + return short_number; } else { reportParseError(number_str, error); return nullptr; @@ -114,7 +115,7 @@ extern "C" { { const char *number_str = PG_GETARG_CSTRING(0); - PhoneNumber* number = parsePhoneNumber(number_str, "US"); + ShortPhoneNumber* number = parsePhoneNumber(number_str, "US"); if(number) { PG_RETURN_POINTER(number); } else { @@ -131,7 +132,7 @@ extern "C" { const char *number_str = PG_GETARG_CSTRING(0); const char *country = PG_GETARG_CSTRING(1); - PhoneNumber* number = parsePhoneNumber(number_str, country); + ShortPhoneNumber* number = parsePhoneNumber(number_str, country); if(number) { PG_RETURN_POINTER(number); } else { @@ -146,19 +147,18 @@ extern "C" { phone_number_out(PG_FUNCTION_ARGS) { try { - const PhoneNumber *number = (PhoneNumber*)PG_GETARG_POINTER(0); - std::string formatted; - char *result; + const ShortPhoneNumber* short_number = (ShortPhoneNumber*)PG_GETARG_POINTER(0); + PhoneNumber number = *short_number; - phoneUtil->Format(*number, PhoneNumberUtil::INTERNATIONAL, &formatted); + std::string formatted; + phoneUtil->Format(number, PhoneNumberUtil::INTERNATIONAL, &formatted); //Copy the formatted number to a C-style string. //We must use the PostgreSQL allocator, not new/malloc. size_t len = formatted.length(); - result = (char*)palloc(len + 1); + char* result = (char*)palloc(len + 1); if(result == nullptr) { - reportOutOfMemory(); - PG_RETURN_NULL(); + throw std::bad_alloc(); } memcpy(result, formatted.data(), len); result[len] = '\0'; diff --git a/short_phone_number.cpp b/short_phone_number.cpp index c132d5d..679116a 100644 --- a/short_phone_number.cpp +++ b/short_phone_number.cpp @@ -30,3 +30,12 @@ ShortPhoneNumber::ShortPhoneNumber(i18n::phonenumbers::PhoneNumber number) { _leading_zeros = 0; } } + +ShortPhoneNumber::operator PhoneNumber() const { + PhoneNumber number; + number.set_country_code(_country_code); + number.set_national_number(_national_number); + number.set_italian_leading_zero(_leading_zeros > 0); + number.set_number_of_leading_zeros(_leading_zeros); + return number; +} diff --git a/short_phone_number.h b/short_phone_number.h index 8e30176..16154d1 100644 --- a/short_phone_number.h +++ b/short_phone_number.h @@ -12,7 +12,7 @@ class PhoneNumberTooLongException : std::runtime_error { static const i18n::phonenumbers::PhoneNumberUtil* const phoneUtil; }; -struct ShortPhoneNumber { +class ShortPhoneNumber { public: enum : size_t { MAX_COUNTRY_CODE = 999, @@ -23,6 +23,8 @@ struct ShortPhoneNumber { ShortPhoneNumber(i18n::phonenumbers::PhoneNumber number); + operator i18n::phonenumbers::PhoneNumber() const; + private: google::protobuf::uint32 _country_code : 10; google::protobuf::uint64 _national_number : 50;