Runtime error with libphonenumber c++

105 views
Skip to first unread message

Aaron

unread,
Feb 8, 2012, 2:52:37 PM2/8/12
to libphonenumber-discuss
I started using the libphonenumber c++ port for a project at work and
I am getting some strange issues. I have gotten everything to compile
and run but when I call PhoneNumberUtil::GetInstance(), I get an
unhandled exception. The exception is that the logger has not been
initialized. So i fixed the problem by changing the to follow code:

Old:
static inline void set_logger_impl(Logger* logger) {
impl_ = logger;
}

// Private constructor. Also takes care of initialisation.
PhoneNumberUtil::PhoneNumberUtil()
: logger_(new StdoutLogger()),
reg_exps_(new PhoneNumberRegExpsAndMappings),
country_calling_code_to_region_code_map_(new
vector<IntRegionsPair>()),
nanpa_regions_(new set<string>()),
region_to_metadata_map_(new map<string, PhoneMetadata>()),
country_code_to_non_geographical_metadata_map_(
new map<int, PhoneMetadata>) {
Logger::set_logger_impl(logger_.get());

New:
static inline Logger* set_logger_impl(Logger* logger) {
impl_ = logger;
return logger;
}

// Private constructor. Also takes care of initialisation.
PhoneNumberUtil::PhoneNumberUtil()
: logger_(Logger::set_logger_impl(new StdoutLogger())),
reg_exps_(new PhoneNumberRegExpsAndMappings),
country_calling_code_to_region_code_map_(new
vector<IntRegionsPair>()),
nanpa_regions_(new set<string>()),
region_to_metadata_map_(new map<string, PhoneMetadata>()),
country_code_to_non_geographical_metadata_map_(
new map<int, PhoneMetadata>) {

Basically, I make sure the logger is initialized before anything else
is done. But the real reason why the logger was being called upon in
the first place was because of an error in create an IcuRegExp. It was
one of the regex's for PhoneNumberRegExpsAndMappings. This was the
regular expression that failed '[\d]+(?:[~⠓∼~][\d]+)?'. I was
wondering if you could provide any insight on this.

Lara Rennie

unread,
Feb 9, 2012, 4:58:11 AM2/9/12
to libphonenum...@googlegroups.com
What are you building on? Linux? Windows? Mac?

'[\d]+(?:[~⠓∼~][\d]+)?' looks very suspect as well.... they are
supposed to be UTF8 chars :)

2012/2/8 Aaron <amc...@gmail.com>:

Aaron Cooper

unread,
Feb 9, 2012, 10:22:40 AM2/9/12
to libphonenum...@googlegroups.com
Im building on windows with visual studio 2010. The output was grabbed from the visual studio debugging output, so it might not be displaying the characters correctly. I did some more debugging yesterday and found that the IcuRegExp is always returning an error saying there are invalid characters in the input string. I dont know if this is a problem with the icu libraries I downloaded or if its some other problem. I downloaded the ICU binaries directly from their page.

Thanks,
Aaron

John C.

unread,
Mar 30, 2012, 5:34:57 PM3/30/12
to libphonenum...@googlegroups.com
I sent a reply directly to Aaron, which I'll blame on the new UI layout ;)

Anyways, I think I mentioned on a previous post that the ICU regex input requires strings to be C format. I made a patch for my own code base here.

Index: regexp_adapter_icu.cc
===================================================================
--- regexp_adapter_icu.cc (revision 88887)
+++ regexp_adapter_icu.cc (working copy)
@@ -17,6 +17,7 @@
 
 #include "phonenumbers/regexp_adapter_icu.h"
 
+#include <stddef.h>
 #include <string>
 
 #include <unicode/regex.h>
@@ -90,10 +91,8 @@
   explicit IcuRegExp(const string& utf8_regexp) {
     UParseError parse_error;
     UErrorCode status = U_ZERO_ERROR;
-    UnicodeString unistr = UnicodeString::fromUTF8(utf8_regexp.c_str());
-    RegexPattern* pattern = RegexPattern::compile(unistr, 0, parse_error, status);
-
-    utf8_regexp_.reset(pattern);
+    utf8_regexp_.reset(RegexPattern::compile(
+        UnicodeString::fromUTF8(utf8_regexp.c_str()), 0, parse_error, status));
     if (U_FAILURE(status)) {
       // The provided regular expressions should compile correctly.
       LOG(ERROR) << "Error compiling regular expression: " << utf8_regexp;
Reply all
Reply to author
Forward
0 new messages