}
// Portable implementation of strtoll().
-inline int64_t StringToInt(const char *str, char **endptr = nullptr, int base = 10) {
+inline int64_t StringToInt(const char *str, char **endptr = nullptr,
+ int base = 10) {
#ifdef _MSC_VER
return _strtoi64(str, endptr, base);
#else
}
// Portable implementation of strtoull().
-inline int64_t StringToUInt(const char *str, char **endptr = nullptr, int base = 10) {
+inline uint64_t StringToUInt(const char *str, char **endptr = nullptr,
+ int base = 10) {
#ifdef _MSC_VER
return _strtoui64(str, endptr, base);
#else
*val = (T)i;
return NoError();
}
+template<> inline CheckedError atot<uint64_t>(const char *s, Parser &parser,
+ uint64_t *val) {
+ (void)parser;
+ *val = StringToUInt(s);
+ return NoError();
+}
template<> inline CheckedError atot<bool>(const char *s, Parser &parser,
bool *val) {
(void)parser;
}
// Parses exactly nibbles worth of hex digits into a number, or error.
-CheckedError Parser::ParseHexNum(int nibbles, int64_t *val) {
+CheckedError Parser::ParseHexNum(int nibbles, uint64_t *val) {
for (int i = 0; i < nibbles; i++)
if (!isxdigit(static_cast<const unsigned char>(cursor_[i])))
return Error("escape code must be followed by " + NumToString(nibbles) +
case '/': attribute_ += '/'; cursor_++; break;
case 'x': { // Not in the JSON standard
cursor_++;
- int64_t val;
+ uint64_t val;
ECHECK(ParseHexNum(2, &val));
attribute_ += static_cast<char>(val);
break;
}
case 'u': {
cursor_++;
- int64_t val;
+ uint64_t val;
ECHECK(ParseHexNum(4, &val));
if (val >= 0xD800 && val <= 0xDBFF) {
if (unicode_high_surrogate != -1) {
return NoError();
} else if (isdigit(static_cast<unsigned char>(c)) || c == '-') {
const char *start = cursor_ - 1;
- if (c == '-' && *cursor_ == '0' && (cursor_[1] == 'x' || cursor_[1] == 'X')) {
+ if (c == '-' && *cursor_ == '0' &&
+ (cursor_[1] == 'x' || cursor_[1] == 'X')) {
++start;
++cursor_;
attribute_.append(&c, &c + 1);
cursor_++;
while (isxdigit(static_cast<unsigned char>(*cursor_))) cursor_++;
attribute_.append(start + 2, cursor_);
- attribute_ = NumToString(StringToUInt(attribute_.c_str(), nullptr, 16));
+ attribute_ = NumToString(static_cast<int64_t>(
+ StringToUInt(attribute_.c_str(), nullptr, 16)));
token_ = kTokenIntegerConstant;
return NoError();
}
TEST_NOTNULL(pos_table_ptr);
TEST_EQ_STR(pos_table_ptr->name()->c_str(), "MyGame.Example.Vec3");
-
+
// Now use it to dynamically access a buffer.
auto &root = *flatbuffers::GetAnyRoot(flatbuf);
reinterpret_cast<const uint8_t *>(resizingbuf.data()),
resizingbuf.size());
TEST_EQ(VerifyMonsterBuffer(resize_verifier), true);
-
+
// Test buffer is valid using reflection as well
TEST_EQ(flatbuffers::Verify(schema, *schema.root_table(), resizingbuf.data(),
resizingbuf.size()), true);
-
+
// As an additional test, also set it on the name field.
// Note: unlike the name change above, this just overwrites the offset,
// rather than changing the string in-place.
flatbuffers::Parser parser;
// Simple schema.
- TEST_EQ(parser.Parse(std::string("table X { Y:" + std::string(type_name) + "; } root_type X;").c_str()), true);
+ TEST_EQ(parser.Parse(std::string("table X { Y:" + std::string(type_name) +
+ "; } root_type X;").c_str()), true);
TEST_EQ(parser.Parse(json), true);
- auto root = flatbuffers::GetRoot<T>(parser.builder_.GetBufferPointer());
- // root will point to the table, which is a 32bit vtable offset followed
- // by a float:
- TEST_EQ(sizeof(flatbuffers::soffset_t), 4); // Test assumes 32bit offsets
- return root[1];
+ auto root = flatbuffers::GetRoot<flatbuffers::Table>(
+ parser.builder_.GetBufferPointer());
+ return root->GetField<T>(flatbuffers::FieldIndexToOffset(0), 0);
}
bool FloatCompare(float a, float b) { return fabs(a - b) < 0.001; }
// Additional parser testing not covered elsewhere.
void ValueTest() {
// Test scientific notation numbers.
- TEST_EQ(FloatCompare(TestValue<float>("{ Y:0.0314159e+2 }","float"), (float)3.14159), true);
+ TEST_EQ(FloatCompare(TestValue<float>("{ Y:0.0314159e+2 }","float"),
+ (float)3.14159), true);
// Test conversion functions.
- TEST_EQ(FloatCompare(TestValue<float>("{ Y:cos(rad(180)) }","float"), -1), true);
+ TEST_EQ(FloatCompare(TestValue<float>("{ Y:cos(rad(180)) }","float"), -1),
+ true);
// Test negative hex constant.
- TEST_EQ(TestValue<int>("{ Y:-0x80 }","int") == -128, true);
+ TEST_EQ(TestValue<int>("{ Y:-0x80 }","int"), -128);
+
+ // Make sure we do unsigned 64bit correctly.
+ TEST_EQ(TestValue<uint64_t>("{ Y:12335089644688340133 }","ulong"),
+ 12335089644688340133ULL);
}
void EnumStringsTest() {