diff options
author | Tiger Wang <ziwei.tiger@outlook.com> | 2021-01-11 17:39:43 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-11 17:39:43 +0100 |
commit | eeb63b8901a9c049f1bb594abb9ce9b4a9c47620 (patch) | |
tree | b07daae788f918b83eeb0bdbd51e49292f1c8d88 /src/WorldStorage/FastNBT.h | |
parent | Fixed switch-ups regarding some slab and stair recipes (#5099) (diff) | |
download | cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.tar cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.tar.gz cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.tar.bz2 cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.tar.lz cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.tar.xz cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.tar.zst cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.zip |
Diffstat (limited to 'src/WorldStorage/FastNBT.h')
-rw-r--r-- | src/WorldStorage/FastNBT.h | 48 |
1 files changed, 24 insertions, 24 deletions
diff --git a/src/WorldStorage/FastNBT.h b/src/WorldStorage/FastNBT.h index 257115fe1..0df520e21 100644 --- a/src/WorldStorage/FastNBT.h +++ b/src/WorldStorage/FastNBT.h @@ -152,7 +152,7 @@ Each primitive tag also stores the length of the contained data, in bytes. class cParsedNBT { public: - cParsedNBT(const char * a_Data, size_t a_Length); + cParsedNBT(ContiguousByteBufferView a_Data); bool IsValid(void) const { return (m_Error == eNBTParseError::npSuccess); } @@ -179,7 +179,7 @@ public: /** Returns the length of the tag's data, in bytes. Not valid for Compound or List tags! */ - size_t GetDataLength (int a_Tag) const + size_t GetDataLength(int a_Tag) const { ASSERT(m_Tags[static_cast<size_t>(a_Tag)].m_Type != TAG_List); ASSERT(m_Tags[static_cast<size_t>(a_Tag)].m_Type != TAG_Compound); @@ -188,11 +188,11 @@ public: /** Returns the data stored in this tag. Not valid for Compound or List tags! */ - const char * GetData(int a_Tag) const + const std::byte * GetData(int a_Tag) const { ASSERT(m_Tags[static_cast<size_t>(a_Tag)].m_Type != TAG_List); ASSERT(m_Tags[static_cast<size_t>(a_Tag)].m_Type != TAG_Compound); - return m_Data + m_Tags[static_cast<size_t>(a_Tag)].m_DataStart; + return m_Data.data() + m_Tags[static_cast<size_t>(a_Tag)].m_DataStart; } /** Returns the direct child tag of the specified name, or -1 if no such tag. */ @@ -227,21 +227,21 @@ public: inline Int16 GetShort(int a_Tag) const { ASSERT(m_Tags[static_cast<size_t>(a_Tag)].m_Type == TAG_Short); - return GetBEShort(m_Data + m_Tags[static_cast<size_t>(a_Tag)].m_DataStart); + return GetBEShort(GetData(a_Tag)); } /** Returns the value stored in an Int tag. Not valid for any other tag type. */ inline Int32 GetInt(int a_Tag) const { ASSERT(m_Tags[static_cast<size_t>(a_Tag)].m_Type == TAG_Int); - return GetBEInt(m_Data + m_Tags[static_cast<size_t>(a_Tag)].m_DataStart); + return GetBEInt(GetData(a_Tag)); } /** Returns the value stored in a Long tag. Not valid for any other tag type. */ inline Int64 GetLong(int a_Tag) const { ASSERT(m_Tags[static_cast<size_t>(a_Tag)].m_Type == TAG_Long); - return NetworkToHostLong8(m_Data + m_Tags[static_cast<size_t>(a_Tag)].m_DataStart); + return NetworkToHostLong8(GetData(a_Tag)); } /** Returns the value stored in a Float tag. Not valid for any other tag type. */ @@ -256,7 +256,7 @@ public: UNUSED_VAR(Check1); UNUSED_VAR(Check2); - Int32 i = GetBEInt(m_Data + m_Tags[static_cast<size_t>(a_Tag)].m_DataStart); + Int32 i = GetBEInt(GetData(a_Tag)); float f; memcpy(&f, &i, sizeof(f)); return f; @@ -273,29 +273,33 @@ public: UNUSED_VAR(Check2); ASSERT(m_Tags[static_cast<size_t>(a_Tag)].m_Type == TAG_Double); - return NetworkToHostDouble8(m_Data + m_Tags[static_cast<size_t>(a_Tag)].m_DataStart); + return NetworkToHostDouble8(GetData(a_Tag)); } /** Returns the value stored in a String tag. Not valid for any other tag type. */ inline AString GetString(int a_Tag) const { + return AString(GetStringView(a_Tag)); + } + + /** Returns the value stored in a String tag. Not valid for any other tag type. */ + inline std::string_view GetStringView(int a_Tag) const + { ASSERT(m_Tags[static_cast<size_t>(a_Tag)].m_Type == TAG_String); - AString res; - res.assign(m_Data + m_Tags[static_cast<size_t>(a_Tag)].m_DataStart, static_cast<size_t>(m_Tags[static_cast<size_t>(a_Tag)].m_DataLength)); - return res; + return { reinterpret_cast<const char *>(GetData(a_Tag)), GetDataLength(a_Tag) }; } /** Returns the tag's name. For tags that are not named, returns an empty string. */ inline AString GetName(int a_Tag) const { AString res; - res.assign(m_Data + m_Tags[static_cast<size_t>(a_Tag)].m_NameStart, static_cast<size_t>(m_Tags[static_cast<size_t>(a_Tag)].m_NameLength)); + res.assign(reinterpret_cast<const char *>(m_Data.data()) + m_Tags[static_cast<size_t>(a_Tag)].m_NameStart, static_cast<size_t>(m_Tags[static_cast<size_t>(a_Tag)].m_NameLength)); return res; } protected: - const char * m_Data; - size_t m_Length; + + ContiguousByteBufferView m_Data; std::vector<cFastNBTTag> m_Tags; eNBTParseError m_Error; // npSuccess if parsing succeeded @@ -343,7 +347,7 @@ public: AddByteArray(a_Name, a_Value.data(), a_Value.size()); } - const AString & GetResult(void) const {return m_Result; } + ContiguousByteBufferView GetResult(void) const { return m_Result; } void Finish(void); @@ -363,11 +367,11 @@ protected: sParent m_Stack[MAX_STACK]; int m_CurrentStack; - AString m_Result; + ContiguousByteBuffer m_Result; bool IsStackTopCompound(void) const { return (m_Stack[m_CurrentStack].m_Type == TAG_Compound); } - void WriteString(const char * a_Data, UInt16 a_Length); + void WriteString(std::string_view a_Data); inline void TagCommon(const AString & a_Name, eTagType a_Type) { @@ -377,8 +381,8 @@ protected: if (IsStackTopCompound()) { // Compound: add the type and name: - m_Result.push_back(static_cast<char>(a_Type)); - WriteString(a_Name.c_str(), static_cast<UInt16>(a_Name.length())); + m_Result.push_back(std::byte(a_Type)); + WriteString(a_Name); } else { @@ -387,7 +391,3 @@ protected: } } } ; - - - - |