Browse Source

CPP: Make r570 compile in Chromium.

This CL ensures that:
- All declarations in headers are made in the i18n::phonenumbers namespace.
- All USE flags/macros are prefixed with I18N_PHONENUMBERS_ to avoid name
  clashes.
- All the code in base/ is actually used (by deleting unused code).
- Outdated occurrences of USE_GOOGLE_BASE don't exist anymore.
- Logging in PhoneNumberUtil is disabled by default (in production). However it
  can be enabled by calling PhoneNumberUtil::SetLogger() as it is now done
  during testing.

BUG=http://crbug.com/236272
R=jia.shao.peng@gmail.com

Review URL: https://codereview.appspot.com/9162043
pull/567/head
Philippe Liard 13 years ago
committed by Mihaela Rosca
parent
commit
09b71c10f3
21 changed files with 94 additions and 212 deletions
  1. +3
    -3
      cpp/CMakeLists.txt
  2. +13
    -94
      cpp/src/phonenumbers/base/basictypes.h
  3. +10
    -19
      cpp/src/phonenumbers/base/memory/scoped_ptr.h
  4. +5
    -4
      cpp/src/phonenumbers/base/thread_checker.h
  5. +0
    -5
      cpp/src/phonenumbers/default_logger.cc
  6. +3
    -26
      cpp/src/phonenumbers/default_logger.h
  7. +10
    -31
      cpp/src/phonenumbers/phonenumbermatcher.cc
  8. +4
    -5
      cpp/src/phonenumbers/phonenumberutil.cc
  9. +6
    -11
      cpp/src/phonenumbers/phonenumberutil.h
  10. +1
    -1
      cpp/src/phonenumbers/regexp_cache.cc
  11. +2
    -2
      cpp/src/phonenumbers/regexp_cache.h
  12. +7
    -7
      cpp/src/phonenumbers/regexp_factory.h
  13. +6
    -1
      cpp/src/phonenumbers/utf/unicodetext.cc
  14. +6
    -1
      cpp/src/phonenumbers/utf/unicodetext.h
  15. +4
    -0
      cpp/src/phonenumbers/utf/unilib.cc
  16. +4
    -0
      cpp/src/phonenumbers/utf/unilib.h
  17. +2
    -0
      cpp/test/phonenumbers/asyoutypeformatter_test.cc
  18. +2
    -0
      cpp/test/phonenumbers/phonenumbermatcher_test.cc
  19. +2
    -0
      cpp/test/phonenumbers/phonenumberutil_test.cc
  20. +2
    -0
      cpp/test/phonenumbers/shortnumberutil_test.cc
  21. +2
    -2
      cpp/test/phonenumbers/utf/unicodetext_test.cc

+ 3
- 3
cpp/CMakeLists.txt View File

@ -127,7 +127,7 @@ if (${USE_STD_MAP} STREQUAL "OFF")
INCLUDE (CheckIncludeFileCXX) INCLUDE (CheckIncludeFileCXX)
CHECK_INCLUDE_FILE_CXX ("tr1/unordered_map" HAVE_CXX_TR1_UNORDERED_MAP) CHECK_INCLUDE_FILE_CXX ("tr1/unordered_map" HAVE_CXX_TR1_UNORDERED_MAP)
if (HAVE_CXX_TR1_UNORDERED_MAP) if (HAVE_CXX_TR1_UNORDERED_MAP)
add_definitions ("-DUSE_TR1_UNORDERED_MAP")
add_definitions ("-DI18N_PHONENUMBERS_USE_TR1_UNORDERED_MAP")
endif () endif ()
endif () endif ()
@ -215,12 +215,12 @@ if (${USE_RE2} STREQUAL "ON")
# regexp_factory.h and regexp_adapter_test.cc. # regexp_factory.h and regexp_adapter_test.cc.
# When both ICU regexp and RE2 are defined, the regexp engine adapter defaults # When both ICU regexp and RE2 are defined, the regexp engine adapter defaults
# to RE2 unless the ICU implementation is instantiated explictly obviously. # to RE2 unless the ICU implementation is instantiated explictly obviously.
add_definitions (-DUSE_RE2)
add_definitions ("-DI18N_PHONENUMBERS_USE_RE2")
list (APPEND SOURCES "src/phonenumbers/regexp_adapter_re2.cc") list (APPEND SOURCES "src/phonenumbers/regexp_adapter_re2.cc")
endif () endif ()
if (${USE_ICU_REGEXP} STREQUAL "ON") if (${USE_ICU_REGEXP} STREQUAL "ON")
add_definitions (-DUSE_ICU_REGEXP)
add_definitions ("-DI18N_PHONENUMBERS_USE_ICU_REGEXP")
list (APPEND SOURCES "src/phonenumbers/regexp_adapter_icu.cc") list (APPEND SOURCES "src/phonenumbers/regexp_adapter_icu.cc")
# The phone number matcher needs ICU. # The phone number matcher needs ICU.
list (APPEND SOURCES "src/phonenumbers/alternate_format.cc") list (APPEND SOURCES "src/phonenumbers/alternate_format.cc")


+ 13
- 94
cpp/src/phonenumbers/base/basictypes.h View File

