MCPcopy Create free account
hub / github.com/apache/fory / read_varint_at_checked

Function read_varint_at_checked

cpp/fory/serialization/struct_serializer.h:3869–3911  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3867
3868template <typename T>
3869FORY_ALWAYS_INLINE T read_varint_at_checked(Buffer &buffer, uint32_t &offset,
3870 Error &error) {
3871 uint32_t bytes_read = 0;
3872 if constexpr (std::is_same_v<T, int32_t> || std::is_same_v<T, int>) {
3873 uint32_t raw = buffer.get_var_uint32(offset, &bytes_read);
3874 if (FORY_PREDICT_FALSE(bytes_read == 0)) {
3875 error.set_buffer_out_of_bound(offset, 1, buffer.size());
3876 return T{};
3877 }
3878 offset += bytes_read;
3879 return static_cast<T>((raw >> 1) ^ (~(raw & 1) + 1));
3880 } else if constexpr (std::is_same_v<T, int64_t> ||
3881 std::is_same_v<T, long long>) {
3882 uint64_t raw = buffer.get_var_uint64(offset, &bytes_read);
3883 if (FORY_PREDICT_FALSE(bytes_read == 0)) {
3884 error.set_buffer_out_of_bound(offset, 1, buffer.size());
3885 return T{};
3886 }
3887 offset += bytes_read;
3888 return static_cast<T>((raw >> 1) ^ (~(raw & 1) + 1));
3889 } else if constexpr (std::is_same_v<T, uint32_t> ||
3890 std::is_same_v<T, unsigned int>) {
3891 uint32_t raw = buffer.get_var_uint32(offset, &bytes_read);
3892 if (FORY_PREDICT_FALSE(bytes_read == 0)) {
3893 error.set_buffer_out_of_bound(offset, 1, buffer.size());
3894 return T{};
3895 }
3896 offset += bytes_read;
3897 return static_cast<T>(raw);
3898 } else if constexpr (std::is_same_v<T, uint64_t> ||
3899 std::is_same_v<T, unsigned long long>) {
3900 uint64_t raw = buffer.get_var_uint64(offset, &bytes_read);
3901 if (FORY_PREDICT_FALSE(bytes_read == 0)) {
3902 error.set_buffer_out_of_bound(offset, 1, buffer.size());
3903 return T{};
3904 }
3905 offset += bytes_read;
3906 return static_cast<T>(raw);
3907 } else {
3908 static_assert(sizeof(T) == 0, "Unsupported checked varint type");
3909 return T{};
3910 }
3911}
3912
3913template <size_t Bytes>
3914FORY_ALWAYS_INLINE bool ensure_offset_readable(Buffer &buffer, uint32_t offset,

Callers

nothing calls this directly

Calls 4

get_var_uint32Method · 0.80
get_var_uint64Method · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected