diff options
author | philip.liard@gmail.com <philip.liard@gmail.com@ee073f10-1060-11df-b6a4-87a95322a99c> | 2013-04-30 11:35:55 +0000 |
---|---|---|
committer | philip.liard@gmail.com <philip.liard@gmail.com@ee073f10-1060-11df-b6a4-87a95322a99c> | 2013-04-30 11:35:55 +0000 |
commit | af4a2ce290b619b39c2cb2a682ea4d7746d3fb21 (patch) | |
tree | bdd65747d74debf0d0ebe65072b2a188f5e9cde5 | |
parent | 711fdcd8f9ebfcdcb7a8bb09a39b142be7eaa611 (diff) | |
download | phonenumbers-af4a2ce290b619b39c2cb2a682ea4d7746d3fb21.tar.gz |
CPP: Move base/ to phonenumbers/.
This will be needed to make libphonenumber depend on its own base/ rather than
Chromium's one as it is the case currently.
BUG=http://crbug.com/236272
R=jia.shao.peng@gmail.com
Review URL: https://codereview.appspot.com/8797048
git-svn-id: http://libphonenumber.googlecode.com/svn/trunk/cpp/src/phonenumbers@567 ee073f10-1060-11df-b6a4-87a95322a99c
34 files changed, 898 insertions, 45 deletions
diff --git a/asyoutypeformatter.cc b/asyoutypeformatter.cc index beef03b..e80a04e 100644 --- a/asyoutypeformatter.cc +++ b/asyoutypeformatter.cc @@ -20,8 +20,8 @@ #include <google/protobuf/message_lite.h> -#include "base/logging.h" -#include "base/memory/scoped_ptr.h" +#include "phonenumbers/base/logging.h" +#include "phonenumbers/base/memory/scoped_ptr.h" #include "phonenumbers/phonemetadata.pb.h" #include "phonenumbers/phonenumberutil.h" #include "phonenumbers/regexp_cache.h" diff --git a/asyoutypeformatter.h b/asyoutypeformatter.h index 690bb9c..e1f850b 100644 --- a/asyoutypeformatter.h +++ b/asyoutypeformatter.h @@ -36,8 +36,8 @@ #include <list> #include <string> -#include "base/basictypes.h" -#include "base/memory/scoped_ptr.h" +#include "phonenumbers/base/basictypes.h" +#include "phonenumbers/base/memory/scoped_ptr.h" #include "phonenumbers/regexp_adapter.h" #include "phonenumbers/regexp_cache.h" #include "phonenumbers/phonemetadata.pb.h" diff --git a/base/basictypes.h b/base/basictypes.h new file mode 100644 index 0000000..4a9d693 --- /dev/null +++ b/base/basictypes.h @@ -0,0 +1,302 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef 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 <stddef.h> // For size_t +#include <string.h> // for memcpy + +#ifndef COMPILER_MSVC +// stdint.h is part of C99 but MSVC doesn't have it. +#include <stdint.h> // For intptr_t. +#endif + +#ifdef INT64_MAX + +// INT64_MAX is defined if C99 stdint.h is included; use the +// native types if available. +typedef int8_t int8; +typedef int16_t int16; +typedef int32_t int32; +typedef int64_t int64; +typedef uint8_t uint8; +typedef uint16_t uint16; +typedef uint32_t uint32; +typedef uint64_t uint64; + +const uint8 kuint8max = UINT8_MAX; +const uint16 kuint16max = UINT16_MAX; +const uint32 kuint32max = UINT32_MAX; +const uint64 kuint64max = UINT64_MAX; +const int8 kint8min = INT8_MIN; +const int8 kint8max = INT8_MAX; +const int16 kint16min = INT16_MIN; +const int16 kint16max = INT16_MAX; +const int32 kint32min = INT32_MIN; +const int32 kint32max = INT32_MAX; +const int64 kint64min = INT64_MIN; +const int64 kint64max = INT64_MAX; + +#else // !INT64_MAX + +typedef signed char int8; +typedef short int16; +// TODO: Remove these type guards. These are to avoid conflicts with +// obsolete/protypes.h in the Gecko SDK. +#ifndef _INT32 +#define _INT32 +typedef int int32; +#endif + +// The NSPR system headers define 64-bit as |long| when possible. In order to +// not have typedef mismatches, we do the same on LP64. +#if __LP64__ +typedef long int64; +#else +typedef long long int64; +#endif + +// NOTE: unsigned types are DANGEROUS in loops and other arithmetical +// places. Use the signed types unless your variable represents a bit +// pattern (eg a hash value) or you really need the extra bit. Do NOT +// use 'unsigned' to express "this value should always be positive"; +// use assertions for this. + +typedef unsigned char uint8; +typedef unsigned short uint16; +// TODO: Remove these type guards. These are to avoid conflicts with +// obsolete/protypes.h in the Gecko SDK. +#ifndef _UINT32 +#define _UINT32 +typedef unsigned int uint32; +#endif + +// See the comment above about NSPR and 64-bit. +#if __LP64__ +typedef unsigned long uint64; +#else +typedef unsigned long long uint64; +#endif + +#endif // !INT64_MAX + +typedef signed char schar; + +// A type to represent a Unicode code-point value. As of Unicode 4.0, +// such values require up to 21 bits. +// (For type-checking on pointers, make this explicitly signed, +// and it should always be the signed version of whatever int32 is.) +typedef signed int char32; + +// A macro to disallow the copy constructor and operator= functions +// This should be used in the private: declarations for a class +#define DISALLOW_COPY_AND_ASSIGN(TypeName) \ + TypeName(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) + +// The arraysize(arr) macro returns the # of elements in an array arr. +// The expression is a compile-time constant, and therefore can be +// used in defining new arrays, for example. If you use arraysize on +// a pointer by mistake, you will get a compile-time error. +// +// One caveat is that arraysize() doesn't accept any array of an +// anonymous type or a type defined inside a function. In these rare +// cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is +// due to a limitation in C++'s template system. The limitation might +// eventually be removed, but it hasn't happened yet. + +// This template function declaration is used in defining arraysize. +// Note that the function doesn't need an implementation, as we only +// use its type. +template <typename T, size_t N> +char (&ArraySizeHelper(T (&array)[N]))[N]; + +// That gcc wants both of these prototypes seems mysterious. VC, for +// its part, can't decide which to use (another mystery). Matching of +// template overloads: the final frontier. +#ifndef _MSC_VER +template <typename T, size_t N> +char (&ArraySizeHelper(const T (&array)[N]))[N]; +#endif + +#define arraysize(array) (sizeof(ArraySizeHelper(array))) + +// ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize, +// but can be used on anonymous types or types defined inside +// functions. It's less safe than arraysize as it accepts some +// (although not all) pointers. Therefore, you should use arraysize +// whenever possible. +// +// The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type +// size_t. +// +// ARRAYSIZE_UNSAFE catches a few type errors. If you see a compiler error +// +// "warning: division by zero in ..." +// +// when using ARRAYSIZE_UNSAFE, you are (wrongfully) giving it a pointer. +// You should only use ARRAYSIZE_UNSAFE on statically allocated arrays. +// +// The following comments are on the implementation details, and can +// be ignored by the users. +// +// ARRAYSIZE_UNSAFE(arr) works by inspecting sizeof(arr) (the # of bytes in +// the array) and sizeof(*(arr)) (the # of bytes in one array +// element). If the former is divisible by the latter, perhaps arr is +// indeed an array, in which case the division result is the # of +// elements in the array. Otherwise, arr cannot possibly be an array, +// and we generate a compiler error to prevent the code from +// compiling. +// +// Since the size of bool is implementation-defined, we need to cast +// !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final +// result has type size_t. +// +// This macro is not perfect as it wrongfully accepts certain +// pointers, namely where the pointer size is divisible by the pointee +// size. Since all our code has to go through a 32-bit compiler, +// 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. + +#define ARRAYSIZE_UNSAFE(a) \ + ((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; +} + +// 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 +// size of a static array: +// +// COMPILE_ASSERT(ARRAYSIZE_UNSAFE(content_type_names) == CONTENT_NUM_TYPES, +// content_type_names_incorrect_size); +// +// or to make sure a struct is smaller than a certain size: +// +// COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large); +// +// The second argument to the macro is the name of the variable. If +// the expression is false, most compilers will issue a warning/error +// containing the name of the variable. + +template <bool> +struct CompileAssert { +}; + +#undef COMPILE_ASSERT +#define COMPILE_ASSERT(expr, msg) \ + typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] + +// 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&) { +} + +// The following enum should be used only as a constructor argument to indicate +// that the variable has static storage class, and that the constructor should +// do nothing to its state. It indicates to the reader that it is legal to +// declare a static instance of the class, provided the constructor is given +// the base::LINKER_INITIALIZED argument. Normally, it is unsafe to declare a +// static variable that has a constructor or a destructor because invocation +// order is undefined. However, IF the type can be initialized by filling with +// zeroes (which the loader does for static variables), AND the destructor also +// does nothing to the storage, AND there are no virtual methods, then a +// constructor declared as +// explicit MyClass(base::LinkerInitialized x) {} +// and invoked as +// static MyClass my_variable_name(base::LINKER_INITIALIZED); +namespace base { +enum LinkerInitialized { LINKER_INITIALIZED }; +} // base + +#endif // I18N_PHONENUMBERS_BASE_BASICTYPES_H_ diff --git a/base/logging.h b/base/logging.h new file mode 100644 index 0000000..6cca5c6 --- /dev/null +++ b/base/logging.h @@ -0,0 +1,37 @@ +// Copyright (C) 2011 The Libphonenumber Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Author: Philippe Liard + +// This file provides a minimalist implementation of common macros. + +#ifndef I18N_PHONENUMBERS_BASE_LOGGING_H_ +#define I18N_PHONENUMBERS_BASE_LOGGING_H_ + +#include <cassert> + +#define CHECK_EQ(X, Y) assert((X) == (Y)) + +# define DCHECK(X) assert(X) +# define DCHECK_EQ(X, Y) CHECK_EQ((X), (Y)) +# define DCHECK_GE(X, Y) assert((X) >= (Y)) +# define DCHECK_GT(X, Y) assert((X) > (Y)) +# define DCHECK_LT(X, Y) assert((X) < (Y)) + +template <typename T> T* CHECK_NOTNULL(T* ptr) { + assert(ptr); + return ptr; +} + +#endif // I18N_PHONENUMBERS_BASE_LOGGING_H_ diff --git a/base/memory/scoped_ptr.h b/base/memory/scoped_ptr.h new file mode 100644 index 0000000..d9a08ec --- /dev/null +++ b/base/memory/scoped_ptr.h @@ -0,0 +1,24 @@ +// Copyright (C) 2011 The Libphonenumber Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Author: Philippe Liard + +#ifndef I18N_PHONENUMBERS_BASE_MEMORY_SCOPED_PTR_H_ +#define I18N_PHONENUMBERS_BASE_MEMORY_SCOPED_PTR_H_ + +#include <boost/scoped_ptr.hpp> + +using boost::scoped_ptr; + +#endif // I18N_PHONENUMBERS_BASE_MEMORY_SCOPED_PTR_H_ diff --git a/base/memory/singleton.h b/base/memory/singleton.h new file mode 100644 index 0000000..86d3cbc --- /dev/null +++ b/base/memory/singleton.h @@ -0,0 +1,52 @@ +// Copyright (C) 2011 The Libphonenumber Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Author: Philippe Liard + +#ifndef I18N_PHONENUMBERS_BASE_SINGLETON_H_ +#define I18N_PHONENUMBERS_BASE_SINGLETON_H_ + +#include <boost/scoped_ptr.hpp> +#include <boost/thread/once.hpp> +#include <boost/utility.hpp> + +namespace i18n { +namespace phonenumbers { + +template <class T> +class Singleton : private boost::noncopyable { + public: + virtual ~Singleton() {} + + static T* GetInstance() { + boost::call_once(Init, flag); + return instance.get(); + } + + private: + static void Init() { + instance.reset(new T()); + } + + static boost::scoped_ptr<T> instance; + static boost::once_flag flag; +}; + +template <class T> boost::scoped_ptr<T> Singleton<T>::instance; +template <class T> boost::once_flag Singleton<T>::flag = BOOST_ONCE_INIT; + +} // namespace phonenumbers +} // namespace i18n + +#endif // I18N_PHONENUMBERS_BASE_SINGLETON_H_ diff --git a/base/strings/string_piece.cc b/base/strings/string_piece.cc new file mode 100644 index 0000000..774c94e --- /dev/null +++ b/base/strings/string_piece.cc @@ -0,0 +1,219 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// Copied from strings/stringpiece.cc with modifications + +#include <algorithm> +#include <ostream> + +#include "phonenumbers/base/strings/string_piece.h" + +namespace base { + +typedef StringPiece::size_type size_type; + +std::ostream& operator<<(std::ostream& o, const StringPiece& piece) { + o.write(piece.data(), static_cast<std::streamsize>(piece.size())); + return o; +} + +bool operator==(const StringPiece& x, const StringPiece& y) { + if (x.size() != y.size()) + return false; + + return StringPiece::wordmemcmp(x.data(), y.data(), x.size()) == 0; +} + +void StringPiece::CopyToString(std::string* target) const { + target->assign(!empty() ? data() : "", size()); +} + +void StringPiece::AppendToString(std::string* target) const { + if (!empty()) + target->append(data(), size()); +} + +size_type StringPiece::copy(char* buf, size_type n, size_type pos) const { + size_type ret = std::min(length_ - pos, n); + memcpy(buf, ptr_ + pos, ret); + return ret; +} + +size_type StringPiece::find(const StringPiece& s, size_type pos) const { + if (pos > length_) + return npos; + + const char* result = std::search(ptr_ + pos, ptr_ + length_, + s.ptr_, s.ptr_ + s.length_); + const size_type xpos = result - ptr_; + return xpos + s.length_ <= length_ ? xpos : npos; +} + +size_type StringPiece::find(char c, size_type pos) const { + if (pos >= length_) + return npos; + + const char* result = std::find(ptr_ + pos, ptr_ + length_, c); + return result != ptr_ + length_ ? static_cast<size_t>(result - ptr_) : npos; +} + +size_type StringPiece::rfind(const StringPiece& s, size_type pos) const { + if (length_ < s.length_) + return npos; + + if (s.empty()) + return std::min(length_, pos); + + const char* last = ptr_ + std::min(length_ - s.length_, pos) + s.length_; + const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_); + return result != last ? static_cast<size_t>(result - ptr_) : npos; +} + +size_type StringPiece::rfind(char c, size_type pos) const { + if (length_ == 0) + return npos; + + for (size_type i = std::min(pos, length_ - 1); ; --i) { + if (ptr_[i] == c) + return i; + if (i == 0) + break; + } + return npos; +} + +// For each character in characters_wanted, sets the index corresponding +// to the ASCII code of that character to 1 in table. This is used by +// the find_.*_of methods below to tell whether or not a character is in +// the lookup table in constant time. +// The argument `table' must be an array that is large enough to hold all +// the possible values of an unsigned char. Thus it should be be declared +// as follows: +// bool table[UCHAR_MAX + 1] +static inline void BuildLookupTable(const StringPiece& characters_wanted, + bool* table) { + const size_type length = characters_wanted.length(); + const char* const data = characters_wanted.data(); + for (size_type i = 0; i < length; ++i) { + table[static_cast<unsigned char>(data[i])] = true; + } +} + +size_type StringPiece::find_first_of(const StringPiece& s, + size_type pos) const { + if (length_ == 0 || s.length_ == 0) + return npos; + + // Avoid the cost of BuildLookupTable() for a single-character search. + if (s.length_ == 1) + return find_first_of(s.ptr_[0], pos); + + bool lookup[UCHAR_MAX + 1] = { false }; + BuildLookupTable(s, lookup); + for (size_type i = pos; i < length_; ++i) { + if (lookup[static_cast<unsigned char>(ptr_[i])]) { + return i; + } + } + return npos; +} + +size_type StringPiece::find_first_not_of(const StringPiece& s, + size_type pos) const { + if (length_ == 0) + return npos; + + if (s.length_ == 0) + return 0; + + // Avoid the cost of BuildLookupTable() for a single-character search. + if (s.length_ == 1) + return find_first_not_of(s.ptr_[0], pos); + + bool lookup[UCHAR_MAX + 1] = { false }; + BuildLookupTable(s, lookup); + for (size_type i = pos; i < length_; ++i) { + if (!lookup[static_cast<unsigned char>(ptr_[i])]) { + return i; + } + } + return npos; +} + +size_type StringPiece::find_first_not_of(char c, size_type pos) const { + if (length_ == 0) + return npos; + + for (; pos < length_; ++pos) { + if (ptr_[pos] != c) { + return pos; + } + } + return npos; +} + +size_type StringPiece::find_last_of(const StringPiece& s, size_type pos) const { + if (length_ == 0 || s.length_ == 0) + return npos; + + // Avoid the cost of BuildLookupTable() for a single-character search. + if (s.length_ == 1) + return find_last_of(s.ptr_[0], pos); + + bool lookup[UCHAR_MAX + 1] = { false }; + BuildLookupTable(s, lookup); + for (size_type i = std::min(pos, length_ - 1); ; --i) { + if (lookup[static_cast<unsigned char>(ptr_[i])]) + return i; + if (i == 0) + break; + } + return npos; +} + +size_type StringPiece::find_last_not_of(const StringPiece& s, + size_type pos) const { + if (length_ == 0) + return npos; + + size_type i = std::min(pos, length_ - 1); + if (s.length_ == 0) + return i; + + // Avoid the cost of BuildLookupTable() for a single-character search. + if (s.length_ == 1) + return find_last_not_of(s.ptr_[0], pos); + + bool lookup[UCHAR_MAX + 1] = { false }; + BuildLookupTable(s, lookup); + for (; ; --i) { + if (!lookup[static_cast<unsigned char>(ptr_[i])]) + return i; + if (i == 0) + break; + } + return npos; +} + +size_type StringPiece::find_last_not_of(char c, size_type pos) const { + if (length_ == 0) + return npos; + + for (size_type i = std::min(pos, length_ - 1); ; --i) { + if (ptr_[i] != c) + return i; + if (i == 0) + break; + } + return npos; +} + +StringPiece StringPiece::substr(size_type pos, size_type n) const { + if (pos > length_) pos = length_; + if (n > length_ - pos) n = length_ - pos; + return StringPiece(ptr_ + pos, n); +} + +const StringPiece::size_type StringPiece::npos = size_type(-1); + +} // namespace base diff --git a/base/strings/string_piece.h b/base/strings/string_piece.h new file mode 100644 index 0000000..8ff8633 --- /dev/null +++ b/base/strings/string_piece.h @@ -0,0 +1,192 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// Copied from strings/stringpiece.h with modifications +// +// A string-like object that points to a sized piece of memory. +// +// Functions or methods may use const StringPiece& parameters to accept either +// a "const char*" or a "string" value that will be implicitly converted to +// a StringPiece. The implicit conversion means that it is often appropriate +// to include this .h file in other files rather than forward-declaring +// StringPiece as would be appropriate for most other Google classes. +// +// Systematic usage of StringPiece is encouraged as it will reduce unnecessary +// conversions from "const char*" to "string" and back again. +// + +#ifndef I18N_PHONENUMBERS_BASE_STRINGS_STRING_PIECE_H_ +#define I18N_PHONENUMBERS_BASE_STRINGS_STRING_PIECE_H_ +#pragma once + +#include <string> + +#include "phonenumbers/base/basictypes.h" + +namespace base { + +class StringPiece { + public: + // standard STL container boilerplate + typedef size_t size_type; + typedef char value_type; + typedef const char* pointer; + typedef const char& reference; + typedef const char& const_reference; + typedef ptrdiff_t difference_type; + typedef const char* const_iterator; + typedef const char* iterator; + typedef std::reverse_iterator<const_iterator> const_reverse_iterator; + typedef std::reverse_iterator<iterator> reverse_iterator; + + static const size_type npos; + + public: + // We provide non-explicit singleton constructors so users can pass + // in a "const char*" or a "string" wherever a "StringPiece" is + // expected. + StringPiece() : ptr_(NULL), length_(0) { } + StringPiece(const char* str) + : ptr_(str), length_((str == NULL) ? 0 : strlen(str)) { } + StringPiece(const std::string& str) + : ptr_(str.data()), length_(str.size()) { } + StringPiece(const char* offset, size_type len) + : ptr_(offset), length_(len) { } + + // data() may return a pointer to a buffer with embedded NULs, and the + // returned buffer may or may not be null terminated. Therefore it is + // typically a mistake to pass data() to a routine that expects a NUL + // terminated string. + const char* data() const { return ptr_; } + size_type size() const { return length_; } + size_type length() const { return length_; } + bool empty() const { return length_ == 0; } + + void clear() { + ptr_ = NULL; + length_ = 0; + } + void set(const char* data, size_type len) { + ptr_ = data; + length_ = len; + } + void set(const char* str) { + ptr_ = str; + length_ = str ? strlen(str) : 0; + } + void set(const void* data, size_type len) { + ptr_ = reinterpret_cast<const char*>(data); + length_ = len; + } + + char operator[](size_type i) const { return ptr_[i]; } + + void remove_prefix(size_type n) { + ptr_ += n; + length_ -= n; + } + + void remove_suffix(size_type n) { + length_ -= n; + } + + int compare(const StringPiece& x) const { + int r = wordmemcmp( + ptr_, x.ptr_, (length_ < x.length_ ? length_ : x.length_)); + if (r == 0) { + if (length_ < x.length_) r = -1; + else if (length_ > x.length_) r = +1; + } + return r; + } + + std::string as_string() const { + // std::string doesn't like to take a NULL pointer even with a 0 size. + return std::string(!empty() ? data() : "", size()); + } + + void CopyToString(std::string* target) const; + void AppendToString(std::string* target) const; + + // Does "this" start with "x" + bool starts_with(const StringPiece& x) const { + return ((length_ >= x.length_) && + (wordmemcmp(ptr_, x.ptr_, x.length_) == 0)); + } + + // Does "this" end with "x" + bool ends_with(const StringPiece& x) const { + return ((length_ >= x.length_) && + (wordmemcmp(ptr_ + (length_-x.length_), x.ptr_, x.length_) == 0)); + } + + iterator begin() const { return ptr_; } + iterator end() const { return ptr_ + length_; } + const_reverse_iterator rbegin() const { + return const_reverse_iterator(ptr_ + length_); + } + const_reverse_iterator rend() const { + return const_reverse_iterator(ptr_); + } + + size_type max_size() const { return length_; } + size_type capacity() const { return length_; } + + size_type copy(char* buf, size_type n, size_type pos = 0) const; + + size_type find(const StringPiece& s, size_type pos = 0) const; + size_type find(char c, size_type pos = 0) const; + size_type rfind(const StringPiece& s, size_type pos = npos) const; + size_type rfind(char c, size_type pos = npos) const; + + size_type find_first_of(const StringPiece& s, size_type pos = 0) const; + size_type find_first_of(char c, size_type pos = 0) const { + return find(c, pos); + } + size_type find_first_not_of(const StringPiece& s, size_type pos = 0) const; + size_type find_first_not_of(char c, size_type pos = 0) const; + size_type find_last_of(const StringPiece& s, size_type pos = npos) const; + size_type find_last_of(char c, size_type pos = npos) const { + return rfind(c, pos); + } + size_type find_last_not_of(const StringPiece& s, size_type pos = npos) const; + size_type find_last_not_of(char c, size_type pos = npos) const; + + StringPiece substr(size_type pos, size_type n = npos) const; + + static int wordmemcmp(const char* p, const char* p2, size_type N) { + return memcmp(p, p2, N); + } + + private: + const char* ptr_; + size_type length_; +}; + +bool operator==(const StringPiece& x, const StringPiece& y); + +inline bool operator!=(const StringPiece& x, const StringPiece& y) { + return !(x == y); +} + +inline bool operator<(const StringPiece& x, const StringPiece& y) { + const int r = StringPiece::wordmemcmp( + x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size())); + return ((r < 0) || ((r == 0) && (x.size() < y.size()))); +} + +inline bool operator>(const StringPiece& x, const StringPiece& y) { + return y < x; +} + +inline bool operator<=(const StringPiece& x, const StringPiece& y) { + return !(x > y); +} + +inline bool operator>=(const StringPiece& x, const StringPiece& y) { + return !(x < y); +} + +} // namespace base + +#endif // I18N_PHONENUMBERS_BASE_STRINGS_STRING_PIECE_H_ diff --git a/base/synchronization/lock.h b/base/synchronization/lock.h new file mode 100644 index 0000000..29e9bf2 --- /dev/null +++ b/base/synchronization/lock.h @@ -0,0 +1,27 @@ +// Copyright (C) 2011 The Libphonenumber Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Author: Philippe Liard + +#ifndef I18N_PHONENUMBERS_BASE_SYNCHRONIZATION_LOCK_H_ +#define I18N_PHONENUMBERS_BASE_SYNCHRONIZATION_LOCK_H_ + +#include <boost/thread/mutex.hpp> + +namespace base { + typedef boost::mutex Lock; + typedef boost::mutex::scoped_lock AutoLock; +} + +#endif // I18N_PHONENUMBERS_BASE_SYNCHRONIZATION_LOCK_H_ diff --git a/encoding_utils.h b/encoding_utils.h index 415ce62..d427183 100644 --- a/encoding_utils.h +++ b/encoding_utils.h @@ -15,7 +15,7 @@ #ifndef I18N_PHONENUMBERS_ENCODING_UTILS_H_ #define I18N_PHONENUMBERS_ENCODING_UTILS_H_ -#include "base/basictypes.h" +#include "phonenumbers/base/basictypes.h" #include "phonenumbers/utf/unilib.h" #include "phonenumbers/utf/utf.h" diff --git a/geocoding/area_code_map.h b/geocoding/area_code_map.h index ea6b348..c9bd1de 100644 --- a/geocoding/area_code_map.h +++ b/geocoding/area_code_map.h @@ -20,8 +20,8 @@ #include <map> #include <string> -#include "base/basictypes.h" -#include "base/memory/scoped_ptr.h" +#include "phonenumbers/base/basictypes.h" +#include "phonenumbers/base/memory/scoped_ptr.h" namespace i18n { namespace phonenumbers { diff --git a/geocoding/default_map_storage.cc b/geocoding/default_map_storage.cc index 2ea3daa..5c4ae4b 100644 --- a/geocoding/default_map_storage.cc +++ b/geocoding/default_map_storage.cc @@ -16,8 +16,8 @@ #include "phonenumbers/geocoding/default_map_storage.h" -#include "base/basictypes.h" -#include "base/logging.h" +#include "phonenumbers/base/basictypes.h" +#include "phonenumbers/base/logging.h" #include "phonenumbers/geocoding/geocoding_data.h" namespace i18n { diff --git a/geocoding/default_map_storage.h b/geocoding/default_map_storage.h index a2a276f..750985c 100644 --- a/geocoding/default_map_storage.h +++ b/geocoding/default_map_storage.h @@ -19,7 +19,7 @@ #ifndef I18N_PHONENUMBERS_DEFAULT_MAP_STORAGE_H_ #define I18N_PHONENUMBERS_DEFAULT_MAP_STORAGE_H_ -#include "base/basictypes.h" +#include "phonenumbers/base/basictypes.h" namespace i18n { namespace phonenumbers { diff --git a/geocoding/geocoding_data.h b/geocoding/geocoding_data.h index 90f7956..37f1bd6 100644 --- a/geocoding/geocoding_data.h +++ b/geocoding/geocoding_data.h @@ -17,7 +17,7 @@ #ifndef I18N_PHONENUMBERS_GEOCODING_DATA #define I18N_PHONENUMBERS_GEOCODING_DATA -#include "base/basictypes.h" +#include "phonenumbers/base/basictypes.h" namespace i18n { namespace phonenumbers { diff --git a/geocoding/mapping_file_provider.h b/geocoding/mapping_file_provider.h index 1234a5e..88f282a 100644 --- a/geocoding/mapping_file_provider.h +++ b/geocoding/mapping_file_provider.h @@ -19,7 +19,7 @@ #include <string> -#include "base/basictypes.h" +#include "phonenumbers/base/basictypes.h" namespace i18n { namespace phonenumbers { diff --git a/geocoding/phonenumber_offline_geocoder.h b/geocoding/phonenumber_offline_geocoder.h index e0389b7..243404c 100644 --- a/geocoding/phonenumber_offline_geocoder.h +++ b/geocoding/phonenumber_offline_geocoder.h @@ -22,8 +22,8 @@ #include <unicode/locid.h> // NOLINT(build/include_order) -#include "base/basictypes.h" -#include "base/memory/scoped_ptr.h" +#include "phonenumbers/base/basictypes.h" +#include "phonenumbers/base/memory/scoped_ptr.h" namespace i18n { namespace phonenumbers { diff --git a/phonenumbermatch.h b/phonenumbermatch.h index 5ebfd9d..8cc7dc3 100644 --- a/phonenumbermatch.h +++ b/phonenumbermatch.h @@ -53,7 +53,7 @@ #include <string> -#include "base/basictypes.h" +#include "phonenumbers/base/basictypes.h" #include "phonenumbers/phonenumber.pb.h" namespace i18n { diff --git a/phonenumbermatcher.cc b/phonenumbermatcher.cc index b158515..039ff90 100644 --- a/phonenumbermatcher.cc +++ b/phonenumbermatcher.cc @@ -25,9 +25,9 @@ #endif // USE_ICU_REGEXP #include <ctype.h> -#include <map> #include <iostream> #include <limits> +#include <map> #include <stddef.h> #include <string> #include <utility> @@ -35,10 +35,10 @@ #include <unicode/uchar.h> -#include "base/logging.h" -#include "base/memory/scoped_ptr.h" -#include "base/memory/singleton.h" #include "phonenumbers/alternate_format.h" +#include "phonenumbers/base/logging.h" +#include "phonenumbers/base/memory/scoped_ptr.h" +#include "phonenumbers/base/memory/singleton.h" #include "phonenumbers/callback.h" #include "phonenumbers/default_logger.h" #include "phonenumbers/encoding_utils.h" diff --git a/phonenumbermatcher.h b/phonenumbermatcher.h index ad061ae..d9ead8d 100644 --- a/phonenumbermatcher.h +++ b/phonenumbermatcher.h @@ -25,8 +25,8 @@ #include <string> #include <vector> -#include "base/basictypes.h" -#include "base/memory/scoped_ptr.h" +#include "phonenumbers/base/basictypes.h" +#include "phonenumbers/base/memory/scoped_ptr.h" #include "phonenumbers/callback.h" #include "phonenumbers/regexp_adapter.h" diff --git a/phonenumberutil.cc b/phonenumberutil.cc index ba07b48..a7967a7 100644 --- a/phonenumberutil.cc +++ b/phonenumberutil.cc @@ -32,10 +32,10 @@ #include <unicode/uchar.h> #include <unicode/utf8.h> -#include "base/basictypes.h" -#include "base/logging.h" -#include "base/memory/singleton.h" #include "phonenumbers/asyoutypeformatter.h" +#include "phonenumbers/base/basictypes.h" +#include "phonenumbers/base/logging.h" +#include "phonenumbers/base/memory/singleton.h" #include "phonenumbers/default_logger.h" #include "phonenumbers/encoding_utils.h" #include "phonenumbers/metadata.h" diff --git a/phonenumberutil.h b/phonenumberutil.h index fe3a83d..1ba4ddf 100644 --- a/phonenumberutil.h +++ b/phonenumberutil.h @@ -28,9 +28,9 @@ #include <utility> #include <vector> -#include "base/basictypes.h" -#include "base/memory/scoped_ptr.h" -#include "base/memory/singleton.h" +#include "phonenumbers/base/basictypes.h" +#include "phonenumbers/base/memory/scoped_ptr.h" +#include "phonenumbers/base/memory/singleton.h" #include "phonenumbers/phonenumber.pb.h" class TelephoneNumber; diff --git a/regexp_adapter_icu.cc b/regexp_adapter_icu.cc index 804d739..ba8215c 100644 --- a/regexp_adapter_icu.cc +++ b/regexp_adapter_icu.cc @@ -28,9 +28,9 @@ #include <unicode/stringpiece.h> #include <unicode/unistr.h> -#include "base/basictypes.h" -#include "base/logging.h" -#include "base/memory/scoped_ptr.h" +#include "phonenumbers/base/basictypes.h" +#include "phonenumbers/base/logging.h" +#include "phonenumbers/base/memory/scoped_ptr.h" #include "phonenumbers/default_logger.h" #include "phonenumbers/string_byte_sink.h" diff --git a/regexp_adapter_re2.cc b/regexp_adapter_re2.cc index 500a6ca..64a58ed 100644 --- a/regexp_adapter_re2.cc +++ b/regexp_adapter_re2.cc @@ -23,8 +23,8 @@ #include <re2/re2.h> #include <re2/stringpiece.h> -#include "base/basictypes.h" -#include "base/logging.h" +#include "phonenumbers/base/basictypes.h" +#include "phonenumbers/base/logging.h" #include "phonenumbers/stringutil.h" namespace i18n { diff --git a/regexp_cache.cc b/regexp_cache.cc index 50755ed..24adc5b 100644 --- a/regexp_cache.cc +++ b/regexp_cache.cc @@ -20,7 +20,7 @@ #include <string> #include <utility> -#include "base/synchronization/lock.h" +#include "phonenumbers/base/synchronization/lock.h" #include "phonenumbers/regexp_adapter.h" using std::string; diff --git a/regexp_cache.h b/regexp_cache.h index fd01de3..46234c0 100644 --- a/regexp_cache.h +++ b/regexp_cache.h @@ -30,9 +30,9 @@ #include <cstddef> #include <string> -#include "base/basictypes.h" -#include "base/memory/scoped_ptr.h" -#include "base/synchronization/lock.h" +#include "phonenumbers/base/basictypes.h" +#include "phonenumbers/base/memory/scoped_ptr.h" +#include "phonenumbers/base/synchronization/lock.h" #ifdef USE_TR1_UNORDERED_MAP # include <tr1/unordered_map> diff --git a/shortnumberutil.cc b/shortnumberutil.cc index 43180b6..acd5c82 100644 --- a/shortnumberutil.cc +++ b/shortnumberutil.cc @@ -16,7 +16,7 @@ #include "phonenumbers/shortnumberutil.h" -#include "base/memory/scoped_ptr.h" +#include "phonenumbers/base/memory/scoped_ptr.h" #include "phonenumbers/phonemetadata.pb.h" #include "phonenumbers/phonenumberutil.h" #include "phonenumbers/regexp_adapter.h" diff --git a/shortnumberutil.h b/shortnumberutil.h index 53758a1..32d9d87 100644 --- a/shortnumberutil.h +++ b/shortnumberutil.h @@ -27,7 +27,7 @@ #include <string> -#include "base/basictypes.h" +#include "phonenumbers/base/basictypes.h" namespace i18n { namespace phonenumbers { diff --git a/stringutil.h b/stringutil.h index 55d7ee4..04f1312 100644 --- a/stringutil.h +++ b/stringutil.h @@ -21,7 +21,7 @@ #include <string> #include <vector> -#include "base/basictypes.h" +#include "phonenumbers/base/basictypes.h" namespace i18n { namespace phonenumbers { diff --git a/utf/stringpiece.h b/utf/stringpiece.h index 82196b9..962b117 100644 --- a/utf/stringpiece.h +++ b/utf/stringpiece.h @@ -16,7 +16,7 @@ #ifndef STRINGS_STRINGPIECE_H_ #define STRINGS_STRINGPIECE_H_ -#include "base/strings/string_piece.h" +#include "phonenumbers/base/strings/string_piece.h" using base::StringPiece; diff --git a/utf/stringprintf.h b/utf/stringprintf.h index 208d338..e83d2b4 100644 --- a/utf/stringprintf.h +++ b/utf/stringprintf.h @@ -17,6 +17,6 @@ #define STRINGS_STRINGPRINTF_H_ //#include "third_party/chromium/src/base/string_util.h" -#include "base/string_util.h" +#include "phonenumbers/base/string_util.h" #endif // STRINGS_STRINGPRINTF_H_ diff --git a/utf/unicodetext.cc b/utf/unicodetext.cc index 6d4762e..262a129 100644 --- a/utf/unicodetext.cc +++ b/utf/unicodetext.cc @@ -19,7 +19,7 @@ #include <cassert> #include "phonenumbers/utf/unicodetext.h" -//#include "base/logging.h" +//#include "phonenumbers/base/logging.h" #include "phonenumbers/utf/stringpiece.h" //#include "utf/stringprintf.h" #include "phonenumbers/utf/utf.h" diff --git a/utf/unicodetext.h b/utf/unicodetext.h index fb37a33..990842d 100644 --- a/utf/unicodetext.h +++ b/utf/unicodetext.h @@ -20,7 +20,7 @@ #include <iterator> #include <string> #include <utility> -#include "base/basictypes.h" +#include "phonenumbers/base/basictypes.h" //#include "util/utf8/public/config.h" using std::string; diff --git a/utf/unilib.cc b/utf/unilib.cc index db03a11..064c724 100644 --- a/utf/unilib.cc +++ b/utf/unilib.cc @@ -18,7 +18,7 @@ #include "phonenumbers/utf/unilib.h" -#include "base/basictypes.h" +#include "phonenumbers/base/basictypes.h" #include "phonenumbers/utf/utf.h" namespace UniLib { diff --git a/utf/unilib.h b/utf/unilib.h index 4cfc787..b70966c 100644 --- a/utf/unilib.h +++ b/utf/unilib.h @@ -32,7 +32,7 @@ #define UTIL_UTF8_UNILIB_H__ #include <string> -#include "base/basictypes.h" +#include "phonenumbers/base/basictypes.h" namespace UniLib { |