Update more code to use standard int types. (#9851)

pull/9858/head
David L. Jones 3 years ago committed by GitHub
parent 089e09168b
commit 125dfd343e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      src/google/protobuf/compiler/js/js_generator.cc
  2. 2
      src/google/protobuf/compiler/js/js_generator.h
  3. 2
      src/google/protobuf/compiler/ruby/ruby_generator.cc
  4. 14
      src/google/protobuf/stubs/common.cc
  5. 20
      src/google/protobuf/stubs/int128.cc
  6. 78
      src/google/protobuf/stubs/int128.h
  7. 20
      src/google/protobuf/stubs/int128_unittest.cc
  8. 108
      src/google/protobuf/stubs/port.h
  9. 71
      src/google/protobuf/stubs/structurally_valid.cc
  10. 88
      src/google/protobuf/stubs/strutil.cc
  11. 90
      src/google/protobuf/stubs/strutil.h
  12. 46
      src/google/protobuf/stubs/time.cc
  13. 14
      src/google/protobuf/stubs/time.h

@ -406,7 +406,7 @@ std::string GetMessagesFileName(const GeneratorOptions& options, const SCC* scc,
GetSnakeFilename(scc->GetRepresentative()->file()->name()));
(*long_name_dict)[scc->GetRepresentative()] =
StrCat(snake_name, "_long_sccs_",
static_cast<uint64>((*long_name_dict).size()));
static_cast<uint64_t>((*long_name_dict).size()));
}
filename_base = (*long_name_dict)[scc->GetRepresentative()];
}
@ -575,7 +575,7 @@ std::string JSOneofIndex(const OneofDescriptor* oneof) {
}
// Decodes a codepoint in \x0000 -- \xFFFF.
uint16 DecodeUTF8Codepoint(uint8* bytes, size_t* length) {
uint16_t DecodeUTF8Codepoint(uint8_t* bytes, size_t* length) {
if (*length == 0) {
return 0;
}
@ -620,13 +620,13 @@ uint16 DecodeUTF8Codepoint(uint8* bytes, size_t* length) {
bool EscapeJSString(const std::string& in, std::string* out) {
size_t decoded = 0;
for (size_t i = 0; i < in.size(); i += decoded) {
uint16 codepoint = 0;
uint16_t codepoint = 0;
// Decode the next UTF-8 codepoint.
size_t have_bytes = in.size() - i;
uint8 bytes[3] = {
static_cast<uint8>(in[i]),
static_cast<uint8>(((i + 1) < in.size()) ? in[i + 1] : 0),
static_cast<uint8>(((i + 2) < in.size()) ? in[i + 2] : 0),
uint8_t bytes[3] = {
static_cast<uint8_t>(in[i]),
static_cast<uint8_t>(((i + 1) < in.size()) ? in[i + 1] : 0),
static_cast<uint8_t>(((i + 2) < in.size()) ? in[i + 2] : 0),
};
codepoint = DecodeUTF8Codepoint(bytes, &have_bytes);
if (have_bytes == 0) {
@ -814,13 +814,13 @@ std::string JSFieldDefault(const FieldDescriptor* field) {
// integer values as signed integer values. In order to exactly match the
// output, we need to reinterpret as base-2 signed. Ugh.
return MaybeNumberString(
field, StrCat(static_cast<int32>(field->default_value_uint32())));
field, StrCat(static_cast<int32_t>(field->default_value_uint32())));
case FieldDescriptor::CPPTYPE_INT64:
return MaybeNumberString(field, StrCat(field->default_value_int64()));
case FieldDescriptor::CPPTYPE_UINT64:
// See above note for uint32 -- reinterpreting as signed.
return MaybeNumberString(
field, StrCat(static_cast<int64>(field->default_value_uint64())));
field, StrCat(static_cast<int64_t>(field->default_value_uint64())));
case FieldDescriptor::CPPTYPE_ENUM:
return StrCat(field->default_value_enum()->number());
case FieldDescriptor::CPPTYPE_BOOL:

@ -151,7 +151,7 @@ class PROTOC_EXPORT Generator : public CodeGenerator {
const std::string& parameter, GeneratorContext* context,
std::string* error) const override;
uint64 GetSupportedFeatures() const override {
uint64_t GetSupportedFeatures() const override {
return FEATURE_PROTO3_OPTIONAL;
}

@ -158,7 +158,7 @@ std::string DefaultValueForField(const FieldDescriptor* field) {
for (int i = 0; i < default_str.length(); ++i) {
// Write the hex form of each byte.
os << "\\x" << std::hex << std::setw(2)
<< ((uint16)((unsigned char)default_str.at(i)));
<< ((uint16_t)((unsigned char)default_str.at(i)));
}
os << "\".force_encoding(\"ASCII-8BIT\")";
}

@ -298,15 +298,15 @@ void DoNothing() {}
// TODO(xiaofeng): PROTOBUF_LITTLE_ENDIAN is unfortunately defined in
// google/protobuf/io/coded_stream.h and therefore can not be used here.
// Maybe move that macro definition here in the future.
uint32 ghtonl(uint32 x) {
uint32_t ghtonl(uint32_t x) {
union {
uint32 result;
uint8 result_array[4];
uint32_t result;
uint8_t result_array[4];
};
result_array[0] = static_cast<uint8>(x >> 24);
result_array[1] = static_cast<uint8>((x >> 16) & 0xFF);
result_array[2] = static_cast<uint8>((x >> 8) & 0xFF);
result_array[3] = static_cast<uint8>(x & 0xFF);
result_array[0] = static_cast<uint8_t>(x >> 24);
result_array[1] = static_cast<uint8_t>((x >> 16) & 0xFF);
result_array[2] = static_cast<uint8_t>((x >> 8) & 0xFF);
result_array[3] = static_cast<uint8_t>(x & 0xFF);
return result;
}

@ -57,14 +57,14 @@ const uint128_pod kuint128max = {uint64_t{0xFFFFFFFFFFFFFFFFu},
(pos) |= (sh); \
} \
} while (0)
static inline int Fls64(uint64 n) {
static inline int Fls64(uint64_t n) {
GOOGLE_DCHECK_NE(0, n);
int pos = 0;
STEP(uint64, n, pos, 0x20);
uint32 n32 = n;
STEP(uint32, n32, pos, 0x10);
STEP(uint32, n32, pos, 0x08);
STEP(uint32, n32, pos, 0x04);
STEP(uint64_t, n, pos, 0x20);
uint32_t n32 = n;
STEP(uint32_t, n32, pos, 0x10);
STEP(uint32_t, n32, pos, 0x08);
STEP(uint32_t, n32, pos, 0x04);
return pos + ((uint64_t{0x3333333322221100u} >> (n32 << 2)) & 0x3);
}
#undef STEP
@ -72,7 +72,7 @@ static inline int Fls64(uint64 n) {
// Like Fls64() above, but returns the 0-based position of the last set bit
// (i.e., most significant bit) in the given uint128. The argument may not be 0.
static inline int Fls128(uint128 n) {
if (uint64 hi = Uint128High64(n)) {
if (uint64_t hi = Uint128High64(n)) {
return Fls64(hi) + 64;
}
return Fls64(Uint128Low64(n));
@ -132,16 +132,16 @@ std::ostream& operator<<(std::ostream& o, const uint128& b) {
switch (flags & std::ios::basefield) {
case std::ios::hex:
div =
static_cast<uint64>(uint64_t{0x1000000000000000u}); // 16^15
static_cast<uint64_t>(uint64_t{0x1000000000000000u}); // 16^15
div_base_log = 15;
break;
case std::ios::oct:
div = static_cast<uint64>(
div = static_cast<uint64_t>(
uint64_t{01000000000000000000000u}); // 8^21
div_base_log = 21;
break;
default: // std::ios::dec
div = static_cast<uint64>(
div = static_cast<uint64_t>(
uint64_t{10000000000000000000u}); // 10^19
div_base_log = 19;
break;

@ -53,17 +53,17 @@ struct uint128_pod;
class PROTOBUF_EXPORT uint128 {
public:
UINT128_CONSTEXPR uint128(); // Sets to 0, but don't trust on this behavior.
UINT128_CONSTEXPR uint128(uint64 top, uint64 bottom);
UINT128_CONSTEXPR uint128(uint64_t top, uint64_t bottom);
#ifndef SWIG
UINT128_CONSTEXPR uint128(int bottom);
UINT128_CONSTEXPR uint128(uint32 bottom); // Top 96 bits = 0
UINT128_CONSTEXPR uint128(uint32_t bottom); // Top 96 bits = 0
#endif
UINT128_CONSTEXPR uint128(uint64 bottom); // hi_ = 0
UINT128_CONSTEXPR uint128(uint64_t bottom); // hi_ = 0
UINT128_CONSTEXPR uint128(const uint128_pod &val);
// Trivial copy constructor, assignment operator and destructor.
void Initialize(uint64 top, uint64 bottom);
void Initialize(uint64_t top, uint64_t bottom);
// Arithmetic operators.
uint128& operator+=(const uint128& b);
@ -82,8 +82,8 @@ class PROTOBUF_EXPORT uint128 {
uint128& operator++();
uint128& operator--();
friend uint64 Uint128Low64(const uint128& v);
friend uint64 Uint128High64(const uint128& v);
friend uint64_t Uint128Low64(const uint128& v);
friend uint64_t Uint128High64(const uint128& v);
// We add "std::" to avoid including all of port.h.
PROTOBUF_EXPORT friend std::ostream& operator<<(std::ostream& o,
@ -96,12 +96,12 @@ class PROTOBUF_EXPORT uint128 {
// Little-endian memory order optimizations can benefit from
// having lo_ first, hi_ last.
// See util/endian/endian.h and Load128/Store128 for storing a uint128.
uint64 lo_;
uint64 hi_;
uint64_t lo_;
uint64_t hi_;
// Not implemented, just declared for catching automatic type conversions.
uint128(uint8);
uint128(uint16);
uint128(uint8_t);
uint128(uint16_t);
uint128(float v);
uint128(double v);
};
@ -114,8 +114,8 @@ struct uint128_pod {
// of static instances, which is the primary reason for this struct in the
// first place. This does not seem to defeat any optimizations wrt
// operations involving this struct.
uint64 hi;
uint64 lo;
uint64_t hi;
uint64_t lo;
};
PROTOBUF_EXPORT extern const uint128_pod kuint128max;
@ -127,8 +127,8 @@ PROTOBUF_EXPORT extern std::ostream& operator<<(std::ostream& o,
// Methods to access low and high pieces of 128-bit value.
// Defined externally from uint128 to facilitate conversion
// to native 128-bit types when compilers support them.
inline uint64 Uint128Low64(const uint128& v) { return v.lo_; }
inline uint64 Uint128High64(const uint128& v) { return v.hi_; }
inline uint64_t Uint128Low64(const uint128& v) { return v.lo_; }
inline uint64_t Uint128High64(const uint128& v) { return v.hi_; }
// TODO: perhaps it would be nice to have int128, a signed 128-bit type?
@ -144,22 +144,22 @@ inline bool operator!=(const uint128& lhs, const uint128& rhs) {
}
inline UINT128_CONSTEXPR uint128::uint128() : lo_(0), hi_(0) {}
inline UINT128_CONSTEXPR uint128::uint128(uint64 top, uint64 bottom)
inline UINT128_CONSTEXPR uint128::uint128(uint64_t top, uint64_t bottom)
: lo_(bottom), hi_(top) {}
inline UINT128_CONSTEXPR uint128::uint128(const uint128_pod& v)
: lo_(v.lo), hi_(v.hi) {}
inline UINT128_CONSTEXPR uint128::uint128(uint64 bottom)
inline UINT128_CONSTEXPR uint128::uint128(uint64_t bottom)
: lo_(bottom), hi_(0) {}
#ifndef SWIG
inline UINT128_CONSTEXPR uint128::uint128(uint32 bottom)
inline UINT128_CONSTEXPR uint128::uint128(uint32_t bottom)
: lo_(bottom), hi_(0) {}
inline UINT128_CONSTEXPR uint128::uint128(int bottom)
: lo_(bottom), hi_(static_cast<int64>((bottom < 0) ? -1 : 0)) {}
: lo_(bottom), hi_(static_cast<int64_t>((bottom < 0) ? -1 : 0)) {}
#endif
#undef UINT128_CONSTEXPR
inline void uint128::Initialize(uint64 top, uint64 bottom) {
inline void uint128::Initialize(uint64_t top, uint64_t bottom) {
hi_ = top;
lo_ = bottom;
}
@ -183,9 +183,9 @@ CMP128(<=)
// Unary operators
inline uint128 operator-(const uint128& val) {
const uint64 hi_flip = ~Uint128High64(val);
const uint64 lo_flip = ~Uint128Low64(val);
const uint64 lo_add = lo_flip + 1;
const uint64_t hi_flip = ~Uint128High64(val);
const uint64_t lo_flip = ~Uint128Low64(val);
const uint64_t lo_add = lo_flip + 1;
if (lo_add < lo_flip) {
return uint128(hi_flip + 1, lo_add);
}
@ -235,9 +235,9 @@ inline uint128 operator<<(const uint128& val, int amount) {
if (amount == 0) {
return val;
}
uint64 new_hi = (Uint128High64(val) << amount) |
(Uint128Low64(val) >> (64 - amount));
uint64 new_lo = Uint128Low64(val) << amount;
uint64_t new_hi = (Uint128High64(val) << amount) |
(Uint128Low64(val) >> (64 - amount));
uint64_t new_lo = Uint128Low64(val) << amount;
return uint128(new_hi, new_lo);
} else if (amount < 128) {
return uint128(Uint128Low64(val) << (amount - 64), 0);
@ -252,9 +252,9 @@ inline uint128 operator>>(const uint128& val, int amount) {
if (amount == 0) {
return val;
}
uint64 new_hi = Uint128High64(val) >> amount;
uint64 new_lo = (Uint128Low64(val) >> amount) |
(Uint128High64(val) << (64 - amount));
uint64_t new_hi = Uint128High64(val) >> amount;
uint64_t new_lo = (Uint128Low64(val) >> amount) |
(Uint128High64(val) << (64 - amount));
return uint128(new_hi, new_lo);
} else if (amount < 128) {
return uint128(0, Uint128High64(val) >> (amount - 64));
@ -319,7 +319,7 @@ inline uint128 operator%(const uint128& lhs, const uint128& rhs) {
inline uint128& uint128::operator+=(const uint128& b) {
hi_ += b.hi_;
uint64 lolo = lo_ + b.lo_;
uint64_t lolo = lo_ + b.lo_;
if (lolo < lo_)
++hi_;
lo_ = lolo;
@ -335,19 +335,19 @@ inline uint128& uint128::operator-=(const uint128& b) {
}
inline uint128& uint128::operator*=(const uint128& b) {
uint64 a96 = hi_ >> 32;
uint64 a64 = hi_ & 0xffffffffu;
uint64 a32 = lo_ >> 32;
uint64 a00 = lo_ & 0xffffffffu;
uint64 b96 = b.hi_ >> 32;
uint64 b64 = b.hi_ & 0xffffffffu;
uint64 b32 = b.lo_ >> 32;
uint64 b00 = b.lo_ & 0xffffffffu;
uint64_t a96 = hi_ >> 32;
uint64_t a64 = hi_ & 0xffffffffu;
uint64_t a32 = lo_ >> 32;
uint64_t a00 = lo_ & 0xffffffffu;
uint64_t b96 = b.hi_ >> 32;
uint64_t b64 = b.hi_ & 0xffffffffu;
uint64_t b32 = b.lo_ >> 32;
uint64_t b00 = b.lo_ & 0xffffffffu;
// multiply [a96 .. a00] x [b96 .. b00]
// terms higher than c96 disappear off the high side
// terms c96 and c64 are safe to ignore carry bit
uint64 c96 = a96 * b00 + a64 * b32 + a32 * b64 + a00 * b96;
uint64 c64 = a64 * b00 + a32 * b32 + a00 * b64;
uint64_t c96 = a96 * b00 + a64 * b32 + a32 * b64 + a00 * b96;
uint64_t c64 = a64 * b00 + a32 * b32 + a00 * b64;
this->hi_ = (c96 << 32) + c64;
this->lo_ = 0;
// add terms after this one at a time to capture carry

@ -53,7 +53,7 @@ TEST(Int128, AllTests) {
uint128 bigger(2001, 1);
uint128 biggest(kuint128max);
uint128 high_low(1, 0);
uint128 low_high(0, kuint64max);
uint128 low_high(0, std::numeric_limits<uint64_t>::max());
EXPECT_LT(one, two);
EXPECT_GT(two, one);
EXPECT_LT(one, big);
@ -121,8 +121,8 @@ TEST(Int128, AllTests) {
big_copy = big;
EXPECT_EQ(big >> 128, big_copy >>= 128);
EXPECT_EQ(Uint128High64(biggest), kuint64max);
EXPECT_EQ(Uint128Low64(biggest), kuint64max);
EXPECT_EQ(Uint128High64(biggest), std::numeric_limits<uint64_t>::max());
EXPECT_EQ(Uint128Low64(biggest), std::numeric_limits<uint64_t>::max());
EXPECT_EQ(zero + one, one);
EXPECT_EQ(one + one, two);
EXPECT_EQ(big_minus_one + one, big);
@ -131,13 +131,13 @@ TEST(Int128, AllTests) {
EXPECT_EQ(zero - one, biggest);
EXPECT_EQ(big - big, zero);
EXPECT_EQ(big - one, big_minus_one);
EXPECT_EQ(big + kuint64max, bigger);
EXPECT_EQ(big + std::numeric_limits<uint64_t>::max(), bigger);
EXPECT_EQ(biggest + 1, zero);
EXPECT_EQ(zero - 1, biggest);
EXPECT_EQ(high_low - one, low_high);
EXPECT_EQ(low_high + one, high_low);
EXPECT_EQ(Uint128High64((uint128(1) << 64) - 1), 0);
EXPECT_EQ(Uint128Low64((uint128(1) << 64) - 1), kuint64max);
EXPECT_EQ(Uint128Low64((uint128(1) << 64) - 1), std::numeric_limits<uint64_t>::max());
EXPECT_TRUE(!!one);
EXPECT_TRUE(!!high_low);
EXPECT_FALSE(!!zero);
@ -317,7 +317,7 @@ TEST(Int128, AliasTests) {
x1 += x1;
EXPECT_EQ(x2, x1);
uint128 x3(1, static_cast<uint64>(1) << 63);
uint128 x3(1, static_cast<uint64_t>(1) << 63);
uint128 x4(3, 0);
x3 += x3;
EXPECT_EQ(x4, x3);
@ -403,10 +403,10 @@ TEST(Int128, DivideAndMod) {
EXPECT_EQ(expected_r, result_r);
}
static uint64 RandomUint64() {
uint64 v1 = rand();
uint64 v2 = rand();
uint64 v3 = rand();
static uint64_t RandomUint64() {
uint64_t v1 = rand();
uint64_t v2 = rand();
uint64_t v3 = rand();
return v1 * v2 + v3;
}

@ -163,68 +163,68 @@ void __sanitizer_unaligned_store64(void *p, uint64_t v);
} // extern "C"
#endif // __cplusplus
inline uint16 GOOGLE_UNALIGNED_LOAD16(const void *p) {
inline uint16_t GOOGLE_UNALIGNED_LOAD16(const void *p) {
return __sanitizer_unaligned_load16(p);
}
inline uint32 GOOGLE_UNALIGNED_LOAD32(const void *p) {
inline uint32_t GOOGLE_UNALIGNED_LOAD32(const void *p) {
return __sanitizer_unaligned_load32(p);
}
inline uint64 GOOGLE_UNALIGNED_LOAD64(const void *p) {
inline uint64_t GOOGLE_UNALIGNED_LOAD64(const void *p) {
return __sanitizer_unaligned_load64(p);
}
inline void GOOGLE_UNALIGNED_STORE16(void *p, uint16 v) {
inline void GOOGLE_UNALIGNED_STORE16(void *p, uint16_t v) {
__sanitizer_unaligned_store16(p, v);
}
inline void GOOGLE_UNALIGNED_STORE32(void *p, uint32 v) {
inline void GOOGLE_UNALIGNED_STORE32(void *p, uint32_t v) {
__sanitizer_unaligned_store32(p, v);
}
inline void GOOGLE_UNALIGNED_STORE64(void *p, uint64 v) {
inline void GOOGLE_UNALIGNED_STORE64(void *p, uint64_t v) {
__sanitizer_unaligned_store64(p, v);
}
#elif defined(GOOGLE_PROTOBUF_USE_UNALIGNED) && GOOGLE_PROTOBUF_USE_UNALIGNED
#define GOOGLE_UNALIGNED_LOAD16(_p) (*reinterpret_cast<const uint16 *>(_p))
#define GOOGLE_UNALIGNED_LOAD32(_p) (*reinterpret_cast<const uint32 *>(_p))
#define GOOGLE_UNALIGNED_LOAD64(_p) (*reinterpret_cast<const uint64 *>(_p))
#define GOOGLE_UNALIGNED_LOAD16(_p) (*reinterpret_cast<const uint16_t *>(_p))
#define GOOGLE_UNALIGNED_LOAD32(_p) (*reinterpret_cast<const uint32_t *>(_p))
#define GOOGLE_UNALIGNED_LOAD64(_p) (*reinterpret_cast<const uint64_t *>(_p))
#define GOOGLE_UNALIGNED_STORE16(_p, _val) (*reinterpret_cast<uint16 *>(_p) = (_val))
#define GOOGLE_UNALIGNED_STORE32(_p, _val) (*reinterpret_cast<uint32 *>(_p) = (_val))
#define GOOGLE_UNALIGNED_STORE64(_p, _val) (*reinterpret_cast<uint64 *>(_p) = (_val))
#define GOOGLE_UNALIGNED_STORE16(_p, _val) (*reinterpret_cast<uint16_t *>(_p) = (_val))
#define GOOGLE_UNALIGNED_STORE32(_p, _val) (*reinterpret_cast<uint32_t *>(_p) = (_val))
#define GOOGLE_UNALIGNED_STORE64(_p, _val) (*reinterpret_cast<uint64_t *>(_p) = (_val))
#else
inline uint16 GOOGLE_UNALIGNED_LOAD16(const void *p) {
uint16 t;
inline uint16_t GOOGLE_UNALIGNED_LOAD16(const void *p) {
uint16_t t;
memcpy(&t, p, sizeof t);
return t;
}
inline uint32 GOOGLE_UNALIGNED_LOAD32(const void *p) {
uint32 t;
inline uint32_t GOOGLE_UNALIGNED_LOAD32(const void *p) {
uint32_t t;
memcpy(&t, p, sizeof t);
return t;
}
inline uint64 GOOGLE_UNALIGNED_LOAD64(const void *p) {
uint64 t;
inline uint64_t GOOGLE_UNALIGNED_LOAD64(const void *p) {
uint64_t t;
memcpy(&t, p, sizeof t);
return t;
}
inline void GOOGLE_UNALIGNED_STORE16(void *p, uint16 v) {
inline void GOOGLE_UNALIGNED_STORE16(void *p, uint16_t v) {
memcpy(p, &v, sizeof v);
}
inline void GOOGLE_UNALIGNED_STORE32(void *p, uint32 v) {
inline void GOOGLE_UNALIGNED_STORE32(void *p, uint32_t v) {
memcpy(p, &v, sizeof v);
}
inline void GOOGLE_UNALIGNED_STORE64(void *p, uint64 v) {
inline void GOOGLE_UNALIGNED_STORE64(void *p, uint64_t v) {
memcpy(p, &v, sizeof v);
}
#endif
@ -251,14 +251,14 @@ inline void GOOGLE_UNALIGNED_STORE64(void *p, uint64 v) {
#elif !defined(__linux__) && !defined(__ANDROID__) && !defined(__CYGWIN__)
#ifndef bswap_16
static inline uint16 bswap_16(uint16 x) {
return static_cast<uint16>(((x & 0xFF) << 8) | ((x & 0xFF00) >> 8));
static inline uint16_t bswap_16(uint16_t x) {
return static_cast<uint16_t>(((x & 0xFF) << 8) | ((x & 0xFF00) >> 8));
}
#define bswap_16(x) bswap_16(x)
#endif
#ifndef bswap_32
static inline uint32 bswap_32(uint32 x) {
static inline uint32_t bswap_32(uint32_t x) {
return (((x & 0xFF) << 24) |
((x & 0xFF00) << 8) |
((x & 0xFF0000) >> 8) |
@ -268,7 +268,7 @@ static inline uint32 bswap_32(uint32 x) {
#endif
#ifndef bswap_64
static inline uint64 bswap_64(uint64 x) {
static inline uint64_t bswap_64(uint64_t x) {
return (((x & uint64_t{0xFFu}) << 56) | ((x & uint64_t{0xFF00u}) << 40) |
((x & uint64_t{0xFF0000u}) << 24) |
((x & uint64_t{0xFF000000u}) << 8) |
@ -287,9 +287,9 @@ static inline uint64 bswap_64(uint64 x) {
class Bits {
public:
static uint32 Log2FloorNonZero(uint32 n) {
static uint32_t Log2FloorNonZero(uint32_t n) {
#if defined(__GNUC__)
return 31 ^ static_cast<uint32>(__builtin_clz(n));
return 31 ^ static_cast<uint32_t>(__builtin_clz(n));
#elif defined(_MSC_VER)
unsigned long where;
_BitScanReverse(&where, n);
@ -299,7 +299,7 @@ class Bits {
#endif
}
static uint32 Log2FloorNonZero64(uint64 n) {
static uint32_t Log2FloorNonZero64(uint64_t n) {
// Older versions of clang run into an instruction-selection failure when
// it encounters __builtin_clzll:
// https://bugs.chromium.org/p/nativeclient/issues/detail?id=4395
@ -307,7 +307,7 @@ class Bits {
// To work around this, when we build with those we use the portable
// implementation instead.
#if defined(__GNUC__) && !defined(GOOGLE_PROTOBUF_USE_PORTABLE_LOG2)
return 63 ^ static_cast<uint32>(__builtin_clzll(n));
return 63 ^ static_cast<uint32_t>(__builtin_clzll(n));
#elif defined(_MSC_VER) && defined(_M_X64)
unsigned long where;
_BitScanReverse64(&where, n);
@ -317,14 +317,14 @@ class Bits {
#endif
}
private:
static int Log2FloorNonZero_Portable(uint32 n) {
static int Log2FloorNonZero_Portable(uint32_t n) {
if (n == 0)
return -1;
int log = 0;
uint32 value = n;
uint32_t value = n;
for (int i = 4; i >= 0; --i) {
int shift = (1 << i);
uint32 x = value >> shift;
uint32_t x = value >> shift;
if (x != 0) {
value = x;
log += shift;
@ -334,11 +334,11 @@ class Bits {
return log;
}
static int Log2FloorNonZero64_Portable(uint64 n) {
const uint32 topbits = static_cast<uint32>(n >> 32);
static int Log2FloorNonZero64_Portable(uint64_t n) {
const uint32_t topbits = static_cast<uint32_t>(n >> 32);
if (topbits == 0) {
// Top bits are zero, so scan in bottom bits
return static_cast<int>(Log2FloorNonZero(static_cast<uint32>(n)));
return static_cast<int>(Log2FloorNonZero(static_cast<uint32_t>(n)));
} else {
return 32 + static_cast<int>(Log2FloorNonZero(topbits));
}
@ -347,60 +347,60 @@ class Bits {
// ===================================================================
// from google3/util/endian/endian.h
PROTOBUF_EXPORT uint32 ghtonl(uint32 x);
PROTOBUF_EXPORT uint32_t ghtonl(uint32_t x);
class BigEndian {
public:
#ifdef PROTOBUF_LITTLE_ENDIAN
static uint16 FromHost16(uint16 x) { return bswap_16(x); }
static uint16 ToHost16(uint16 x) { return bswap_16(x); }
static uint16_t FromHost16(uint16_t x) { return bswap_16(x); }
static uint16_t ToHost16(uint16_t x) { return bswap_16(x); }
static uint32 FromHost32(uint32 x) { return bswap_32(x); }
static uint32 ToHost32(uint32 x) { return bswap_32(x); }
static uint32_t FromHost32(uint32_t x) { return bswap_32(x); }
static uint32_t ToHost32(uint32_t x) { return bswap_32(x); }
static uint64 FromHost64(uint64 x) { return bswap_64(x); }
static uint64 ToHost64(uint64 x) { return bswap_64(x); }
static uint64_t FromHost64(uint64_t x) { return bswap_64(x); }
static uint64_t ToHost64(uint64_t x) { return bswap_64(x); }
static bool IsLittleEndian() { return true; }
#else
static uint16 FromHost16(uint16 x) { return x; }
static uint16 ToHost16(uint16 x) { return x; }
static uint16_t FromHost16(uint16_t x) { return x; }
static uint16_t ToHost16(uint16_t x) { return x; }
static uint32 FromHost32(uint32 x) { return x; }
static uint32 ToHost32(uint32 x) { return x; }
static uint32_t FromHost32(uint32_t x) { return x; }
static uint32_t ToHost32(uint32_t x) { return x; }
static uint64 FromHost64(uint64 x) { return x; }
static uint64 ToHost64(uint64 x) { return x; }
static uint64_t FromHost64(uint64_t x) { return x; }
static uint64_t ToHost64(uint64_t x) { return x; }
static bool IsLittleEndian() { return false; }
#endif /* ENDIAN */
// Functions to do unaligned loads and stores in big-endian order.
static uint16 Load16(const void *p) {
static uint16_t Load16(const void *p) {
return ToHost16(GOOGLE_UNALIGNED_LOAD16(p));
}
static void Store16(void *p, uint16 v) {
static void Store16(void *p, uint16_t v) {
GOOGLE_UNALIGNED_STORE16(p, FromHost16(v));
}
static uint32 Load32(const void *p) {
static uint32_t Load32(const void *p) {
return ToHost32(GOOGLE_UNALIGNED_LOAD32(p));
}
static void Store32(void *p, uint32 v) {
static void Store32(void *p, uint32_t v) {
GOOGLE_UNALIGNED_STORE32(p, FromHost32(v));
}
static uint64 Load64(const void *p) {
static uint64_t Load64(const void *p) {
return ToHost64(GOOGLE_UNALIGNED_LOAD64(p));
}
static void Store64(void *p, uint64 v) {
static void Store64(void *p, uint64_t v) {
GOOGLE_UNALIGNED_STORE64(p, FromHost64(v));
}
};

@ -42,9 +42,9 @@ namespace internal {
// in making a string replacement, how many bytes to add 0..255, and the offset
// 0..64k-1 of the replacement string in remap_string.
struct RemapEntry {
uint8 delete_bytes;
uint8 add_bytes;
uint16 bytes_offset;
uint8_t delete_bytes;
uint8_t add_bytes;
uint16_t bytes_offset;
};
// Exit type codes for state tables. All but the first get stuffed into
@ -81,18 +81,18 @@ typedef enum {
// byte value and 6 for space-optimized tables subscripted by only six
// significant bits in UTF-8 continuation bytes.
typedef struct {
const uint32 state0;
const uint32 state0_size;
const uint32 total_size;
const uint32_t state0;
const uint32_t state0_size;
const uint32_t total_size;
const int max_expand;
const int entry_shift;
const int bytes_per_entry;
const uint32 losub;
const uint32 hiadd;
const uint8* state_table;
const uint32_t losub;
const uint32_t hiadd;
const uint8_t* state_table;
const RemapEntry* remap_base;
const uint8* remap_string;
const uint8* fast_state;
const uint8_t* remap_string;
const uint8_t* fast_state;
} UTF8StateMachineObj;
typedef UTF8StateMachineObj UTF8ScanObj;
@ -122,7 +122,7 @@ static const unsigned int utf8acceptnonsurrogates_BYTES = 1;
static const unsigned int utf8acceptnonsurrogates_LOSUB = 0x20202020;
static const unsigned int utf8acceptnonsurrogates_HIADD = 0x00000000;
static const uint8 utf8acceptnonsurrogates[] = {
static const uint8_t utf8acceptnonsurrogates[] = {
// state[0] 0x000000 Byte 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@ -376,9 +376,9 @@ static const UTF8ScanObj utf8acceptnonsurrogates_obj = {
// Return true if current Tbl pointer is within state0 range
// Note that unsigned compare checks both ends of range simultaneously
static inline bool InStateZero(const UTF8ScanObj* st, const uint8* Tbl) {
const uint8* Tbl0 = &st->state_table[st->state0];
return (static_cast<uint32>(Tbl - Tbl0) < st->state0_size);
static inline bool InStateZero(const UTF8ScanObj* st, const uint8_t* Tbl) {
const uint8_t* Tbl0 = &st->state_table[st->state0];
return (static_cast<uint32_t>(Tbl - Tbl0) < st->state0_size);
}
namespace {
@ -394,19 +394,19 @@ int UTF8GenericScan(const UTF8ScanObj* st,
if (str_length == 0) return kExitOK;
int eshift = st->entry_shift;
const uint8* isrc = reinterpret_cast<const uint8*>(str);
const uint8* src = isrc;
const uint8* srclimit = isrc + str_length;
const uint8* srclimit8 = str_length < 7 ? isrc : srclimit - 7;
const uint8* Tbl_0 = &st->state_table[st->state0];
const uint8_t* isrc = reinterpret_cast<const uint8_t*>(str);
const uint8_t* src = isrc;
const uint8_t* srclimit = isrc + str_length;
const uint8_t* srclimit8 = str_length < 7 ? isrc : srclimit - 7;
const uint8_t* Tbl_0 = &st->state_table[st->state0];
DoAgain:
// Do state-table scan
int e = 0;
uint8 c;
const uint8* Tbl2 = &st->fast_state[0];
const uint32 losub = st->losub;
const uint32 hiadd = st->hiadd;
uint8_t c;
const uint8_t* Tbl2 = &st->fast_state[0];
const uint32_t losub = st->losub;
const uint32_t hiadd = st->hiadd;
// Check initial few bytes one at a time until 8-byte aligned
//----------------------------
while ((((uintptr_t)src & 0x07) != 0) &&
@ -420,12 +420,12 @@ int UTF8GenericScan(const UTF8ScanObj* st,
// including slowing slightly on cr/lf/ht
//----------------------------
while (src < srclimit8) {
uint32 s0123 = (reinterpret_cast<const uint32 *>(src))[0];
uint32 s4567 = (reinterpret_cast<const uint32 *>(src))[1];
uint32_t s0123 = (reinterpret_cast<const uint32_t *>(src))[0];
uint32_t s4567 = (reinterpret_cast<const uint32_t *>(src))[1];
src += 8;
// This is a fast range check for all bytes in [lowsub..0x80-hiadd)
uint32 temp = (s0123 - losub) | (s0123 + hiadd) |
(s4567 - losub) | (s4567 + hiadd);
uint32_t temp = (s0123 - losub) | (s0123 + hiadd) |
(s4567 - losub) | (s4567 + hiadd);
if ((temp & 0x80808080) != 0) {
// We typically end up here on cr/lf/ht; src was incremented
int e0123 = (Tbl2[src[-8]] | Tbl2[src[-7]]) |
@ -448,7 +448,7 @@ int UTF8GenericScan(const UTF8ScanObj* st,
// Byte-at-a-time scan
//----------------------------
const uint8* Tbl = Tbl_0;
const uint8_t* Tbl = Tbl_0;
while (src < srclimit) {
c = *src;
e = Tbl[c];
@ -502,10 +502,10 @@ int UTF8GenericScanFastAscii(const UTF8ScanObj* st,
*bytes_consumed = 0;
if (str_length == 0) return kExitOK;
const uint8* isrc = reinterpret_cast<const uint8*>(str);
const uint8* src = isrc;
const uint8* srclimit = isrc + str_length;
const uint8* srclimit8 = str_length < 7 ? isrc : srclimit - 7;
const uint8_t* isrc = reinterpret_cast<const uint8_t*>(str);
const uint8_t* src = isrc;
const uint8_t* srclimit = isrc + str_length;
const uint8_t* srclimit8 = str_length < 7 ? isrc : srclimit - 7;
int n;
int rest_consumed;
int exit_reason;
@ -517,8 +517,9 @@ int UTF8GenericScanFastAscii(const UTF8ScanObj* st,
}
if (((uintptr_t)src & 0x07) == 0) {
while ((src < srclimit8) &&
(((reinterpret_cast<const uint32*>(src)[0] |
reinterpret_cast<const uint32*>(src)[1]) & 0x80808080) == 0)) {
(((reinterpret_cast<const uint32_t*>(src)[0] |
reinterpret_cast<const uint32_t*>(src)[1]) &
0x80808080) == 0)) {
src += 8;
}
}

@ -498,13 +498,13 @@ int CEscapeInternal(const char* src, int src_len, char* dest,
// Note that if we emit \xNN and the src character after that is a hex
// digit then that digit must be escaped too to prevent it being
// interpreted as part of the character code by C.
if ((!utf8_safe || static_cast<uint8>(*src) < 0x80) &&
if ((!utf8_safe || static_cast<uint8_t>(*src) < 0x80) &&
(!isprint(*src) ||
(last_hex_escape && isxdigit(*src)))) {
if (dest_len - used < 4) // need space for 4 letter escape
return -1;
sprintf(dest + used, (use_hex ? "\\x%02x" : "\\%03o"),
static_cast<uint8>(*src));
static_cast<uint8_t>(*src));
is_hex_escape = use_hex;
used += 4;
} else {
@ -628,39 +628,39 @@ std::string CHexEscape(const std::string &src) {
// platforms, including errno preservation in error-free calls.
// ----------------------------------------------------------------------
int32 strto32_adaptor(const char *nptr, char **endptr, int base) {
int32_t strto32_adaptor(const char *nptr, char **endptr, int base) {
const int saved_errno = errno;
errno = 0;
const long result = strtol(nptr, endptr, base);
if (errno == ERANGE && result == LONG_MIN) {
return kint32min;
return std::numeric_limits<int32_t>::min();
} else if (errno == ERANGE && result == LONG_MAX) {
return kint32max;
} else if (errno == 0 && result < kint32min) {
return std::numeric_limits<int32_t>::max();
} else if (errno == 0 && result < std::numeric_limits<int32_t>::min()) {
errno = ERANGE;
return kint32min;
} else if (errno == 0 && result > kint32max) {
return std::numeric_limits<int32_t>::min();
} else if (errno == 0 && result > std::numeric_limits<int32_t>::max()) {
errno = ERANGE;
return kint32max;
return std::numeric_limits<int32_t>::max();
}
if (errno == 0)
errno = saved_errno;
return static_cast<int32>(result);
return static_cast<int32_t>(result);
}
uint32 strtou32_adaptor(const char *nptr, char **endptr, int base) {
uint32_t strtou32_adaptor(const char *nptr, char **endptr, int base) {
const int saved_errno = errno;
errno = 0;
const unsigned long result = strtoul(nptr, endptr, base);
if (errno == ERANGE && result == ULONG_MAX) {
return kuint32max;
} else if (errno == 0 && result > kuint32max) {
return std::numeric_limits<uint32_t>::max();
} else if (errno == 0 && result > std::numeric_limits<uint32_t>::max()) {
errno = ERANGE;
return kuint32max;
return std::numeric_limits<uint32_t>::max();
}
if (errno == 0)
errno = saved_errno;
return static_cast<uint32>(result);
return static_cast<uint32_t>(result);
}
inline bool safe_parse_sign(std::string *text /*inout*/,
@ -800,7 +800,7 @@ bool safe_uint_internal(std::string text, IntType *value_p) {
// null character. Also used by FastInt64ToBufferLeft.
static const int kFastInt64ToBufferOffset = 21;
char *FastInt64ToBuffer(int64 i, char* buffer) {
char *FastInt64ToBuffer(int64_t i, char* buffer) {
// We could collapse the positive and negative sections, but that
// would be slightly slower for positive numbers...
// 22 bytes is enough to store -2**64, -18446744073709551616.
@ -845,7 +845,7 @@ static const int kFastInt32ToBufferOffset = 11;
// Yes, this is a duplicate of FastInt64ToBuffer. But, we need this for the
// compiler to generate 32 bit arithmetic instructions. It's much faster, at
// least with 32 bit binaries.
char *FastInt32ToBuffer(int32 i, char* buffer) {
char *FastInt32ToBuffer(int32_t i, char* buffer) {
// We could collapse the positive and negative sections, but that
// would be slightly slower for positive numbers...
// 12 bytes is enough to store -2**32, -4294967296.
@ -896,7 +896,7 @@ char *FastHexToBuffer(int i, char* buffer) {
return p + 1;
}
char *InternalFastHexToBuffer(uint64 value, char* buffer, int num_byte) {
char *InternalFastHexToBuffer(uint64_t value, char* buffer, int num_byte) {
static const char *hexdigits = "0123456789abcdef";
buffer[num_byte] = '\0';
for (int i = num_byte - 1; i >= 0; i--) {
@ -906,18 +906,18 @@ char *InternalFastHexToBuffer(uint64 value, char* buffer, int num_byte) {
// platforms, we use 64-bit '&' directly.
buffer[i] = hexdigits[value & 0xf];
#else
buffer[i] = hexdigits[uint32(value) & 0xf];
buffer[i] = hexdigits[uint32_t(value) & 0xf];
#endif
value >>= 4;
}
return buffer;
}
char *FastHex64ToBuffer(uint64 value, char* buffer) {
char *FastHex64ToBuffer(uint64_t value, char* buffer) {
return InternalFastHexToBuffer(value, buffer, 16);
}
char *FastHex32ToBuffer(uint32 value, char* buffer) {
char *FastHex32ToBuffer(uint32_t value, char* buffer) {
return InternalFastHexToBuffer(value, buffer, 8);
}
@ -960,8 +960,8 @@ static const char two_ASCII_digits[100][2] = {
{'9','5'}, {'9','6'}, {'9','7'}, {'9','8'}, {'9','9'}
};
char* FastUInt32ToBufferLeft(uint32 u, char* buffer) {
uint32 digits;
char* FastUInt32ToBufferLeft(uint32_t u, char* buffer) {
uint32_t digits;
const char *ASCII_digits = nullptr;
// The idea of this implementation is to trim the number of divides to as few
// as possible by using multiplication and subtraction rather than mod (%),
@ -1042,8 +1042,8 @@ done:
goto sublt100_000_000;
}
char* FastInt32ToBufferLeft(int32 i, char* buffer) {
uint32 u = 0;
char* FastInt32ToBufferLeft(int32_t i, char* buffer) {
uint32_t u = 0;
if (i < 0) {
*buffer++ = '-';
u -= i;
@ -1053,14 +1053,14 @@ char* FastInt32ToBufferLeft(int32 i, char* buffer) {
return FastUInt32ToBufferLeft(u, buffer);
}
char* FastUInt64ToBufferLeft(uint64 u64, char* buffer) {
char* FastUInt64ToBufferLeft(uint64_t u64, char* buffer) {
int digits;
const char *ASCII_digits = nullptr;
uint32 u = static_cast<uint32>(u64);
uint32_t u = static_cast<uint32_t>(u64);
if (u == u64) return FastUInt32ToBufferLeft(u, buffer);
uint64 top_11_digits = u64 / 1000000000;
uint64_t top_11_digits = u64 / 1000000000;
buffer = FastUInt64ToBufferLeft(top_11_digits, buffer);
u = u64 - (top_11_digits * 1000000000);
@ -1095,8 +1095,8 @@ char* FastUInt64ToBufferLeft(uint64 u64, char* buffer) {
return buffer;
}
char* FastInt64ToBufferLeft(int64 i, char* buffer) {
uint64 u = 0;
char* FastInt64ToBufferLeft(int64_t i, char* buffer) {
uint64_t u = 0;
if (i < 0) {
*buffer++ = '-';
u -= i;
@ -1341,19 +1341,19 @@ bool safe_strtod(const char* str, double* value) {
return *str != '\0' && *endptr == '\0';
}
bool safe_strto32(const std::string &str, int32 *value) {
bool safe_strto32(const std::string &str, int32_t *value) {
return safe_int_internal(str, value);
}
bool safe_strtou32(const std::string &str, uint32 *value) {
bool safe_strtou32(const std::string &str, uint32_t *value) {
return safe_uint_internal(str, value);
}
bool safe_strto64(const std::string &str, int64 *value) {
bool safe_strto64(const std::string &str, int64_t *value) {
return safe_int_internal(str, value);
}
bool safe_strtou64(const std::string &str, uint64 *value) {
bool safe_strtou64(const std::string &str, uint64_t *value) {
return safe_uint_internal(str, value);
}
@ -1400,12 +1400,12 @@ namespace strings {
AlphaNum::AlphaNum(strings::Hex hex) {
char *const end = &digits[kFastToBufferSize];
char *writer = end;
uint64 value = hex.value;
uint64 width = hex.spec;
uint64_t value = hex.value;
uint64_t width = hex.spec;
// We accomplish minimum width by OR'ing in 0x10000 to the user's value,
// where 0x10000 is the smallest hex number that is as wide as the user
// asked for.
uint64 mask = (static_cast<uint64>(1) << ((width - 1) * 4)) | value;
uint64_t mask = (static_cast<uint64_t>(1) << ((width - 1) * 4)) | value;
static const char hexdigits[] = "0123456789abcdef";
do {
*--writer = hexdigits[value & 0xF];
@ -2104,7 +2104,7 @@ int Base64EscapeInternal(const unsigned char *src, int szsrc,
// Three bytes of data encodes to four characters of ciphertext.
// So we can pump through three-byte chunks atomically.
while (cur_src < limit_src - 3) { // keep going as long as we have >= 32 bits
uint32 in = BigEndian::Load32(cur_src) >> 8;
uint32_t in = BigEndian::Load32(cur_src) >> 8;
cur_dest[0] = base64[in >> 18];
in &= 0x3FFFF;
@ -2130,7 +2130,7 @@ int Base64EscapeInternal(const unsigned char *src, int szsrc,
// One byte left: this encodes to two characters, and (optionally)
// two pad characters to round out the four-character cipherblock.
if ((szdest -= 2) < 0) return 0;
uint32 in = cur_src[0];
uint32_t in = cur_src[0];
cur_dest[0] = base64[in >> 2];
in &= 0x3;
cur_dest[1] = base64[in << 4];
@ -2147,7 +2147,7 @@ int Base64EscapeInternal(const unsigned char *src, int szsrc,
// Two bytes left: this encodes to three characters, and (optionally)
// one pad character to round out the four-character cipherblock.
if ((szdest -= 3) < 0) return 0;
uint32 in = BigEndian::Load16(cur_src);
uint32_t in = BigEndian::Load16(cur_src);
cur_dest[0] = base64[in >> 10];
in &= 0x3FF;
cur_dest[1] = base64[in >> 4];
@ -2166,7 +2166,7 @@ int Base64EscapeInternal(const unsigned char *src, int szsrc,
// the loop because the loop above always reads 4 bytes, and the fourth
// byte is past the end of the input.
if ((szdest -= 4) < 0) return 0;
uint32 in = (cur_src[0] << 16) + BigEndian::Load16(cur_src + 1);
uint32_t in = (cur_src[0] << 16) + BigEndian::Load16(cur_src + 1);
cur_dest[0] = base64[in >> 18];
in &= 0x3FFFF;
cur_dest[1] = base64[in >> 12];
@ -2243,8 +2243,8 @@ void WebSafeBase64EscapeWithPadding(StringPiece src, std::string *dest) {
// Helper to append a Unicode code point to a string as UTF8, without bringing
// in any external dependencies.
int EncodeAsUTF8Char(uint32 code_point, char* output) {
uint32 tmp = 0;
int EncodeAsUTF8Char(uint32_t code_point, char* output) {
uint32_t tmp = 0;
int len = 0;
if (code_point <= 0x7f) {
tmp = code_point;
@ -2296,7 +2296,7 @@ int UTF8FirstLetterNumBytes(const char* src, int len) {
if (len == 0) {
return 0;
}
return kUTF8LenTbl[*reinterpret_cast<const uint8*>(src)];
return kUTF8LenTbl[*reinterpret_cast<const uint8_t*>(src)];
}
// ----------------------------------------------------------------------

@ -355,20 +355,20 @@ PROTOBUF_EXPORT std::string CHexEscape(const std::string& src);
// platforms, so using these is safer, from the point of view of
// overflow behavior, than using the standard libc functions.
// ----------------------------------------------------------------------
PROTOBUF_EXPORT int32 strto32_adaptor(const char* nptr, char** endptr,
int base);
PROTOBUF_EXPORT uint32 strtou32_adaptor(const char* nptr, char** endptr,
PROTOBUF_EXPORT int32_t strto32_adaptor(const char* nptr, char** endptr,
int base);
PROTOBUF_EXPORT uint32_t strtou32_adaptor(const char* nptr, char** endptr,
int base);
inline int32 strto32(const char *nptr, char **endptr, int base) {
if (sizeof(int32) == sizeof(long))
inline int32_t strto32(const char *nptr, char **endptr, int base) {
if (sizeof(int32_t) == sizeof(long))
return strtol(nptr, endptr, base);
else
return strto32_adaptor(nptr, endptr, base);
}
inline uint32 strtou32(const char *nptr, char **endptr, int base) {
if (sizeof(uint32) == sizeof(unsigned long))
inline uint32_t strtou32(const char *nptr, char **endptr, int base) {
if (sizeof(uint32_t) == sizeof(unsigned long))
return strtoul(nptr, endptr, base);
else
return strtou32_adaptor(nptr, endptr, base);
@ -376,15 +376,15 @@ inline uint32 strtou32(const char *nptr, char **endptr, int base) {
// For now, long long is 64-bit on all the platforms we care about, so these
// functions can simply pass the call to strto[u]ll.
inline int64 strto64(const char *nptr, char **endptr, int base) {
static_assert(sizeof(int64) == sizeof(long long),
"sizeof_int64_is_not_sizeof_long_long");
inline int64_t strto64(const char *nptr, char **endptr, int base) {
static_assert(sizeof(int64_t) == sizeof(long long),
"sizeof int64_t is not sizeof long long");
return strtoll(nptr, endptr, base);
}
inline uint64 strtou64(const char *nptr, char **endptr, int base) {
static_assert(sizeof(uint64) == sizeof(unsigned long long),
"sizeof_uint64_is_not_sizeof_long_long");
inline uint64_t strtou64(const char *nptr, char **endptr, int base) {
static_assert(sizeof(uint64_t) == sizeof(unsigned long long),
"sizeof uint64_t is not sizeof unsigned long long");
return strtoull(nptr, endptr, base);
}
@ -399,33 +399,33 @@ inline uint64 strtou64(const char *nptr, char **endptr, int base) {
// ----------------------------------------------------------------------
PROTOBUF_EXPORT bool safe_strtob(StringPiece str, bool* value);
PROTOBUF_EXPORT bool safe_strto32(const std::string& str, int32* value);
PROTOBUF_EXPORT bool safe_strtou32(const std::string& str, uint32* value);
inline bool safe_strto32(const char* str, int32* value) {
PROTOBUF_EXPORT bool safe_strto32(const std::string& str, int32_t* value);
PROTOBUF_EXPORT bool safe_strtou32(const std::string& str, uint32_t* value);
inline bool safe_strto32(const char* str, int32_t* value) {
return safe_strto32(std::string(str), value);
}
inline bool safe_strto32(StringPiece str, int32* value) {
inline bool safe_strto32(StringPiece str, int32_t* value) {
return safe_strto32(str.ToString(), value);
}
inline bool safe_strtou32(const char* str, uint32* value) {
inline bool safe_strtou32(const char* str, uint32_t* value) {
return safe_strtou32(std::string(str), value);
}
inline bool safe_strtou32(StringPiece str, uint32* value) {
inline bool safe_strtou32(StringPiece str, uint32_t* value) {
return safe_strtou32(str.ToString(), value);
}
PROTOBUF_EXPORT bool safe_strto64(const std::string& str, int64* value);
PROTOBUF_EXPORT bool safe_strtou64(const std::string& str, uint64* value);
inline bool safe_strto64(const char* str, int64* value) {
PROTOBUF_EXPORT bool safe_strto64(const std::string& str, int64_t* value);
PROTOBUF_EXPORT bool safe_strtou64(const std::string& str, uint64_t* value);
inline bool safe_strto64(const char* str, int64_t* value) {
return safe_strto64(std::string(str), value);
}
inline bool safe_strto64(StringPiece str, int64* value) {
inline bool safe_strto64(StringPiece str, int64_t* value) {
return safe_strto64(str.ToString(), value);
}
inline bool safe_strtou64(const char* str, uint64* value) {
inline bool safe_strtou64(const char* str, uint64_t* value) {
return safe_strtou64(std::string(str), value);
}
inline bool safe_strtou64(StringPiece str, uint64* value) {
inline bool safe_strtou64(StringPiece str, uint64_t* value) {
return safe_strtou64(str.ToString(), value);
}
@ -470,13 +470,13 @@ inline bool safe_strtod(StringPiece str, double* value) {
// DoubleToBuffer() and FloatToBuffer().
static const int kFastToBufferSize = 32;
PROTOBUF_EXPORT char* FastInt32ToBuffer(int32 i, char* buffer);
PROTOBUF_EXPORT char* FastInt64ToBuffer(int64 i, char* buffer);
char* FastUInt32ToBuffer(uint32 i, char* buffer); // inline below
char* FastUInt64ToBuffer(uint64 i, char* buffer); // inline below
PROTOBUF_EXPORT char* FastInt32ToBuffer(int32_t i, char* buffer);
PROTOBUF_EXPORT char* FastInt64ToBuffer(int64_t i, char* buffer);
char* FastUInt32ToBuffer(uint32_t i, char* buffer); // inline below
char* FastUInt64ToBuffer(uint64_t i, char* buffer); // inline below
PROTOBUF_EXPORT char* FastHexToBuffer(int i, char* buffer);
PROTOBUF_EXPORT char* FastHex64ToBuffer(uint64 i, char* buffer);
PROTOBUF_EXPORT char* FastHex32ToBuffer(uint32 i, char* buffer);
PROTOBUF_EXPORT char* FastHex64ToBuffer(uint64_t i, char* buffer);
PROTOBUF_EXPORT char* FastHex32ToBuffer(uint32_t i, char* buffer);
// at least 22 bytes long
inline char* FastIntToBuffer(int i, char* buffer) {
@ -512,17 +512,17 @@ inline char* FastULongToBuffer(unsigned long i, char* buffer) {
// terminating the string).
// ----------------------------------------------------------------------
PROTOBUF_EXPORT char* FastInt32ToBufferLeft(int32 i, char* buffer);
PROTOBUF_EXPORT char* FastUInt32ToBufferLeft(uint32 i, char* buffer);
PROTOBUF_EXPORT char* FastInt64ToBufferLeft(int64 i, char* buffer);
PROTOBUF_EXPORT char* FastUInt64ToBufferLeft(uint64 i, char* buffer);
PROTOBUF_EXPORT char* FastInt32ToBufferLeft(int32_t i, char* buffer);
PROTOBUF_EXPORT char* FastUInt32ToBufferLeft(uint32_t i, char* buffer);
PROTOBUF_EXPORT char* FastInt64ToBufferLeft(int64_t i, char* buffer);
PROTOBUF_EXPORT char* FastUInt64ToBufferLeft(uint64_t i, char* buffer);
// Just define these in terms of the above.
inline char* FastUInt32ToBuffer(uint32 i, char* buffer) {
inline char* FastUInt32ToBuffer(uint32_t i, char* buffer) {
FastUInt32ToBufferLeft(i, buffer);
return buffer;
}
inline char* FastUInt64ToBuffer(uint64 i, char* buffer) {
inline char* FastUInt64ToBuffer(uint64_t i, char* buffer) {
FastUInt64ToBufferLeft(i, buffer);
return buffer;
}
@ -595,7 +595,7 @@ enum PadSpec {
};
struct Hex {
uint64 value;
uint64_t value;
enum PadSpec spec;
template <class Int>
explicit Hex(Int v, PadSpec s = NO_PAD)
@ -607,10 +607,10 @@ struct Hex {
sizeof(v) == 1 || sizeof(v) == 2 || sizeof(v) == 4 || sizeof(v) == 8,
"Unknown integer type");
#endif
value = sizeof(v) == 1 ? static_cast<uint8>(v)
: sizeof(v) == 2 ? static_cast<uint16>(v)
: sizeof(v) == 4 ? static_cast<uint32>(v)
: static_cast<uint64>(v);
value = sizeof(v) == 1 ? static_cast<uint8_t>(v)
: sizeof(v) == 2 ? static_cast<uint16_t>(v)
: sizeof(v) == 4 ? static_cast<uint32_t>(v)
: static_cast<uint64_t>(v);
}
};
@ -789,7 +789,7 @@ std::string Join(const Range& components, const char* delim) {
// ToHex()
// Return a lower-case hex string representation of the given integer.
// ----------------------------------------------------------------------
PROTOBUF_EXPORT std::string ToHex(uint64 num);
PROTOBUF_EXPORT std::string ToHex(uint64_t num);
// ----------------------------------------------------------------------
// GlobalReplaceSubstring()
@ -863,7 +863,7 @@ PROTOBUF_EXPORT void Base64Escape(const unsigned char* src, int szsrc,
PROTOBUF_EXPORT void WebSafeBase64Escape(const unsigned char* src, int szsrc,
std::string* dest, bool do_padding);
inline bool IsValidCodePoint(uint32 code_point) {
inline bool IsValidCodePoint(uint32_t code_point) {
return code_point < 0xD800 ||
(code_point >= 0xE000 && code_point <= 0x10FFFF);
}
@ -875,7 +875,7 @@ static const int UTFmax = 4;
// in any external dependencies. The output buffer must be as least 4 bytes
// large.
// ----------------------------------------------------------------------
PROTOBUF_EXPORT int EncodeAsUTF8Char(uint32 code_point, char* output);
PROTOBUF_EXPORT int EncodeAsUTF8Char(uint32_t code_point, char* output);
// ----------------------------------------------------------------------
// UTF8FirstLetterNumBytes()

@ -10,23 +10,23 @@ namespace protobuf {
namespace internal {
namespace {
static const int64 kSecondsPerMinute = 60;
static const int64 kSecondsPerHour = 3600;
static const int64 kSecondsPerDay = kSecondsPerHour * 24;
static const int64 kSecondsPer400Years =
static const int64_t kSecondsPerMinute = 60;
static const int64_t kSecondsPerHour = 3600;
static const int64_t kSecondsPerDay = kSecondsPerHour * 24;
static const int64_t kSecondsPer400Years =
kSecondsPerDay * (400 * 365 + 400 / 4 - 3);
// Seconds from 0001-01-01T00:00:00 to 1970-01-01T:00:00:00
static const int64 kSecondsFromEraToEpoch = 62135596800LL;
static const int64_t kSecondsFromEraToEpoch = 62135596800LL;
// The range of timestamp values we support.
static const int64 kMinTime = -62135596800LL; // 0001-01-01T00:00:00
static const int64 kMaxTime = 253402300799LL; // 9999-12-31T23:59:59
static const int64_t kMinTime = -62135596800LL; // 0001-01-01T00:00:00
static const int64_t kMaxTime = 253402300799LL; // 9999-12-31T23:59:59
static const int kNanosPerMillisecond = 1000000;
static const int kNanosPerMicrosecond = 1000;
// Count the seconds from the given year (start at Jan 1, 00:00) to 100 years
// after.
int64 SecondsPer100Years(int year) {
int64_t SecondsPer100Years(int year) {
if (year % 400 == 0 || year % 400 > 300) {
return kSecondsPerDay * (100 * 365 + 100 / 4);
} else {
@ -36,7 +36,7 @@ int64 SecondsPer100Years(int year) {
// Count the seconds from the given year (start at Jan 1, 00:00) to 4 years
// after.
int64 SecondsPer4Years(int year) {
int64_t SecondsPer4Years(int year) {
if ((year % 100 == 0 || year % 100 > 96) &&
!(year % 400 == 0 || year % 400 > 396)) {
// No leap years.
@ -51,7 +51,7 @@ bool IsLeapYear(int year) {
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}
int64 SecondsPerYear(int year) {
int64_t SecondsPerYear(int year) {
return kSecondsPerDay * (IsLeapYear(year) ? 366 : 365);
}
@ -59,7 +59,7 @@ static const int kDaysInMonth[13] = {
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
int64 SecondsPerMonth(int month, bool leap) {
int64_t SecondsPerMonth(int month, bool leap) {
if (month == 2 && leap) {
return kSecondsPerDay * (kDaysInMonth[month] + 1);
}
@ -88,8 +88,8 @@ bool ValidateDateTime(const DateTime& time) {
// Count the number of seconds elapsed from 0001-01-01T00:00:00 to the given
// time.
int64 SecondsSinceCommonEra(const DateTime& time) {
int64 result = 0;
int64_t SecondsSinceCommonEra(const DateTime& time) {
int64_t result = 0;
// Years should be between 1 and 9999.
assert(time.year >= 1 && time.year <= 9999);
int year = 1;
@ -130,7 +130,7 @@ int64 SecondsSinceCommonEra(const DateTime& time) {
// Format nanoseconds with either 3, 6, or 9 digits depending on the required
// precision to represent the exact value.
std::string FormatNanos(int32 nanos) {
std::string FormatNanos(int32_t nanos) {
if (nanos % kNanosPerMillisecond == 0) {
return StringPrintf("%03d", nanos / kNanosPerMillisecond);
} else if (nanos % kNanosPerMicrosecond == 0) {
@ -167,7 +167,7 @@ const char* ParseInt(const char* data, int width, int min_value,
// Consumes the fractional parts of a second into nanos. For example,
// "010" will be parsed to 10000000 nanos.
const char* ParseNanos(const char* data, int32* nanos) {
const char* ParseNanos(const char* data, int32_t* nanos) {
if (!ascii_isdigit(*data)) {
return nullptr;
}
@ -190,7 +190,7 @@ const char* ParseNanos(const char* data, int32* nanos) {
return data;
}
const char* ParseTimezoneOffset(const char* data, int64* offset) {
const char* ParseTimezoneOffset(const char* data, int64_t* offset) {
// Accept format "HH:MM". E.g., "08:00"
int hour;
if ((data = ParseInt(data, 2, 0, 23, &hour)) == nullptr) {
@ -208,7 +208,7 @@ const char* ParseTimezoneOffset(const char* data, int64* offset) {
}
} // namespace
bool SecondsToDateTime(int64 seconds, DateTime* time) {
bool SecondsToDateTime(int64_t seconds, DateTime* time) {
if (seconds < kMinTime || seconds > kMaxTime) {
return false;
}
@ -253,7 +253,7 @@ bool SecondsToDateTime(int64 seconds, DateTime* time) {
return true;
}
bool DateTimeToSeconds(const DateTime& time, int64* seconds) {
bool DateTimeToSeconds(const DateTime& time, int64_t* seconds) {
if (!ValidateDateTime(time)) {
return false;
}
@ -261,14 +261,14 @@ bool DateTimeToSeconds(const DateTime& time, int64* seconds) {
return true;
}
void GetCurrentTime(int64* seconds, int32* nanos) {
void GetCurrentTime(int64_t* seconds, int32_t* nanos) {
// TODO(xiaofeng): Improve the accuracy of this implementation (or just
// remove this method from protobuf).
*seconds = time(nullptr);
*nanos = 0;
}
std::string FormatTime(int64 seconds, int32 nanos) {
std::string FormatTime(int64_t seconds, int32_t nanos) {
DateTime time;
if (nanos < 0 || nanos > 999999999 || !SecondsToDateTime(seconds, &time)) {
return "InvalidTime";
@ -282,7 +282,7 @@ std::string FormatTime(int64 seconds, int32 nanos) {
return result + "Z";
}
bool ParseTime(const std::string& value, int64* seconds, int32* nanos) {
bool ParseTime(const std::string& value, int64_t* seconds, int32_t* nanos) {
DateTime time;
const char* data = value.c_str();
// We only accept:
@ -341,14 +341,14 @@ bool ParseTime(const std::string& value, int64* seconds, int32* nanos) {
++data;
} else if (*data == '+') {
++data;
int64 offset;
int64_t offset;
if ((data = ParseTimezoneOffset(data, &offset)) == nullptr) {
return false;
}
*seconds -= offset;
} else if (*data == '-') {
++data;
int64 offset;
int64_t offset;
if ((data = ParseTimezoneOffset(data, &offset)) == nullptr) {
return false;
}

@ -30,6 +30,8 @@
#ifndef GOOGLE_PROTOBUF_STUBS_TIME_H_
#define GOOGLE_PROTOBUF_STUBS_TIME_H_
#include <cstdint>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/port_def.inc>
@ -51,12 +53,12 @@ struct DateTime {
// negative to represent time before 1970-01-01) to DateTime. Returns false
// if the timestamp is not in the range between 0001-01-01T00:00:00 and
// 9999-12-31T23:59:59.
bool PROTOBUF_EXPORT SecondsToDateTime(int64 seconds, DateTime* time);
bool PROTOBUF_EXPORT SecondsToDateTime(int64_t seconds, DateTime* time);
// Converts DateTime to a timestamp (seconds since 1970-01-01T00:00:00).
// Returns false if the DateTime is not valid or is not in the valid range.
bool PROTOBUF_EXPORT DateTimeToSeconds(const DateTime& time, int64* seconds);
bool PROTOBUF_EXPORT DateTimeToSeconds(const DateTime& time, int64_t* seconds);
void PROTOBUF_EXPORT GetCurrentTime(int64* seconds, int32* nanos);
void PROTOBUF_EXPORT GetCurrentTime(int64_t* seconds, int32_t* nanos);
// Formats a time string in RFC3339 format.
//
@ -65,11 +67,11 @@ void PROTOBUF_EXPORT GetCurrentTime(int64* seconds, int32* nanos);
// value.
//
// Note that "nanos" must in the range of [0, 999999999].
std::string PROTOBUF_EXPORT FormatTime(int64 seconds, int32 nanos);
std::string PROTOBUF_EXPORT FormatTime(int64_t seconds, int32_t nanos);
// Parses a time string. This method accepts RFC3339 date/time string with UTC
// offset. For example, "2015-05-20T13:29:35.120-08:00".
bool PROTOBUF_EXPORT ParseTime(const std::string& value, int64* seconds,
int32* nanos);
bool PROTOBUF_EXPORT ParseTime(const std::string& value, int64_t* seconds,
int32_t* nanos);
} // namespace internal
} // namespace protobuf

Loading…
Cancel
Save