Created ShortPhoneNumber type
This commit is contained in:
parent
3f79c02391
commit
481e2d9058
2
Makefile
2
Makefile
|
@ -18,4 +18,4 @@ $(extension_script): $(EXTENSION).sql.template get_sizeof_phone_number
|
||||||
sed "s/SIZEOF_PHONE_NUMBER/$(shell ./get_sizeof_phone_number)/" $< > $@
|
sed "s/SIZEOF_PHONE_NUMBER/$(shell ./get_sizeof_phone_number)/" $< > $@
|
||||||
|
|
||||||
get_sizeof_phone_number: get_sizeof_phone_number.cpp
|
get_sizeof_phone_number: get_sizeof_phone_number.cpp
|
||||||
g++ $< -o $@
|
$(CXX) -std=c++11 $< -o $@
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "phonenumbers/phonenumber.pb.h"
|
#include "short_phone_number.h"
|
||||||
|
|
||||||
using namespace i18n::phonenumbers;
|
|
||||||
|
|
||||||
int main(int argc, const char** argv) {
|
int main(int argc, const char** argv) {
|
||||||
std::cout << sizeof(PhoneNumber) << std::endl;
|
std::cout << sizeof(ShortPhoneNumber) << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,21 +65,22 @@ static void logInfo(const char* msg) {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//Internal function used by phone_number_in and parse_phone_number
|
//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 {
|
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?)
|
//TODO: can this be removed? (palloc normally handles this, right?)
|
||||||
if(number == nullptr) {
|
if(short_number == nullptr) {
|
||||||
throw std::bad_alloc();
|
throw std::bad_alloc();
|
||||||
}
|
}
|
||||||
new(number) PhoneNumber();
|
|
||||||
|
|
||||||
PhoneNumberUtil::ErrorType error;
|
PhoneNumberUtil::ErrorType error;
|
||||||
error = phoneUtil->Parse(number_str, country, number);
|
error = phoneUtil->Parse(number_str, country, &number);
|
||||||
if(error == PhoneNumberUtil::NO_PARSING_ERROR) {
|
if(error == PhoneNumberUtil::NO_PARSING_ERROR) {
|
||||||
return number;
|
new(short_number) ShortPhoneNumber(number);
|
||||||
|
return short_number;
|
||||||
} else {
|
} else {
|
||||||
reportParseError(number_str, error);
|
reportParseError(number_str, error);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -114,7 +115,7 @@ extern "C" {
|
||||||
{
|
{
|
||||||
const char *number_str = PG_GETARG_CSTRING(0);
|
const char *number_str = PG_GETARG_CSTRING(0);
|
||||||
|
|
||||||
PhoneNumber* number = parsePhoneNumber(number_str, "US");
|
ShortPhoneNumber* number = parsePhoneNumber(number_str, "US");
|
||||||
if(number) {
|
if(number) {
|
||||||
PG_RETURN_POINTER(number);
|
PG_RETURN_POINTER(number);
|
||||||
} else {
|
} else {
|
||||||
|
@ -131,7 +132,7 @@ extern "C" {
|
||||||
const char *number_str = PG_GETARG_CSTRING(0);
|
const char *number_str = PG_GETARG_CSTRING(0);
|
||||||
const char *country = PG_GETARG_CSTRING(1);
|
const char *country = PG_GETARG_CSTRING(1);
|
||||||
|
|
||||||
PhoneNumber* number = parsePhoneNumber(number_str, country);
|
ShortPhoneNumber* number = parsePhoneNumber(number_str, country);
|
||||||
if(number) {
|
if(number) {
|
||||||
PG_RETURN_POINTER(number);
|
PG_RETURN_POINTER(number);
|
||||||
} else {
|
} else {
|
||||||
|
@ -146,19 +147,18 @@ extern "C" {
|
||||||
phone_number_out(PG_FUNCTION_ARGS)
|
phone_number_out(PG_FUNCTION_ARGS)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
const PhoneNumber *number = (PhoneNumber*)PG_GETARG_POINTER(0);
|
const ShortPhoneNumber* short_number = (ShortPhoneNumber*)PG_GETARG_POINTER(0);
|
||||||
std::string formatted;
|
PhoneNumber number = *short_number;
|
||||||
char *result;
|
|
||||||
|
|
||||||
phoneUtil->Format(*number, PhoneNumberUtil::INTERNATIONAL, &formatted);
|
std::string formatted;
|
||||||
|
phoneUtil->Format(number, PhoneNumberUtil::INTERNATIONAL, &formatted);
|
||||||
|
|
||||||
//Copy the formatted number to a C-style string.
|
//Copy the formatted number to a C-style string.
|
||||||
//We must use the PostgreSQL allocator, not new/malloc.
|
//We must use the PostgreSQL allocator, not new/malloc.
|
||||||
size_t len = formatted.length();
|
size_t len = formatted.length();
|
||||||
result = (char*)palloc(len + 1);
|
char* result = (char*)palloc(len + 1);
|
||||||
if(result == nullptr) {
|
if(result == nullptr) {
|
||||||
reportOutOfMemory();
|
throw std::bad_alloc();
|
||||||
PG_RETURN_NULL();
|
|
||||||
}
|
}
|
||||||
memcpy(result, formatted.data(), len);
|
memcpy(result, formatted.data(), len);
|
||||||
result[len] = '\0';
|
result[len] = '\0';
|
||||||
|
|
|
@ -30,3 +30,12 @@ ShortPhoneNumber::ShortPhoneNumber(i18n::phonenumbers::PhoneNumber number) {
|
||||||
_leading_zeros = 0;
|
_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;
|
||||||
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ class PhoneNumberTooLongException : std::runtime_error {
|
||||||
static const i18n::phonenumbers::PhoneNumberUtil* const phoneUtil;
|
static const i18n::phonenumbers::PhoneNumberUtil* const phoneUtil;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ShortPhoneNumber {
|
class ShortPhoneNumber {
|
||||||
public:
|
public:
|
||||||
enum : size_t {
|
enum : size_t {
|
||||||
MAX_COUNTRY_CODE = 999,
|
MAX_COUNTRY_CODE = 999,
|
||||||
|
@ -23,6 +23,8 @@ struct ShortPhoneNumber {
|
||||||
|
|
||||||
ShortPhoneNumber(i18n::phonenumbers::PhoneNumber number);
|
ShortPhoneNumber(i18n::phonenumbers::PhoneNumber number);
|
||||||
|
|
||||||
|
operator i18n::phonenumbers::PhoneNumber() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
google::protobuf::uint32 _country_code : 10;
|
google::protobuf::uint32 _country_code : 10;
|
||||||
google::protobuf::uint64 _national_number : 50;
|
google::protobuf::uint64 _national_number : 50;
|
||||||
|
|
Loading…
Reference in New Issue