@ -4,7 +4,6 @@
#ifndef I18N_PHONENUMBERS_BASE_BASICTYPES_H_ #ifndef I18N_PHONENUMBERS_BASE_BASICTYPES_H_
#define I18N_PHONENUMBERS_BASE_BASICTYPES_H_ #define I18N_PHONENUMBERS_BASE_BASICTYPES_H_
#pragma once
#include <limits.h> // So we can set the bounds of our types #include <limits.h> // So we can set the bounds of our types
#include <stddef.h> // For size_t #include <stddef.h> // For size_t
@ -15,6 +14,9 @@
#include <stdint.h> // For intptr_t. #include <stdint.h> // For intptr_t.
#endif #endif
namespace i18n {
namespace phonenumbers {
#ifdef INT64_MAX #ifdef INT64_MAX
// INT64_MAX is defined if C99 stdint.h is included; use the // INT64_MAX is defined if C99 stdint.h is included; use the
@ -94,26 +96,11 @@ typedef signed int char32;
// A macro to disallow the copy constructor and operator= functions // A macro to disallow the copy constructor and operator= functions
// This should be used in the private: declarations for a class // This should be used in the private: declarations for a class
#if !defined(DISALLOW_COPY_AND_ASSIGN)
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \ #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
TypeName(const TypeName&); \ TypeName(const TypeName&); \
void operator=(const TypeName&) void operator=(const TypeName&)
// An older, deprecated, politically incorrect name for the above.
// NOTE: The usage of this macro was baned from our code base, but some
// third_party libraries are yet using it.
// TODO(tfarina): Figure out how to fix the usage of this macro in the
// third_party libraries and get rid of it.
#define DISALLOW_EVIL_CONSTRUCTORS(TypeName) DISALLOW_COPY_AND_ASSIGN(TypeName)
// A macro to disallow all the implicit constructors, namely the
// default constructor, copy constructor and operator= functions.
//
// This should be used in the private: declarations for a class
// that wants to prevent anyone from instantiating it. This is
// especially useful for classes containing only static methods.
#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
TypeName(); \
DISALLOW_COPY_AND_ASSIGN(TypeName)
#endif
// The arraysize(arr) macro returns the # of elements in an array arr. // The arraysize(arr) macro returns the # of elements in an array arr.
// The expression is a compile-time constant, and therefore can be // The expression is a compile-time constant, and therefore can be
@ -140,7 +127,9 @@ template <typename T, size_t N>
char (&ArraySizeHelper(const T (&array)[N]))[N]; char (&ArraySizeHelper(const T (&array)[N]))[N];
#endif #endif
#if !defined(arraysize)
#define arraysize(array) (sizeof(ArraySizeHelper(array))) #define arraysize(array) (sizeof(ArraySizeHelper(array)))
#endif
// ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize, // ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize,
// but can be used on anonymous types or types defined inside // but can be used on anonymous types or types defined inside
@ -179,32 +168,11 @@ char (&ArraySizeHelper(const T (&array)[N]))[N];
// where a pointer is 4 bytes, this means all pointers to a type whose // where a pointer is 4 bytes, this means all pointers to a type whose
// size is 3 or greater than 4 will be (righteously) rejected. // size is 3 or greater than 4 will be (righteously) rejected.
#if !defined(ARRAYSIZE_UNSAFE)
#define ARRAYSIZE_UNSAFE(a) \ #define ARRAYSIZE_UNSAFE(a) \
((sizeof(a) / sizeof(*(a))) / \ ((sizeof(a) / sizeof(*(a))) / \
static_cast<size_t>(!(sizeof(a) % sizeof(*(a))))) static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
// Use implicit_cast as a safe version of static_cast or const_cast
// for upcasting in the type hierarchy (i.e. casting a pointer to Foo
// to a pointer to SuperclassOfFoo or casting a pointer to Foo to
// a const pointer to Foo).
// When you use implicit_cast, the compiler checks that the cast is safe.
// Such explicit implicit_casts are necessary in surprisingly many
// situations where C++ demands an exact type match instead of an
// argument type convertable to a target type.
//
// The From type can be inferred, so the preferred syntax for using
// implicit_cast is the same as for static_cast etc.:
//
// implicit_cast<ToType>(expr)
//
// implicit_cast would have been part of the C++ standard library,
// but the proposal was submitted too late. It will probably make
// its way into the language in the future.
template<typename To, typename From>
inline To implicit_cast(From const &f) {
return f;
}
#endif
// The COMPILE_ASSERT macro can be used to verify that a compile time // The COMPILE_ASSERT macro can be used to verify that a compile time
// expression is true. For example, you could use it to verify the // expression is true. For example, you could use it to verify the
@ -225,61 +193,12 @@ template <bool>
struct CompileAssert { struct CompileAssert {
}; };
#undef COMPILE_ASSERT
#if !defined(COMPILE_ASSERT)
#define COMPILE_ASSERT(expr, msg) \ #define COMPILE_ASSERT(expr, msg) \
typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
#endif
// Implementation details of COMPILE_ASSERT:
//
// - COMPILE_ASSERT works by defining an array type that has -1
// elements (and thus is invalid) when the expression is false.
//
// - The simpler definition
//
// #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1]
//
// does not work, as gcc supports variable-length arrays whose sizes
// are determined at run-time (this is gcc's extension and not part
// of the C++ standard). As a result, gcc fails to reject the
// following code with the simple definition:
//
// int foo;
// COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is
// // not a compile-time constant.
//
// - By using the type CompileAssert<(bool(expr))>, we ensures that
// expr is a compile-time constant. (Template arguments must be
// determined at compile-time.)
//
// - The outter parentheses in CompileAssert<(bool(expr))> are necessary
// to work around a bug in gcc 3.4.4 and 4.0.1. If we had written
//
// CompileAssert<bool(expr)>
//
// instead, these compilers will refuse to compile
//
// COMPILE_ASSERT(5 > 0, some_message);
//
// (They seem to think the ">" in "5 > 0" marks the end of the
// template argument list.)
//
// - The array size is (bool(expr) ? 1 : -1), instead of simply
//
// ((expr) ? 1 : -1).
//
// This is to avoid running into a bug in MS VC 7.1, which
// causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
// Used to explicitly mark the return value of a function as unused. If you are
// really sure you don't want to do anything with the return value of a function
// that has been marked WARN_UNUSED_RESULT, wrap it with this. Example:
//
// scoped_ptr<MyType> my_var = ...;
// if (TakeOwnership(my_var.get()) == SUCCESS)
// ignore_result(my_var.release());
//
template<typename T>
inline void ignore_result(const T&) {
}
} // namespace phonenumbers
} // namespace i18n
#endif // I18N_PHONENUMBERS_BASE_BASICTYPES_H_ #endif // I18N_PHONENUMBERS_BASE_BASICTYPES_H_

+ 10
- 19
cpp/src/phonenumbers/base/memory/scoped_ptr.h View File

@ -93,8 +93,6 @@ struct FreeDeleter {
} }
}; };
namespace internal {
// Minimal implementation of the core logic of scoped_ptr, suitable for // Minimal implementation of the core logic of scoped_ptr, suitable for
// reuse in both scoped_ptr and its specializations. // reuse in both scoped_ptr and its specializations.
template <class T, class D> template <class T, class D>
@ -197,10 +195,6 @@ class scoped_ptr_impl {
DISALLOW_COPY_AND_ASSIGN(scoped_ptr_impl); DISALLOW_COPY_AND_ASSIGN(scoped_ptr_impl);
}; };
} // namespace internal
} // namespace phonenumbers
} // namespace i18n
// A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T> // A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T>
// automatically deletes the pointer it holds (if any). // automatically deletes the pointer it holds (if any).
// That is, scoped_ptr<T> owns the T object that it points to. // That is, scoped_ptr<T> owns the T object that it points to.
@ -217,7 +211,7 @@ class scoped_ptr_impl {
// unique_ptr<> features. Known deficiencies include not supporting move-only // unique_ptr<> features. Known deficiencies include not supporting move-only
// deleteres, function pointers as deleters, and deleters with reference // deleteres, function pointers as deleters, and deleters with reference
// types. // types.
template <class T, class D = i18n::phonenumbers::DefaultDeleter<T> >
template <class T, class D = DefaultDeleter<T> >
class scoped_ptr { class scoped_ptr {
public: public:
// The element and deleter types. // The element and deleter types.
@ -245,8 +239,7 @@ class scoped_ptr {
// implementation of scoped_ptr. // implementation of scoped_ptr.
template <typename U, typename V> template <typename U, typename V>
scoped_ptr(scoped_ptr<U, V> other) : impl_(&other.impl_) { scoped_ptr(scoped_ptr<U, V> other) : impl_(&other.impl_) {
COMPILE_ASSERT(!i18n::phonenumbers::is_array<U>::value,
U_cannot_be_an_array);
COMPILE_ASSERT(!is_array<U>::value, U_cannot_be_an_array);
} }
// operator=. Allows assignment from a scoped_ptr rvalue for a convertible // operator=. Allows assignment from a scoped_ptr rvalue for a convertible
@ -261,8 +254,7 @@ class scoped_ptr {
// scoped_ptr. // scoped_ptr.
template <typename U, typename V> template <typename U, typename V>
scoped_ptr& operator=(scoped_ptr<U, V> rhs) { scoped_ptr& operator=(scoped_ptr<U, V> rhs) {
COMPILE_ASSERT(!i18n::phonenumbers::is_array<U>::value,
U_cannot_be_an_array);
COMPILE_ASSERT(!is_array<U>::value, U_cannot_be_an_array);
impl_.TakeState(&rhs.impl_); impl_.TakeState(&rhs.impl_);
return *this; return *this;
} }
@ -290,8 +282,7 @@ class scoped_ptr {
// Allow scoped_ptr<element_type> to be used in boolean expressions, but not // Allow scoped_ptr<element_type> to be used in boolean expressions, but not
// implicitly convertible to a real bool (which is dangerous). // implicitly convertible to a real bool (which is dangerous).
private: private:
typedef i18n::phonenumbers::internal::scoped_ptr_impl<
element_type, deleter_type> scoped_ptr::*Testable;
typedef scoped_ptr_impl<element_type, deleter_type> scoped_ptr::*Testable;
public: public:
operator Testable() const { return impl_.get() ? &scoped_ptr::impl_ : NULL; } operator Testable() const { return impl_.get() ? &scoped_ptr::impl_ : NULL; }
@ -319,8 +310,7 @@ class scoped_ptr {
private: private:
// Needed to reach into |impl_| in the constructor. // Needed to reach into |impl_| in the constructor.
template <typename U, typename V> friend class scoped_ptr; template <typename U, typename V> friend class scoped_ptr;
i18n::phonenumbers::internal::scoped_ptr_impl<
element_type, deleter_type> impl_;
scoped_ptr_impl<element_type, deleter_type> impl_;
// Forbid comparison of scoped_ptr types. If U != T, it totally // Forbid comparison of scoped_ptr types. If U != T, it totally
// doesn't make sense, and if U == T, it still doesn't make sense // doesn't make sense, and if U == T, it still doesn't make sense
@ -376,8 +366,7 @@ class scoped_ptr<T[], D> {
// Allow scoped_ptr<element_type> to be used in boolean expressions, but not // Allow scoped_ptr<element_type> to be used in boolean expressions, but not
// implicitly convertible to a real bool (which is dangerous). // implicitly convertible to a real bool (which is dangerous).
private: private:
typedef i18n::phonenumbers::internal::scoped_ptr_impl<
element_type, deleter_type> scoped_ptr::*Testable;
typedef scoped_ptr_impl<element_type, deleter_type> scoped_ptr::*Testable;
public: public:
operator Testable() const { return impl_.get() ? &scoped_ptr::impl_ : NULL; } operator Testable() const { return impl_.get() ? &scoped_ptr::impl_ : NULL; }
@ -407,8 +396,7 @@ class scoped_ptr<T[], D> {
enum { type_must_be_complete = sizeof(element_type) }; enum { type_must_be_complete = sizeof(element_type) };
// Actually hold the data. // Actually hold the data.
i18n::phonenumbers::internal::scoped_ptr_impl<
element_type, deleter_type> impl_;
scoped_ptr_impl<element_type, deleter_type> impl_;
// Disable initialization from any type other than element_type*, by // Disable initialization from any type other than element_type*, by
// providing a constructor that matches such an initialization, but is // providing a constructor that matches such an initialization, but is
@ -455,5 +443,8 @@ scoped_ptr<T> make_scoped_ptr(T* ptr) {
return scoped_ptr<T>(ptr); return scoped_ptr<T>(ptr);
} }
} // namespace phonenumbers
} // namespace i18n
#endif // !I18N_PHONENUMBERS_USE_BOOST #endif // !I18N_PHONENUMBERS_USE_BOOST
#endif // I18N_PHONENUMBERS_BASE_MEMORY_SCOPED_PTR_H_ #endif // I18N_PHONENUMBERS_BASE_MEMORY_SCOPED_PTR_H_

+ 5
- 4
cpp/src/phonenumbers/base/thread_checker.h View File

@ -14,8 +14,8 @@
// Author: Philippe Liard // Author: Philippe Liard
#ifndef I18N_PHONENUMBERS_BASE_THREAD_SAFETY_CHECK_H_
#define I18N_PHONENUMBERS_BASE_THREAD_SAFETY_CHECK_H_
#ifndef I18N_PHONENUMBERS_BASE_THREAD_CHECKER_H_
#define I18N_PHONENUMBERS_BASE_THREAD_CHECKER_H_
#if !defined(I18N_PHONENUMBERS_USE_BOOST) #if !defined(I18N_PHONENUMBERS_USE_BOOST)
@ -23,7 +23,8 @@
// user of the library know that it can't be used in a thread-safe manner when // user of the library know that it can't be used in a thread-safe manner when
// it is not depending on Boost. // it is not depending on Boost.
#if !defined(I18N_PHONENUMBERS_NO_THREAD_SAFETY) #if !defined(I18N_PHONENUMBERS_NO_THREAD_SAFETY)
#error "Building without Boost, please provide -DNO_THREAD_SAFETY"
#error Building without Boost, please provide \
-DI18N_PHONENUMBERS_NO_THREAD_SAFETY
#endif #endif
#endif #endif
@ -65,4 +66,4 @@ class ThreadChecker {
} // namespace phonenumbers } // namespace phonenumbers
} // namespace i18n } // namespace i18n
#endif // I18N_PHONENUMBERS_BASE_THREAD_SAFETY_CHECK_H_
#endif // I18N_PHONENUMBERS_BASE_THREAD_CHECKER_H_

+ 0
- 5
cpp/src/phonenumbers/default_logger.cc View File

@ -14,9 +14,6 @@
// Author: Philippe Liard // Author: Philippe Liard
// This file should not be compiled when using Google base/.
#ifndef USE_GOOGLE_BASE
#include <iostream> #include <iostream>
#include "phonenumbers/default_logger.h" #include "phonenumbers/default_logger.h"
@ -50,5 +47,3 @@ void StdoutLogger::WriteLevel() {
} // namespace phonenumbers } // namespace phonenumbers
} // namespace i18n } // namespace i18n
#endif // USE_GOOGLE_BASE

+ 3
- 26
cpp/src/phonenumbers/default_logger.h View File

@ -19,33 +19,16 @@
#include "phonenumbers/logger.h" #include "phonenumbers/logger.h"
#ifdef USE_GOOGLE_BASE
#include <sstream>
#include <string>
namespace i18n { namespace i18n {
namespace phonenumbers { namespace phonenumbers {
// If Google base/ is used, LOG() and VLOG() from base/logging.h are used
// therefore the default logger implementation (StdoutLogger) instantiated in
// phonenumberutil will actually never be used.
typedef NullLogger StdoutLogger;
} // namespace phonenumbers
} // namespace i18n
#else
#include <sstream>
#include <string>
using i18n::phonenumbers::Logger;
using std::string; using std::string;
using std::stringstream; using std::stringstream;
// Make the logging functions private (not declared in logger.h) as the client
// should not have any reason to use them.
namespace {
using i18n::phonenumbers::Logger;
// Class template used to inline the right implementation for the T -> string // Class template used to inline the right implementation for the T -> string
// conversion. // conversion.
template <typename T> template <typename T>
@ -91,11 +74,6 @@ class LoggerHandler {
Logger* const impl_; Logger* const impl_;
}; };
} // namespace
namespace i18n {
namespace phonenumbers {
inline LoggerHandler VLOG(int n) { inline LoggerHandler VLOG(int n) {
Logger* const logger_impl = Logger::mutable_logger_impl(); Logger* const logger_impl = Logger::mutable_logger_impl();
if (logger_impl->level() < n) { if (logger_impl->level() < n) {
@ -122,5 +100,4 @@ class StdoutLogger : public Logger {
} // namespace phonenumbers } // namespace phonenumbers
} // namespace i18n } // namespace i18n
#endif // USE_GOOGLE_BASE
#endif // I18N_PHONENUMBERS_DEFAULT_LOGGER_H_ #endif // I18N_PHONENUMBERS_DEFAULT_LOGGER_H_

+ 10
- 31
cpp/src/phonenumbers/phonenumbermatcher.cc View File

@ -20,9 +20,10 @@
#include "phonenumbers/phonenumbermatcher.h" #include "phonenumbers/phonenumbermatcher.h"
#ifndef USE_ICU_REGEXP
#error phonenumbermatcher depends on ICU (i.e. USE_ICU_REGEXP must be set)
#endif // USE_ICU_REGEXP
#ifndef I18N_PHONENUMBERS_USE_ICU_REGEXP
#error phonenumbermatcher depends on ICU \
(i.e. I18N_PHONENUMBERS_USE_ICU_REGEXP must be set)
#endif // I18N_PHONENUMBERS_USE_ICU_REGEXP
#include <ctype.h> #include <ctype.h>
#include <iostream> #include <iostream>
@ -51,9 +52,9 @@
#include "phonenumbers/regexp_adapter_icu.h" #include "phonenumbers/regexp_adapter_icu.h"
#include "phonenumbers/stringutil.h" #include "phonenumbers/stringutil.h"
#ifdef USE_RE2
#ifdef I18N_PHONENUMBERS_USE_RE2
#include "phonenumbers/regexp_adapter_re2.h" #include "phonenumbers/regexp_adapter_re2.h"
#endif // USE_RE2_AND_ICU
#endif // I18N_PHONENUMBERS_USE_RE2_AND_ICU
using std::cerr; using std::cerr;
using std::endl; using std::endl;
@ -168,14 +169,10 @@ bool LoadAlternateFormats(PhoneMetadataCollection* alternate_formats) {
} }
} // namespace } // namespace
#ifdef USE_GOOGLE_BASE
class PhoneNumberMatcherRegExps {
friend struct DefaultSingletonTraits<PhoneNumberMatcherRegExps>;
#else
class PhoneNumberMatcherRegExps : public Singleton<PhoneNumberMatcherRegExps> { class PhoneNumberMatcherRegExps : public Singleton<PhoneNumberMatcherRegExps> {
friend class Singleton<PhoneNumberMatcherRegExps>;
#endif // USE_GOOGLE_BASE
private: private:
friend class Singleton<PhoneNumberMatcherRegExps>;
string opening_parens_; string opening_parens_;
string closing_parens_; string closing_parens_;
string non_parens_; string non_parens_;
@ -248,12 +245,6 @@ class PhoneNumberMatcherRegExps : public Singleton<PhoneNumberMatcherRegExps> {
// Phone number pattern allowing optional punctuation. // Phone number pattern allowing optional punctuation.
scoped_ptr<const RegExp> pattern_; scoped_ptr<const RegExp> pattern_;
#ifdef USE_GOOGLE_BASE
static PhoneNumberMatcherRegExps* GetInstance() {
return Singleton<PhoneNumberMatcherRegExps>::get();
}
#endif // USE_GOOGLE_BASE
PhoneNumberMatcherRegExps() PhoneNumberMatcherRegExps()
: opening_parens_("(\\[\xEF\xBC\x88\xEF\xBC\xBB" /* "(\\[([" */), : opening_parens_("(\\[\xEF\xBC\x88\xEF\xBC\xBB" /* "(\\[([" */),
closing_parens_(")\\]\xEF\xBC\x89\xEF\xBC\xBD" /* ")\\])]" */), closing_parens_(")\\]\xEF\xBC\x89\xEF\xBC\xBD" /* ")\\])]" */),
@ -281,11 +272,11 @@ class PhoneNumberMatcherRegExps : public Singleton<PhoneNumberMatcherRegExps> {
PhoneNumberUtil::GetInstance()->GetExtnPatternsForMatching(), PhoneNumberUtil::GetInstance()->GetExtnPatternsForMatching(),
")?")), ")?")),
regexp_factory_for_pattern_(new ICURegExpFactory()), regexp_factory_for_pattern_(new ICURegExpFactory()),
#ifdef USE_RE2
#ifdef I18N_PHONENUMBERS_USE_RE2
regexp_factory_(new RE2RegExpFactory()), regexp_factory_(new RE2RegExpFactory()),
#else #else
regexp_factory_(new ICURegExpFactory()), regexp_factory_(new ICURegExpFactory()),
#endif // USE_RE2
#endif // I18N_PHONENUMBERS_USE_RE2
pub_pages_(regexp_factory_->CreateRegExp( pub_pages_(regexp_factory_->CreateRegExp(
"\\d{1,5}-+\\d{1,5}\\s{0,4}\\(\\d{1,4}")), "\\d{1,5}-+\\d{1,5}\\s{0,4}\\(\\d{1,4}")),
slash_separated_dates_(regexp_factory_->CreateRegExp( slash_separated_dates_(regexp_factory_->CreateRegExp(
@ -315,24 +306,12 @@ class PhoneNumberMatcherRegExps : public Singleton<PhoneNumberMatcherRegExps> {
DISALLOW_COPY_AND_ASSIGN(PhoneNumberMatcherRegExps); DISALLOW_COPY_AND_ASSIGN(PhoneNumberMatcherRegExps);
}; };
#ifdef USE_GOOGLE_BASE
class AlternateFormats {
friend struct DefaultSingletonTraits<AlternateFormats>;
#else
class AlternateFormats : public Singleton<AlternateFormats> { class AlternateFormats : public Singleton<AlternateFormats> {
friend class Singleton<AlternateFormats>;
#endif // USE_GOOGLE_BASE
public: public:
PhoneMetadataCollection format_data_; PhoneMetadataCollection format_data_;
map<int, const PhoneMetadata*> calling_code_to_alternate_formats_map_; map<int, const PhoneMetadata*> calling_code_to_alternate_formats_map_;
#ifdef USE_GOOGLE_BASE
static AlternateFormats* GetInstance() {
return Singleton<AlternateFormats>::get();
}
#endif // USE_GOOGLE_BASE
AlternateFormats() AlternateFormats()
: format_data_(), : format_data_(),
calling_code_to_alternate_formats_map_() { calling_code_to_alternate_formats_map_() {


+ 4
- 5
cpp/src/phonenumbers/phonenumberutil.cc View File

@ -369,7 +369,8 @@ PhoneNumberUtil::ValidationResult TestNumberLengthAgainstPattern(
} // namespace } // namespace
void PhoneNumberUtil::SetLogger(Logger* logger) { void PhoneNumberUtil::SetLogger(Logger* logger) {
Logger::set_logger_impl(logger);
logger_.reset(logger);
Logger::set_logger_impl(logger_.get());
} }
class PhoneNumberRegExpsAndMappings { class PhoneNumberRegExpsAndMappings {
@ -657,7 +658,7 @@ class PhoneNumberRegExpsAndMappings {
// Private constructor. Also takes care of initialisation. // Private constructor. Also takes care of initialisation.
PhoneNumberUtil::PhoneNumberUtil() PhoneNumberUtil::PhoneNumberUtil()
: logger_(Logger::set_logger_impl(new StdoutLogger())),
: logger_(Logger::set_logger_impl(new NullLogger())),
reg_exps_(new PhoneNumberRegExpsAndMappings), reg_exps_(new PhoneNumberRegExpsAndMappings),
country_calling_code_to_region_code_map_(new vector<IntRegionsPair>()), country_calling_code_to_region_code_map_(new vector<IntRegionsPair>()),
nanpa_regions_(new set<string>()), nanpa_regions_(new set<string>()),
@ -739,11 +740,9 @@ void PhoneNumberUtil::GetSupportedRegions(set<string>* regions) const {
// Public wrapper function to get a PhoneNumberUtil instance with the default // Public wrapper function to get a PhoneNumberUtil instance with the default
// metadata file. // metadata file.
// static // static
#ifdef USE_GOOGLE_BASE
PhoneNumberUtil* PhoneNumberUtil::GetInstance() { PhoneNumberUtil* PhoneNumberUtil::GetInstance() {
return Singleton<PhoneNumberUtil>::get();
return Singleton<PhoneNumberUtil>::GetInstance();
} }
#endif
const string& PhoneNumberUtil::GetExtnPatternsForMatching() const { const string& PhoneNumberUtil::GetExtnPatternsForMatching() const {
return reg_exps_->extn_patterns_for_matching_; return reg_exps_->extn_patterns_for_matching_;


+ 6
- 11
cpp/src/phonenumbers/phonenumberutil.h View File

@ -59,13 +59,8 @@ class RegExp;
// codes can be found here: // codes can be found here:
// http://www.iso.org/iso/english_country_names_and_code_elements // http://www.iso.org/iso/english_country_names_and_code_elements
#ifdef USE_GOOGLE_BASE
class PhoneNumberUtil {
friend struct DefaultSingletonTraits<PhoneNumberUtil>;
#else
class PhoneNumberUtil : public Singleton<PhoneNumberUtil> { class PhoneNumberUtil : public Singleton<PhoneNumberUtil> {
friend class Singleton<PhoneNumberUtil>;
#endif
private:
friend class AsYouTypeFormatter; friend class AsYouTypeFormatter;
friend class PhoneNumberMatcher; friend class PhoneNumberMatcher;
friend class PhoneNumberMatcherRegExps; friend class PhoneNumberMatcherRegExps;
@ -74,6 +69,8 @@ class PhoneNumberUtil : public Singleton<PhoneNumberUtil> {
friend class PhoneNumberUtilTest; friend class PhoneNumberUtilTest;
friend class ShortNumberUtil; friend class ShortNumberUtil;
friend class ShortNumberUtilTest; friend class ShortNumberUtilTest;
friend class Singleton<PhoneNumberUtil>;
public: public:
~PhoneNumberUtil(); ~PhoneNumberUtil();
static const char kRegionCodeForNonGeoEntity[]; static const char kRegionCodeForNonGeoEntity[];
@ -172,9 +169,7 @@ class PhoneNumberUtil : public Singleton<PhoneNumberUtil> {
// //
// The PhoneNumberUtil is implemented as a singleton. Therefore, calling // The PhoneNumberUtil is implemented as a singleton. Therefore, calling
// GetInstance multiple times will only result in one instance being created. // GetInstance multiple times will only result in one instance being created.
#ifdef USE_GOOGLE_BASE
static PhoneNumberUtil* GetInstance(); static PhoneNumberUtil* GetInstance();
#endif
// Returns true if the number is a valid vanity (alpha) number such as 800 // Returns true if the number is a valid vanity (alpha) number such as 800
// MICROSOFT. A valid vanity number will start with at least 3 digits and will // MICROSOFT. A valid vanity number will start with at least 3 digits and will
@ -552,9 +547,9 @@ class PhoneNumberUtil : public Singleton<PhoneNumberUtil> {
MatchType IsNumberMatchWithOneString(const PhoneNumber& first_number, MatchType IsNumberMatchWithOneString(const PhoneNumber& first_number,
const string& second_number) const; const string& second_number) const;
// Overrides the default logging system. The provided logger destruction is
// handled by this class (i.e don't delete it).
static void SetLogger(Logger* logger);
// Overrides the default logging system. This takes ownership of the provided
// logger.
void SetLogger(Logger* logger);
// Gets an AsYouTypeFormatter for the specific region. // Gets an AsYouTypeFormatter for the specific region.
// Returns an AsYouTypeFormatter object, which could be used to format phone // Returns an AsYouTypeFormatter object, which could be used to format phone


+ 1
- 1
cpp/src/phonenumbers/regexp_cache.cc View File

@ -31,7 +31,7 @@ namespace phonenumbers {
RegExpCache::RegExpCache(const AbstractRegExpFactory& regexp_factory, RegExpCache::RegExpCache(const AbstractRegExpFactory& regexp_factory,
size_t min_items) size_t min_items)
: regexp_factory_(regexp_factory), : regexp_factory_(regexp_factory),
#ifdef USE_TR1_UNORDERED_MAP
#ifdef I18N_PHONENUMBERS_USE_TR1_UNORDERED_MAP
cache_impl_(new CacheImpl(min_items)) cache_impl_(new CacheImpl(min_items))
#else #else
cache_impl_(new CacheImpl()) cache_impl_(new CacheImpl())


+ 2
- 2
cpp/src/phonenumbers/regexp_cache.h View File

@ -34,7 +34,7 @@
#include "phonenumbers/base/memory/scoped_ptr.h" #include "phonenumbers/base/memory/scoped_ptr.h"
#include "phonenumbers/base/synchronization/lock.h" #include "phonenumbers/base/synchronization/lock.h"
#ifdef USE_TR1_UNORDERED_MAP
#ifdef I18N_PHONENUMBERS_USE_TR1_UNORDERED_MAP
# include <tr1/unordered_map> # include <tr1/unordered_map>
#else #else
# include <map> # include <map>
@ -50,7 +50,7 @@ class RegExp;
class RegExpCache { class RegExpCache {
private: private:
#ifdef USE_TR1_UNORDERED_MAP
#ifdef I18N_PHONENUMBERS_USE_TR1_UNORDERED_MAP
typedef std::tr1::unordered_map<string, const RegExp*> CacheImpl; typedef std::tr1::unordered_map<string, const RegExp*> CacheImpl;
#else #else
typedef std::map<string, const RegExp*> CacheImpl; typedef std::map<string, const RegExp*> CacheImpl;


+ 7
- 7
cpp/src/phonenumbers/regexp_factory.h View File

@ -18,25 +18,25 @@
#define I18N_PHONENUMBERS_REGEXP_ADAPTER_FACTORY_H_ #define I18N_PHONENUMBERS_REGEXP_ADAPTER_FACTORY_H_
// This file selects the right implementation of the abstract regexp factory at // This file selects the right implementation of the abstract regexp factory at
// compile time depending on the compilation flags (USE_RE2). The default
// abstract regexp factory implementation can be obtained using the type
// RegExpFactory. This will be set to RE2RegExpFactory if RE2 is used or
// compile time depending on the compilation flags (I18N_PHONENUMBERS_USE_RE2).
// The default abstract regexp factory implementation can be obtained using the
// type RegExpFactory. This will be set to RE2RegExpFactory if RE2 is used or
// ICURegExpFactory otherwise. // ICURegExpFactory otherwise.
#ifdef USE_RE2
#ifdef I18N_PHONENUMBERS_USE_RE2
#include "phonenumbers/regexp_adapter_re2.h" #include "phonenumbers/regexp_adapter_re2.h"
#else #else
#include "phonenumbers/regexp_adapter_icu.h" #include "phonenumbers/regexp_adapter_icu.h"
#endif // USE_RE2
#endif // I18N_PHONENUMBERS_USE_RE2
namespace i18n { namespace i18n {
namespace phonenumbers { namespace phonenumbers {
#ifdef USE_RE2
#ifdef I18N_PHONENUMBERS_USE_RE2
typedef RE2RegExpFactory RegExpFactory; typedef RE2RegExpFactory RegExpFactory;
#else #else
typedef ICURegExpFactory RegExpFactory; typedef ICURegExpFactory RegExpFactory;
#endif // USE_RE2
#endif // I18N_PHONENUMBERS_USE_RE2
} // namespace phonenumbers } // namespace phonenumbers
} // namespace i18n } // namespace i18n


+ 6
- 1
cpp/src/phonenumbers/utf/unicodetext.cc View File

@ -19,12 +19,14 @@
#include <cassert> #include <cassert>
#include "phonenumbers/utf/unicodetext.h" #include "phonenumbers/utf/unicodetext.h"
//#include "phonenumbers/base/logging.h"
#include "phonenumbers/utf/stringpiece.h" #include "phonenumbers/utf/stringpiece.h"
//#include "utf/stringprintf.h" //#include "utf/stringprintf.h"
#include "phonenumbers/utf/utf.h" #include "phonenumbers/utf/utf.h"
#include "phonenumbers/utf/unilib.h" #include "phonenumbers/utf/unilib.h"
namespace i18n {
namespace phonenumbers {
using std::stringstream; using std::stringstream;
using std::max; using std::max;
using std::hex; using std::hex;
@ -512,3 +514,6 @@ string UnicodeText::const_iterator::DebugString() const {
return result; return result;
} }
} // namespace phonenumbers
} // namespace i18n

+ 6
- 1
cpp/src/phonenumbers/utf/unicodetext.h View File

@ -21,7 +21,9 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include "phonenumbers/base/basictypes.h" #include "phonenumbers/base/basictypes.h"
//#include "util/utf8/public/config.h"
namespace i18n {
namespace phonenumbers {
using std::string; using std::string;
using std::bidirectional_iterator_tag; using std::bidirectional_iterator_tag;
@ -453,4 +455,7 @@ inline string UnicodeTextToUTF8(const UnicodeText& t) {
return string(t.utf8_data(), t.utf8_length()); return string(t.utf8_data(), t.utf8_length());
} }
} // namespace phonenumbers
} // namespace i18n
#endif // UTIL_UTF8_UNICODETEXT_H__ #endif // UTIL_UTF8_UNICODETEXT_H__

+ 4
- 0
cpp/src/phonenumbers/utf/unilib.cc View File

@ -21,6 +21,8 @@
#include "phonenumbers/base/basictypes.h" #include "phonenumbers/base/basictypes.h"
#include "phonenumbers/utf/utf.h" #include "phonenumbers/utf/utf.h"
namespace i18n {
namespace phonenumbers {
namespace UniLib { namespace UniLib {
namespace { namespace {
@ -62,3 +64,5 @@ int SpanInterchangeValid(const char* begin, int byte_length) {
} }
} // namespace UniLib } // namespace UniLib
} // namespace phonenumbers
} // namespace i18n

+ 4
- 0
cpp/src/phonenumbers/utf/unilib.h View File

@ -34,6 +34,8 @@
#include <string> #include <string>
#include "phonenumbers/base/basictypes.h" #include "phonenumbers/base/basictypes.h"
namespace i18n {
namespace phonenumbers {
namespace UniLib { namespace UniLib {
// Returns true unless a surrogate code point // Returns true unless a surrogate code point
@ -91,5 +93,7 @@ inline bool IsInterchangeValid(const std::string& src) {
} }
} // namespace UniLib } // namespace UniLib
} // namespace phonenumbers
} // namespace i18n
#endif // UTIL_UTF8_PUBLIC_UNILIB_H_ #endif // UTIL_UTF8_PUBLIC_UNILIB_H_

+ 2
- 0
cpp/test/phonenumbers/asyoutypeformatter_test.cc View File

@ -24,6 +24,7 @@
#include "phonenumbers/base/logging.h" #include "phonenumbers/base/logging.h"
#include "phonenumbers/base/memory/scoped_ptr.h" #include "phonenumbers/base/memory/scoped_ptr.h"
#include "phonenumbers/default_logger.h"
#include "phonenumbers/phonenumberutil.h" #include "phonenumbers/phonenumberutil.h"
#include "phonenumbers/test_util.h" #include "phonenumbers/test_util.h"
@ -35,6 +36,7 @@ class PhoneMetadata;
class AsYouTypeFormatterTest : public testing::Test { class AsYouTypeFormatterTest : public testing::Test {
protected: protected:
AsYouTypeFormatterTest() : phone_util_(*PhoneNumberUtil::GetInstance()) { AsYouTypeFormatterTest() : phone_util_(*PhoneNumberUtil::GetInstance()) {
PhoneNumberUtil::GetInstance()->SetLogger(new StdoutLogger());
} }
const PhoneMetadata* GetCurrentMetadata() const { const PhoneMetadata* GetCurrentMetadata() const {


+ 2
- 0
cpp/test/phonenumbers/phonenumbermatcher_test.cc View File

@ -25,6 +25,7 @@
#include "phonenumbers/base/basictypes.h" #include "phonenumbers/base/basictypes.h"
#include "phonenumbers/base/memory/scoped_ptr.h" #include "phonenumbers/base/memory/scoped_ptr.h"
#include "phonenumbers/base/memory/singleton.h" #include "phonenumbers/base/memory/singleton.h"
#include "phonenumbers/default_logger.h"
#include "phonenumbers/phonenumber.h" #include "phonenumbers/phonenumber.h"
#include "phonenumbers/phonenumber.pb.h" #include "phonenumbers/phonenumber.pb.h"
#include "phonenumbers/phonenumbermatch.h" #include "phonenumbers/phonenumbermatch.h"
@ -77,6 +78,7 @@ class PhoneNumberMatcherTest : public testing::Test {
RegionCode::US(), RegionCode::US(),
PhoneNumberMatcher::VALID, 5), PhoneNumberMatcher::VALID, 5),
offset_(0) { offset_(0) {
PhoneNumberUtil::GetInstance()->SetLogger(new StdoutLogger());
} }
bool IsLatinLetter(char32 letter) { bool IsLatinLetter(char32 letter) {


+ 2
- 0
cpp/test/phonenumbers/phonenumberutil_test.cc View File

@ -30,6 +30,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "phonenumbers/default_logger.h"
#include "phonenumbers/phonemetadata.pb.h" #include "phonenumbers/phonemetadata.pb.h"
#include "phonenumbers/phonenumber.h" #include "phonenumbers/phonenumber.h"
#include "phonenumbers/phonenumber.pb.h" #include "phonenumbers/phonenumber.pb.h"
@ -50,6 +51,7 @@ static const int kInvalidCountryCode = 2;
class PhoneNumberUtilTest : public testing::Test { class PhoneNumberUtilTest : public testing::Test {
protected: protected:
PhoneNumberUtilTest() : phone_util_(*PhoneNumberUtil::GetInstance()) { PhoneNumberUtilTest() : phone_util_(*PhoneNumberUtil::GetInstance()) {
PhoneNumberUtil::GetInstance()->SetLogger(new StdoutLogger());
} }
// Wrapper functions for private functions that we want to test. // Wrapper functions for private functions that we want to test.


+ 2
- 0
cpp/test/phonenumbers/shortnumberutil_test.cc View File

@ -16,6 +16,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "phonenumbers/default_logger.h"
#include "phonenumbers/phonenumberutil.h" #include "phonenumbers/phonenumberutil.h"
#include "phonenumbers/shortnumberutil.h" #include "phonenumbers/shortnumberutil.h"
#include "phonenumbers/test_util.h" #include "phonenumbers/test_util.h"
@ -26,6 +27,7 @@ namespace phonenumbers {
class ShortNumberUtilTest : public testing::Test { class ShortNumberUtilTest : public testing::Test {
protected: protected:
ShortNumberUtilTest() : short_util_() { ShortNumberUtilTest() : short_util_() {
PhoneNumberUtil::GetInstance()->SetLogger(new StdoutLogger());
} }
const ShortNumberUtil short_util_; const ShortNumberUtil short_util_;


+ 2
- 2
cpp/test/phonenumbers/utf/unicodetext_test.cc View File

@ -19,7 +19,7 @@
#include "phonenumbers/utf/unicodetext.h" #include "phonenumbers/utf/unicodetext.h"
namespace i18n { namespace i18n {
namespace unicodetext {
namespace phonenumbers {
TEST(UnicodeTextTest, Iterator) { TEST(UnicodeTextTest, Iterator) {
struct value { struct value {
@ -41,5 +41,5 @@ TEST(UnicodeTextTest, Iterator) {
} }
} }
} // namespace unicodetext
} // namespace phonenumbers
} // namespace i18n } // namespace i18n

Loading…
Cancel
Save